<!--
 * @Author: ysh
 * @Date: 2024-04-08 13:56:14
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2024-07-31 16:56:06
-->
<template>
  <div class="app-container">
    <h2 style="text-align:center;">生产确认</h2>
    <el-form ref="scConfirm" :model="form" :rules="rules" label-width="100px" :disabled="disabled">
      <el-form-item label="确认信息：" prop="scComment">
        <el-input type="textarea" :rows="4" placeholder="请输入确认信息" v-model="form.scComment">
        </el-input>
      </el-form-item>
      <el-form-item label="签名：">
        <span class="auditor">{{ form.scApproverName }}</span>
        <el-button @click.once="sign" type="primary" size="mini" v-if="taskName == '生产确认'">签名</el-button>
      </el-form-item>
      <el-form-item label="确认时间：">
        {{ form.scApprovalTime }}
      </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'
import { getProject } from '@/api/oa/project/project'
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: {
        scApprover: '',
        scApproverName: '',
        scApprovalTime: ''
      },
      rules: {
        scComment: [{ 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
          this.form.scApprover = '';
          if (res.data.scApproverName) {
            this.form.scApproverName = res.data.scApproverName
          }
        } else {
          this.form.scApprover = '';
          this.form.scApproverName = '';
          this.form.scApprovalTime = '';
        }
        // if (res.data) {
        //   this.form = res.data
        //   debugger
        //   if (this.taskName == '生产确认') {
        //     if (res.data.scApproverName) {
        //       this.form.scApproverName = res.data.scApproverName
        //       this.form.scApprover = this.$store.state.user.id;
        //     } else {
        //       this.form.scApprover = this.$store.state.user.id;
        //       this.form.scApproverName = this.$store.state.user.name;
        //       this.form.scApprovalTime = parseTime(new Date(), '{y}-{m}-{d}')
        //     }
        //   } else {
        //     if (res.data.scApproverName)
        //       this.form.scApproverName = res.data.scApproverName
        //   }
        // } else {
        //   if (this.taskName == '生产确认') {
        //     this.form.scApprover = this.$store.state.user.id;
        //     this.form.scApproverName = this.$store.state.user.name;
        //     this.form.scApprovalTime = parseTime(new Date(), '{y}-{m}-{d}')
        //   } else {
        //     this.form.scApprover = '';
        //     this.form.scApprovalTime = '';
        //   }
        // }
      })
    },
    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)
        })
      }
    },
    sign() {
      this.form.scApprover = this.$store.state.user.id;
      if (this.form.scApproverName != null && this.form.scApproverName != undefined && this.form.scApproverName != '') {
        if (this.form.scApproverName.indexOf(this.$store.state.user.name) == -1) {
          let sign = this.form.scApproverName + ',' + this.$store.state.user.name;
          this.form.scApproverName = sign
        }
      } else {
        this.form.scApproverName = this.$store.state.user.name;
      }
      this.form.scApprovalTime = parseTime(new Date(), '{y}-{m}-{d}')
    },
    confirm() {
      this.$refs['scConfirm'].validate((valid) => {
        if (valid) {
          this.form.projectId = this.taskForm.formId
          if (this.form.scApprover == '' || this.form.scApprover == null) {
            if (this.form.scApproverName != '' && this.form.scApproverName != null) {
              this.form.scApprover = this.$store.state.user.id;
            } else {
              this.$modal.msgError('请签名');
              return
            }
          }
          updateProjectComment(this.form)
          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");
                });
              }
            })

          });
        } else {
          this.$modal.msgError('必填项未写');
          return false;
        }
      })

    },

  },
}
</script>

<style lang="scss" scoped>
.auditor {
  font-family: 'xdks';
  font-size: 20px;
  margin-right: 20px;
}

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