<template>
  <div class="classes-base-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="6">
        <el-button type="primary" @click="showImport">
          {{ __('批量导入') }}
        </el-button>
      </vab-query-form-left-panel>
    </vab-query-form>

    <el-table
      v-loading="listLoading"
      border
      :data="list"
      :height="tableConfig.height"
      @selection-change="setSelectRows"
    >
      <el-table-column
        align="center"
        fixed="left"
        :label="__('发票编号')"
        prop="order_invoice_id"
        show-overflow-tooltip
        width="100"
      />
      <el-table-column
        align="center"
        :label="__('订单编号')"
        prop="order_id"
        show-overflow-tooltip
        width="180"
      />
      <el-table-column
        align="center"
        :label="__('供应商名称')"
        prop="supply_user_name"
        show-overflow-tooltip
        width="100"
      />
      <el-table-column
        align="center"
        :label="__('税率')"
        prop="customer_tax_rate"
        show-overflow-tooltip
        width="100"
      />
      <el-table-column
        align="center"
        :label="__('发票抬头')"
        prop="invoice_title"
        show-overflow-tooltip
        width="180"
      />
      <el-table-column
        align="center"
        :label="__('发票内容')"
        prop="invoice_content"
        show-overflow-tooltip
        width="180"
      />
      <el-table-column
        align="center"
        :label="__('开票金额')"
        prop="invoice_amount"
        show-overflow-tooltip
        width="100"
      />
      <el-table-column
        align="center"
        :label="__('纳税人识别号')"
        prop="invoice_company_code"
        show-overflow-tooltip
        width="180"
      />
      <el-table-column
        align="center"
        :formatter="formatType"
        :label="__('发票类型')"
        prop="invoice_type"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :formatter="formatKind"
        :label="__('发票类型')"
        prop="invoice_kind"
        show-overflow-tooltip
      />

      <el-table-column
        align="center"
        :formatter="formatTime"
        :label="__('申请时间')"
        prop="invoice_datetime"
        show-overflow-tooltip
        width="180"
      />
      <el-table-column
        align="center"
        :label="__('单位地址')"
        prop="invoice_address"
        show-overflow-tooltip
        width="180"
      />
      <el-table-column
        align="center"
        :label="__('单位电话')"
        prop="invoice_phone"
        show-overflow-tooltip
        width="180"
      />
      <el-table-column
        align="center"
        :label="__('开户银行')"
        prop="invoice_bankname"
        show-overflow-tooltip
        width="180"
      />
      <el-table-column
        align="center"
        :label="__('银行账号')"
        prop="invoice_bankaccount"
        show-overflow-tooltip
        width="180"
      />
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.pageNum"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
    <excelExe-import ref="excelExeImport" @fetch-data="fetchData" />
  </div>
</template>

<script>
  import { translateTitle as __ } from '@/utils/i18n'
  import { getList, impPurchaseInvoiceTemp } from '@/api/order/purchaseInvoice'
  import excelExeImport from './components/ExcelExeImport'
  import { renderTime } from '@/utils/date'

  export default {
    name: 'PurchaseInvoice',
    components: { excelExeImport },
    data() {
      return {
        tableConfig: {
          height: window.innerHeight - 220 - 90,
        },
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        queryForm: {
          pageNum: 1,
          pageSize: 20,
          title: '',
        },
        lecturerList: [],
        cycleList: [],
        classes_lecturer_id: '',
        invoiceType: [
          {
            value: 'person',
            label: this.__('个人'),
          },
          {
            value: 'company',
            label: this.__('公司'),
          },
        ],
        invoiceKind: [
          {
            value: 1,
            label: this.__('纸质发票'),
          },
          {
            value: 2,
            label: this.__('电子发票'),
          },
        ],
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      __,
      setSelectRows(val) {
        this.selectRows = val
      },
      showImport() {
        this.$refs.excelExeImport.showEdit()
      },
      handleEdit(row) {
        if (row.course_contract_id) {
          this.$refs['edit'].showEdit(row)
        } else {
          this.$refs['edit'].showEdit()
        }
      },
      formatType(row) {
        const invoiceType = row.invoice_type
        const obj = this.invoiceType.find((item) => item.value === invoiceType)
        return obj ? obj.label : ''
      },
      formatKind(row) {
        const invoiceKind = parseInt(row.invoice_kind)
        const obj = this.invoiceKind.find((item) => item.value === invoiceKind)
        return obj ? obj.label : ''
      },
      formatTime(row) {
        const invoice_datetime = row.invoice_datetime
        return invoice_datetime ? renderTime(invoice_datetime) : ''
      },
      async handleImport() {
        await impPurchaseInvoiceTemp()
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNum = val
        this.fetchData()
      },
      queryData() {
        this.queryForm.pageNum = 1
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        const { data } = await getList(this.queryForm)
        this.list = data.items
        this.total = data.records
        this.listLoading = false
      },
    },
  }
</script>
