<!--
 * @Author: ysh
 * @Date: 2024-04-08 13:56:14
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2025-01-22 15:03:06
-->
<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="交底情况：">
        <el-row>
          <el-checkbox v-model="safe" disabled>安全交底</el-checkbox>
          <el-link class="ml20 mr20" type="primary" @click="openSafe = true">安全交底记录表</el-link>
          <el-button plain type="warning" size="mini" @click="handleUrge('1')" v-if="!safe">发送企业微信消息提醒相关人员</el-button>
        </el-row>
        <el-row>
          <el-checkbox v-model="tech" disabled>技术交底</el-checkbox>
          <el-link class="ml20 mr20" type="primary" @click="openTech = true">技术交底记录表</el-link>
          <el-button plain type="warning" size="mini" @click="handleUrge('2')" v-if="!tech">发送企业微信消息提醒相关人员</el-button>
        </el-row>
        <el-row>
          <el-checkbox v-model="plan" disabled>技术方案</el-checkbox>
          <el-link class="ml20" type="primary" @click="openPlan = true">技术方案</el-link>
        </el-row>
      </el-form-item>
      <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-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>

    <el-dialog title="安全交底记录表" :visible.sync="openSafe" width="1000px" append-to-body>
      <safe-tab :taskForm="taskForm"></safe-tab>
    </el-dialog>
    <el-dialog title="技术交底记录表" :visible.sync="openTech" width="1000px" append-to-body>
      <technical-tab :taskForm="taskForm"></technical-tab>
    </el-dialog>
    <el-dialog title="技术方案" :visible.sync="openPlan" width="1000px" append-to-body>
      <plan-tab :taskForm="taskForm"></plan-tab>
    </el-dialog>
  </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 { getProject } from "@/api/oa/project/project"
import { getUsersDeptLeaderByDept, getUserByPost } from '@/api/system/post'
import { getSafe } from "@/api/oa/safe/safe";
import { getTechnical } from "@/api/oa/technical/technical";
import { getTechnicalPlan } from "@/api/oa/technical/technicalPlan";
import { sendQyMessage } from "@/api/qywx/index"
import { getUser } from "@/api/system/user"
import safeTab from '@/views/flowable/form/projectProcess/safeTab.vue';
import PlanTab from '@/views/flowable/form/projectProcess/planTab.vue';
import TechnicalTab from '@/views/flowable/form/projectProcess/technicalTab.vue';
export default {
  computed: {
    ...mapGetters(['name', 'userId']),
  },
  components: { safeTab, PlanTab, TechnicalTab },
  props: {
    disabled: {
      type: Boolean,
      require: true,
    },
    taskForm: {
      type: Object,
      required: true,
    },
    taskName: {
      type: String,
      required: true,
    },
  },
  data() {
    return {
      projectLeader: undefined,
      form: {
        manageApprover: '',
        manageApproverName: '',
        manageApprovalTime: '',
      },
      safe: false,
      tech: false,
      plan: false,
      openSafe: false,
      openTech: false,
      openPlan: false,
      rules: {
        manageComment: [{ required: true, message: "请输入审核意见", trigger: "blur" }]
      },
      deptLeaderList: [],
      project: null
    }
  },
  created() {
    if (this.$route.query.taskName != '项目登记' && this.$route.query.taskName != '项目安排') {
      this.haveJD();
      this.initForm();
    }
  },
  watch: {
    underDeptId(newVal, oldVal) {
      this.getDeptLeader(newVal)
    }
  },
  computed: {
    underDeptId() {
      return this.$store.state.projectProcess.underDeptId;
    }
  },
  methods: {
    initForm() {
      getProject(this.taskForm.formId).then(res => {
        if (res.data) {
          this.project = res.data
          this.projectLeader = res.data.projectLeader;
        }
      })
      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 = '';
          }
        }
      })
    },
    async haveJD() {
      let formId = this.taskForm.formId;
      let resSafe = await getSafe(formId);
      let resPlan = await getTechnicalPlan(formId);
      let resTech = await getTechnical(formId);
      if (resSafe.data) {
        if (resSafe.data.acceptComment && resSafe.data.acceptTime)
          this.safe = true
      }
      if (resPlan.data) {
        if (resPlan.data.technicalDocument && resPlan.data.technicalComment)
          this.plan = true
      }
      if (resTech.data) {
        if (resTech.data.acceptComment)
          this.tech = true
      }
    },
    getDeptLeader(val) {
      this.deptLeaderList = [this.projectLeader];
      for (let i = 0; i < val.length; i++) {
        getUsersDeptLeaderByDept({ deptId: Number(val[i]) }).then(res => {
          if (res.data) {
            this.deptLeaderList.push(res.data.userId);
            if (i == val.length - 1)
              this.deptLeaderList.shift();
          }
          this.$store.commit('SET_UNDERLEADERID', this.deptLeaderList)
        })
      }
    },
    confirm() {
      if (!(this.safe && this.tech)) {
        this.$modal.confirm('安全交底或者技术交底未全部完成，是否确认审核？').then(() => {
          this.submitComment()
        })
      } else {
        this.submitComment()
      }
    },
    submitComment() {
      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 => {
        getProject(this.taskForm.formId).then(res => {
          if (res.data) {
            this.$set(this.taskForm.variables, "approval", res.data.projectLeader);
            complete(this.taskForm).then(response => {
              this.$modal.msgSuccess(response.msg);
              this.$emit("goBack");
            });
          }
        })
      });
    },
    async handleUrge(type) {
      let message = ''
      let user = []
      let userStirngList = []
      if (type == '1') {
        let { data } = await getUserByPost({ postName: "专职安全员" });
        user.push(data[0].userId)
        message = "<font color='warning'>请尽快进行安全交底：</font>  \n>" +
          "项目编号：" + this.project.projectNumber + "  \n>" +
          "项目名称：" + this.project.projectName + "  \n>"
      } else if (type == '2') {
        message = "<font color='warning'>请尽快进行技术交底：</font>   \n>" +
          "项目编号：" + this.project.projectNumber + "  \n>" +
          "项目名称：" + this.project.projectName + "  \n>"
      }
      let deptList = (this.project.undertakingDept.split(',')).map(Number)
      for (let d of deptList) {
        let { data } = await getUsersDeptLeaderByDept({ deptId: d });
        user.push(data.userId)
      }
      user.push(this.projectLeader)
      for (let u of user) {
        let { data } = await getUser(u)
        userStirngList.push(data.pinyin)
      }
      let userString = userStirngList.join('|')
      this.$confirm('是否发送企业微信消息？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let formData = new FormData();
        formData.append('message', message)
        formData.append('userString', userString + '|YuSiHan|WangRongHua')
        sendQyMessage(formData).then(() => {
          this.$message.success('消息已发送！')
        })
      })
    }
  },
}
</script>

<style lang="scss" scoped>
@import "@/assets/styles/element-reset.scss";

::v-deep .el-textarea.is-disabled .el-textarea__inner {
  color: #121212 !important;
}

::v-deep .el-checkbox__input.is-disabled.is-checked .el-checkbox__inner::after {
  border-color: #fff;
}

::v-deep .el-checkbox__input.is-disabled.is-checked .el-checkbox__inner {
  background-color: #409eff;
  border-color: #409eff;
}

::v-deep .el-checkbox__input.is-disabled+span.el-checkbox__label {
  color: #000;
}

::v-deep .el-checkbox__label {
  font-size: 12px;
}
</style>