<!--
  @description: 服务费退款开票
  @Modifier: sunxinman/haofengxian
  @routerPath: /fee/serviceRefund
  @date: 2020/04/08-15:40
-->
<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="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="paymentParty">
        <el-select v-model="searchForm.paymentParty" placeholder="请选择" style="width:228px">
          <el-option
            v-for="(item,index) in collectionObjList"
            :key="index"
            :label="item.typename"
            :value="item.typecode"
          />
        </el-select>
      </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="paymentMethod" label="收取方式" prop="paymentMethod">
        <el-select v-model="searchForm.paymentMethod" placeholder="请选择" style="width:228px">
          <el-option
            v-for="(item,index) in collectionWayList"
            :key="index"
            :label="item.typename"
            :value="item.typecode"
          />
        </el-select>
      </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="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="tenderManager" 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="invoiceAmount" label="开票金额（元）" prop="invoiceAmount">
        <el-input
          v-model.trim="searchForm.invoiceAmount"
          placeholder="开票金额（元）"
          style="width:228px"
          @keyup.enter.native="search.onSearch"
        />
      </el-form-item>
      <!--退款状态-->
      <el-form-item ref="tenderMethod" label="退款状态" prop="tenderMethod">
        <el-select v-model="searchForm.tenderMethod" 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="outTime" label="退款时间" prop="outTime">
        <el-date-picker
          v-model="searchForm.outTime"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          style="width:228px"
          @change="handleOutTime"
        />
      </el-form-item>
    </template>
    <template v-slot:menuLeft>
      <el-row class="table-alert-info">
        <el-col>
          <el-button type="primary" style="margin-right: 16px" @click="exportWinServiceOutInvoiceList">
            导出退款记录
          </el-button>
          <el-link :underline="false">
            单页退款总价：<span class="sum_color">{{ serviceInvoiceStatistics.pageInvoiceAmount }}</span>元
          </el-link>
          <el-link :underline="false">
            退款总价：<span class="sum_color">{{ serviceInvoiceStatistics.totalInvoiceAmount }}</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(selectInvoiceAmount) }}</span>元
          </el-link>
          <el-link :underline="false">
            已选中不含税金额<span class="sum_color">{{ moneyFormat(selectExcludTaxAmount) }}</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-key="getRowKeys"
        size="mini"
        row-class-name="row-line-height-cell16"
        @selection-change="selectionChange"
      >
        <el-table-column
          :reserve-selection="true"
          :selectable="isDisabled"
          disabled="true"
          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
          :formatter="formatPaymentParty"
          header-align="center"
          prop="paymentParty"
          label="招标代理服务费收取对象"
          align="center"
          width="220"
        />
        <!-- 招标代理服务费收取方式-->
        <el-table-column
          :formatter="formatPaymentMethod"
          header-align="center"
          label="招标代理服务费收取方式"
          prop="paymentMethod"
          min-width="200"
        />
        <!-- 付费单位 -->
        <el-table-column

          header-align="center"
          label="付费单位"
          prop="supplierName"
          min-width="200"
        />
        <!-- 开票金额（元） -->
        <el-table-column
          header-align="center"
          label="开票金额（元）"
          prop="invoiceAmount"
          align="right"
          width="120"
        />
        <!-- 不含税金额（元） -->
        <el-table-column
          header-align="center"
          label="不含税金额（元）"
          prop="excludTaxAmount"
          align="right"
          width="150"
        />
        <!-- 税率（%） -->
        <el-table-column
          header-align="center"
          label="税率（%）"
          prop="taxRate"
          align="right"
          width="90"
        />
        <!-- 税额（元） -->
        <el-table-column
          header-align="center"
          label="税额（元）"
          prop="taxAmount"
          align="right"
          width="90"
        />
        <!-- 业务部门 -->
        <el-table-column
          header-align="center"
          label="业务部门"
          prop="tenderDepart"
          min-width="150"
        />
        <!-- 项目经理 -->
        <el-table-column
          header-align="center"
          label="项目经理"
          prop="tenderManager"
          min-width="160"
        />
        <!-- 退款状态 -->
        <el-table-column
          :formatter="formatAuditStatus"
          label="退款状态"
          prop="auditStatus"
          align="center"
          width="110"
        />
        <!-- 退款时间 -->
        <el-table-column
          header-align="center"
          label="退款时间"
          prop="outTime"
          width="130"
        />
        <!-- 操作 -->
        <el-table-column
          header-align="center"
          label="操作"
          fixed="right"
          width="120"
        >
          <template slot-scope="scope">
            <!--待红冲-09 红冲中-10 红冲失败-11 已红冲-12-->
            <el-link
              v-if="scope.row.auditStatus==='09' || scope.row.auditStatus ==='11'"
              type="primary"
              @click.stop="redDash(scope.row)"
            >
              [红冲]
            </el-link>
            <el-link
              type="primary"
              @click.stop="viewDetail(scope.row)"
            >
              [查看]
            </el-link>
          </template>
        </el-table-column>
      </el-table>
    </template>
  </table-crud>
</template>
<script>
/* 接口*/
import {
  queryWinServiceOutInvoiceList,
  queryWinServiceOutInvoiceStatistics,
  checkTenderFeeWinService,
  queryWinServiceOutInvoiceListExport
} from '@/api/fee/servicefee'
  /* 公用方法*/
import { download } from '@assets/js/utils'// 常用方法
import { ServiceGroupMixin } from '../../mixins/serviceFeeGroupMixin'

export default {
  name: 'ServiceRefundList',
  mixins: [ServiceGroupMixin],
  data() {
    return {
      // 获取row的key值
      getRowKeys(row) {
        return row.outId
      },
      loading: true,
      selectInvoiceAmount: 0, // 已选中开票金额
      selectExcludTaxAmount: 0, // 已选中不含税金额
      selectTaxAmount: 0, // 已选中税额
      // 搜索
      searchForm: {
        outTime: '', // 退款时间
        auditStatus: '', // 开票状态/退款状
        invoiceAmount: '', // 开票金额
        outTimeEnd: '', // 退款时间结束
        outTimeStart: '', // 退款时间开始
        packId: '', // 标段id
        packName: '', // 标段名称
        packNo: '', // 标段编号
        paymentMethod: '', // 服务费支付方式00差额定率累进制 01固定金额 02固定比例
        paymentParty: '', // 服务费支付方00中标人 01招标人
        postStatus: '', // 邮寄状态00未邮寄01已邮寄
        supplierId: '', // 付费单位id
        supplierName: '', // 付费单位名称
        taxRate: '', // 税率%
        tenderDepart: '', // 业务部门
        tenderDepartId: '', // 业务部门ID
        tenderId: '', // 项目id
        tenderManager: '', // 项目经理
        tenderManagerId: '', // 项目经理ID
        tenderName: '', // 项目名称
        tenderNo: '', // 项目编号
        useStatus: '', // 使用状态00开票申请01开票02退款申请03退款开票
        winServiceId: '' // 开票信息id
      },
      // 服务费退款开票列表统计信息
      serviceInvoiceStatistics: {
        pageInvoiceAmount: 0, // 单页开票/退款金额
        totalInvoiceAmount: 0 // 总开票/退款金额
      },
      tableData: [],
      checkedTr: [] // 表格已选数据
    }
  },
  methods: {
    // 服务费退款开票列表-导出退款记录
    exportWinServiceOutInvoiceList() {
      const params = this.$refs.tableCrud.getQueryParams()
      queryWinServiceOutInvoiceListExport(params).then(res => {
        download(res, '服务费退款记录.xls')
      })
    },
    // 服务费退款开票列表
    loadData(params, callback) {
      queryWinServiceOutInvoiceList(params).then(response => {
        this.loading = false
        this.tableData = response.data.records
        callback(response.data.total)
      }).catch(() => {
        this.loading = false
      })
      // 服务费退款开票列表统计信息
      this.getWinServiceOutInvoiceStatistics(params)
    },
    // 服务费退款开票列表统计信息
    getWinServiceOutInvoiceStatistics(params) {
      queryWinServiceOutInvoiceStatistics(params).then(response => {
        this.serviceInvoiceStatistics = response.data
      }).catch(() => {
      })
    },
    // 红冲
    redDash(row) {
      this.$confirm('确认红冲电子发票吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        confirmButtonClass: 'confirmBtn',
        cancelButtonClass: 'cancelBtn',
        type: 'warning'
      }).then(() => {
        checkTenderFeeWinService(row.winServiceId).then(response => {
          this.$message({
            type: 'success',
            message: '操作成功'
          })
          this.$refs.tableCrud.onSearchChange()
        }).catch(() => {
        })
      })
    },
    // 查看
    viewDetail(row) {
      this.$router.push({
        path: '/fee/serviceRefundDetail',
        query: {
          outId: row.outId // 服务费退款信息ID
        }
      })
    },
    // 复选框部分禁用
    isDisabled(row) {
      return row.auditStatus === '09' || row.auditStatus === '11' // 09 待红冲 红冲失败11
    },
    // 表格复选框已选数据
    selectionChange(val) {
      this.checkedTr = val
      this.handleDocumentPrice()
    },
    // 处理搜索退款时间
    handleOutTime(val) {
      if (val) {
        this.searchForm.outTimeStart = this.$Moment(val[0]).format(this.momentSaveFormat)// 开始时间
        this.searchForm.outTimeEnd = this.$Moment(val[1]).format(this.momentSaveFormat)// 结束时间
      }
    },
    // 已选中开票金额/不含税金额/税额计算
    handleDocumentPrice() {
      let invoiceAmount = 0
      let excludTaxAmount = 0
      let taxAmount = 0
      this.checkedTr.forEach(item => {
        invoiceAmount += Number(item.invoiceAmount) // 开票金额
        excludTaxAmount += Number(item.excludTaxAmount) // 不含税金额
        taxAmount += Number(item.taxAmount) // 税额
      })
      this.selectInvoiceAmount = invoiceAmount.toFixed(2)
      this.selectExcludTaxAmount = excludTaxAmount.toFixed(2)
      this.selectTaxAmount = taxAmount.toFixed(2)
    }
  }
}
</script>
<style lang="less" scoped>

</style>
