<!--
  @description: 采购计划-计划结果执行确认
  @author: lxl
  @routerPath:/procurement/procurementPlanResultAffirmList
  @date： 2020-10-30 13:52
-->
<template>
  <div>
    <table-crud
      ref="tableCrud"
      :menu="true"
      :tabs="tabList"
      :search-form.sync="searchForm"
      search-label-width="100px"
      @on-load="loadData"
    >
      <template v-slot:header>
        <span>结果执行确认</span>
      </template>
      <template v-slot:search="search">
        <!-- 采购计划名称 -->
        <el-form-item
          ref="purchasePlanName"
          label="采购单编号"
          prop="purchasePlanName"
        >
          <gx-input
            v-model.trim="searchForm.purchasePlanName"
            @keyup.enter.native="search.onSearch"
          />
        </el-form-item>
        <!-- 物资名称 -->
        <el-form-item
          ref="purchaseDesc"
          label="物资描述"
          prop="purchaseDesc"
        >
          <gx-input
            v-model.trim="searchForm.purchaseDesc"
            @keyup.enter.native="search.onSearch"
          />
        </el-form-item>
        <!-- 行项目编号 -->
        <el-form-item
          ref="purchaseNo"
          label="行项目编号"
          prop="purchaseNo"
        >
          <gx-input
            v-model.trim="searchForm.purchaseNo"
            @keyup.enter.native="search.onSearch"
          />
        </el-form-item>
        <!-- 采购人 -->
        <el-form-item
          ref="purchaseDepartName"
          label="上报人"
          prop="purchaseDepartName"
        >
          <gx-input
            v-model.trim="searchForm.purchaseDepartName"
            @keyup.enter.native="search.onSearch"
          />
        </el-form-item>
      </template>
      <template v-slot:menuLeft>
        <div class="mb10">
          <!-- 寻源下可有执行采购计划和关闭采购计划 -->
          <template v-if="searchForm.resultAffirmStatus==='00'">
            <el-button type="primary" @click="onAffirm">
              确认执行结果
            </el-button>
            <el-button type="primary" @click="onAnew">
              重新打包采购
            </el-button>
          </template>

          <el-button type="primary" :loading="exportLoading" @click="onExportFile">
            导出
          </el-button>
        </div>
      </template>
      <template v-slot:GXTable>
        <!-- 待确认表格数据 -->
        <restStaAffirmTable v-if="searchForm.resultAffirmStatus==='00'" :table-data="tableData" @orderData="onOrderData" @MultipleData="onMultipleData" @checkFile="onCheckFile" />
        <!-- 已确认表格数据 -->
        <resultAffirmTable v-if="searchForm.resultAffirmStatus==='01'" :table-data="tableData" @orderData="onOrderData" @MultipleData="onMultipleData" @checkFile="onCheckFile" />
        <!-- 重新确认表格数据 -->
        <resultAffirmAnewTable v-if="searchForm.resultAffirmStatus==='02'" :table-data="tableData" @orderData="onOrderData" @MultipleData="onMultipleData" @checkFile="onCheckFile" />
      </template>
    </table-crud>
    <template>
      <!-- 结果执行确认 -->
      <executeRestDialog :dialog-execute-data="dialogExecuteData" @close="onClose" />
      <!-- 重新执行采购 -->
      <anewBaleDialog :dialog-anew-data="dialogAnewData" @close="onClose" />
      <!-- 查看附件 -->
      <checkFIleCompDialog :dialog-file-data="dialogFileData" />
    </template>
  </div>
</template>
<script>
/* api*/
import { getPurchasePlanDetailResultAffirmList, downProcurementPlanDetailResAffirmList } from '@/api/procurement/purchasplan'
/* 弹框组件*/
import executeRestDialog from './executeRest-dialog' // 确认执行弹框
import anewBaleDialog from './anewBale-dialog' // 重新执行采购弹框
import checkFIleCompDialog from '@/views/procurement/procurementPlan/components/checkFIleComp-dialog.vue'// 查看附件
/* 表格组件*/
import restStaAffirmTable from './restStaAffirmTable.vue' // 待确认
import resultAffirmTable from './resultAffirmTable.vue' // 已确认
import resultAffirmAnewTable from './resultAffirmAnewTable.vue' // 重新打包
/* 公用方法*/
// import { procurementPlanGrounpMixins } from '@views/procurement/procurementPlan/mixins/procurementPlanGroupMixins' // 字典信息
import { filterDictText, download, filterObj } from '@/assets/js/utils' // 字典方法
export default {
  name: 'PurPlanRestAffirmList',
  components: { executeRestDialog, anewBaleDialog, restStaAffirmTable, checkFIleCompDialog, resultAffirmAnewTable, resultAffirmTable },
  // mixins: [procurementPlanGrounpMixins],
  data() {
    return {
      dialogAnewData: {
        AnewVisible: false, // 重新执行采购弹框状态
        paramsData: [] // 重新执行采购弹框数据
      },
      dialogExecuteData: {
        executeVisible: false, // 确认执行结果弹框状态
        executeTableData: [], // 结果执行确认弹框数据
        planDetailId: '' // 确认接口参数
      },
      dialogFileData: {
        checkFileVisible: false, // 查看附件弹框状态
        bizId: ''
      },
      loading: true,
      exportLoading: false, // 导出loading
      selMethods: [], // 选择数据存储
      selectFlg: false, // 判断选择数据状态
      tableData: [], // 表格数据
      tabList: [
        {
          name: '待确认',
          index: '00'
        },
        {
          name: '已确认',
          index: '01'
        },
        {
          name: '重新打包采购',
          index: '02'
        }
      ],
      searchForm: {
        purchasePlanName: '', // 采购计划名称
        purchaseDesc: '', // 物资名称
        purchaseNo: '', // 行项目编号
        purchaseDepartName: '', // 采购人
        resultAffirmStatus: '00'
      }
    }
  },
  methods: {
    filterDictText,
    // 导出
    onExportFile() {
      const params = {
        resultAffirmStatus: this.searchForm.resultAffirmStatus,
        ...this.searchForm
      }
      const index = this.tabList.findIndex((item) => this.searchForm.resultAffirmStatus === item.typecode)
      if (this.tableData.length <= 0) {
        this.$message.warning('没有可导出的数据!')
        return false
      } else {
        this.exportLoading = true
        downProcurementPlanDetailResAffirmList(filterObj(params)).then((res) => {
          this.exportLoading = false
          download(res, `结果执行确认${this.tabList[index].name}.xls`)
        }).catch(() => {
          this.exportLoading = false
        })
      }
    },
    // 查看附件
    onCheckFile(res, flg) {
      this.dialogFileData.checkFileVisible = flg
      this.dialogFileData.bizId = res.planDetailId
    },
    // 初始化数据
    loadData(params, callback, tabData) {
      if (tabData) {
        params.resultAffirmStatus = tabData.index
        this.searchForm.resultAffirmStatus = tabData.index
      }
      getPurchasePlanDetailResultAffirmList(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
      })
    },
    // 判断选择数据返回状态
    selFlg() {
      if (this.selMethods.length > 0) {
        return true
      } else {
        this.$message.warning('请选择数据')
        return false
      }
    },
    // 确认执行结果弹框
    onAffirm() {
      if (this.selFlg()) {
        this.dialogExecuteData.executeVisible = true
      }
    },
    // 重新执行采购弹框
    onAnew() {
      if (this.selFlg()) {
        this.dialogAnewData.AnewVisible = true
      }
    },
    // 弹框关闭时清楚选择数据
    onClose() {
      this.$refs.tableCrud.onSearchChange()
    },
    // 选择表格数据方法
    onMultipleData(res) {
      const bales = []
      this.selMethods = res
      this.dialogExecuteData.executeTableData = res
      res.filter((item) => {
        bales.push(item.planDetailId)
      })
      this.dialogExecuteData.planDetailId = bales.join(',')
      this.dialogAnewData.paramsData = res
    },
    // 排序
    onOrderData(res) {
      if (this.tableData.length > 0) {
        this.searchForm.column = res.column
        this.searchForm.orderSort = res.orderSort
        this.$refs.tableCrud.onSearchChange()
      }
    }
  }
}
</script>
