<!--
  @description: 采购计划-集采计划打包-采购方式变更编辑、弹框工作流编辑页面共用
  @author: lxl
  @date： 2020-11-03 10:32
-->
<template>
  <div>
    <section>
      <el-form
        ref="formData"
        :model="formData"
        :inline-message="true"
      >
        <el-table :data="formData.tableData">
          <el-table-column width="60" type="index" label="序号" align="center" />
          <el-table-column min-width="150" prop="purchaseNo" label="行项目编码" align="center" />
          <el-table-column min-width="150" prop="purchaseCode" label="物资编码" align="center" />
          <el-table-column min-width="300" prop="purchaseSpecification" label="物资信息" show-overflow-toolti header-align="center" align="left" />
          <el-table-column min-width="180" label="原采购方式" show-overflow-toolti header-align="center" align="left">
            <template slot-scope="scope">
              {{ filterDictText(tenderMethodList,scope.row.initTenderMethod) }}
            </template>
          </el-table-column>
          <el-table-column
            min-width="180"
            fixed="right"
            header-align="center"
            align="center"
          >
            <template slot="header">
              <span class="red-star">*</span>
              <span>变更后采购方式</span>
            </template>
            <template slot-scope="scope">
              <el-form-item
                :prop="'tableData.' + scope.$index + '.tenderMethod'"
                :rules="formData.rules.tenderMethod"
              >
                <el-select
                  v-model="scope.row.tenderMethod"
                  filterable
                  placeholder="请选择"
                >
                  <el-option
                    v-for="item in tenderMethodList"
                    :key="item.typename"
                    :label="item.typename"
                    :value="item.typecode"
                  />
                </el-select>
              </el-form-item>
            </template>
          </el-table-column>
        </el-table>
        <pagination :pagination-data="paginationData" @change="loadData" />
        <table class="from_table">
          <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>
    </section>
    <!--工作流选择节点组件-->
    <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 { procurementPlanGrounpMixins } from '@views/procurement/procurementPlan/mixins/procurementPlanGroupMixins' // 字典信息
/* 公用方法*/
import { filterDictText } from '@/assets/js/utils' // 字典方法
import { ListMixin } from '@/mixins/ListMixin' // 分页
import pagination from '@comp/common/pagination/pagination' // 分页组件
import { paging } from '@/assets/js/utils/index.js' // 前端分页
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: 'ModifyDialog',
  components: {
    pagination, // 分页
    processNodeCom, // 工作流节点
    departDialog // 选择审核人员
  },
  mixins: [ListMixin, workCommonMixins, purPlanBaleMixins],
  props: {
    dialogModifyData: {
      type: Object,
      default: () => {
        return {
          bizAuditId: ''
        }
      }
    },
    paramsId: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      flowStatus: this.$route.query.flowStatus, // true 工作流进入驳回编辑
      loading: true,
      confirmLoading: false,
      /* --------------字典----------------*/
      tenderMethodList: [], // 采购方式
      dictionaryType: ['tenderMethod'], // 字典请求参数
      /* ------------------------------*/
      formData: {
        tableData: [], // 表格数据
        bizReason: '', // 变更原因
        processDefinitionKey: '', // 工作流节点key
        rules: {
          tenderMethod: { required: true, message: '请选择变更后采购方式', trigger: 'change' },
          bizReason: { required: true, validator: characters1_auto, trigger: 'blur' }
        }
      }
    }
  },
  created() {
    this.purchasplanChangeAudit() // 获取是否启用工作流
    this.processDefinitionKey = workconstant.purPlanChange // 工作流流程key
    this.handelFlow()
    this.getGroupType() // 字典请求初始化
  },
  methods: {
    filterDictText,
    // 字典请求
    getGroupType() {
      this.$store.dispatch('getGroupType', this.dictionaryType).then((data) => {
        // 遍历key存储相应的数据
        for (const key in data.bidTypeData) {
          /* 是否有数据*/
          if (data.bidTypeData[key]) {
            this[key] = data.bidTypeData[key]
          }
        }
      })
    },
    // 处理是否工作流进入
    handelFlow() {
      // flowStatus true 工作流进入
      if (this.flowStatus) {
        this.dialogModifyData.bizAuditId = this.$route.query.bizId
        this.loadData()
      }
    },
    // 初始化接口方法
    loadData() {
      const params = { planDetailIds: this.paramsId }
      const data = {
        bizAuditId: this.dialogModifyData.bizAuditId
      }
      if (this.dialogModifyData.modifyKey === '00') {
        modifyProtPurchaseTenderMethodPage(params).then(response => {
          this.loading = false
          this.formData.tableData = paging({}, response.data.list, this.paginationData)
        }).catch(() => {
          this.loading = false
        })
      } else {
        purchasePlanDetailsAuditPage(data).then(response => {
          this.loading = false
          this.formData.bizReason = response.data.bizReason
          this.formData.tableData = paging({}, response.data.packList, this.paginationData)
        }).catch(() => {
          this.loading = false
        })
      }
    },
    // 变更点击确定接口方法
    purchasePlanDetail() {
      // this.processInitDto.type = '01' // 01：初次提交 02：审核驳回再次提交
      const params = this.getParams()
      // 再次编辑提交参数
      const paramsData = {
        bizReason: this.formData.bizReason,
        bizAuditId: this.dialogModifyData.bizAuditId,
        bizTypeCode: '02'
        // processInitDto: this.processInitDto // 审核人信息传回得信息
      }
      // 判断是否进入工作流 true进入工作流
      if (this.purchasplanChangeAuditConfig) {
        params.processInitDto = this.processInitDto // 审核人信息传回得信息
        paramsData.processInitDto = this.processInitDto // 审核人信息传回得信息
      }
      if (this.dialogModifyData.modifyKey === '00') {
        purchasePlanDetailsWaitPackAudit(params).then(response => {
          this.handleClose()
        }).catch(() => {
        })
      } else {
        purchasePlanDetailsReSubmitAudit(paramsData).then(response => {
          this.handleClose()
        }).catch(() => {
        })
      }
    },
    // 变更接口参数
    getParams() {
      const paramsData = this.formData.tableData.map((item) => {
        return {
          planDetailId: item.planDetailId,
          purchaseCode: item.purchaseCode,
          purchaseDepartId: item.purchaseDepartId,
          purchaseDepartName: item.purchaseDepartName,
          purchaseName: item.purchaseName,
          purchaseNo: item.purchaseNo,
          purchaseSpecification: item.purchaseSpecification,
          initTenderMethod: item.initTenderMethod,
          tenderMethod: item.tenderMethod
        }
      })
      const data = {
        // bizTypeCode: 00-集采转自采;01-退回计划;02-采购方式变更;03-业务部退回计划;04-【结果执行确认】重新打包采购
        bizTypeCode: '02',
        packList: paramsData,
        bizReason: this.formData.bizReason
        // processDefinitionKey: this.processDefinitionKey, // 工作流节点key
      }
      // 判断是否进入工作流 true进入工作流
      if (this.purchasplanChangeAuditConfig) {
        data.processDefinitionKey = this.processDefinitionKey // 工作流节点key
      }
      return data
    },
    // 审批流提交
    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 lang="less">
  /deep/.el-form-item--small.el-form-item{
    margin: 0;
  }
</style>
