<!--
 * @Author: ysh
 * @Date: 2024-05-10 14:45:03
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2026-03-26 16:39:47
-->
<template>
  <div class="mt20">
    <div class="nav-btn" v-if="taskName != ''">
      <div>
        <el-button class="mb20" type="primary" plain size="mini" icon="el-icon-plus" v-if="taskName == '项目生产'"
          @click="addSettleFlow">结算申请</el-button>
      </div>
      <div>
        <el-button icon="el-icon-refresh" circle size="mini" @click="getSettleData()"></el-button>
      </div>
    </div>

    <el-table :data="dataList" style="width: 100%" class="custom-table">
      <el-table-column type="index" label="序号" width="50" />
      <el-table-column label="上报人" prop="reporterName"  width="110"/>
      <el-table-column label="申请时间" prop="reportTime" width="110" />
      <el-table-column label="综合事务部意见" prop="zhComment" show-overflow-tooltip width="110" />
      <el-table-column label="技术质量与安全部意见" prop="jsComment" show-overflow-tooltip width="110" />
      <el-table-column label="项目管理部意见" prop="xmComment" show-overflow-tooltip width="110" />
      <el-table-column label="承担部门意见" prop="deptComment" show-overflow-tooltip width="110" />
      <el-table-column label="经营管理部意见" prop="jyComment" show-overflow-tooltip width="110" />
      <el-table-column label="分管审核意见" prop="managerComment" show-overflow-tooltip width="110" />
      <el-table-column label="总经理意见" prop="gmComment" show-overflow-tooltip width="110" />
      <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%">
      <settle-print :form="clickRow" :chooseProject="chooseProject" :workList="workList" :settleList="summaryList"
        @cancel="open = false"></settle-print>
    </el-dialog>
  </div>
</template>

<script>
import { getProject } from "@/api/oa/project/project";
import { listSettle, getSettle, delSettle, addSettle, updateSettle } from "@/api/oa/settle/settle";
import { listSettleWork } from '@/api/oa/settle/settleWork';
import { listSettleSummary } from '@/api/oa/settle/settleSummary';
import { getPrice } from "@/api/oa/price/price";
import { listDefinition } from "@/api/flowable/definition";
import { getNextFlowNodeByStart } from "@/api/flowable/todo";
import { definitionStart } from "@/api/flowable/definition";
import { todoList } from "@/api/flowable/todo";
import { MessageBox } from 'element-ui'
import { listUser, getUser } from "@/api/system/user";
import { Snowflake } from '@/utils/snowFlake.js'
import SettleForm from '../settleForm.vue';
import SettlePrint from '../components/print/settlePrint.vue';
export default {
  components: {
    SettleForm,
    SettlePrint
  },
  props: {
    taskForm: {
      type: Object,
      required: true
    },
    taskName: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      open: false,
      dataList: [],
      definitionList: [], //流程列表
      summaryList: [],
      workList: [],
      settleList: [],
      userList: [],
      chooseProject: {},
      clickRow: {}
    }
  },
  mounted() {
    this.getUserList();
    this.getSettleData();
    this.listDefinition();
    if (this.taskName != '项目登记')
      this.getChooseProject();
  },
  methods: {
    getUserList() {
      listUser({
        pageNum: 1,
        pageSize: 9999
      }).then(res => {
        if (res.code == 200) {
          this.userList = res.rows
        }
      })
    },
    getSettleData() {
      listSettle({ pageSize: 9999, projectId: this.taskForm.formId }).then(res => {
        if (res.code == 200) {
          this.dataList = res.rows
          for (let row of res.rows) {
            this.getReporter(row);
          }
        }
      })
    },
    getChooseProject() {
      getProject(this.taskForm.formId).then(res => {
        if (res.data)
          this.chooseProject = res.data
      })
    },
    getSettleWorkList(settleId) {
      listSettleWork({ pageNum: 1, pageSize: 9999, settleId }).then(res => {
        if (res.code == 200) {
          this.workList = res.rows;
          for (let work of this.workList) {
            getPrice(work.priceId).then(res => {
              if (res.data) {
                this.$set(work, "scale", res.data.scaleGrade);
                this.$set(work, "unit", res.data.unit);
                if (work.groundType == "0") {
                  work.price = res.data.commonPrice;
                } else {
                  work.price = res.data.complexPrice;
                }
              }
            });
          }
        }
      })
    },
    getReporter(row) {
      getUser(row.reporter).then(res => {
        this.$set(row, 'reporterName', res.data.nickName)
      })
    },
    getSummaryList(settleId) {
      listSettleSummary({ settleId: settleId, pageSize: 20 }).then(res => {
        if (res.rows) {
          this.summaryList = res.rows
        }
      })
    },
    listDefinition() {
      listDefinition({
        pageNum: 1,
        pageSize: 9999,
        name: '项目结算'
      }).then(response => {
        this.definitionList = response.data.records;
      });
    },
    handleLook(row) {
      this.clickRow = row
      this.clickRow.zhUserName = this.getUserNickName(row.zhUserId);
      this.clickRow.jsUserName = this.getUserNickName(row.jsUserId);
      this.clickRow.xmUserName = this.getUserNickName(row.xmUserId);
      this.clickRow.deptUserName = this.getUserNickName(row.deptUserId);
      this.clickRow.jyUserName = this.getUserNickName(row.jyUserId);
      this.clickRow.managerUserName = this.getUserNickName(row.managerUserId);
      this.clickRow.gmUserName = this.getUserNickName(row.gmUserId);
      this.getSummaryList(row.settleId)
      this.getSettleWorkList(row.settleId)
      this.open = true
    },
    getUserNickName(id) {
      for (let user of this.userList) {
        if (user.userId == id) {
          return user.nickName
        }
      }
    },
    addSettleFlow() {
      let row = this.definitionList[0];
      let formId = new Snowflake(1n, 1n, 0n).nextId().toString();
      getNextFlowNodeByStart({ deploymentId: row.deploymentId, variables: { formId: formId } }).then(res => {
        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/settleApply',
                  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>