<!--
 * @Author: ysh
 * @Date: 2024-04-08 13:56:14
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2024-04-30 11:30:01
-->
<template>
  <div class="app-container">
    <h2 style="text-align:center;">分管审核</h2>
    <el-form :model="form" :rules="rules" label-width="100px" :disabled="disabled">
      <el-form-item label="审核意见：" prop="manageComment">
        <el-input type="textarea" :rows="4" placeholder="请输入审核意见" v-model="form.manageComment">
        </el-input>
      </el-form-item>
      <el-form-item label="签名：">
        <span class="auditor">{{ form.manageApproverName }}</span>
      </el-form-item>
      <el-form-item label="审核时间：">
        {{ form.manageApprovalTime }}
      </el-form-item>
      <el-form-item>
        <p style="text-align:center;"><el-button type="primary" @click="confirm">确认审核</el-button></p>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { mapGetters } from 'vuex';
import { parseTime } from "@/utils/ruoyi";
import { complete, rejectTask, returnList, returnTask, getNextFlowNode, delegate, flowTaskForm } from "@/api/flowable/todo";
import { getProjectComment, addProjectComment, updateProjectComment } from "@/api/oa/project/projectComment"
import { getUsersDeptLeaderByDept } from '@/api/system/post'
export default {
  computed: {
    ...mapGetters(['name', 'userId']),
  },
  props: {
    disabled: {
      type: Boolean,
      require: true,
    },
    taskForm: {
      type: Object,
      required: true,
    },
    taskName: {
      type: String,
      required: true,
    },
  },
  data() {
    return {
      form: {
        manageApprover: '',
        manageApproverName: '',
        manageApprovalTime: ''
      },
      rules: {
        manageComment: [{ required: true, message: "请输入审核意见", trigger: "blur" }]
      },
      deptLeaderList: []
    }
  },
  created() {
    if (this.$route.query.taskName != '项目登记' && this.$route.query.taskName != '项目安排') {
      this.initForm();
    }
  },
  watch: {
    underDeptId(newVal, oldVal) {
      this.getDeptLeader(newVal)
    }
  },
  computed: {
    underDeptId() {
      return this.$store.state.projectProcess.underDeptId;
    }
  },
  methods: {
    initForm() {
      getProjectComment(this.taskForm.formId).then(res => {
        if (res.data) {
          this.form = res.data
          if (this.taskName == '分管审核') {
            this.form.manageApprover = this.$store.state.user.userId;
            this.form.manageApproverName = this.$store.state.user.name;
            this.form.manageApprovalTime = parseTime(new Date(), '{y}-{m}-{d}')
          } else {
            if (res.data.managerUser)
              this.form.manageApproverName = res.data.managerUser.nickName
          }
        } else {
          if (this.taskName == '分管审核') {
            this.form.manageApprover = this.$store.state.user.userId;
            this.form.manageApproverName = this.$store.state.user.name;
            this.form.manageApprovalTime = parseTime(new Date(), '{y}-{m}-{d}')
          } else {
            this.form.manageApprover = '';
            this.form.manageApprovalTime = '';
          }
        }
      })
    },
    getDeptLeader(val) {
      this.deptLeaderList = [];
      for (let v of val) {
        getUsersDeptLeaderByDept({ deptId: Number(v) }).then(res => {
          this.deptLeaderList.push(res.data.userId);
          this.$store.commit('SET_UNDERLEADERID', this.deptLeaderList)
        })
      }
    },
    confirm() {
      this.form.projectId = this.taskForm.formId
      updateProjectComment({ projectId: this.taskForm.formId, manageComment: this.form.manageComment })
      const params = { taskId: this.taskForm.taskId };
      // 获取下一个流程节点
      getNextFlowNode(params).then(res => {
        this.$set(this.taskForm.variables, "approvalList", this.deptLeaderList);
        complete(this.taskForm).then(response => {
          this.$modal.msgSuccess(response.msg);
          this.$emit("goBack");
        });
      });
    },

  },
}
</script>

<style lang="scss" scoped>
::v-deep .el-textarea.is-disabled .el-textarea__inner {
  color: #121212 !important;
}
</style>