<template>
  <el-dialog
    append-to-body
    custom-class="center-dialog"
    :title="title"
    top="0vh"
    :visible.sync="dialogFormVisible"
    width="1000px"
    @close="close"
  >
    <el-table
      v-loading="listLoading"
      border
      :data="list"
      height="500"
      width="60%"
    >
      <el-table-column
        align="center"
        :label="__('代金券编号')"
        prop="user_voucher_id"
        show-overflow-tooltip
        width="100"
      />
      <el-table-column
        align="center"
        :formatter="formatStateId"
        :label="__('代金券状态')"
        prop="voucher_state_id"
        show-overflow-tooltip
        width="100"
      />
      <el-table-column
        align="center"
        :formatter="formatType"
        :label="__('优惠券类型')"
        prop="voucher_type"
        show-overflow-tooltip
        width="100"
      />
      <el-table-column
        align="center"
        :formatter="formatVoucherTime"
        :label="__('代金券发放日期')"
        prop="user_voucher_time"
        show-overflow-tooltip
        width="160"
      />
      <el-table-column
        align="center"
        :label="__('所属用户')"
        prop="user_id"
        show-overflow-tooltip
        width="100"
      />
      <el-table-column
        align="center"
        :label="__('订单编号')"
        prop="order_id"
        show-overflow-tooltip
        width="160"
      />
      <el-table-column
        align="center"
        :formatter="formatActiveTime"
        :label="__('使用时间')"
        prop="user_voucher_activetime"
        show-overflow-tooltip
        width="160"
      />
      <el-table-column
        align="center"
        :formatter="formatStartDate"
        :label="__('生效日期')"
        prop="voucher_start_date"
        show-overflow-tooltip
        width="160"
      />
      <el-table-column
        align="center"
        :formatter="formatEndDate"
        :label="__('失效日期')"
        prop="voucher_end_date"
        show-overflow-tooltip
        width="160"
      />
      <el-table-column
        align="center"
        :label="__('线下核销码')"
        prop="writeoff_code"
        show-overflow-tooltip
        width="100"
      />
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.pageNum"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
  </el-dialog>
</template>
<script>
  import { translateTitle as __ } from '@/utils/i18n'
  import { getList } from '@/api/user/voucher/record'
  import {renderTime} from "@/utils/date";

  export default {
    name: 'VoucherRecordTable',
    data() {
      return {
        list: [],
        form: {},
        listLoading: true,
        dialogFormVisible: false,
        title: '',
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        queryForm: {
          pageNum: 1,
          pageSize: 20,
        },
      }
    },
    created() {},
    methods: {
      __,
      showTable(row) {
        this.queryForm.activity_id = row.activity_id
        this.dialogFormVisible = true
        this.title = this.__('优惠券领取记录')
        this.fetchData()
      },
      close() {
        this.dialogFormVisible = false
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNum = val
        this.fetchData()
      },
      formatStateId(row) {
        return row.voucher_state_id === 1501
          ? this.__('未用')
          : row.voucher_state_id === 1502
            ? this.__('已用')
            : row.voucher_state_id === 1503
              ? this.__('过期')
              : this.__('收回')
      },
      formatType(row) {
        return row.voucher_type === 0
          ? this.__('普通优惠券')
          : this.__('免拼券')
      },
      formatVoucherTime(row) {
        return renderTime(row.user_voucher_time)
      },
      formatActiveTime(row) {
        return renderTime(row.user_voucher_activetime)
      },
      formatEndDate(row) {
        return renderTime(row.voucher_end_date)
      },
      formatStartDate(row) {
        return renderTime(row.voucher_start_date)
      },
      async fetchData() {
        this.listLoading = true
        const { data } = await getList(this.queryForm)
        this.list = data.items
        this.total = data.records
        this.listLoading = false
      },
    },
  }
</script>
