<!--
  @description: 采购计划-退回计划管理删除信息弹框/驳回共用
  @author: lxl
  @date： 2020-11-02 13:52
-->
<template>
  <div>
    <div>
      <p style="padding:6px 0">
        已选计划:
      </p>
    </div>
    <section>
      <el-table
        v-loading="loading"
        :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>
      <page-nation :pagination-data="paginationData" @change="loadData" />
    </section>
    <section style="margin:10px 0px">
      <el-form
        ref="formData"
        label-position="left"
        :rules="rules"
        :model="formData"
        inline
      >
        <table class="tableData">
          <tr>
            <td>
              <el-form-item
                label="删除原因:"
                class="el-form-item--two"
                prop="bizReason"
              />
            </td>
            <td>
              <gx-input
                v-model.trim="formData.bizReason"
                gx-maxlength="varchar500"
                show-word-limit
                type="textarea"
              />
            </td>
          </tr>
        </table>
      </el-form>
    </section>

    <div class="but_footer">
      <div class="but_footer-h">
        <div class="but_footer-but">
          <el-button type="primary" :loading="conformLoading" @click="ensure('formData')">
            提 交
          </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 // 初始化数据
} from '@/api/procurement/purchasplan'
/*  公共*/
import { ListMixin } from '@/mixins/ListMixin' // 分页
import { paging } from '@/assets/js/utils/index.js' // 前端分页
import pagination from '@comp/common/pagination/pagination' // 分页组件
import { characters1_auto } from '@/assets/js/validate'// 校验输入内容
import { purPlanBaleMixins } from '../../purPlanBale/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: 'BackAdminDeleteAdd',
  components: {
    'page-nation': pagination,
    processNodeCom,
    departDialog
  },
  mixins: [ListMixin, workCommonMixins, purPlanBaleMixins],
  props: {
    dialogDelData: { // 弹框状态
      type: Object,
      default: () => {}
    },
    paramsId: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      formData: {
        bizReason: ''
      },
      flowStatus: this.$route.query.flowStatus, // true 工作流进入驳回编辑
      conformLoading: false,
      processDefinitionKey: '', // 工作流流程id
      tableData: [], // 表格数据
      loading: false,
      datas: [], //
      rules: {
        bizReason: [{ required: true, validator: characters1_auto, trigger: 'blur' }]
      }
    }
  },
  created() {
    this.purchasplanChangeAudit() // 获取是否启用工作流
    this.processDefinitionKey = workconstant.purPlanRetuenDel // 工作流流程key
    this.loadData()
  },
  methods: {
    // 数据请求方法
    loadData() {
      const params = {
        planDetailIds: this.paramsId || this.$route.query.id
      }
      modifyProtPurchaseTenderMethodPage(params)
        .then(response => {
          this.loading = false
          this.tableData = paging({}, response.data.list, this.paginationData)
        })
        .catch(() => {
          this.loading = false
        })
    },
    purPlanBackDel() {
      // this.processInitDto.type = '01' // 01：初次提交 02：审核驳回再次提交
      const params = {
        // bizTypeCode: 00-集采转自采;01-退回计划;02-采购方式变更;03-业务部退回计划;04-【结果执行确认】重新打包采购 05 重新打包 06删除退回计划
        bizReason: this.formData.bizReason,
        bizTypeCode: '06',
        packList: this.tableData
        // processDefinitionKey: this.processDefinitionKey, // 工作流节点key
        // nodeId: 'startbmjl', // 工作流nodeId
        // processInitDto: this.processInitDto // 审核人信息传回得信息
      }
      if (this.purchasplanChangeAuditConfig) {
        params.processDefinitionKey = this.processDefinitionKey // 工作流节点key
        params.nodeId = 'startbmjl' // 工作流nodeId
        params.processInitDto = this.processInitDto // 审核人信息传回得信息
      }
      purchasePlanDetailsWaitPackAudit(params).then(response => {
        this.$message.success(response.header.msg)
        this.handleClose()
      }).catch((error) => {
        this.$message.warning(error)
      })
    },
    submit() {
      this.purPlanBackDel()
    },
    // 确定
    ensure(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.conformLoading = true
          this.$confirm('是否确定提交？', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            confirmButtonClass: 'confirmBtn',
            cancelButtonClass: 'cancelBtn',
            type: 'warning'
          }).then(() => {
            this.conformLoading = false
            if (this.purchasplanChangeAuditConfig) {
              // 判断审核人员是否是多个
              if (JSON.stringify(this.processNode) !== '{}' && this.processNode.nodeId) {
                this.$refs.departDialog.getUserDepart(this.processNode)
              }
            } else {
              // 直接提交
              this.submit()
            }
          }).catch(() => {
            this.conformLoading = false
          })
        } else {
          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('handleClose')
        // this.dialogDelData.dialogDelVisible = false
      }
    }
  }
}
</script>
<style scoped>
</style>
