<!--
 * @Author: ysh
 * @Date: 2024-04-08 13:56:14
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2025-01-22 14:55:32
-->
<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="jyComment">
        <el-input type="textarea" :rows="4" placeholder="请输入审核意见" v-model="form.jyComment">
        </el-input>
      </el-form-item>
      <el-form-item label="签名：">
        <span class="auditor">{{ form.jyApproverName }}</span>
      </el-form-item>
      <el-form-item label="审核时间：">
        {{ form.jyApprovalTime }}
      </el-form-item>
      <el-row class="text-center">
        <el-button type="danger" @click="$emit('returnBack')" :disabled="disabled">退 回</el-button>
        <el-button type="primary" @click="confirm">确认审核</el-button>
      </el-row>
    </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 { listProject, getProject, addProject, updateProject, delProject, submitProject, modifyProject } from "@/api/oa/project/project";
import { getUsersManageLeaderByDept } 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: {
        jyApprover: '',
        jyApproverName: '',
        jyApprovalTime: ''
      },
      rules: {
        jyComment: [{ required: true, message: "请输入审核意见", trigger: "blur" }]
      },
      jyApprovalTime: "",
      managerList: [],
      formTotal: 0,
    }
  },
  created() {
    if (this.$route.query.taskName != '项目登记' && this.$route.query.taskName != '项目安排') {
      this.initForm();
    }
  },
  mounted() {
    if (this.$route.query.taskName != '项目登记' && this.$route.query.taskName != '项目安排') {
      this.getDeptManageLeader()
    }
  },
  methods: {
    initForm() {
      getProjectComment(this.taskForm.formId).then(res => {
        if (res.data) {
          this.formTotal = 1;
          this.form = res.data
          if (this.form.jyUser) {
            this.form.jyApproverName = this.form.jyUser.nickName
          }
        } else {
          if (this.taskName == '经营审核') {
            this.form.jyApproverName = this.name;
            this.form.jyApprover = this.userId;
            this.form.jyApprovalTime = parseTime(new Date(), '{y}-{m}-{d}')
          } else {
            this.form.jyApprover = '';
            this.form.jyApprovalTime = '';
          }
        }
      })
    },
    getDeptManageLeader() {
      getProject(this.taskForm.formId).then(res => {
        if (res.data && res.data.undertakingDept) {
          let deptId = res.data.undertakingDept.split(',');
          this.$store.commit('SET_UNDERDEPTID', deptId[0]);
          this.managerList = []
          getUsersManageLeaderByDept({ deptId: Number(deptId[0]) }).then(resopnse => {
            if (resopnse.data) {
              resopnse.data.forEach(element => {
                this.managerList.push(element.userId)
              });
            }
          })
          // for (let d of deptId) {
          //   getUsersManageLeaderByDept({ deptId: Number(d) }).then(resopnse => {
          //     if (resopnse.data) {
          //       resopnse.data.forEach(element => {
          //         this.managerList.push(element.userId)
          //       });
          //     }
          //   })
          // }
        }
      })
    },
    confirm() {
      this.form.projectId = this.taskForm.formId
      if (this.formTotal != 0) {
        updateProjectComment(this.form)
      } else {
        addProjectComment({ projectId: this.taskForm.formId, jyComment: this.form.jyComment })
      }
      const params = { taskId: this.taskForm.taskId };
      // 获取下一个流程节点
      getNextFlowNode(params).then(res => {
        // this.managerList.push(10)
        // this.managerList.push(7)
        // this.managerList = Array.from(new Set(this.managerList));
        this.$set(this.taskForm.variables, "approvalList", this.managerList);
        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>