<!--
 * @Author: ysh
 * @Date: 2024-03-19 09:24:06
 * @LastEditors: wrh
 * @LastEditTime: 2024-04-02 17:45:20
-->
<template>
  <div class="app-container">
    <h2 style="text-align: center;">项目安排</h2>
    <el-form :model="form" :rules="rules" label-width="100px" :disable="disable">
      <el-form-item label="承担部门：" prop="undertakingDept">
        <el-select v-model="form.deptId" multiple clearable style="width:500px" @change="getDeptLeader">
          <el-option v-for="item in deptList" :key="item.deptId" :label="item.deptName" :value="item.deptId"
            v-if="item.deptName != '四川中水成勘院测绘工程有限责任公司'">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="部门负责人" prop="deptLeader">
        <el-select v-model="form.deptLeader" multiple clearable style="width:500px">
          <el-option v-for="item in userList" :key="item.userId" :label="item.nickName" :value="item.userId"
            v-if="item.nickName != 'admin'">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="项目负责人" prop="projectLeader">
        <el-tag effect="plain" type="warning" style="margin-right: 10px;">{{ form.projectLeader }}</el-tag>
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="choosePeople">选择</el-button>
      </el-form-item>
      <el-form-item label="现场负责人" prop="siteLeader">
        <el-select v-model="form.siteLeader" filterable clearable style="width:500px">
          <el-option v-for="item in userList" :key="item.userId" :label="item.nickName" :value="item.userId"
            v-if="item.nickName != 'admin'">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="项目预算">
        <el-input v-model="form.deptLeader"></el-input>
      </el-form-item>
    </el-form>
    <!-- 人员选择对话框 -->
    <el-dialog width="320px" title="选择项目负责人" :visible.sync="peopleVisible" append-to-body>
      <el-form>
        <el-form-item label="部门：">
          <el-select v-model="peopleForm.deptId" @change="handleChangeDept">
            <el-option v-for="item in peopleForm.deptList" :key="item.deptId" :label="item.deptName" :value="item.deptId"
              v-if="item.deptName != '四川中水成勘院测绘工程有限责任公司'">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="人员：">
          <el-select v-model="peopleForm.userId">
            <el-option v-for="item in peopleForm.userList" :key="item.userId" :label="item.nickName" :value="item.userId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item style="text-align: center;">
          <el-button @click="confirmPeople" type="primary">确定</el-button>
          <el-button @click="cancelChoosePeople">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
    <div slot="footer" class="dialog-footer" style="text-align: center;">
      <el-button type="primary" @click="confirmPlanForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </div>
</template>

<script>
import { listDept } from '@/api/system/dept'
import { listUser } from '@/api/system/user'
import { getUsersDeptLeaderByDept } from '@/api/system/post'
import { complete, rejectTask, returnList, returnTask, getNextFlowNode, delegate, flowTaskForm, } from "@/api/flowable/todo";
export default {
  props: {
    disable: {
      type: Boolean,
      require: true
    },
    taskForm: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      form: {},
      rules: {},
      deptList: [],
      userList: [],
      deptLeaderList: [],
      file: {
        fileList: []
      },
      peopleVisible: false,
      peopleForm: {
        deptId: '',
        deptList: [],
        userId: '',
        userList: []
      },
    }
  },
  created() {
    this.getDeptList();
    this.getUserList();
  },
  methods: {
    getUserList() {
      listUser({ pageSize: 9999, pageNum: 1 }).then(res => {
        this.userList = res.rows
      })
    },
    getDeptList() {
      listDept({
        deptName: undefined,
        status: undefined
      }).then(res => {
        this.deptList = res.data
        this.peopleForm.deptList = res.data
      })
    },
    getDeptLeader(val) {
      this.deptLeaderList = [];
      for (let v of val) {
        getUsersDeptLeaderByDept({ deptId: v }).then(res => {
          this.deptLeaderList.push(res.data.userId);
          this.form.deptLeader = this.deptLeaderList;
        })
      }
    },
    // 人员选择
    choosePeople() {
      this.peopleVisible = true;
    },
    // 改变部门选择
    handleChangeDept(deptId) {
      this.peopleForm.userId = '';
      listUser({ pageSize: 999999, deptId }).then(res => {
        this.peopleForm.userList = res.rows
      })
    },
    // 确定项目负责人
    confirmPeople() {
      this.form.projectLeader = this.peopleForm.userId;
      this.form.projectLeader = this.tagLeader()
      this.peopleVisible = false
    },
    tagLeader() {
      if (this.form.projectLeader == '' || this.form.projectLeader == undefined || this.form.projectLeader == null) {
        return undefined
      } else {
        for (let user of this.peopleForm.userList) {
          if (this.form.projectLeader == user.userId) {
            return user.nickName
          }
        }
      }
    },
    cancelChoosePeople() {
      this.peopleForm.deptId = '';
      this.peopleForm.userId = '';
      this.peopleVisible = false;
    },
    confirmPlanForm() {
      let formData = new FormData();
      let form = JSON.stringify(this.form);
      formData.append("form", form);
      const params = { taskId: this.taskForm.taskId };
      // 获取下一个流程节点
      getNextFlowNode(params).then(res => {
        this.$set(this.taskForm.variables, "budgetInitiator", this.peopleForm.userId);
        getUsersDeptLeaderByDept({ deptId: 105 }).then(res => {
          let userId = res.data.userId;
          this.$set(this.taskForm.variables, "approval", userId);
          complete(this.taskForm).then(response => {
            this.$modal.msgSuccess(response.msg);
            this.$emit('goBack')
          });
        })
      })
    },
    cancel() {
      this.form = {}
    },
  }
}
</script>

<style lang="scss" scoped></style>