<template>
  <div class="mt20" style="width:99%">
    <div class="nav-btn" v-if="taskName != ''">
      <div>
        <el-button class="mb20" type="primary" plain size="mini" icon="el-icon-plus" @click="addAchi">成果汇交</el-button>
      </div>
      <div>
        <el-button icon="el-icon-refresh" circle size="mini" @click="getAchiData()"></el-button>
      </div>
    </div>

    <el-table :data="achiList" style="width: 100%" class="custom-table">
      <el-table-column type="index" label="序号" width="50" />
      <el-table-column label="成果汇交时间" prop="submitTime" width="100" />
      <el-table-column label="生产部门检查员" prop="produceInspector" width="120">
        <template slot-scope="scope">
          {{ findUserName(scope.row.produceInspector) }}
        </template>
      </el-table-column>
      <el-table-column label="质量检查员" prop="qualityInspector" show-overflow-tooltip width="120">
        <template slot-scope="scope">
          {{ findUserName(scope.row.qualityInspector) }}
        </template>
      </el-table-column>
      <el-table-column label="技术部负责人" prop="deptLeader" show-overflow-tooltip width="120">
        <template slot-scope="scope">
          {{ findUserName(scope.row.deptLeader) }}
        </template>
      </el-table-column>
      <el-table-column label="归档人" prop="archiver" show-overflow-tooltip width="120">
        <template slot-scope="scope">
          {{ findUserName(scope.row.archiver) }}
        </template>
      </el-table-column>
      <el-table-column label="资料接受时间" prop="receiveTime" show-overflow-tooltip width="120" />
      <el-table-column label="质量检查时间" prop="qualityTime" show-overflow-tooltip width="120" />
      <el-table-column label="生产部门检查时间" prop="produceTime" show-overflow-tooltip width="130" />
      <el-table-column label="归档时间" prop="archiveTime" show-overflow-tooltip width="120" />
      <el-table-column label="生产部门意见" prop="produceComment" show-overflow-tooltip width="120" />
      <el-table-column label="质量检查意见" prop="qualityComment" show-overflow-tooltip width="120" />
      <el-table-column label="技术部审核意见" prop="deptComment" show-overflow-tooltip width="120" />
      <el-table-column label="归档意见" prop="archiveComment" show-overflow-tooltip width="120" />
      <el-table-column label="操作" fixed="right" header-align="center">
        <template slot-scope="scope">
          <el-button icon="el-icon-view" type="text" size="mini" @click="handleLook(scope.row)">查看明细</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog title=" 成果明细" :visible.sync="open" append-to-body width="65%">
      <archive-form :taskForm="openObj" :taskName="''" :isFlow="false"></archive-form>
      <!-- <el-form ref="form" :model="form" label-width="150px">
        <el-form-item label="项目信息:" prop="projectName">
          {{ form.projectName }}
        </el-form-item>
        <el-form-item label="成果汇交时间:" prop="submitTime">
          {{ form.submitTime }}
        </el-form-item>
        <el-form-item label="生产部门检查员:" prop="submitTime">
          {{ findUserName(form.produceInspector) }}
        </el-form-item>
        <el-form-item label="质量检查员:" prop="submitTime">
          {{ findUserName(form.qualityInspector) }}
        </el-form-item>
        <el-form-item label="技术部负责人:" prop="submitTime">
          {{ findUserName(form.deptLeader) }}
        </el-form-item>
        <el-form-item label="归档人:" prop="submitTime">
          {{ findUserName(form.archiver) }}
        </el-form-item>
        <el-form-item label="资料接受时间:" prop="submitTime">
          {{ form.receiveTime }}
        </el-form-item>
        <el-form-item label="质量检查时间:" prop="submitTime">
          {{ form.qualityTime }}
        </el-form-item>
        <el-form-item label="生产部门检查时间:" prop="submitTime">
          {{ form.produceTime }}
        </el-form-item>
        <el-form-item label="归档时间:" prop="submitTime">
          {{ form.archiveTime }}
        </el-form-item>
      </el-form> -->
    </el-dialog>
  </div>
</template>

<script>
import { listArchive } from "@/api/oa/archive/archive"
import { listDefinition } from "@/api/flowable/definition";
import { getNextFlowNodeByStart } from "@/api/flowable/todo";
import { definitionStart, flowXmlAndNode } from "@/api/flowable/definition";
import { getProject } from "@/api/oa/project/project";
import { todoList } from "@/api/flowable/todo";
import { Snowflake } from '@/utils/snowFlake.js'
import { MessageBox } from 'element-ui'
import archiveForm from '../archiveForm.vue';
export default {
  components: { archiveForm },
  props: {
    taskForm: {
      type: Object,
      required: true
    },
    taskName: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      achiList: [],
      definitionList: [],
      open:false,
      openObj:{},
      form:{
        projectName:''
      }
    }
  },
  mounted() {
    this.getAchiData()
    this.listDefinition();
  },
  methods: {
    getAchiData() {
      listArchive({ projectId: this.taskForm.formId }).then(res => {
        if (res.code == 200) {
          this.achiList = res.rows
        }
      })
    },
    findUserName(id) {
      let userList = this.$store.state.user.userList
      let user = userList.find(obj => obj.userId == id)
      if (user)
        return user.nickName
      else {
        return ''
      }
    },
    listDefinition() {
      listDefinition({
        pageNum: 1,
        pageSize: 9999,
        name: '成果归档'
      }).then(response => {
        this.definitionList = response.data.records;
      });
    },
    handleLook(row) {
      this.form= row
      this.openObj.formId = row.archiveId
      getProject(row.projectId).then(res=>{
        if(res.data)
          this.$set(this.form,'projectName',res.data.projectNumber + '-' + res.data.projectName)
        this.open = true
      })
    },
    addAchi() {
      let row = this.definitionList[0];
      let formId = new Snowflake(1n, 1n, 0n).nextId().toString();
      getNextFlowNodeByStart({ deploymentId: row.deploymentId, variables: { formId: formId } }).then(res => {
        let data = res.data
        const variables = {};
        const formData = {};
        formData.formId = formId
        if (row.id) {
          MessageBox.confirm('是否发起成果归档？', '系统提示', { confirmButtonText: '确认', cancelButtonText: '取消', type: 'warning' }).then(() => {
            variables.variables = formData;
            // 启动流程并将表单数据加入流程变量
            definitionStart(row.id, JSON.stringify(variables)).then(res => {
              this.$modal.msgSuccess(res.msg);
              let procInstanceId = res.data;
              todoList({
                pageNum: 1,
                pageSize: 99999999,
                processInsId: procInstanceId
              }).then(toDoRes => {
                let records = toDoRes.data.records;
                if (records.length == 1) {
                  records = records[0]
                }
                this.$router.push({
                  path: '/applyForm/archive',
                  query: {
                    procInsId: records.procInsId,
                    executionId: records.executionId,
                    deployId: records.deployId,
                    taskId: records.taskId,
                    taskName: records.taskName,
                    startUser: records.startUserName + '--' + records.startDeptName,
                    formId: formData.formId,
                    procDefName: records.procDefName,
                    projectId: this.taskForm.formId
                  }
                })
              })
            })
          })
        }
      })
    },
  }
}
</script>

<style lang="scss" scoped>
.nav-btn {
  display: flex;
  justify-content: space-between;
}

.custom-table {
  display: block;
  /* 确保表格不会超出容器 */
  overflow-x: auto;
  /* 横向滚动条 */
}
</style>