<!--
  @description: 标书费收款记账
  @Modifier: sunxinman/haofengxian
  @routerPath: /fee/tenderFeeBookKeeping
  @date: 2020/03/23-09:30
-->
<template>
  <div>
    <table-crud
      ref="tableCrud"
      :menu="true"
      :search-form="searchForm"
      search-label-width="110px"
      @on-load="getTallyList"
    >
      <template v-slot:header>
        标书费收款{{ docfeeShowShielding ? '记账':'查看' }}
      </template>
      <template v-slot:search="search">
        <el-form
          ref="searchForm"
          :model="searchForm"
          inline
          label-width="110px"
          label-position="left"
        >
          <!-- 标段（包）编号 -->
          <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>
          <template v-if="docfeeShowShielding">
            <!-- 收款开票状态 -->
            <el-form-item label="收款开票状态" prop="invoiceStatus">
              <el-select v-model="searchForm.invoiceStatus" placeholder="请选择" style="width:228px">
                <el-option
                  v-for="(item,index) in docInvoiceStatusList"
                  :key="index"
                  :label="item.typename"
                  :value="item.typecode"
                />
              </el-select>
            </el-form-item>
            <!--收款记账状态-->
            <el-form-item ref="bookkeepingStatus" label="收款记账状态" prop="bookkeepingStatus">
              <el-select v-model="searchForm.bookkeepingStatus" placeholder="请选择" style="width:228px">
                <el-option
                  v-for="(item,index) in collectionBookkeepingStatusList"
                  :key="index"
                  :label="item.typename"
                  :value="item.typecode"
                />
              </el-select>
            </el-form-item>
          </template>
          <!-- 开票金额 -->
          <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>
          <template v-if="docfeeShowShielding">
            <!-- 到账日期 -->
            <el-form-item ref="accountingTime" label="到账日期" prop="accountingTime">
              <el-date-picker
                v-model="searchForm.accountingTime"
                type="daterange"
                range-separator="-"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                style="width:228px"
                @change="changeAccountingTime"
              />
            </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>
            <!-- 记账日期 -->
            <el-form-item v-if="docfeeShowShielding" ref="bookkeepingTime" label="记账日期" prop="bookkeepingTime">
              <el-date-picker
                v-model="searchForm.bookkeepingTime"
                type="daterange"
                range-separator="-"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                style="width:228px"
                @change="changeBookkeepingTime"
              />
            </el-form-item>
          </template>
        </el-form>
      </template>
      <template v-slot:menuLeft>
        <el-row v-if="docfeeShowShielding" class="table-alert-info">
          <el-col>
            <el-button type="primary" @click="surePatch">
              批量记账
            </el-button>
            <el-button type="primary" plain @click="exportExcel">
              导出标书费记账记录
            </el-button>
            <el-link :underline="false">
              单页开票金额<span class="sum_color">{{ statistics.pageDocumentPrice }}</span>元
            </el-link>
            <el-link :underline="false">
              单页开票不含税金额<span class="sum_color">{{ statistics.pageNoTaxRatePrice }}</span>元
            </el-link>
            <el-link :underline="false">
              单页开票税额<span class="sum_color">{{ statistics.pageTaxAmount }}</span>元
            </el-link>
            <el-link :underline="false">
              总开票金额<span class="sum_color">{{ statistics.totalDocumentPrice }}</span>元
            </el-link>
            <el-link :underline="false">
              总页开票不含税金额<span class="sum_color">{{ statistics.totalNoTaxRatePrice }}</span>元
            </el-link>
            <el-link :underline="false">
              总页开票税额<span class="sum_color">{{ statistics.totalTaxAmount }}</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="keepingAccountsTable"
          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"
            disabled="true"
            type="selection"
            fixed="left"
            align="center"
            width="55"
          />
          <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="标段（包）编号/名称"
            prop="packNo"
            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
            v-if="docfeeShowShielding"
            :formatter="formatBookkeepingStatus"
            header-align="center"
            label="收款记账状态"
            prop="bookkeepingStatus"
            align="left"
            min-width="100"
          />
          <!-- 开票金额（元） -->
          <el-table-column
            header-align="center"
            label="金额（元）"
            prop="documentPrice"
            align="right"
            width="110"
          />
          <!-- 费用类型 -->
          <el-table-column
            v-if="docfeeShowShielding"
            :formatter="formatFeeType"
            header-align="center"
            label="费用类型"
            prop="feeType"
            align="left"
            min-width="120"
          />
          <!-- 业务部门 -->
          <el-table-column

            header-align="center"
            label="业务部门"
            prop="department"
            min-width="160"
          />

          <template v-if="docfeeShowShielding">
            <!-- 收款开票状态 -->
            <el-table-column
              :formatter="formatInvoiceStatus"
              header-align="center"
              label="收款开票状态"
              prop="invoiceStatus"
              align="center"
              min-width="100"
            />
            <!-- 开票时间 -->
            <el-table-column
              header-align="center"
              label="开票时间"
              prop="invoiceTime"
              align="center"
              width="140"
            />
            <!-- 到账时间 -->
            <el-table-column
              header-align="center"
              label="到账时间"
              prop="accountingTime"
              align="center"
              width="140"
            />
            <!-- 记账时间 -->
            <el-table-column
              v-if="docfeeShowShielding"
              header-align="center"
              label="记账时间"
              prop="bookkeepingTime"
              align="center"
              width="140"
            />
          </template>
          <!-- 项目经理 -->
          <el-table-column
            label="项目经理"
            prop="tenderManager"
            align="center"
            min-width="80"
          />
          <!-- 操作 -->
          <!--未记账00 已记账01 记账失败02 记账申请中03-->
          <el-table-column
            header-align="center"
            label="操作"
            fixed="right"
            width="100"
          >
            <template slot-scope="scope">
              <template v-if="docfeeShowShielding">
                <el-link
                  v-if="scope.row.bookkeepingStatus==='00' || scope.row.bookkeepingStatus==='02'"
                  type="primary"
                  @click.stop="synchronization(scope.row)"
                >
                  [记账]
                </el-link>
              </template>
              <el-link
                type="primary"
                @click.stop="view(scope.row)"
              >
                [查看]
              </el-link>
            </template>
          </el-table-column>
        </el-table>
      </template>
    </table-crud>
  </div>
</template>
<script>
/* 接口*/
import { queryTallyListPages, queryTallyStatistics, exportTallyList, doBatchcTally } from '@api/fee/docfee'
/* 公用方法*/
import { moneyFormat, download } from '@assets/js/utils'// 常用方法
/* 混合开发方法-参数字段*/
import { TenderFeeGroupMixin } from '../mixins/tenderFeeGroupMixin'
import { docfeeSystemConfig } from '../mixins/docfeeSystemConfig'

export default {
  name: 'TenderFeeIncoiceManageList',
  mixins: [TenderFeeGroupMixin, docfeeSystemConfig],
  data() {
    return {
      // 获取row的key值
      getRowKeys(row) {
        return row.id
      },
      moneyFormat: moneyFormat, // 金额添加千分位
      loading: true,
      selectDocumentPrice: 0, // 已选中开票金额
      selectNoTaxRatePrice: 0, // 已选中不含税金额
      selectTaxAmount: 0, // 已选中税额
      // 开标费用统计数据
      statistics: {
        pageDocumentPrice: 0, // 单页开票金额
        pageNoTaxRatePrice: 0, // 单页开票不含税金额
        pageTaxAmount: 0, // 单页开票税额
        totalDocumentPrice: 0, // 总开票金额
        totalNoTaxRatePrice: 0, // 总开票不含税金额
        totalTaxAmount: 0 // 总页开票税额
      },
      // 搜索
      searchForm: {
        accountingTime: '', // 到账时间
        invoiceTime: '', // 开票时间
        bookkeepingTime: '', // 记账时间
        accountingEndTime: '', // 到账结束时间
        accountingStartTime: '', // 到账开始时间
        bookkeepingEndTime: '', // 记账结束时间
        bookkeepingStartTime: '', // 记账开始时间
        bookkeepingStatus: '', // 接口记账状态: 00 未记账 01已经记账 02 记账失败 03 记账申请中
        department: '', // 业务部门
        documentPrice: '', // 开票金额
        invoiceEndTime: '', // 开票结束时间
        invoiceStartTime: '', // 开票开始时间
        invoiceStatus: '', // 开票状态: 00 未开票 01已开票 02 开票失败 03 开票申请中 04已电子开票重开 05电子红冲中 06已电子红冲 07电子红冲失败
        packName: '', // 标段名称
        packNo: '', // 标段编号
        supplierName: '', // 投标人
        tenderManager: '', // 项目经理
        tenderName: '', // 项目名称
        tenderNo: '' // 项目编号
      },
      tableData: [],
      checkedTr: []// 确认记账批量记账存放数据
    }
  },
  methods: {
    // 财预部-标书费收款记账列表
    getTallyList(params, callback) {
      this.params = params
      queryTallyListPages(params).then(response => {
        this.loading = false
        this.tableData = response.data.records
        callback(response.data.total)
      }).catch(() => {
        this.loading = false
      })
      // 财务部-记账金额统计
      this.getTallyStatistics(params)
    },
    // 财务部-记账金额统计
    getTallyStatistics(params) {
      queryTallyStatistics(params).then(response => {
        this.statistics = response.data
      }).catch(() => {
      })
    },
    // 导出退款记录
    exportExcel() {
      // 导出标书费收款记账列表
      this.exportTally()
    },
    // 财预部-导出标书费收款记账列表
    exportTally() {
      const params = this.$refs.tableCrud.getQueryParams()
      exportTallyList(params).then(res => {
        download(res, '标书费收款记账列表.xls')
      }).catch(() => {
      })
    },
    // 开票
    chargeAccount(feeDocIds) {
      const params = {
        feeDocIds
      }
      doBatchcTally(params).then(res => {
        this.$message({
          message: res.header.msg,
          type: 'success'
        })
        // 获取列表信息
        this.$refs.tableCrud.onSearchChange()
        this.$refs.keepingAccountsTable.clearSelection()
      }).catch(() => {
      })
    },
    // 记账
    synchronization(row) {
      this.$confirm('确认推送NC记账吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        confirmButtonClass: 'confirmBtn',
        cancelButtonClass: 'cancelBtn',
        type: 'warning'
      }).then(() => {
        this.chargeAccount(row.id)
      })
    },
    // 确认 批量记账记账
    surePatch() {
      if (this.checkedTr.length) {
        this.$confirm('确认批量记账吗？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          confirmButtonClass: 'confirmBtn',
          cancelButtonClass: 'cancelBtn',
          type: 'warning'
        }).then(() => {
          const feeDocIds = this.checkedTr.map(row => row.id).join(',')
          this.chargeAccount(feeDocIds)
        })
      } else {
        this.$message({ message: '请选择一条记录!', type: 'warning' })
      }
    },
    // 查看
    view(row) {
      this.$router.push({
        path: '/fee/tenderFeeBookKeepingDetail',
        name: 'TenderFeeBookKeepingDetail',
        params: {
          feeDocId: row.id
        }
      })
    },
    // 到账时间
    changeAccountingTime(val) {
      if (val) {
        this.searchForm.accountingStartTime = this.$Moment(val[0]).format(this.momentDate)// 到账开始时间
        this.searchForm.accountingEndTime = this.$Moment(val[1]).format(this.momentDate)// 到账结束时间
      }
    },
    // 开票时间
    changeInvoiceTime(val) {
      if (val) {
        this.searchForm.invoiceStartTime = this.$Moment(val[0]).format(this.momentDate)//  开票开始时间
        this.searchForm.invoiceEndTime = this.$Moment(val[1]).format(this.momentDate)// 开票结束时间
      }
    },
    // 记账时间
    changeBookkeepingTime(val) {
      if (val) {
        this.searchForm.bookkeepingStartTime = this.$Moment(val[0]).format(this.momentDate)//  记账开始时间
        this.searchForm.bookkeepingEndTime = this.$Moment(val[1]).format(this.momentDate)// 记账结束时间
      }
    },
    // 选择 批量记账记账
    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) // 开票金额
        if (item.noTaxRatePrice) {
          noTaxRatePrice += Number(item.noTaxRatePrice) // 不含税金额
        }
        if (item.taxAmount) {
          taxAmount += Number(item.taxAmount) // 税额
        }
      })
      this.selectDocumentPrice = documentPrice.toFixed(2)
      this.selectNoTaxRatePrice = noTaxRatePrice.toFixed(2)
      this.selectTaxAmount = taxAmount.toFixed(2)
    },
    // 返回
    back() {
      this.$router.push({
        path: '/fee/tenderFeeManage'
      })
    },
    // 复选框部分禁用
    isDisabled(row) {
      return row.bookkeepingStatus === '00' || row.bookkeepingStatus === '02' // 00可以制作
    }
  }
}
</script>
<style lang="less" scoped>
</style>
