<!--
 * @Author: ysh
 * @Date: 2024-07-08 16:16:27
 * @LastEditors: wrh
 * @LastEditTime: 2025-02-12 15:22:45
-->
<template>
  <div class="app-container">
    <h2 style="text-align: center" id="scrollToMe">核定工作</h2>
    <el-form :model="form" :disabled="disabled" label-width="100px">
      <el-form-item label="项目任务书：">
        <div v-if="form.taskDocument">
          <el-link type="primary" @click="reviewWord(`${baseUrl}${'/profile/upload' + form.taskDocument}`)">
            {{ getFileName(form.taskDocument) }}
          </el-link>
          <el-link class="ml20" type="warning" :href="`${baseUrl}${'/profile/upload' + form.taskDocument}`"
            :underline="false" target="_blank">
            <span class="el-icon-download">下载文件</span>
          </el-link>
        </div>
      </el-form-item>
      <el-form-item label-width="0px">
        <table class="table" border="1">
          <tr>
            <td style="width: 180px">工作内容</td>
            <td>等级或比例尺</td>
            <td>单位</td>
            <td>工作量</td>
            <td style="width: 100px">要求完成时间</td>
            <td>备注</td>
          </tr>
          <tr v-for="(work, index) in workList" :key="index">
            <td>
              <el-input v-model="work.content" type="textarea" clearable
                :autosize="{ minRows: 4, maxRows: 10 }"></el-input>
            </td>
            <td>
              <el-select v-model="work.scale" placeholder="请选择" clearable>
                <el-option v-for="dict in dict.type.cmc_scale_grade" :key="dict.value" :label="dict.label"
                  :value="dict.label" />
              </el-select>
            </td>
            <td>
              <el-select v-model="work.unit" placeholder="请选择" clearable>
                <el-option v-for="dict in dict.type.cmc_unit" :key="dict.value" :label="dict.label"
                  :value="dict.label" />
              </el-select>
            </td>
            <td>
              <el-input v-model="work.workload" clearable></el-input>
            </td>
            <td>
              <el-date-picker style="width: 140px" v-model="work.deadline" value-format="yyyy-MM-dd" type="date"
                placeholder="选择日期">
              </el-date-picker>
            </td>
            <td>
              <el-input v-model="work.remark" type="textarea" clearable
                :autosize="{ minRows: 4, maxRows: 10 }"></el-input>
            </td>
            <td>
              <el-button type="text" style="color:#F56C6C" size="mini" @click="deletWorkItem(index)">删除行</el-button>
            </td>
          </tr>
        </table>
        <el-button icon="el-icon-plus" type="primary" plain size="mini" @click="addWorkList"></el-button>
      </el-form-item>
    </el-form>

    <div slot="footer" class="dialog-footer" style="text-align: center;">
      <el-button type="danger" @click="$emit('returnBack')" :disabled="disabled">退 回</el-button>
      <el-button type="warning" @click="save" :disabled="disabled">保 存</el-button>
      <el-button type="primary" @click="confirm" :disabled="disabled">提 交</el-button>
    </div>

  </div>
</template>

<script>
import { listProject, getProject, addProject, updateProject, delProject } from "@/api/oa/project/project";
import { listProjectWork, addProjectWork, delProjectWork } from "@/api/oa/project/projectWork";
import { getUsersDeptLeaderByDept, getDeptLeadersByDeptId } from "@/api/system/post";
import { complete, getNextFlowNode } from "@/api/flowable/todo";
export default {
  dicts: ['cmc_scale_grade', 'cmc_unit'],
  props: {
    disabled: {
      type: Boolean
    },
    taskForm: {
      type: Object,
      required: true
    },
    taskName: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      baseUrl: process.env.VUE_APP_BASE_API,
      form: {},
      total: 0,
      workList: [
        {
          content: "",
          scale: "",
          unit: "",
          workload: "",
          deadline: undefined,
          remark: "",
        },
      ],
    }
  },
  created() {

  },
  mounted() {
    if (this.taskName != '项目登记')
      this.initForm();
  },
  methods: {
    async initForm() {
      let projectRes = await getProject(this.taskForm.formId)
      if (projectRes.code == 200 && projectRes.data) {
        this.form = projectRes.data
        if (this.form.undertakingDept) {
          let arr = (this.form.undertakingDept.split(',')).map(Number)
          this.getDeptLeader(arr)
        }
        this.total = 1
        this.getProjectWorkList();
      }
    },
    getProjectWorkList() {
      listProjectWork({ pageNum: 1, pageSize: 100, projectId: this.taskForm.formId }).then(res => {
        if (res.rows.length != 0) {
          this.workList = res.rows;
        }
      })
    },
    addWorkList() {
      this.workList.push({
        content: "",
        scale: "",
        unit: "",
        workload: "",
        deadline: undefined,
        remark: "",
      });
    },
    deletWorkItem(index) {
      let arr = this.workList;
      if (arr.length == 1) {
        return;
      }
      if (index >= 0 && index < arr.length) {
        arr.splice(index, 1);
      }
    },
    // 更新工作量
    async updateWorkList() {
      await delProjectWork(this.taskForm.formId)
      for (let work of this.workList) {
        work.projectId = this.taskForm.formId;
        await addProjectWork(work);
      }
      this.$message.success('保存成功')
    },
    // 保存按钮
    save() {
      if (!this.total) {
        this.workList.forEach(work => {
          work.projectId = this.taskForm.formId;
          addProjectWork(work).then(res => {
            this.$message.success('保存成功')
          });
        })
      } else {
        this.updateWorkList();
      }
    },
    async confirm() {
      this.save();
      const params = { taskId: this.taskForm.taskId };
      getNextFlowNode(params).then(res => {
        complete(this.taskForm).then(response => {
          this.$modal.msgSuccess(response.msg);
          this.$emit('goBack');
        });
      })
    },
    getDeptLeader(val) {
      getUsersDeptLeaderByDept({ deptId: Number(val[0]) }).then(res => {
        this.taskForm.variables = {}
        this.$set(this.taskForm.variables, "approval", res.data.userId);
      })
    }
  },
}
</script>

<style lang="scss" scoped>
table {
  /*居中*/
  margin: 0 auto;
  /*边框*/
  /* border: 1px solid black; */
  text-align: center;
  border-collapse: collapse;
  /*设置背景颜色*/
  /* background-color: #bfa; */
}
</style>