<template>
  <el-dialog
    append-to-body
    :title="title"
    :visible.sync="dialogFormVisible"
    width="1000px"
    @close="close"
  >
    <vab-query-form>
      <vab-query-form-right-panel :span="24">
        <el-button icon="el-icon-plus" type="primary" @click="showImport">
          {{ __('导入') }}
        </el-button>
      </vab-query-form-right-panel>
    </vab-query-form>
    <el-table
      v-loading="listLoading"
      border
      :data="list"
      height="500"
      width="60%"
    >
      <el-table-column align="center" :label="__('操作')" width="125">
        <template #default="{ row }">
          <el-button
            :disabled="row.card_is_use === 1"
            type="text"
            @click="handleDelete(row)"
          >
            {{ __('删除') }}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="__('券码名称')"
        prop="card_history_remark"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('真实券码')"
        prop="card_code"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('订单编号')"
        prop="order_id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('是否使用')"
        prop="card_is_use"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <el-row v-if="scope.row.card_is_use === 0">
            {{ __('未使用') }}
          </el-row>
          <el-row v-if="scope.row.card_is_use === 1">
            {{ __('已使用') }}
          </el-row>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.pageNum"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
    <payCardImport ref="payCardImport" @setPayCardImport="fetchData" />
  </el-dialog>
</template>
<script>
  import { translateTitle as __ } from '@/utils/i18n'
  import { getHistoryList, doHistoryDelete } from '@/api/card/type'
  import payCardImport from './payCardImport'

  export default {
    name: 'PayCardHistoryTable',
    components: { payCardImport },
    data() {
      return {
        list: [],
        form: {},
        total: 0,
        listLoading: true,
        dialogFormVisible: false,
        title: '',
        app_id: 0,
        layout: 'total, sizes, prev, pager, next, jumper',
        queryForm: {
          pageNum: 1,
          pageSize: 20,
          pay_type: 3,
          title: '',
        },
      }
    },
    created() {},
    methods: {
      __,
      showTable(row) {
        this.queryForm.card_type_id = row.card_type_id
        this.queryForm.app_id = row.app_id
        this.app_id = row.app_id
        this.dialogFormVisible = true
        this.fetchData()
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNum = val
        this.fetchData()
      },
      showImport() {
        this.$refs.payCardImport.showEdit()
      },
      queryData() {
        this.queryForm.pageNum = 1
        this.fetchData()
      },
      handleDelete(row) {
        this.$baseConfirm(this.__('你确定要删除当前项吗'), null, async () => {
          const { msg, status } = await doHistoryDelete({
            card_history_id: row.card_history_id,
            card_type_id: row.card_type_id,
          })
          if (200 == status) {
            this.$baseMessage(msg, 'success')
          } else {
            this.$baseMessage(msg, 'error')
          }
          await this.refresh()
        })
      },
      async refresh() {
        this.fetchData()
        this.$emit('fetch-data')
      },
      close() {
        this.dialogFormVisible = false
      },
      handleEdit(activity_id) {
        this.$refs['TableEdit'].showTable(activity_id)
      },
      async fetchData() {
        this.listLoading = true
        const { data } = await getHistoryList(this.queryForm)
        this.list = data.items
        this.total = data.records
        this.listLoading = false
      },
    },
  }
</script>
