<!--
  @description: 保证金退款记账
  @Modifier: sunxinman/haofengxian
  @routerPath: /fee/cashDepositRefundBookkeeping
  @date: 2020/04/01-11:58
-->
<template>
  <table-crud
    ref="tableCrud"
    :menu="true"
    :search-form="searchForm"
    search-label-width="110px"
    @on-load="loadData"
  >
    <template v-slot:header>
      保证金退款{{ bailShowShielding?'记账':'查看' }}
    </template>
    <template v-slot:search="search">
      <!-- 标段（包）编号 -->
      <el-form-item ref="packNo" label="标段（包）编号" prop="packNo">
        <el-input v-model.trim="searchForm.packNo" placeholder="标段（包）编号" style="width:228px" @keyup.enter.native="search.onSearch" />
      </el-form-item>
      <!-- 项目编号 -->
      <el-form-item ref="tenderNo" label="项目编号" prop="tenderNo">
        <el-input v-model.trim="searchForm.tenderNo" placeholder="项目编号" style="width:228px" @keyup.enter.native="search.onSearch" />
      </el-form-item>
      <!-- 项目名称 -->
      <el-form-item ref="tenderName" label="项目名称" prop="tenderName">
        <el-input v-model.trim="searchForm.tenderName" placeholder="项目名称" style="width:228px" @keyup.enter.native="search.onSearch" />
      </el-form-item>
      <!-- 标段（包）名称 -->
      <el-form-item ref="packName" label="标段（包）名称" prop="packName">
        <el-input v-model.trim="searchForm.packName" placeholder="标段（包）名称" style="width:228px" @keyup.enter.native="search.onSearch" />
      </el-form-item>
      <!-- 退款时间 -->
      <el-form-item v-if="bailShowShielding" ref="refundTime" label="退款时间" prop="refundTime">
        <el-date-picker
          v-model="searchForm.refundTime"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          style="width:228px"
          @change="changeRefundTime"
        />
      </el-form-item>
      <!-- 项目经理 -->
      <el-form-item label="项目经理" prop="tenderManager">
        <el-input v-model.trim="searchForm.tenderManager" placeholder="项目经理" style="width:228px" @keyup.enter.native="search.onSearch" />
      </el-form-item>
      <!-- 退款记账状态 -->
      <el-form-item v-if="bailShowShielding" label="退款记账状态" prop="bookkeepingStatus">
        <el-select v-model="searchForm.bookkeepingStatus" placeholder="请选择" style="width:228px">
          <el-option
            v-for="(item,index) in billingStatusList"
            :key="index"
            :label="item.typename"
            :value="item.typecode"
          />
        </el-select>
      </el-form-item>
      <!-- 业务部门 -->
      <el-form-item ref="tenderDepart" label="业务部门" prop="tenderDepart">
        <el-input v-model.trim="searchForm.tenderDepart" placeholder="业务部门" style="width:228px" @keyup.enter.native="search.onSearch" />
      </el-form-item>
      <!-- 投标人 -->
      <el-form-item ref="supplierName" label="投标人" prop="supplierName">
        <el-input v-model.trim="searchForm.supplierName" placeholder="投标人" style="width:228px" @keyup.enter.native="search.onSearch" />
      </el-form-item>
      <!-- 退款金额（元） -->
      <el-form-item ref="amount" label="退款金额（元）" prop="amount">
        <el-input v-model.trim="searchForm.amount" placeholder="退款金额（元）" style="width:228px" @keyup.enter.native="search.onSearch" />
      </el-form-item>
      <!-- 错误来款 -->
      <el-form-item label="错误来款" prop="errorStatus">
        <el-select v-model="searchForm.errorStatus" placeholder="请选择" style="width:228px">
          <el-option
            v-for="(item,index) in errorModelList"
            :key="index"
            :label="item.typename"
            :value="item.typecode"
          />
        </el-select>
      </el-form-item>
    </template>
    <template v-slot:menuLeft>
      <section class="table-alert-info">
        <el-button v-if="bailShowShielding" type="primary" @click="batchBookkeeping">
          批量记账
        </el-button>
        <el-button @click="exportDepositBookkeepingOut">
          导出当前记录
        </el-button>
        <el-link :underline="false">
          单页到账金额:<span class="sum_color">{{ bookkeepingOutStatistics.pagePrice }}</span>元
        </el-link>
        <el-link :underline="false">
          总页到账金额:<span class="sum_color">{{ bookkeepingOutStatistics.totalPrice }}</span>元
        </el-link>
      </section>
    </template>
    <template v-slot:GXTable>
      <el-table
        ref="collectionAccountTable"
        v-loading="loading"
        :data="tableData"
        :row-key="getRowKeys"
        row-class-name="row-line-height-cell16"
        size="mini"
        @selection-change="selectionChange"
      >
        <el-table-column
          :reserve-selection="true"
          :selectable="isDisabled"
          type="selection"
          fixed="left"
          width="55"
        />
        <el-table-column
          type="index"
          label="序号"
          fixed="left"
          width="60px"
          align="center"
        />
        <!-- 招标项目编号/名称  -->
        <el-table-column
          label="招标项目编号/名称"
          min-width="220"
        >
          <template slot-scope="scope">
            <p>{{ scope.row.tenderNo }}</p>
            <p>{{ scope.row.tenderName }}</p>
          </template>
        </el-table-column>
        <!-- 标段（包）编号/名称 -->
        <el-table-column
          header-align="center"
          label="标段（包）编号/名称"
          prop="packName"
          min-width="200"
        >
          <template slot-scope="scope">
            <p>{{ scope.row.packNo }}</p>
            <p>{{ scope.row.packName }}</p>
          </template>
        </el-table-column>
        <!-- 投标人 -->
        <el-table-column
          header-align="center"
          prop="supplierName"
          label="投标人"
          min-width="220"
        />
        <!-- 退款金额（元） -->
        <el-table-column
          header-align="center"
          prop="amount"
          label="退款金额（元）"
          min-width="110"
          align="center"
        />
        <!-- 错误来款 -->
        <el-table-column
          :formatter="formatErrorStatus"
          header-align="center"
          prop="errorStatus"
          label="错误来款"
          align="center"
          min-width="80"
        />
        <!-- 退款时间 -->
        <el-table-column
          header-align="center"
          prop="refundTime"
          label="退款时间"
          width="140"
          align="center"
        />
        <!-- 记账时间 -->
        <el-table-column
          v-if="bailShowShielding"
          header-align="center"
          prop="bookkeepingTime"
          label="记账时间"
          width="140"
          align="center"
        />
        <!-- 业务部门 -->
        <el-table-column
          header-align="center"
          label="业务部门"
          prop="tenderDepart"
          min-width="160"
        />
        <!-- 供应商企业性质 -->
        <!-- <el-table-column
          :formatter="formatPriEnterprise"
          align="center"
          label="供应商企业性质"
          prop="isPriEnterprise"
          width="150"
        /> -->
        <!-- 项目经理 -->
        <el-table-column
          label="项目经理"
          prop="tenderManager"
          align="center"
          min-width="80"
        />
        <!-- 退款记账状态 -->
        <el-table-column
          v-if="bailShowShielding"
          :formatter="formatBookkeepingStatus"
          header-align="center"
          label="退款记账状态"
          prop="bookkeepingStatus"
          min-width="100"
        />
        <!-- 操作 -->
        <el-table-column
          header-align="center"
          label="操作"
          fixed="right"
          width="180"
        >
          <!--bookkeepingStatus 未记账00 记账中01 已记账02 记账失败03 手工记账04-->
          <template slot-scope="scope">
            <template v-if="bailShowShielding">
              <el-link
                v-if="scope.row.bookkeepingStatus==='00' || scope.row.bookkeepingStatus==='03'"
                type="primary"
                @click="bookKeeping(scope.row)"
              >
                记账
              </el-link>
              <el-link
                v-if="scope.row.bookkeepingStatus==='00' || scope.row.bookkeepingStatus==='03'"
                type="primary"
                @click="manualAccounting(scope.row)"
              >
                手工记账
              </el-link>
            </template>
            <el-link
              type="primary"
              @click="viewDetail(scope.row)"
            >
              查看
            </el-link>
          </template>
        </el-table-column>
      </el-table>
    </template>
  </table-crud>
</template>
<script>
/* 接口*/
import { queryDepositBookkeepingOutList, ncDepositBookkeepingOut, manualDepositBookkeepingOut, queryBookkeepingOutStatistics, exportDepositBookkeepingOutList } from '@api/fee/bailfee'
/* 混合开发方法-参数字段*/
import { cashDepositGroupMixin } from '../mixins/cashDepositGroupMixin'
import { depositSystemConfig } from '../mixins/depositSystemConfig'
import { download, filterDictText } from '@/assets/js/utils'// 常用方法
export default {
  name: 'CashDepositRefundBookkeepingList',
  mixins: [cashDepositGroupMixin, depositSystemConfig],
  data() {
    return {
      /* 字典信息-start */
      isPriEnterpriseList: [], // 供应商企业性质
      /* 字典信息-end */
      // 获取row的key值
      getRowKeys(row) {
        return row.bookkeepingOutId
      },
      loading: true,
      // 搜索
      searchForm: {
        refundTime: [], // 到账时间
        amount: '', // 交易金额
        bookkeepingOutId: '', // 收款记账信息ID
        bookkeepingTimeEnd: '', // 记账时间结束
        bookkeepingTimeStart: '', // 记账时间开始
        inDateEnd: '', // 到账时间结束
        inDateStart: '', // 到账时间开始
        packId: '', // 标段ID
        packName: '', // 标段名称
        packNo: '', // 标段编号
        supplierId: '', // 供应商ID
        supplierName: '', // 供应商名称
        tenderDepart: '', // 业务部门
        tenderDepartId: '', // 业务部门ID
        tenderId: '', // 项目ID
        tenderManager: '', // 项目经理
        tenderManagerId: '', // 项目经理ID
        tenderName: '', // 项目名称
        tenderNo: '' // 项目编号
      },
      // 退款费用统计数据
      bookkeepingOutStatistics: {
        pagePrice: 0, // 单页退款金额
        totalPrice: 0 // 总退款金额
      },
      tableData: [],
      checkedTr: [] // 批量记账数据存放
    }
  },
  created() {
    this.initGroup()// 获取字典方法
  },
  methods: {
    // 获取字典方法
    initGroup() {
      const data = [
        'isPriEnterprise' // 供应商企业性质
      ]
      this.$store.dispatch('getGroupType', data).then(res => {
        Object.keys(res.bidTypeData).forEach((key) => {
          this[key] = res.bidTypeData[key]
        })
      })
    },
    // 表格字典值替换文本-供应商企业性质
    formatPriEnterprise(row, column, cellValue) {
      return filterDictText(this.isPriEnterpriseList, cellValue)
    },
    // 获取数据
    loadData(params, callback) {
      queryDepositBookkeepingOutList(params).then(response => {
        this.loading = false
        this.tableData = response.data.records
        callback(response.data.total)
      }).catch(() => {
        this.loading = false
      })
      // 保证金收款记账列表-费用统计数据
      this.getBackFeeStatistics(params)
    },
    // 保证金收款记账列表-费用统计数据
    getBackFeeStatistics(params) {
      queryBookkeepingOutStatistics(params).then(response => {
        const { pagePrice, totalPrice } = response.data
        this.bookkeepingOutStatistics.pagePrice = pagePrice// 单页退款金额
        this.bookkeepingOutStatistics.totalPrice = totalPrice// 总退款金额
      }).catch(() => {

      })
    },
    // 提交NC记账调用后端接口
    submitNcDepositBookkeepingOut(bookkeepingOutIds) {
      ncDepositBookkeepingOut(bookkeepingOutIds).then(res => {
        this.$message({
          message: res.header.msg,
          type: 'success'
        })
        // 获取列表信息
        this.$refs.tableCrud.onSearchChange()
        this.$refs.collectionAccountTable.clearSelection()
      }).catch(() => {
      })
    },
    // 批量记账
    batchBookkeeping() {
      if (!this.checkedTr.length) {
        this.$message({
          message: '请选择一条记录',
          type: 'warning'
        })
      } else {
        this.$confirm('确认批量推送NC记账吗？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          confirmButtonClass: 'confirmBtn',
          cancelButtonClass: 'cancelBtn',
          type: 'warning'
        }).then(() => {
          const bookkeepingOutId = this.checkedTr.map(row => row.bookkeepingOutId).join(',')
          this.submitNcDepositBookkeepingOut(bookkeepingOutId)
        })
      }
    },
    // 记账
    bookKeeping(row) {
      this.$confirm('确认推送NC记账吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        confirmButtonClass: 'confirmBtn',
        cancelButtonClass: 'cancelBtn',
        type: 'warning'
      }).then(() => {
        this.submitNcDepositBookkeepingOut(row.bookkeepingOutId)
      })
    },
    // 手工记账
    manualAccounting(row) {
      this.$confirm('确认手工记账吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        confirmButtonClass: 'confirmBtn',
        cancelButtonClass: 'cancelBtn',
        type: 'warning'
      }).then(() => {
        this.submitManualDepositBookkeepingOut(row.bookkeepingOutId)
      })
    },
    // 提交手工记账调用后端方法
    submitManualDepositBookkeepingOut(bookkeepingOutId) {
      manualDepositBookkeepingOut(bookkeepingOutId).then(res => {
        this.$message({
          message: res.header.msg,
          type: 'success'
        })
        // 获取列表信息
        this.$refs.tableCrud.onSearchChange()
      })
    },
    // 选择
    selectionChange(val) {
      this.checkedTr = val
    },
    // 查看
    viewDetail(row) {
      this.$router.push({
        path: '/fee/cashDepositRefundDetail',
        name: 'CashDepositRefundDetail',
        params: {
          bookkeepingOutId: row.bookkeepingOutId
        }
      })
    },
    // 到账日期
    changeRefundTime(val) {
      if (val) {
        this.searchForm.refundTimeStart = this.$Moment(val[0]).format(this.momentSaveFormat)// 开始时间
        this.searchForm.refundTimeEnd = this.$Moment(val[1]).format(this.momentSaveFormat)// 结束时间
      }
    },
    // 复选框部分禁用--bookkeepingStatus 未记账00 记账中01 已记账02 记账失败03 手工记账04
    isDisabled(row) {
      return row.bookkeepingStatus === '00' || row.bookkeepingStatus === '03'
    },
    // 导出保证金退款记录
    exportDepositBookkeepingOut() {
      const params = this.$refs.tableCrud.getQueryParams()
      exportDepositBookkeepingOutList(params).then(res => {
        download(res, '保证金记账信息表.xls')
      }).catch(() => {
      })
    }
  }
}
</script>
<style lang="less" scoped>

</style>
