<!--
  @description: 采购计划-集采计划打包-退回计划/退回上报单位-组件
  @author: lxl
  @date： 2020-11-03 11:32
-->
<template>
  <div>
    <el-table
      :data="tableData"
    >
      <el-table-column width="60" align="center" type="index" label="序号" />
      <el-table-column min-width="150" prop="purchaseNo" align="center" label="行项目编码" />
      <el-table-column min-width="150" prop="purchaseCode" align="center" label="物资编码" />
      <el-table-column min-width="300" prop="purchaseName" header-align="center" align="left" label="物资信息" />
      <el-table-column min-width="100" prop="purchaseNum" header-align="center" align="right" label="数量" />
      <el-table-column min-width="130" header-align="center" align="right" label="预算净单价(元)">
        <template slot-scope="scope">
          {{ scope.row.purchaseBudget && numFilter(scope.row.purchaseBudget) }}
        </template>
      </el-table-column>
      <el-table-column min-width="130" header-align="center" align="right" label="预算净总价(元)">
        <template slot-scope="scope">
          {{ scope.row.purchaseTotalBudget && numFilter(scope.row.purchaseTotalBudget) }}
        </template>
      </el-table-column>
    </el-table>
    <pagination :pagination-data="paginationData" @change="loadData" />
    <el-form
      ref="formData"
      label-position="left"
      :model="formData"
      :rules="rules"
      :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="dialogBackPlanData.bizTypeCode==='01'?'退回计划原因':'退回上报单位原因:'"
              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 { ListMixin } from '@/mixins/ListMixin' // 分页
import pagination from '@comp/common/pagination/pagination' // 分页组件
import { paging, randomData, numFilter, filterDictText } from '@/assets/js/utils/index.js' // 前端分页
import uploadFile from '@/views/procurement/procurementPlan/components/uploadFile' // 附件上传
import { characters1_auto } from '@/assets/js/validate'// 校验输入内容
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: 'BackPlanAdd',
  components: {
    pagination,
    processNodeCom, // 工作流节点信息组件
    departDialog, // 工作流审批人组件
    uploadFile // 附件上传
  },
  mixins: [ListMixin, workCommonMixins, purPlanBaleMixins],
  props: {
    dialogBackPlanData: {
      type: Object,
      default: () => {
        return {
          bizAuditId: '', // 审核表id
          bizTypeCode: '' // 业务类型
        }
      }
    },
    paramsId: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      flowStatus: this.$route.query.flowStatus, // true 工作流进入驳回编辑
      // 附件参数
      lineItemFileType: {
        bizId: '',
        fileType: 'biz_type_business_department_return_plan'
      },
      bizAuditId: '', // 审核表id
      formData: {
        explainFileId: '', // 附件主键id
        bizReason: '' // 原因
      },
      confirmLoading: false,
      processDefinitionKey: '', // 工作流流程key
      tableData: [], // 表格数据
      rules: {
        bizReason: [{ required: true, validator: characters1_auto, trigger: 'blur' }]
      }
    }
  },
  created() {
    this.purchasplanChangeAudit() // 获取是否启用工作流
    this.processDefinitionKey = workconstant.purPlanReturn // 工作流流程key
    this.handelFlow() // 工作流进入数据初始化
  },
  methods: {
    filterDictText, // 字典替换
    numFilter, // 金额补零
    // 处理是否工作流进入
    handelFlow() {
      // flowStatus true 工作流进入
      if (this.flowStatus) {
        this.dialogBackPlanData.bizAuditId = this.$route.query.bizId // 审核表id
        this.dialogBackPlanData.bizTypeCode = this.$route.meta.bizTypeCode // 业务类型
        this.formData.explainFileId = this.$route.query.packId // 新增时随机生成ID
        this.loadData()
      } else {
        this.formData.explainFileId = randomData() // 新增时随机生成ID
      }
      this.lineItemFileType.bizId = this.formData.explainFileId // 附件id
    },
    // 初始化接口方法
    loadData() {
      const params = { planDetailIds: this.paramsId }
      if (this.dialogBackPlanData.backKey === '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.dialogBackPlanData.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, // 原因
        bizTypeCode: this.dialogBackPlanData.bizTypeCode,
        explainFileId: this.formData.explainFileId, // 附件id
        packList: this.tableData // table数据集合
        // processInitDto: this.processInitDto // 审核人信息传回得信息
      }
      // 判断是否进入工作流 true进入工作流
      if (this.purchasplanChangeAuditConfig) {
        params.processInitDto = this.processInitDto // 审核人信息传回得信息
      }
      // 再次编辑提交参数
      const paramsData = {
        bizAuditId: this.dialogBackPlanData.bizAuditId, // 审核表id
        ...params
      }
      if (this.dialogBackPlanData.backKey === '00') {
        purchasePlanDetailsWaitPackAudit(params).then(response => {
          this.handleClose()
        }).catch(() => {
        })
      } else {
        delete paramsData.packList
        purchasePlanDetailsReSubmitAudit(paramsData).then(response => {
          this.handleClose()
        }).catch(() => {
        })
      }
    },
    // 审批流提交
    submit() {
      this.purchasePlanDetail()
    },
    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
        }
      })
    },
    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>

<style scoped>

</style>
