<!--
  @description: 采购员-服务费管理-列表
  @author: 王俊楠
  @routerPath:/procurement/procurementOrderServeFeeReceiveList
  @date： 2020-11-02 17:07:01
-->
<template>
  <div>
    <table-crud
      ref="tableCrud"
      :menu="true"
      :tabs="purchaseServerTabs"
      :search-form="searchForm"
      search-label-width="100px"
      @on-load="loadData"
    >
      <!--标题-->
      <template v-slot:header>
        服务费管理
      </template>
      <template v-slot:search="search">
        <!--订单编号-->
        <el-form-item ref="orderCode" label="订单编号" prop="orderCode">
          <el-input v-model.trim="searchForm.orderCode" @keyup.enter.native="search.onSearch" />
        </el-form-item>
        <!--采购单结果-->
        <el-form-item ref="belongTenderNo" label="采购单结果" prop="belongTenderNo">
          <el-input
            v-model.trim="searchForm.belongTenderNo"
            placeholder="采购单结果"
            @keyup.enter.native="search.belongTenderNo"
          />
        </el-form-item>
        <!-- 订单名称 -->
        <el-form-item ref="orderName" label="订单名称" prop="orderName">
          <el-input v-model.trim="searchForm.orderName" @keyup.enter.native="search.onSearch" />
        </el-form-item>
        <!--供应商名称-->
        <el-form-item ref="supplierName" label="供应商名称" prop="supplierName">
          <el-input v-model.trim="searchForm.supplierName" @keyup.enter.native="search.onSearch" />
        </el-form-item>
        <!--采购单位-->
        <el-form-item ref="purchaseDepartName" label="采购单位" prop="purchaseDepartName">
          <el-input v-model.trim="searchForm.purchaseDepartName" @keyup.enter.native="search.onSearch" />
        </el-form-item>
        <!--采购方式-->
        <el-form-item ref="tenderMethod" label="采购方式" prop="tenderMethod">
          <el-select v-model="searchForm.tenderMethod" placeholder="请选择">
            <el-option
              v-for="item in tendermethodList"
              :key="item.typename"
              :label="item.typename"
              :value="item.typecode"
            />
          </el-select>
        </el-form-item>
        <!-- 订单总额 -->
        <el-form-item prop="min" label="订单总额(元)">
          <el-input v-model="searchForm.orderTotalMoney_begin" type="number" style="width: 115px" />
          ~
          <el-input v-model="searchForm.orderTotalMoney_end" type="number" style="width: 115px" />
        </el-form-item>
        <!--执行单位-->
        <el-form-item ref="purchaseDepartmentExecuteName" label="执行单位" prop="purchaseDepartmentExecuteName">
          <el-input v-model.trim="searchForm.purchaseDepartmentExecuteName" @keyup.enter.native="search.onSearch" />
        </el-form-item>
        <!--创建日期-->
        <el-form-item label="创建日期">
          <el-date-picker
            v-model="searchForm.datePicker"
            type="daterange"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            @change="handleCreateTime"
          />
        </el-form-item>
        <!--订单状态-->
        <!--        <el-form-item ref="purchaseDepartName" label="订单状态" prop="purchaseDepartName">
          <el-input v-model.trim="searchForm.purchaseDepartName" @keyup.enter.native="search.onSearch" />
        </el-form-item>-->
      </template>
      <template v-slot:menuLeft>
        <div class="operation-btn">
          <template v-if="searchForm.status==='00'">
            <el-button type="primary" class="mb10" @click="createReceiptFn">
              生成收款单
            </el-button>
            <el-button type="primary" @click="cancelReceipt">
              减免收费单
            </el-button>
          </template>
          <el-button
            v-if="searchForm.status!=='04' && searchForm.status!=='05'"
            type="primary"
            plain
            @click="exportExcel"
          >
            导出
          </el-button>
        </div>
      </template>
      <template v-slot:GXTable>
        <!-- 待确认收费 -->
        <procurementOrdeWaitReceiptTableComp
          v-if="searchForm.status==='00'"
          :table-data="tableData"
          :loading="loading"
          @selectionChange="onSelectionChange"
        />
        <!-- 审核中服务费-->
        <serviceFeeProcessAuditTableComp
          v-if="searchForm.status==='05'"
          ref="serviceFeeProcessAudit"
          :loading="loading"
          :table-data="tableData"
          :is-show-btn="isShowBtn"
          @close="onClose"
        />
        <!-- 供应商待缴费 -->
        <procurementOrderSupplierBillPayTableComp
          v-if="searchForm.status==='01'"
          :loading="loading"
          :table-data="tableData"
        />
        <!-- 供应商已缴费 -->
        <procurementOrderSupplierPayTableComp
          v-if="searchForm.status==='02'"
          :loading="loading"
          :table-data="tableData"
        />
        <!-- 已减免收费 -->
        <procurementOrderUnReceipTableComp
          v-if="searchForm.status==='03'"
          :loading="loading"
          :table-data="tableData"
        />
        <!-- 异常缴费处理 -->
        <procurementOrderAbnormalPayTableComp
          v-if="searchForm.status==='04'"
          :key="searchForm.status"
          :loading="loading"
          :table-data="tableData"
          :is-show-btn="isShowBtn"
          :status="searchForm.status"
        />
        <!-- 异常处理记录 -->
        <procurementOrderAbnormalPayTableComp
          v-if="searchForm.status==='06'"
          :key="searchForm.status"
          :loading="loading"
          :table-data="tableData"
          :is-show-btn="isShowBtn"
          :status="searchForm.status"
        />
      </template>
    </table-crud>
    <!-- 生成收款单弹框 -->
    <orderReceiptCreateDialog ref="orderReceiptCreate" @close="onClose" />
    <!-- 减免收费单弹框 -->
    <orderReceiptCancelDialog ref="orderReceiptCancel" @close="onClose" />
  </div>
</template>

<script>
/* api接口*/
import { listProcurementServiceFeePages, getExcepPaymentPageInfos, payeeSinglePageList, exportServiceFee } from '@/api/procurement/procurementOrder'
import { orderManagementGroupMixin } from '../../mixins/orderManagementGroupMixins'
// 组件
import procurementOrdeWaitReceiptTableComp from './procurementOrdeWaitReceiptTableComp' // 待确认收费
import procurementOrderSupplierBillPayTableComp from './procurementOrderSupplierBillPayTableComp' // 待缴费
import serviceFeeProcessAuditTableComp from './serviceFeeProcessAuditTableComp' // 审核中服务费
import procurementOrderSupplierPayTableComp from './procurementOrderSupplierPayTableComp' // 供应商已缴费
import procurementOrderUnReceipTableComp from './procurementOrderUnReceipTableComp' // 已减免收费
import orderReceiptCreateDialog from '../serveFeeReceipt/orderReceiptCreate-dialog' // 生成收款单弹窗
import orderReceiptCancelDialog from '../serveFeeBreaksSingle/orderReceiptCancel-dialog' // 取消收费单弹窗
import procurementOrderAbnormalPayTableComp from './procurementOrderAbnormalPayTableComp' // 异常缴费处理
import { download } from '@/assets/js/utils'// 常用方法
export default {
  name: 'OrderManagementList',
  components: {
    procurementOrdeWaitReceiptTableComp, // 待确认收费
    procurementOrderSupplierBillPayTableComp, // 待缴费
    serviceFeeProcessAuditTableComp, // 审核中服务费
    procurementOrderSupplierPayTableComp, // 供应商未缴费 供应商已缴费
    procurementOrderUnReceipTableComp, // 已减免收费
    procurementOrderAbnormalPayTableComp, // 异常缴费处理
    orderReceiptCreateDialog, // 生成收款单弹窗
    orderReceiptCancelDialog // 取消收费单弹窗
  },
  mixins: [orderManagementGroupMixin],
  data() {
    return {
      // 收货服务费管理-列表-tabs
      purchaseServerTabs: [
        { index: '00', name: '待确认收费', type: 'TO_BE_CONFIRMED' },
        { index: '05', name: '审核中服务费' },
        { index: '01', name: '供应商待缴费', type: 'SUPPLIER_TO_BE_PAY' },
        { index: '02', name: '供应商已缴费', type: 'SUPPLIER_PAID' },
        { index: '03', name: '已减免收费', type: 'FEE_EDUCED' },
        { index: '04', name: '异常缴费处理' },
        { index: '06', name: '异常处理记录', type: 'EXCEP_HAND_RECORD' }
      ],
      loading: false,
      searchForm: {
        datePicker: [], // 选择开始时间结束时间
        orderTotalMoney_begin: '', // 订单总额开始
        orderTotalMoney_end: '', // 订单总额结束
        // tenderMethod: '', // 生成方式
        orderStatus: '', // 订单状态
        orderCode: '', // 订单编号
        // orderName: '', // 订单名称
        supplierName: '', // 供应商名称
        // purchaseDepartName: '', // 创建人
        createTime_begin: '', // 创建日期
        createTime_end: '', // 结束日期
        purchaseDepartName: '', // 代理单位
        purchaseDepartmentExecuteName: '', // 执行单位
        status: '00'// 默认选中第一个tab页 00待缴费 01已缴费
      },
      flagList: [], // 选择数据
      tableData: [],
      exportServiceFeeParams: {}, // 导出参数
      isShowBtn: false // 根据角色判断是否显示操作列按钮
    }
  },
  created() {
    this.handleIsShowBtn()
    // 初始化加载页面请求
    this.getProcurementOderInfo()
  },
  methods: {
    // 根据角色判断是否显示操作列按钮
    handleIsShowBtn() {
      const roleCode = this.$store.state.user.info.currentRol.roleCode // 当前角色 10 采购人 11代理机构
      if (roleCode === 'jiesy') {
        this.isShowBtn = true
      } else {
        this.roleCode = false
      }
    },
    // 导出列表
    exportExcel() {
      if (this.tableData.length > 0) {
        const tabIndex = this.purchaseServerTabs.filter(i => i.index === this.searchForm.status)
        const excelName = tabIndex[0].name ? tabIndex[0].name : '服务费'
        // 查询条件
        const params = {
          ...this.exportServiceFeeParams,
          exportTpye: tabIndex[0].type
        }
        exportServiceFee(params).then(res => {
          download(res, excelName + '信息.xls')
        })
      } else {
        this.$message.warning('暂无数据！')
      }
    },
    // 选择批量待确认收费的数据
    onSelectionChange(val) {
      this.flagList = val
    },
    // 创建收款单
    createReceiptFn() {
      if (this.onIsSelect()) {
        // flag 选择生成服务费状态如果供应商id相同 返回true const flag = this.flagList.reduce((pre, cur) => pre.supplierId === cur.supplierId)
        var flag = true
        var str = this.flagList[0].supplierId
        for (var i = 0; i < this.flagList.length; i++) {
          if (this.flagList[i].supplierId !== str) {
            flag = false
          }
        }
        if (flag) {
          this.$refs.orderReceiptCreate.openOrderReceiptCreate(this.flagList)
        } else {
          this.$message.warning('您选择的供应商不一致')
        }
      }
    },
    // 取消收费单
    cancelReceipt() {
      if (this.onIsSelect()) {
        this.$refs.orderReceiptCancel.openOrderReceiptCancel(this.flagList)
      }
    },
    // 判断是否选择数据
    onIsSelect() {
      if (this.flagList.length <= 0) {
        this.$message.warning('请选择')
        return false
      } else {
        return true
      }
    },
    onClose() {
      this.$refs.tableCrud.onSearchChange()
    },
    // 获取订单列表数据
    loadData(params, callback, tabData) {
      if (tabData) {
        this.searchForm.status = tabData.index
        params.status = tabData.index
      }
      this.exportServiceFeeParams = params
      delete params.datePicker
      this.tableData = []
      if (this.searchForm.status === '04') {
        // 异常缴费处理中
        params.flag = '0'
        this.getExcepPayment(params, callback)
      } else if (this.searchForm.status === '06') {
        // 异常缴费处理结束
        params.flag = '1'
        this.getExcepPayment(params, callback)
      } else if (this.searchForm.status === '05') {
        // 审核列表
        this.getPayeeSingle(params, callback)
      } else {
        this.getProcurementServiceFee(params, callback)
      }
    },
    // 获取审核列表
    getPayeeSingle(params, callback) {
      delete params.status
      payeeSinglePageList(params).then(response => {
        this.tableData = response.data.records
        callback(response.data.total)
        this.loading = false
      }).catch(() => {
        this.loading = false
      })
    },
    // 异常服务费缴纳信息（异常缴费处理、异常缴费记录共用）
    getExcepPayment(params, callback) {
      delete params.status
      getExcepPaymentPageInfos(params).then(response => {
        this.tableData = response.data.records
        callback(response.data.total)
        this.loading = false
      }).catch(() => {
        this.loading = false
      })
    },
    // 获取采购员服务费列表（待确认、待缴费、已缴费、已减免收费共用）
    getProcurementServiceFee(params, callback) {
      listProcurementServiceFeePages(params).then(response => {
        this.tableData = response.data.records
        callback(response.data.total)
        this.loading = false
      }).catch(() => {
        this.loading = false
      })
    },
    // 处理搜索时间格式
    handleCreateTime(val) {
      if (val) {
        this.searchForm.createTime_begin = this.$Moment(val[0]).format(this.momentDate)// 创建开始时间
        this.searchForm.createTime_end = this.$Moment(val[1]).format(this.momentDate)// 创建结束时间
      }
    }
  }
}
</script>

<style lang="less" scoped></style>
