<template>
  <div id="tax_table">
    <div style="flex: 1">
      <el-container>
        <el-table
            :data="invoiceList"
            border
            style="width: 100%; flex: 1"
            ref="table"
        >
          <el-table-column
              type="index"
              label="序号"
              width="50">
          </el-table-column>
          <el-table-column
              prop="invoiceCode"
              label="发票代码"
              width="100"
          ></el-table-column>
          <el-table-column
              prop="invoiceNumber"
              label="发票号码"
              width="100"
          ></el-table-column>
          <el-table-column
              prop="invoiceDate"
              label="开票日期"
              width="100"
          ></el-table-column>
          <el-table-column
              prop="sellerName"
              label="销方名称"
              width="200"
          ></el-table-column>
          <el-table-column
              prop="totalAmount"
              label="金额"
              width="100"
          ></el-table-column>
          <el-table-column
              prop="totalTaxes"
              label="税额"
              width="100"
          ></el-table-column>
          <el-table-column
              prop="effectiveTaxes"
              label="有效税额"
              width="100"
          ></el-table-column>
          <el-table-column
              prop="deductibleDate"
              label="勾选日期"
              width="120"
          ></el-table-column>
          <el-table-column
              prop="inputInvoiceCategory"
              label="发票类型"
              width="120"
              :formatter="getInvoiceType"
          ></el-table-column>
          <el-table-column
              prop="certificationType"
              label="用途"
              width="60"
              :formatter="getCertificationType"
          ></el-table-column>
          <el-table-column
              prop="status"
              label="发票状态"
              width="80"
              :formatter="getInvoiceStatus"
          ></el-table-column>
          <el-table-column
              prop="manageStateMemo"
              label="管理状态"
              width="100"
          ></el-table-column>
<!--          <el-table-column-->
<!--              prop="serviceStatus"-->
<!--              label="操作"-->
<!--              width="120"-->
<!--              :formatter="getServerStatus"-->
<!--          ></el-table-column>-->
        </el-table>
      </el-container>
    </div>
    <div style="height: 50px">
      <el-row type="flex" :gutter="20">
        <el-col :span="12"></el-col>
        <el-col :span="12">
          <div style="text-align: right">
            <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="pageNum"
                :page-sizes="pageSizes"
                :page-size="pageSize"
                layout="total, sizes, prev, pager, next"
                :total="totalNumber"
            ></el-pagination>
          </div>
        </el-col>
      </el-row>
    </div>
  </div>
</template>

<script>
import cache from '@/utils/cache'
import { AxiosRequestData } from '@/data/axios_request'
import DateHelper from '@/utils/date'

export default {
  name: 'tax_table',
  data() {
    return {
      token: '',
      pageNum: 1,
      pageSize: 10,
      totalNumber: 0,
      totalPage: 0,
      pageSizes: [10, 30, 50, 100],
      searchData: {
        entTaxId: '',
        selectDate: '',
        present: false,
        invoiceDateBegin: '',
        invoiceDateEnd: '',
        invoiceCode: '',
        invoiceNumber: '',
        inputInvoiceCategory: '',
        sellerName: '',
        sellerEntTaxId: '',
        pageNum: 1,
        pageSize: 10
      },
      invoiceList: [],
      exportInvoiceList: []
    }
  },
  methods: {
    // 获取发票状态
    getInvoiceStatus(row, column, cellValue) {
      if (cellValue === '0') {
        return '正常'
      }
      if (cellValue === '1') {
        return '失控'
      }
      if (cellValue === '2') {
        return '作废'
      }
      if (cellValue === '3') {
        return '红冲'
      }
      if (cellValue === '4') {
        return '异常'
      } else {
        return ''
      }
    },
    // 获取发票种类
    getInvoiceType(row, column, cellValue) {
      if (cellValue === '01') {
        return '增值税专⽤发票'
      }
      if (cellValue === '02') {
        return '货运运输业增值税专⽤发票'
      }
      if (cellValue === '03') {
        return '机动⻋销售统⼀发票'
      }
      if (cellValue === '04') {
        return '增值税电⼦普通发票（通⾏费）'
      } else {
        return ''
      }
    },
    // 获取认证类型
    getCertificationType(row, column, cellValue) {
      if (cellValue === '1') {
        return '抵扣'
      }
      if (cellValue === '2') {
        return '退税'
      }
      if (cellValue === '3') {
        return '代办退税'
      }
      if (cellValue === '4') {
        return '不抵扣'
      }
      if (cellValue === '5') {
        return '撤销抵扣'
      }
      if (cellValue === '6') {
        return '撤销不抵扣 '
      } else {
        return ''
      }
    },
    // 变更每页显示的数据条数
    handleSizeChange(val) {
      this.pageSize = val
      this.getInvoiceInfo()
    },
    // 翻页
    handleCurrentChange(val) {
      this.pageNum = val
      this.getInvoiceInfo()
    },
    // 设置查询数据
    setSearchData(searchData) {
      this.searchData = searchData
      this.token = cache.getToken()
      this.getInvoiceInfo()
      this.getExportInvoiceList()
    },
    // 获取发票分页信息
    async getInvoiceInfo() {
      const url = '/sys/invoice/deduction/deductedInvoices'
      if (this.token) {
        let response = await AxiosRequestData(
            url,
            this.token,
            {
              entTaxId: this.searchData.entTaxId,
              period: this.searchData.selectDate,
              present: this.searchData.present,
              invoiceDateBegin: this.searchData.invoiceDateBegin,
              invoiceDateEnd: this.searchData.invoiceDateEnd,
              invoiceCode: this.searchData.invoiceCode,
              invoiceNumber: this.searchData.invoiceNumber,
              inputInvoiceCategory: this.searchData.inputInvoiceCategory,
              sellerName: this.searchData.sellerName,
              sellerEntTaxId: this.searchData.sellerEntTaxId,
              pageNum: this.pageNum,
              pageSize: this.pageSize
            },
            false,
            2
        )
        if (response) {
          if (response.code === 0) {
            this.totalNumber = response.data.total
            this.totalPage = response.data.pages
            this.invoiceList = response.data.records
          }
        }
      }
    },
    // 获取发票分页信息
    async getExportInvoiceList() {
      const url = '/sys/invoice/deduction/deductedInvoices'
      if (this.token) {
        let response = await AxiosRequestData(
            url,
            this.token,
            {
              entTaxId: this.searchData.entTaxId,
              period: this.searchData.selectDate,
              present: this.searchData.present,
              invoiceDateBegin: this.searchData.invoiceDateBegin,
              invoiceDateEnd: this.searchData.invoiceDateEnd,
              invoiceCode: this.searchData.invoiceCode,
              invoiceNumber: this.searchData.invoiceNumber,
              inputInvoiceCategory: this.searchData.inputInvoiceCategory,
              sellerName: this.searchData.sellerName,
              sellerEntTaxId: this.searchData.sellerEntTaxId,
              pageNum: 1,
              pageSize: 1000
            },
            false,
            2
        )
        if (response) {
          if (response.code === 0) {
            this.exportInvoiceList = response.data.records
          }
        }
      }
    },
    // 发票清单数据导出
    invoiceExport() {
      if (this.exportInvoiceList.length > 0) {
        import('@/utils/Export2Excel').then(excel => {
          const tHeader = ['序号', '发票代码', '发票号码', '开票日期', '销方名称', '金额', '税额', '有效税额', '勾选日期', '发票类型', '用途', '发票状态', '管理状态']
          const filterVal = ['index', 'invoiceCode', 'invoiceNumber', 'invoiceDate', 'sellerName', 'totalAmount', 'totalTaxes', 'effectiveTaxes', 'deductibleDate', 'inputInvoiceCategory', 'certificationType', 'status', 'manageStateMemo']
          const filterJson = this.filterJson(this.exportInvoiceList)
          const data = this.formatJson(filterJson, filterVal)
          let TimeNow = DateHelper.getNowTime()
          excel.export_json_to_excel({
            'header': tHeader,
            'data': data,
            'filename': '发票清单' + TimeNow
          })
          // this.exportLoading = false
        })
      } else {
        this.$message.error('暂无可以导出的发票清单数据，请先进行查询再导出！')
      }
    },
    filterJson(json) {
      let res = []
      json.forEach((item, index) => {
        let obj = {}
        obj.index = index
        obj.invoiceCode = item.invoiceCode
        obj.invoiceNumber = item.invoiceNumber
        obj.invoiceDate = item.invoiceDate
        obj.sellerName = item.sellerName
        obj.totalAmount = item.totalAmount
        obj.totalTaxes = item.totalTaxes
        obj.effectiveTaxes = item.effectiveTaxes
        obj.deductibleDate = item.deductibleDate
        obj.inputInvoiceCategory = this.getInvoiceType(0, 0, item.inputInvoiceCategory)
        obj.certificationType = this.getCertificationType(0, 0, item.certificationType)
        obj.status = this.getInvoiceStatus(0, 0, item.status)
        obj.manageStateMemo = item.manageStateMemo
        res.push(obj)
      })
      return res
    },
    formatJson(json, filterVal) {
      let data = json.map(item => filterVal.map(key => {
        return item[key]
      }))
      return data
    }
  }
}
</script>

<style scoped>

</style>
