<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="项目名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入项目名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="经营状态" prop="businessStatus">
        <el-input
          v-model="queryParams.businessStatus"
          placeholder="请输入经营状态"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="项目业态" prop="business">
        <el-select
          v-model="queryParams.business"
          placeholder="请选择项目业态"
          clearable
        >
          <el-option
            v-for="dict in dict.type.risk_project_business"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="所属区域" prop="deptId">
        <treeselect
          v-model="queryParams.deptId"
          :options="deptOptions"
          :show-count="true"
          placeholder="请选择所属区域"
          style="width: 250px"
        />
      </el-form-item>
      <el-form-item label="所在城市" prop="city">
        <el-input
          v-model="queryParams.city"
          placeholder="请输入所在城市"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="详细地址" prop="address">
        <el-input
          v-model="queryParams.address"
          placeholder="请输入详细地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物业" prop="propertyManagement">
        <el-select
          v-model="queryParams.propertyManagement"
          placeholder="请选择是否为物业"
          clearable
        >
        <el-option key="1" label="是" value="true" />
        <el-option key="0" label="否" value="false" />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择状态"
          clearable
        >
          <el-option
            v-for="dict in dict.type.sys_normal_disable"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:ledgerProject:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:ledgerProject:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:ledgerProject:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:ledgerProject:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="projectList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center">
        <template slot-scope="scope">
          <span>{{ scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="项目名称" align="center" prop="name" />
      <el-table-column label="项目类型" align="center" prop="type" />
      <el-table-column label="项目业态" align="center" prop="business" />
      <el-table-column label="所属区域" align="center" prop="deptName" />
      <el-table-column label="所在城市" align="center" prop="city" />
      <el-table-column label="详细地址" align="center" prop="address" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.sys_normal_disable"
            :value="scope.row.status"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:ledgerProject:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:ledgerProject:remove']"
            >删除</el-button
          >
           <el-button
            size="mini"
            type="text"
            icon="el-icon-setting"
            @click="empower(scope.row)"
            >授权</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改基础数据-项目对话框 -->
    <el-dialog :title="title" :visible.sync="open" :width="title == '选择用户授权'? '70%' : '500px'" append-to-body>
      <div v-if="title == '选择用户授权'">
        <el-form :model="queryUserParams" ref="queryForm" size="small" :inline="true" label-width="68px">
          <el-form-item label="用户名称" prop="userName">
            <el-input
              v-model="queryUserParams.userName"
              placeholder="请输入用户名称"
              clearable
              style="width: 240px"
              @keyup.enter.native="handleUserQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleUserQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetUserQuery">重置</el-button>
          </el-form-item>
        </el-form>
         <el-table v-loading="loading" :data="userList" >
          <el-table-column label="用户编号" align="center" key="userId" prop="userId" width="80" />
          <el-table-column label="用户名称" align="center" key="userName" prop="userName" :show-overflow-tooltip="true"/>
          <el-table-column label="用户昵称" align="center" key="nickName" prop="nickName" :show-overflow-tooltip="true" />
          <el-table-column label="岗位" align="center" key="postName" prop="postName" :show-overflow-tooltip="true" />
          <el-table-column
            label="操作"
            align="center"
            width="160"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="empowerUser(scope.row)"
              >授权</el-button>
            </template>
          </el-table-column>
        </el-table>
        <pagination
          v-show="userTotal>0"
          :total="userTotal"
          :page.sync="queryUserParams.pageNum"
          :limit.sync="queryUserParams.pageSize"
          @pagination="getUserList"
        />
      </div>
      <div v-else>
         <el-form ref="form" :model="form" :rules="rules" label-width="84px">
          <el-form-item label="项目名称" prop="name">
            <el-input v-model="form.name" placeholder="请输入项目名称" />
          </el-form-item>
          <el-form-item label="项目类型" prop="type">
            <el-input v-model="form.type" placeholder="请输入项目类型" />
          </el-form-item>
          <el-form-item label="项目业态" prop="business">
            <el-select v-model="form.business" placeholder="请选择项目业态">
              <el-option
                v-for="dict in dict.type.risk_project_business"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="所属区域" prop="deptId">
            <treeselect
              v-model="form.deptId"
              :options="deptOptions"
              :show-count="true"
              placeholder="请选择所属区域"
            />
          </el-form-item>
          <el-form-item label="所在城市" prop="city">
            <el-input v-model="form.city" placeholder="请输入所在城市" />
          </el-form-item>
          <el-form-item label="详细地址" prop="address">
            <el-input v-model="form.address" placeholder="请输入详细地址" />
          </el-form-item>
          <el-form-item label="状态" prop="status">
            <el-select v-model="form.status" placeholder="请选择状态">
              <el-option
                v-for="dict in dict.type.sys_normal_disable"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="经营状态" prop="businessStatus">
            <el-radio-group v-model="form.businessStatus">
              <el-radio label="自营">自营</el-radio>
              <el-radio label="租赁">租赁</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="是否为物业" prop="propertyManagement">
            <el-radio-group v-model="form.propertyManagement">
              <el-radio :label="true">是</el-radio>
              <el-radio :label="false">否</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </div>
    </el-dialog>
    <el-dialog title="授权" :visible.sync="powerOpen" width="70%" append-to-body>
      <div style="min-height:100px;padding-top:10px;">
        <el-radio-group v-model="userPost">
          <el-radio v-for="(item, index) in postList" :key="index" :label="item.postId">{{item.postName}}</el-radio>
        </el-radio-group>
      </div>
      <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitPowerForm">确 定</el-button>
          <el-button @click="cancelPower">取 消</el-button>
        </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listUser,
  getUser,
  delUser,
  addUser,
  updateUser,
  resetUserPwd,
  changeUserStatus,
  deptTreeSelect,
} from "@/api/system/user";
import {
  listProject,
  getProject,
  delProject,
  addProject,
  updateProject,
} from "@/api/ledger/project/";
import { listPost, saveUserPost } from "@/api/system/post";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Project",
  dicts: ["sys_normal_disable", "data_source", "risk_project_business"],
  components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      userTotal:0,
      // 基础数据-项目表格数据
      projectList: [],
      // 部门树选项
      deptOptions: undefined,
      // 弹出层标题
      title: "",
      // 用户表格数据
      userList: null,
      // 是否显示弹出层
      open: false,
      powerOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        type: null,
        deptId: null,
        city: null,
        address: null,
        source: null,
        status: null,
      },
      queryUserParams:{
        pageNum: 1,
        pageSize: 10,
        projectId:undefined,
        userName: undefined,
      },
      // 表单参数
      form: {
        businessStatus: "",
      },
      // 表单校验
      rules: {},
      projectId:'',
      userPost:null,
      postList:[],
      userInfoData:{}
    };
  },
  watch: {
    // 根据名称筛选部门树
    deptName(val) {
      this.$refs.tree.filter(val);
    },
  },
  created() {
    this.getList();
    this.getDeptTree();
  },
  methods: {
    handleUserQuery(){
      this.queryUserParams.pageNum = 1;
      this.getUserList();
    },
    resetUserQuery(){
      this.dateRange = [];
      this.queryUserParams.userName = undefined;
      this.resetForm("queryUserParams");
      this.handleUserQuery();
    },
    /** 查询用户列表 */
    getUserList() {
      this.loading = true;
      this.queryUserParams.projectId = this.projectId
      listUser(this.addDateRange(this.queryUserParams, this.dateRange)).then(response => {
          this.userList = response.rows;
          this.userTotal = response.total;
          this.loading = false;
        }
      );
    },
    /** 查询基础数据-项目列表 */
    getList() {
      this.loading = true;
      listProject(this.queryParams).then((response) => {
        this.projectList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询部门下拉树结构 */
    getDeptTree() {
      deptTreeSelect().then((response) => {
        this.deptOptions = response.data;
      });
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.deptId = data.id;
      this.handleQuery();
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        type: null,
        deptId: null,
        city: null,
        address: null,
        source: null,
        status: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加基础数据-项目";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getProject(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改基础数据-项目";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateProject(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProject(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除基础数据-项目编号为"' + ids + '"的数据项？')
        .then(function () {
          return delProject(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    // 授权
    empower(row){
      this.title = "选择用户授权";
      this.projectId = row.id
      this.getUserList()
      this.open = true;
    },
    // 授权用户
    empowerUser(row){
      this.userInfoData = row
      this.userPost = Number(row.postId)
      this.getPostList()
      this.powerOpen = true;
    },
    getPostList(){
      this.loading = true;
      let params = {
        pageNum: 1,
        pageSize: 9999,
        postCode: 'inspection',
      }
      listPost(params).then(response => {
        this.postList = response.rows;
        this.loading = false;
      });
    },
    cancelPower(){
      this.powerOpen = false;
      this.userPost = null
    },
    submitPowerForm(){
      let params = {
        id : this.userInfoData.projectUserPostId,
        projectId : this.projectId,
        userId : this.userInfoData.userId,
        postId : this.userPost
      }
      saveUserPost(params).then(response => {
        this.$modal.msgSuccess("配置成功");
        this.powerOpen = false;
        this.userPost = null
        this.getUserList()
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/ledger/project/export",
        {
          ...this.queryParams,
        },
        `project_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
