<template>
  <div class="order-invoice-container">
    <vab-query-form>
      <vab-query-form-right-panel :span="24">
        <el-form :inline="true" :model="queryForm" @submit.native.prevent>
          <el-form-item>
            <el-radio-group v-model="queryForm.invoice_status" @change="queryData">
              <el-radio-button v-for="item in invoiceStatus" :key="item.value" :label="item.value" >{{item.label}}</el-radio-button>
            </el-radio-group>
          </el-form-item>
          <el-form-item>
            <el-input
              v-model.trim="queryForm.order_id"
              clearable
              :placeholder="__('请输入订单编号')"
            />
          </el-form-item>
          <el-form-item>
            <el-select
              v-model.trim="queryForm.user_id"
              clearable
              filterable
              :loading="loading"
              :placeholder="__('请输入用户昵称')"
              remote
              :remote-method="remoteMethod"
              reserve-keyword
            >
              <el-option
                v-for="item in accountOptions"
                :key="item.user_id"
                :label="item.user_nickname"
                :value="item.user_id"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" type="primary" @click="queryData">
              {{ __('查询') }}
            </el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-right-panel>
    </vab-query-form>

    <el-table
      v-loading="listLoading"
      border
      :data="list"
      :height="tableConfig.height"
    >
      <el-table-column
        align="center"
        fixed="left"
        :label="__('操作')"
        width="80"
      >
        <template #default="{ row }">
          <el-button type="text" @click="hadleEdit(row)">
            {{ __('开票') }}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        fixed="left"
        :label="__('订单发票编号')"
        prop="order_invoice_id"
        show-overflow-tooltip
        width="120"
      />
      <el-table-column
        align="center"
        :formatter="formatState"
        :label="__('开票状态')"
        prop="invoice_status"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('发票编号')"
        prop="invoice_number"
        show-overflow-tooltip
        width="100px"
      />
      <el-table-column
        align="center"
        :formatter="formatTime"
        :label="__('开票时间')"
        prop="invoice_date"
        show-overflow-tooltip
        width="160px"
      />
      <el-table-column
        align="center"
        :label="__('订单编号')"
        prop="order_id"
        show-overflow-tooltip
        width="180"
      />
      <el-table-column
        align="center"
        :label="__('所属用户')"
        prop="user_id"
        show-overflow-tooltip
        width="100"
      />
      <el-table-column
        align="center"
        :label="__('店铺编号')"
        prop="store_id"
        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
        width="100"
      />
      <el-table-column
        align="center"
        :formatter="formatKind"
        :label="__('发票类型')"
        prop="invoice_kind"
        show-overflow-tooltip
        width="100"
      />
      <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-column
        align="center"
        :formatter="formatPaid"
        :label="__('付款状态')"
        prop="order_is_paid"
        show-overflow-tooltip
        width="100"
      />
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.pageNo"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
    <edit ref="edit" @fetch-data="fetchData" />
  </div>
</template>

<script>
  import { translateTitle as __ } from '@/utils/i18n'
  import Edit from '@/views/order/invoice/components/addOrderSrEdit'

  import { getList, doEdit } from '@/api/order/invoice'
  import { renderTime } from '@/utils/date'
  import { getAccounts } from '@/api/user/base'

  export default {
    name: 'OrderInvoice',
    components: { Edit },
    data() {
      return {
        tableConfig: {
          height: window.innerHeight - 220 - 90,
        },
        loading: false,
        accountOptions: [],
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        queryForm: {
          pageNo: 1,
          pageSize: 20,
          invoice_status: null,
        },
        invoiceStatus: [
          {
            value: null,
            label: this.__('全部'),
          },
          {
            value: 0,
            label: this.__('未开票'),
          },
          {
            value: 1,
            label: this.__('已开票'),
          },
        ],
        invoiceType: [
          {
            value: 'person',
            label: this.__('个人'),
          },
          {
            value: 'company',
            label: this.__('公司'),
          },
        ],
        invoiceKind: [
          {
            value: 1,
            label: this.__('纸质发票'),
          },
          {
            value: 2,
            label: this.__('电子发票'),
          },
        ],
        paid: [
          {
            value: 3010,
            label: this.__('未付款'),
          },
          {
            value: 3011,
            label: this.__('付款待审核'),
          },
          {
            value: 3012,
            label: this.__('部分付款'),
          },
          {
            value: 3013,
            label: this.__('已付款'),
          },
        ],
      }
    },
    created() {
      let query = this.$route.query
      if (typeof query.invoice_status != "undefined" ) {
        this.queryForm.invoice_status = Number(query.invoice_status)
      }


      this.fetchData()
    },
    methods: {
      async remoteMethod(user_nickname) {
        if (user_nickname.trim() !== '') {
          this.loading = true
          const { data } = await getAccounts({ user_nickname })
          this.accountOptions = data
          this.loading = false
        } else {
          this.accountOptions = []
        }
      },
      formatPaid(row) {
        const order_is_paid = row.order_is_paid
        const obj = this.paid.find((item) => item.value === order_is_paid)
        return obj ? obj.label : ''
      },
      formatType(row) {
        const invoiceType = row.invoice_type
        const obj = this.invoiceType.find((item) => item.value === invoiceType)
        return obj ? obj.label : ''
      },
      formatKind(row) {
        const invoiceKind = 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) : ''
      },
      formatState(row) {
        const invoiceStatus = row.invoice_status
        const obj = this.invoiceStatus.find(
          (item) => item.value === invoiceStatus
        )
        return obj ? obj.label : ''
      },
      __,
      async sChange(row) {
        const { msg, status } = await doEdit({
          order_invoice_id: row.order_invoice_id,
          invoice_status: row.invoice_status,
        })

        if (200 == status) {
          this.$baseMessage(msg, 'success')
        } else {
          this.$baseMessage(msg, 'error')
        }
        await this.fetchData()
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNo = val
        this.fetchData()
      },
      queryData() {
        this.queryForm.pageNo = 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
      },
      hadleEdit(row) {
        this.$refs['edit'].showEdit(row)
      },
    },
  }
</script>
