<!--
  @description: 采购计划-退回计划管理例表页
  @author: lxl
  @routerPath:/procurement/procerementPlanBackAdminList
  @date： 2020-11-02 16: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="purchaseName"
          label="物资名称"
          prop="purchaseName"
        >
          <el-input
            v-model.trim="searchForm.purchaseName"
            @keyup.enter.native="search.onSearch"
          />
        </el-form-item>
        <!-- 物资编码 -->
        <el-form-item
          ref="purchaseCode"
          label="物资编码"
          prop="purchaseCode"
        >
          <el-input
            v-model.trim="searchForm.purchaseCode"
            placeholder="物资编码"
            @keyup.enter.native="search.onSearch"
          />
        </el-form-item>
        <!--创建日期-->
        <el-form-item
          label="开始日期"
        >
          <el-date-picker
            v-model="searchForm.datePicker"
            :value-format="elValueFormatDate"
            type="daterange"
            placeholder="选择开始日期时间"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            style="width:200px;"
            @change="onHandelTime"
          />
        </el-form-item>
        <!--行项目编号-->
        <el-form-item
          ref="purchaseNo"
          label="行项目编号"
          prop="purchaseNo"
        >
          <el-input
            v-model.trim="searchForm.purchaseNo"
            @keyup.enter.native="search.onSearch"
          />
        </el-form-item>
      </template>
      <template v-slot:menuLeft>
        <div class="mb10">
          <!-- 寻源下可有执行采购计划和关闭采购计划 -->
          <template v-if="searchForm.returnStatus==='00'">
            <el-button type="primary" @click="deleteInformation">
              删除
            </el-button>
            <el-button type="primary" @click="reported">
              重新上报
            </el-button>
          </template>
          <el-button type="primary" :loading="exportLoading" @click="onExportFile">
            导出
          </el-button>
        </div>
      </template>
      <template v-slot:GXTable>
        <!-- 已删除 -->
        <alreadyRemoveTable v-if="searchForm.returnStatus==='02'" :table-data="tableData" :loading="loading" @checkFile="onCheckFile" />
        <!-- 被退回 -->
        <quiltBackTable v-if="searchForm.returnStatus==='00'" ref="quiltBackTable" :table-data="tableData" :loading="loading" @delMultiple="onDelMultiple" @checkFile="onCheckFile" @editLine="onEditLine" />
        <!-- 删除记录 -->
        <removeRecordTable v-if="searchForm.returnStatus==='01'" :table-data="tableData" :loading="loading" @checkFile="onCheckFile" @flowTrace="onFlowTrace" />
      </template>
    </table-crud>
    <template>
      <!-- 删除计划弹框 -->
      <backAdminDeleteDialog :dialog-del-data="dialogDelData" :params-id="paramsId" @close="onClose" />
      <!-- 重新上报弹框 -->
      <backReportedDialog :dialog-reported-data="dialogReportedData" :params-id="paramsId" @close="onClose" />
      <!-- 查看附件 -->
      <checkFIleCompDialog :dialog-file-data="dialogFileData" />
      <!--流程追踪查看弹框-->
      <flow-detail-dialog ref="tracingFlow" />
      <!-- 编辑行项目 -->
      <editLineDialog :dialog-data="lineDigData" :planmainid="planmainid" @resout="onClose" />
    </template>
  </div>
</template>
<script>
/* api*/
import { getPlanPurchaseReturnDetailsList, downProcurementPlanDetailPackList } from '@/api/procurement/purchasplan'
// 删除信息弹框
import backAdminDeleteDialog from './backAdminDelete/backAdminDelete-dialog.vue'
// 重新上报
import backReportedDialog from './backReported-dialog.vue'
// 表格数据组件
import alreadyRemoveTable from './alreadyRemoveTable' // 已删除
import quiltBackTable from './quiltBackTable' // 被退回
import removeRecordTable from './removeRecordTable' // 删除记录
import editLineDialog from './editLine-dialog.vue' // 编辑行项目

/* 导出公用方法*/
import { download } from '@/assets/js/utils'// 常用方法
import checkFIleCompDialog from '@/views/procurement/procurementPlan/components/checkFIleComp-dialog.vue'// 查看附件
import flowDetailDialog from '@views/work/process/flowDetail-dialog' //  流程追踪组件

export default {
  name: 'PurPlanBackAdminList',
  components: {
    editLineDialog,
    backAdminDeleteDialog, // 删除信息
    checkFIleCompDialog, // 附件查看
    flowDetailDialog, // 流程追踪
    backReportedDialog, // 重新上报弹框
    alreadyRemoveTable, // 已删除表格
    quiltBackTable, // 被退回表格
    removeRecordTable // 删除记录表格
  },
  data() {
    return {
      // 计划删除弹框
      dialogDelData: {
        dialogDelVisible: false
      },
      // 重新上报弹框
      dialogReportedData: {
        dialogRreVisible: false
      },
      lineDigData: {
        planDetailId: '',
        consignContractVisible: false
      },
      planmainid: '', // 计划行项目id
      loading: true,
      exportLoading: false, // 导出loading
      tableData: [], // 表格数据
      paramsId: '', // 删除数据id参数
      AnewMultipleIds: [], // 重新上报参数
      selMethods: [], // 存储选择数据
      dialogFileData: {
        checkFileVisible: false, // 查看附件弹框状态
        bizId: ''
      },
      searchForm: {
        returnStatus: '00', // tabs默认值
        datePicker: [], // 选择开始时间结束时间
        purchaseName: '', // 物资名称
        purchaseCode: '', // 物资编码
        purchaseNo: '', // 行项目编号
        createTimeStart: '', // 创建开始时间
        createTimeEnd: '' // 创建结束时间
      },
      tabList: [
        {
          name: '被退回',
          index: '00'
        },
        {
          name: '已删除',
          index: '02'
        },
        {
          name: '删除记录',
          index: '01'
        }
      ]
    }
  },
  computed: {
  },
  created() {
  },
  methods: {
    // 选择时间触发事件
    onHandelTime(val) {
      if (val) {
        this.searchForm.createTimeStart = val[0]
        this.searchForm.createTimeEnd = val[1]
      }
    },
    // 导出
    onExportFile() {
      const params = {
        returnStatus: this.searchForm.returnStatus
      }
      const index = this.tabList.findIndex((item) => this.searchForm.returnStatus === item.index)
      if (this.tableData.length <= 0) {
        this.$message.warning('没有可导出的数据!')
        return false
      } else {
        this.exportLoading = true
        downProcurementPlanDetailPackList(params).then((res) => {
          this.exportLoading = false
          download(res, `退回计划管理${this.tabList[index].name}.xls`)
        }).catch(() => {
          this.exportLoading = false
        })
      }
    },
    // 删除信息弹框
    deleteInformation() {
      if (this.selFlg()) {
        this.dialogDelData.dialogDelVisible = true
      }
    },
    // 重新上报
    reported() {
      if (this.selFlg()) {
        this.dialogReportedData.dialogRreVisible = true
      }
    },
    // 判断选择数据返回状态
    selFlg() {
      if (this.selMethods.length > 0) {
        return true
      } else {
        this.$message.warning('请选择数据!')
        return false
      }
    },
    // 数据请求
    loadData(params, callback, tabData) {
      if (tabData) {
        params.returnStatus = tabData.index
        this.searchForm.returnStatus = tabData.index
      }
      getPlanPurchaseReturnDetailsList(params).then(response => {
        this.tableData = response.data.records
        callback(response.data.total)
      }).catch(() => {
      }).finally(() => {
        this.loading = false
        this.searchForm.column = ''
        this.searchForm.orderSort = ''
      })
    },
    // 复选框选择数据
    onDelMultiple(res) {
      const bales = []
      this.selMethods = res
      res.filter((item) => {
        bales.push(item.planDetailId)
      })
      this.paramsId = bales.join(',')
    },
    // 弹框点击关闭是取消选中状态刷新页面
    onClose() {
      if (this.searchForm.returnStatus === '00') {
        this.$refs.quiltBackTable.empitSel()
      }
      this.$refs.tableCrud.onSearchChange()
    },
    // 附件查看
    onCheckFile(res, flg) {
      this.dialogFileData.checkFileVisible = flg
      this.dialogFileData.bizId = res.planDetailId
    },
    // 流程追踪
    onFlowTrace(row) {
      this.$refs.tracingFlow.openDialogInitData(row.bizId)
    },
    // 行项目编辑
    onEditLine(row) {
      this.lineDigData.planDetailId = row.planDetailId
      this.planmainid = row.planMainId
      this.lineDigData.consignContractVisible = true
    }
  }
}
</script>
