<!--
  @description: 采购计划-集采计划打包-集采转自采编辑组件、工作流编辑共用
  @author: lxl
  @date： 2020-11-03 14:32
-->
<template>
  <div>
    <section>
      <el-table
        v-loading="loading"
        class="tableLimit"
        :data="tableData"
      >
        <el-table-column align="center" width="60" type="index" label="序号" />
        <el-table-column prop="purchaseNo" min-width="150" align="center" label="行项目编码" />
        <el-table-column prop="purchaseCode" header-align="center" min-width="180" align="left" label="物资编码" />
        <el-table-column prop="purchaseName" header-align="center" min-width="220" align="left" label="物资信息" />
        <el-table-column prop="purchaseDepartName" header-align="center" min-width="220" align="left" label="上报单位" />
        <el-table-column prop="purchaseSpecification" header-align="center" min-width="220" align="left" label="物资规格" />
      </el-table>
      <pagination :pagination-data="paginationData" @change="loadData" />
    </section>
    <el-form
      ref="formData"
      :rules="rules"
      :model="formData"
      label-position="left"
      :inline-message="true"
    >
      <table class="from_table">
        <tr>
          <td>
            <el-form-item label="附件信息" />
          </td>
          <td>
            <uploadFile :file-data="lineItemFileType" />
          </td>
        </tr>
        <tr>
          <td>
            <el-form-item label="采购单含税总预算(元)" />
          </td>
          <td>{{ formData.budgetTotalTaxAmout && numFilter(formData.budgetTotalTaxAmout) }}</td>
        </tr>
        <tr>
          <td>
            <el-form-item
              label="转自采原因:"
              prop="bizReason"
            />
          </td>
          <td>
            <gx-input
              v-model.trim="formData.bizReason"
              :autosize="{ minRows: 3, maxRows: 3 }"
              type="textarea"
              gx-maxlength="varchar500"
              show-word-limit
            />
          </td>
        </tr>
      </table>
    </el-form>
    <!--保存按钮 工作流驳回进入显示-->
    <div class="but_footer">
      <div class="but_footer-h">
        <div class="but_footer-but">
          <el-button :loading="confirmLoading" type="primary" @click="ensure">
            提 交
          </el-button>
          <el-button @click="handleClose">
            关 闭
          </el-button>
        </div>
      </div>
    </div>
    <!--工作流选择节点组件-->
    <processNodeCom
      ref="processNode"
      :process-definition-key="processDefinitionKey"
      @processNodeData="processNodeData"
    />
    <!--工作流流程节点/获取审核用户-->
    <departDialog
      ref="departDialog"
      :process-node="processNode"
      @isSingleUser="isSingleUser"
      @processInitData="processInitData"
      @submit="submit"
    />
  </div>
</template>

<script>
/* api*/
import {
  purchasePlanDetailsWaitPackAudit, // 初次提交
  modifyProtPurchaseTenderMethodPage, // 初始化数据
  purchasePlanDetailsAuditPage, // 编辑提交页面回显
  purchasePlanDetailsReSubmitAudit // 编辑提交
} from '@/api/procurement/purchasplan'
// 公用
import { filterDictText, numFilter } from '@/assets/js/utils' // 字典方法
import { ListMixin } from '@/mixins/ListMixin' // 分页
import pagination from '@comp/common/pagination/pagination' // 分页组件
import { paging, randomData } from '@/assets/js/utils/index.js' // 前端分页
import { characters1_auto } from '@/assets/js/validate'// 校验输入内容
import uploadFile from '@/views/procurement/procurementPlan/components/uploadFile' // 附件上传
import { purPlanBaleMixins } from '../mixins/purPlanBaleMixins' // 工作流配置（是否启用工作流）

/* ==========工作流===========*/
import { workCommonMixins } from '@views/work/mixins/workCommonMixins' // 工作流公共配置
import workconstant from '@/assets/js/workutils/workconstant/procurement' // 配置流程key
import processNodeCom from '@views/work/processNodeCom' // 工作流选择节点组件
import departDialog from '@views/work/depart-dialog' // 选择审核人员
/* ============================*/
export default {
  name: 'TurnAdd',
  components: {
    pagination, // 分页
    processNodeCom, // 工作流选择节点组件
    departDialog, // 选择审核人员
    uploadFile // 附件上传
  },
  mixins: [ListMixin, workCommonMixins, purPlanBaleMixins],
  props: {
    // 弹框数据
    dialogTurnData: {
      type: Object,
      default: () => {
        return {
          bizAuditId: ''
        }
      }
    },
    paramsId: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      tableData: [], // 表格数据
      flowStatus: this.$route.query.flowStatus, // true 工作流进入驳回编辑
      loading: true,
      // 附件参数
      lineItemFileType: {
        bizId: '',
        fileType: 'biz_type_transfer_to_self_gateway'
      },
      confirmLoading: false, // 按钮loading
      processDefinitionKey: '', // 工作流流程id
      formData: {
        bizReason: '', // 转自采原因
        explainFileId: '', // 附件主键id
        budgetTotalTaxAmout: '' // 采购单含税总预算
      },
      bizAuditId: '',
      rules: {
        bizReason: [{ required: true, validator: characters1_auto, trigger: 'blur' }] // 转自采原因
      }
    }
  },
  created() {
    if (this.flowStatus) {
      this.loadData() // 数据初始化
    }
    this.purchasplanChangeAudit() // 集采转自采获取工作流配置
  },
  methods: {
    filterDictText, // 字典
    numFilter, // 金额
    // 设置参数
    setData() {
      this.bizAuditId = this.dialogTurnData.bizAuditId || this.$route.query.bizId // 获取bizAuditId
      this.processDefinitionKey = workconstant.purPlanTypeTransferToSelf // 工作流流程key
      this.formData.explainFileId = this.flowStatus ? this.$route.query.packId : randomData() // 新增时随机生成ID
      this.lineItemFileType.bizId = this.formData.explainFileId
    },
    // 初始化接口方法
    loadData() {
      this.setData() // 参数设置
      const params = { planDetailIds: this.paramsId }
      // turnKey 00 普通提交 01 再次提交
      if (this.dialogTurnData.turnKey === '00') {
        modifyProtPurchaseTenderMethodPage(params).then(response => {
          this.loading = false
          this.formData.budgetTotalTaxAmout = response.data.budgetTotalTaxAmout
          this.tableData = paging({}, response.data.list, this.paginationData)
        }).catch(() => {
          this.loading = false
        })
      } else {
        purchasePlanDetailsAuditPage({ bizAuditId: this.bizAuditId }).then((response) => {
          this.loading = false
          this.formData.bizReason = response.data.bizReason
          this.formData.budgetTotalTaxAmout = response.data.budgetTotalTaxAmout
          this.tableData = paging({}, response.data.packList, this.paginationData)
        }).catch(() => {
          this.loading = false
        })
      }
    },
    // 集采转自采点击确定接口方法
    purchasePlanDetail() {
      // this.processInitDto.type = '01' // 01：初次提交 02：审核驳回再次提交
      // 初次提交参数
      const params = {
        // bizTypeCode: 00-集采转自采;01-退回计划;02-采购方式变更;03-业务部退回计划;04-【结果执行确认】重新打包采购
        bizReason: this.formData.bizReason,
        explainFileId: this.formData.explainFileId, // 附件id
        bizTypeCode: '00',
        packList: this.tableData
        // processDefinitionKey: this.processDefinitionKey, // 工作流节点key
        // processInitDto: this.processInitDto // 审核人信息传回得信息
      }
      // 判断是否进入工作流 true进入工作流
      if (this.purchasplanChangeAuditConfig) {
        params.processDefinitionKey = this.processDefinitionKey // 工作流节点key
        params.processInitDto = this.processInitDto // 审核人信息传回得信息
      }
      // 再次编辑提交参数
      const paramsData = {
        // bizReason: this.formData.bizReason, // 转自采原因
        // explainFileId: this.formData.explainFileId, // 附件id
        bizAuditId: this.bizAuditId,
        ...params
        // bizTypeCode: '00',
        // processInitDto: this.processInitDto // 审核人信息传回得信息
      }
      // turnKey 00 普通提交 01 再次提交
      if (this.dialogTurnData.turnKey === '00') {
        purchasePlanDetailsWaitPackAudit(params).then(response => {
          this.handleClose()
        }).catch(() => {
        })
      } else {
        delete paramsData.packList
        purchasePlanDetailsReSubmitAudit(paramsData).then(response => {
          this.handleClose()
        }).catch(() => {
        })
      }
    },
    ensure() {
      this.$refs['formData'].validate((valid, field) => {
        if (valid) { // 校验通过
          this.confirmLoading = true
          this.$confirm('是否确定提交？', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            confirmButtonClass: 'confirmBtn',
            cancelButtonClass: 'cancelBtn',
            type: 'warning'
          }).then(() => {
            this.confirmLoading = false
            // 判断是否进入工作流 true进入工作流
            if (this.purchasplanChangeAuditConfig) {
              // 判断审核人员是否是多个
              if (JSON.stringify(this.processNode) !== '{}' && this.processNode.nodeId) {
                this.$refs.departDialog.getUserDepart(this.processNode)
              }
            } else {
              // 直接提交
              this.submit()
            }
          }).catch(() => {
            this.confirmLoading = false
          })
        } else {
          this.confirmLoading = false
          this.$message.warning('请注意页面提示信息')
          return false
        }
      })
    },
    // 审批流提交
    submit() {
      this.purchasePlanDetail()
    },
    handleClose() {
      if (this.flowStatus) {
        const routePathStatus = sessionStorage.getItem('routePathStatus') // 工作流处理 00桌面进入 01待办列表进入
        this.$router.push({
          path: routePathStatus === '00' ? '/home' : '/work/todo'
        })
      } else {
        this.$refs.formData.clearValidate()
        this.formData.bizReason = ''
        this.$emit('handleCloseDialog')
      }
    }
  }
}
</script>
