<!--
  @description: 采购员-结果执行管理
  @author: hdk
  @routerPath:/procurement/ProcurementPlanRstMgmtList
  @date： 2020-10-29
-->
<template>
  <div>
    <table-crud
      ref="tableCrud"
      :menu="true"
      :search-form.sync="searchForm"
      :tabs="executeStatusList"
      search-label-width="100px"
      @on-load="loadData"
    >
      <template v-slot:header>
        结果执行管理
      </template>
      <template v-slot:search="search">
        <!-- 采购单编号 -->
        <el-form-item
          ref="tenderNo"
          label="采购单编号"
          prop="tenderNo"
        >
          <gx-input
            v-model.trim="searchForm.tenderNo"
            @keyup.enter.native="search.onSearch"
          />
        </el-form-item>
        <!--框架计划编号-->
        <el-form-item
          ref="purchasePlanNo"
          label="框架计划编号"
          prop="purchasePlanNo"
        >
          <gx-input
            v-model.trim="searchForm.purchasePlanNo"
            @keyup.enter.native="search.onSearch"
          />
        </el-form-item>
        <!-- 执行编号 -->
        <el-form-item label="执行编号">
          <el-input
            v-model.trim="searchForm.executeNo"
            placeholder="请输入执行编号"
            @keyup.enter.native="search.onSearch"
          />
        </el-form-item>
        <el-form-item
          ref="tenderName"
          label="物资编号"
        >
          <el-input
            v-model.trim="searchForm.purchaseCode"
            placeholder="请输入物资编号"
            @keyup.enter.native="search.onSearch"
          />
        </el-form-item>
        <el-form-item ref="tenderNo" label="物资名称">
          <el-input
            v-model.trim="searchForm.purchaseName"
            placeholder="请输入物资名称"
            @keyup.enter.native="search.onSearch"
          />
        </el-form-item>
        <el-form-item
          ref="tenderName"
          label="供应商名称"
        >
          <el-input
            v-model.trim="searchForm.supplierName"
            placeholder="请输入供应商名称"
            @keyup.enter.native="search.onSearch"
          />
        </el-form-item>
        <el-form-item
          ref="tenderName"
          label="来源编号"
        >
          <el-input
            v-model.trim="searchForm.belongTenderNo"
            placeholder="请输入来源编号"
            @keyup.enter.native="search.onSearch"
          />
        </el-form-item>
        <el-form-item ref="tenderNo" label="执行方式" prop="tenderNo">
          <el-select
            v-model="searchForm.executeType"
            placeholder="请选择"
          >
            <el-option
              v-for="item in executeTypeList"
              :key="item.typecode"
              :label="item.typename"
              :value="item.typecode"
            />
          </el-select>
        </el-form-item>
        <el-form-item
          ref="tenderName"
          label="采购单位"
        >
          <el-input
            v-model.trim="searchForm.belongOrgName"
            placeholder="请输入采购单位"
            @keyup.enter.native="search.onSearch"
          />
        </el-form-item>
        <el-form-item
          ref="tenderName"
          label="来源"
        >
          <el-select v-model="searchForm.tenderMethod" placeholder="请选择">
            <el-option
              v-for="item in tenderMethodList"
              :key="item.typecode"
              :label="item.typename"
              :value="item.typecode"
            />
          </el-select>
        </el-form-item>
        <!-- 执行日期 -->
        <el-form-item ref="date" label="执行日期 ">
          <el-date-picker
            v-model="searchForm.executeTimeValue"
            :value-format="elValueFormatDate"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            @change="changeDate"
          />
        </el-form-item>
      </template>
      <template v-slot:menuLeft>
        <div class="operation-btn">
          <template v-if="searchForm.executeStatus==='00'">
            <el-button type="primary" size="small" @click="newExecutionResults('00')">
              补录招标采购结果
            </el-button>
            <el-button type="primary" size="small" @click="newExecutionResults('01')">
              补录招标框架协议
            </el-button>
          </template>
          <el-button type="primary" plain size="small" :loading="exportLoading" @click="onExportFile()">
            导出
          </el-button>
        </div>
      </template>
      <template v-slot:GXTable>
        <el-table ref="smallTable" v-loading="loading" :data="tableData" @sort-change="sortChange">
          <!-- <el-table-column
          fixed
          type="selection"
          width="55"
        /> -->
          <el-table-column fixed type="index" label="序号" align="center" width="60" />
          <el-table-column
            fixed="left"
            prop="executeNo"
            sortable="custom"
            label="执行编号"
            header-align="center"
            align="center"
            min-width="140"
          />
          <!-- 物资编号 物资信息 暂时隐藏 -->
          <!-- <el-table-column
            fixed
            prop="purchaseCode"
            label="物资编号"
            header-align="center"
            align="center"
            min-width="180"
          />
          <el-table-column
            prop="purchaseName"
            label="物资信息"
            min-width="180"
            header-align="center"
            align="center"
          /> -->
          <el-table-column
            fixed="left"
            prop="belongOrgName"
            sortable="custom"
            label="采购单位"
            min-width="180"
            header-align="center"
            align="left"
          />
          <el-table-column
            prop="executeType"
            sortable="custom"
            label="执行方式"
            min-width="200"
            header-align="center"
            align="center"
          >
            <template slot-scope="scope">
              {{ filterDictText(executeTypeList, scope.row.executeType) }}
            </template>
          </el-table-column>
          <el-table-column
            prop="tenderMethod"
            sortable="custom"
            label="来源"
            width="100"
            header-align="center"
            align="center"
          >
            <template slot-scope="scope">
              {{ filterDictText(tenderMethodList, scope.row.tenderMethod) }}
            </template>
          </el-table-column>
          <el-table-column
            prop="belongTenderNo"
            sortable="custom"
            label="来源编号"
            width="180"
            header-align="center"
            align="center"
          />
          <el-table-column
            label="执行日期"
            sortable="custom"
            prop="executeTime"
            min-width="110"
            align="center"
          >
            <template slot-scope="scope">
              {{ scope.row.executeTime && $Moment(scope.row.executeTime).format(momentDate) }}
            </template>
          </el-table-column>
          <el-table-column
            prop="validityPeriod"
            sortable="custom"
            label="有效期至"
            width="180"
            header-align="center"
            align="center"
          />
          <el-table-column
            prop="exeCreateUserRealName"
            sortable="custom"
            label="创建人"
            width="100"
            header-align="center"
            align="center"
          />
          <el-table-column
            label="创建日期"
            sortable="custom"
            prop="createTime"
            min-width="110"
            header-align="center"
            align="center"
          >
            <template slot-scope="scope">
              {{ scope.row.createTime && $Moment(scope.row.createTime).format(momentDate) }}
            </template>
          </el-table-column>
          <el-table-column
            v-if="searchForm.executeStatus !== '01' || searchForm.executeStatus !== '02'"
            label="状态"
            sortable="custom"
            prop="auditStatus"
            width="100"
            header-align="center"
            align="center"
          >
            <template slot-scope="scope">
              {{ filterDictText(planStatusList, scope.row.auditStatus) }}
            </template>
          </el-table-column>
          <el-table-column
            fixed="right"
            label="操作"
            width="230"
            header-align="center"
            align="center"
          >
            <template slot-scope="scope">
              <el-link
                type="primary"
                @click="showDdtail(scope.row)"
              >
                [查看]
              </el-link>
              <template v-if="scope.row.auditStatus === '02' || scope.row.auditStatus === '03' && searchForm.executeStatus === '00'">
                <el-link
                  type="primary"
                  @click="newExecutionResults('', scope.row)"
                >
                  [编辑]
                </el-link>
                <el-link
                  type="primary"
                  @click="delPurchaseResult(scope.row)"
                >
                  [删除]
                </el-link>
              </template>
              <template v-if="searchForm.executeStatus === '01'">
                <el-link
                  type="primary"
                  @click="newExecutionResults('02', scope.row)"
                >
                  [变更]
                </el-link>
                <el-link
                  type="primary"
                  @click="onTermination(scope.row)"
                >
                  [终止]
                </el-link>
              </template>
              <el-link
                v-if="purchasplanResultExecutionAuditConfig && (scope.row.auditStatus !== '03')"
                type="primary"
                @click="flowTracing(scope.row)"
              >
                [流程追踪]
              </el-link>
            </template>
          </el-table-column>
        </el-table>
        <!--工作流选择节点组件-->
        <processNodeCom
          ref="processNode"
          :process-definition-key="processDefinitionKey"
          @processNodeData="processNodeData"
        />
        <!--工作流流程节点/获取审核用户-->
        <departDialog
          ref="departDialog"
          :process-node="processNode"
          @isSingleUser="isSingleUser"
          @processInitData="processInitData"
          @submit="submitFlow"
        />
      </template>
    </table-crud>
    <template>
      <!--流程追踪查看弹框-->
      <flow-detail-dialog ref="tracingFlow" />
    </template>
  </div>
</template>

<script>
/* api*/
import { getPurchasePlanResultExecuteList, delPurchaseResultExecuteInfo, downProcurementResultExecuteList, parsePurchaseResultExecuteInfo, getPurchasePlanResultExecuteAuditRecordList } from '@/api/procurement/purchasplan'
/* 公共混入*/
import { rstMgmtMixins } from './mixins/rstMgmtMixins'
/* 公共方法*/
import { filterDictText, download, filterObj } from '@/assets/js/utils' // 常用方法
import flowDetailDialog from '@views/work/process/flowDetail-dialog' //  流程追踪组件
/* ==========工作流===========*/
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: 'PurPlanRstMgmtList',
  components: {
    processNodeCom, // 工作流节点信息组件
    departDialog, // 工作流审批人组件
    flowDetailDialog // 流程追踪查看
  },
  mixins: [rstMgmtMixins, workCommonMixins],
  data() {
    return {
      exportLoading: false, // 导出loading
      searchForm: { // 搜索参数
        datePickerValue: [], // 日期
        executeStatus: '00', // 默认展示第一个tab选中
        tenderNo: '', // 采购单编号
        purchasePlanNo: '', // 框架计划编号
        executeNo: '' // 执行编号
      },
      processDefinitionKey: '', // 工作流流程key
      loading: true,
      exeId: '', // 执行表ID
      tableData: [] // 列表数据
    }
  },
  created() {
    this.purchasplanStopAudit() // 获取是否进入工作流
    // 工作流流程key
    this.processDefinitionKey = workconstant.purPlanExeResult
  },
  methods: {
    // 字典检索
    filterDictText,
    // 切换日期
    changeDate(arr) {
      this.searchForm.executeTimeStart = arr[0]
      this.searchForm.executeTimeEnd = arr[1]
    },
    // 导出
    onExportFile() {
      const params = {
        executeStatus: this.searchForm.executeStatus,
        ...this.searchForm
      }
      if (this.tableData.length <= 0) {
        this.$message.warning('没有可导出的数据！')
        return false
      } else {
        this.exportLoading = true
        downProcurementResultExecuteList(filterObj(params)).then((res) => {
          this.exportLoading = false
          download(res, `结果执行管理${filterDictText(this.executeStatusList, this.searchForm.executeStatus)}.xls`)
        }).catch(() => {
          this.exportLoading = false
        })
      }
    },
    // 获取执行结果管理列表数据
    loadData(params, callback, tabData) {
      if (tabData) {
        params.executeStatus = tabData.index
        this.searchForm.executeStatus = tabData.index
      }
      // 列表数据接口 判断如果是已终止或着是变更记录则调用getPurchasePlanResultExecuteAuditRecordList接口
      if (params.executeStatus === '03' || params.executeStatus === '04') {
        getPurchasePlanResultExecuteAuditRecordList(params).then((response) => {
          this.searchForm.column = ''
          this.searchForm.orderSort = ''
          this.loading = false
          this.tableData = response.data.records
          callback(response.data.total)
        }).catch(() => {
          this.searchForm.column = ''
          this.searchForm.orderSort = ''
          this.loading = false
        })
      } else {
        getPurchasePlanResultExecuteList(params).then(response => {
          this.searchForm.column = ''
          this.searchForm.orderSort = ''
          this.loading = false
          this.tableData = response.data.records
          callback(response.data.total)
        }).catch(() => {
          this.searchForm.column = ''
          this.searchForm.orderSort = ''
          this.loading = false
        })
      }
    },
    // 新增、编辑
    newExecutionResults(editMode, row) {
      const params = {
        resultExeType: editMode !== '02' ? editMode : '', // 新增类型 00新增采购、01新增框架 非新增不传
        editMode: editMode !== '' ? editMode : row.executeType, // editMode编辑模式 00新增采购执行结果 01新增框架执行结果 02变更
        executeId: row ? row.executeId : '' // 执行单id
      }
      // 跳转到编辑页
      this.$router.push({
        name: 'rstMgmtAdd',
        params
      })
    },
    // 查看
    showDdtail(row) {
      // 跳转到查看页
      this.$router.push({
        name: 'purPlanRstMgmtDetail',
        params: {
          executeId: row.executeId // 执行单id
        }
      })
    },
    // 审批流提交
    submitFlow() {
      this.purPlanResultExecuteInfo()
    },
    // 终止执行结果
    onTermination(row) {
      this.exeId = row.executeId // 执行表ID
      this.$confirm('是否确定提交？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        confirmButtonClass: 'confirmBtn',
        cancelButtonClass: 'cancelBtn',
        type: 'warning'
      }).then(() => {
        // 判断是否进入工作流 true代表进入工作流 false表示直接提交
        if (this.purchasplanStopAuditConfig) {
          // 判断审核人员是否是多个
          if (JSON.stringify(this.processNode) !== '{}' && this.processNode.nodeId) {
            this.$refs.departDialog.getUserDepart(this.processNode)
          }
        } else {
          // 直接提交
          this.submitFlow()
        }
      }).catch(() => {
      })
    },
    // 终止执行结果接口调起
    purPlanResultExecuteInfo() {
      const params = {
        executeId: this.exeId // 执行表ID
        // processDefinitionKey: this.processDefinitionKey, // 工作流节点key
        // processInitDto: this.processInitDto // 审核人信息传回得信息
      }
      // 判断是否进入工作流
      if (this.purchasplanStopAuditConfig) {
        params.processDefinitionKey = this.processDefinitionKey // 工作流节点key
        params.processInitDto = this.processInitDto
      }
      parsePurchaseResultExecuteInfo(params).then((res) => {
        this.$message({
          type: 'success',
          message: '操作成功！'
        })
        this.$refs.tableCrud.onSearchChange()
      }).catch(() => {})
    },
    // 删除采执行结果
    delPurchaseResult(row) {
      this.$confirm('确定删除采执行结果?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        confirmButtonClass: 'confirmBtn',
        cancelButtonClass: 'cancelBtn',
        type: 'warning'
      }).then(() => {
        const params = {
          executeId: row.executeId
        }
        // 删除采执行结果调用
        delPurchaseResultExecuteInfo(params).then(response => {
          this.$message({
            type: 'success',
            message: '操作成功！'
          })
          this.$refs.tableCrud.onSearchChange()
        }).catch(() => {
        })
      }).catch(() => {
      })
    },
    // 流程追踪
    flowTracing(row) {
      this.$refs.tracingFlow.openDialogInitData(row.bizId)
    },
    // 排序
    sortChange: function(column, prop, order) {
      let orderType = ''
      switch (column.order) {
        case 'ascending':
          orderType = 'ASC'
          break
        case 'descending':
          orderType = 'DESC'
          break
      }
      this.searchForm.column = column.prop
      this.searchForm.orderSort = orderType
      this.$refs.tableCrud.onSearchChange()
    }
  }
}
</script>

<style scoped>
.operation-btn{
  margin-bottom: 10px;
}
</style>
