<!--
  @description: 标书费退款开票
  @Modifier: sunxinman/haofengxian
  @routerPath: /fee/tenderFeeRefundInvoice
  @date: 2020/03/20-15:18
-->
<template>
  <table-crud
    ref="tableCrud"
    :menu="true"
    :search-form="searchForm"
    search-label-width="110px"
    @on-load="loadData"
  >
    <template v-slot:header>
      标书费退款开票
    </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="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="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 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="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 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 ref="department" label="业务部门" prop="department">
        <el-input v-model.trim="searchForm.department" placeholder="业务部门" style="width:228px" @keyup.enter.native="search.onSearch" />
      </el-form-item>
      <!-- 退款状态 -->
      <el-form-item
        label="退款状态"
        prop="stageType"
      >
        <el-select v-model="searchForm.backStatus" placeholder="请选择" style="width:228px">
          <el-option
            v-for="(item,index) in refundStatusList"
            :key="index"
            :label="item.typename"
            :value="item.typecode"
          />
        </el-select>
      </el-form-item>
      <!--费用类型-->
      <el-form-item ref="feeType" label="费用类型" prop="feeType">
        <el-select v-model="searchForm.feeType" placeholder="请选择" style="width:228px">
          <el-option
            v-for="(item,index) in costTypeList"
            :key="index"
            :label="item.typename"
            :value="item.typecode"
          />
        </el-select>
      </el-form-item>
      <!-- 开票金额 -->
      <el-form-item ref="documentPrice" label="开票金额（元）" prop="documentPrice">
        <el-input v-model.trim="searchForm.documentPrice" placeholder="开票金额（元）" style="width:228px" @keyup.enter.native="search.onSearch" />
      </el-form-item>
      <!-- 退款时间 -->
      <el-form-item ref="backTime" label="退款时间" prop="backTime">
        <el-date-picker
          v-model="searchForm.backTime"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          style="width:228px"
          @change="changeBackTime"
        />
      </el-form-item>
      <!-- 开票日期 -->
      <el-form-item ref="invoiceTime" label="开票日期" prop="invoiceTime">
        <el-date-picker
          v-model="searchForm.invoiceTime"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          style="width:228px"
          @change="changeInvoiceTime"
        />
      </el-form-item>
    </template>
    <template v-slot:menuLeft>
      <el-row class="table-alert-info">
        <el-col>
          <el-button type="primary" plain @click="exportRefundList">
            导出退款记录
          </el-button>
          <el-link :underline="false">
            单页退款总价:<span class="sum_color">{{ backFeeStatistics.pageDocumentPrice }}</span>元
          </el-link>
          <el-link :underline="false">
            退款总价:<span class="sum_color">{{ backFeeStatistics.totalBackPrice }}</span>元
          </el-link>
        </el-col>
        <el-col>
          <el-link :underline="false">
            已选中<span class="sum_color">{{ checkedTr.length }}</span>条
          </el-link>
          <el-link :underline="false">
            已选中开票金额<span class="sum_color">{{ moneyFormat(selectDocumentPrice) }}</span>元
          </el-link>
          <el-link :underline="false">
            已选中不含税金额<span class="sum_color">{{ moneyFormat(selectNoTaxRatePrice) }}</span>元
          </el-link>
          <el-link :underline="false">
            已选中税额<span class="sum_color">{{ moneyFormat(selectTaxAmount) }}</span>元
          </el-link>
        </el-col>
      </el-row>
    </template>
    <template v-slot:GXTable>
      <el-table
        ref="smallTable"
        v-loading="loading"
        :data="tableData"
        row-class-name="row-line-height-cell16"
        size="mini"
        @selection-change="selectionChange"
      >
        <el-table-column
          type="selection"
          fixed="left"
          width="55"
          align="center"
        />
        <el-table-column
          type="index"
          label="序号"
          fixed="left"
          width="60px"
          align="center"
        />
        <!-- 招标项目编号/名称  -->
        <el-table-column

          fixed="left"
          label="招标项目编号/名称"
          min-width="260"
        >
          <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="标段（包）编号/名称"
          fixed="left"
          min-width="260"
        >
          <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="160"
        />
        <!-- 蓝票发票号码 -->
        <el-table-column
          header-align="center"
          label="蓝票发票号码"
          prop="invoiceNoBlue"
          align="center"
          min-width="140"
        />
        <!-- 红票发票号码 -->
        <el-table-column
          header-align="center"
          label="红票发票号码"
          prop="invoiceNoRed"
          align="center"
          min-width="140"
        />
        <!-- 开票金额（元） -->
        <el-table-column
          header-align="center"
          label="开票金额（元）"
          prop="documentPrice"
          align="right"
          width="110"
        />
        <!-- 不含税金额（元） -->
        <el-table-column
          header-align="center"
          label="不含税金额（元）"
          prop="noTaxRatePrice"
          align="right"
          width="130"
        />
        <!-- 税率 -->
        <el-table-column
          header-align="center"
          label="税率 (%)"
          prop="documentRate"
          align="right"
          min-width="100"
        />
        <!-- 税额 -->
        <el-table-column
          header-align="center"
          label="税额 (%)"
          prop="taxAmount"
          align="right"
          width="90"
        />
        <!-- 费用类型 -->
        <el-table-column
          :formatter="formatFeeType"
          header-align="center"
          label="费用类型"
          prop="feeType"
          min-width="120"
        />
        <!-- 业务部门 -->
        <el-table-column

          header-align="center"
          label="业务部门"
          prop="department"
          min-width="160"
        />
        <!-- 项目经理 -->
        <el-table-column
          label="项目经理"
          prop="tenderManager"
          align="center"
          min-width="80"
        />
        <!-- 退款状态 -->
        <el-table-column
          :formatter="formatBackStatus"
          header-align="center"
          label="退款状态"
          prop="backStatus"
          min-width="100"
        />
        <!-- 退款时间 -->
        <el-table-column
          header-align="center"
          label="退款时间"
          prop="backTime"
          align="center"
          width="140"
        />
        <!-- 开票状态 -->
        <el-table-column
          :formatter="formatInvoiceStatus"
          header-align="center"
          label="开票状态"
          prop="invoiceStatus"
          align="center"
          min-width="120"
        />
        <!-- 开票时间 -->
        <el-table-column
          header-align="center"
          label="开票时间"
          prop="invoiceTime"
          align="center"
          width="140"
        />
        <!-- 操作 -->
        <el-table-column
          header-align="center"
          label="操作"
          fixed="right"
          width="100"
        >
          <template slot-scope="scope">
            <el-link
              v-if="scope.row.invoiceStatus!=='04'"
              type="primary"
              @click.stop="redRush(scope.row)"
            >
              [红冲]
            </el-link>
            <el-link
              type="primary"
              @click.stop="view(scope.row)"
            >
              [查看]
            </el-link>
          </template>
        </el-table-column>
      </el-table>
    </template>
  </table-crud>
</template>
<script>
/* 接口*/
import { queryPassAuditOutMoneyList, queryBackFeeStatistics, exportPassAuditOutMoneyList, refundRedDashed } from '@api/fee/docfee'
/* 公用方法*/
import { moneyFormat, download } from '@assets/js/utils'// 常用方法
/* 混合开发方法-参数字段*/
import { TenderFeeGroupMixin } from '../mixins/tenderFeeGroupMixin'

export default {
  name: 'TenderFeeRecordInvoiceList',
  mixins: [TenderFeeGroupMixin],
  data() {
    return {
      moneyFormat: moneyFormat, // 金额添加千分位
      loading: true,
      selectDocumentPrice: 0, // 已选中开票金额
      selectNoTaxRatePrice: 0, // 已选中不含税金额
      selectTaxAmount: 0, // 已选中税额
      // 搜索
      searchForm: {
        invoiceTime: '', // 开票时间
        backTime: '', // 退款时间
        backEndTime: '', // 退款结束时间
        backStartTime: '', // 退款开始时间
        backStatus: '', // 退款状态: 00未退款 01已退款 02退款中 03退款申请中 04退款审批驳回 05退款审批通过 06退款失败 07红冲中
        department: '', // 业务部门
        documentPrice: '', // 开票金额
        feeType: '', // 费用类型: 00资格预审文件费 01招标文件费
        invoiceEndTime: '', // 开票结束时间
        invoiceStartTime: '', // 开票开始时间
        packName: '', // 标段名称
        packNo: '', // 标段编号
        supplierName: '', // 投标人
        tenderManager: '', // 项目经理
        tenderName: '', // 项目名称
        tenderNo: '' // 项目编号
      },
      // 退款费用统计数据
      backFeeStatistics: {
        pageDocumentPrice: 0, // 单页退款金额
        totalBackPrice: 0 // 总退款金额
      },
      tableData: [],
      checkedTr: [] // 表格已选数据
    }
  },
  methods: {
    // 获取数据
    loadData(params, callback) {
      queryPassAuditOutMoneyList(params).then(response => {
        this.loading = false
        this.tableData = response.data.records
        callback(response.data.total)
      }).catch(() => {
        this.loading = false
      })
      // 标书费开票管理-开票费用统计数据
      this.getBackFeeStatistics(params)
    },
    // 标书费开票管理-开票费用统计数据
    getBackFeeStatistics(params) {
      queryBackFeeStatistics(params).then(response => {
        this.backFeeStatistics = response.data
      }).catch(() => {
      })
    },
    // 导出退款记录
    exportRefundList() {
      if (!this.checkedTr.length) {
        this.$message({
          message: '请选择一条记录',
          type: 'warning'
        })
      } else {
        const feeDocIds = this.checkedTr.map(row => row.feeDocId).join(',')
        this.exportInvoice(feeDocIds)
      }
    },
    // 标书费退款开票-导出退款开票记录
    exportInvoice(feeDocIds) {
      const params = {
        feeDocIds
      }
      exportPassAuditOutMoneyList(params).then(res => {
        download(res, '退款开票记录.xls')
      }).catch(() => {
      })
    },
    // 开票时间
    changeInvoiceTime(val) {
      if (val) {
        this.searchForm.invoiceStartTime = this.$Moment(val[0]).format(this.momentDate)//  开票开始时间
        this.searchForm.invoiceEndTime = this.$Moment(val[1]).format(this.momentDate)// 开票结束时间
      }
    },
    // 退款时间
    changeBackTime(val) {
      if (val) {
        this.searchForm.backStartTime = this.$Moment(val[0]).format(this.momentDate)//  退款开始时间
        this.searchForm.backEndTime = this.$Moment(val[1]).format(this.momentDate)// 退款结束时间
      }
    },
    // 红冲
    redRush(row) {
      this.$confirm('确认红冲电子发票吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        confirmButtonClass: 'confirmBtn',
        cancelButtonClass: 'cancelBtn',
        type: 'warning'
      }).then(() => {
        refundRedDashed(row.feeDocId).then(res => {
          this.$message({
            message: res.header.msg,
            type: 'success'
          })
          this.$refs.tableCrud.onSearchChange()
        }).catch(() => {
        })
      })
    },
    // 查看
    view(row) {
      this.$router.push({
        path: '/fee/tenderFeeRefundInvoiceDetail',
        name: 'TenderFeeRefundInvoiceDetail',
        params: {
          feeDocId: row.feeDocId
        }
      })
    },
    // 返回
    back() {
      this.$router.push({
        path: '/fee/tenderFeeManage'
      })
    },
    // 表格复选框已选数据
    selectionChange(val) {
      this.checkedTr = val
      this.handleDocumentPrice()
    },
    // 已选中开票金额/不含税金额/税额计算
    handleDocumentPrice() {
      let documentPrice = 0
      let noTaxRatePrice = 0
      let taxAmount = 0
      this.checkedTr.forEach(item => {
        documentPrice += Number(item.documentPrice) // 开票金额
        noTaxRatePrice += Number(item.noTaxRatePrice) // 不含税金额
        taxAmount += Number(item.taxAmount) // 税额
      })
      this.selectDocumentPrice = documentPrice.toFixed(2)
      this.selectNoTaxRatePrice = noTaxRatePrice.toFixed(2)
      this.selectTaxAmount = taxAmount.toFixed(2)
    }
  }
}
</script>
<style lang="less" scoped>
</style>
