<template>
  <el-dialog
    custom-class="center-dialog"
    :title="title"
    top="0vh"
    :visible.sync="infoVisible"
    width="60%"
    @close="close"
  >
    <el-table v-loading="listLoading" border :data="list" height="500">
      <el-table-column
        align="center"
        :label="__('卡片激活码')"
        prop="card_code"
        show-overflow-tooltip
        width="150"
      />
      <el-table-column
        align="center"
        :label="__('卡片密码')"
        prop="card_password"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('卡片名')"
        prop="card_type_name"
        show-overflow-tooltip
        width="150"
      />
      <el-table-column
        align="center"
        :formatter="formatFetch"
        :label="__('使用时间')"
        prop="card_fetch_time"
        show-overflow-tooltip
        width="160"
      />
      <el-table-column
        align="center"
        :label="__('使用人账号')"
        prop="user_account"
        show-overflow-tooltip
        width="150"
      />
      <el-table-column
        align="center"
        :formatter="formatTime"
        :label="__('卡片生成时间')"
        prop="card_time"
        show-overflow-tooltip
        width="160"
      />
    </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 { getInfoList, getList } from '@/api/card/type'
  import { renderTime } from '@/utils/date'

  export default {
    name: 'CardInfo',
    data() {
      return {
        title: '',
        infoVisible: false,
        list: [],
        listLoading: false,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        queryForm: {
          pageNum: 1,
          pageSize: 20,
          card_type_id: '',
        },
      }
    },
    created() {},
    methods: {
      formatFetch(row) {
        const cardFetchTime = row.card_fetch_time
        if (!cardFetchTime) return ''
        return renderTime(cardFetchTime)
      },
      formatTime(row) {
        return renderTime(row.return_channel_time)
      },
      __,
      showEdit(row) {
        this.queryForm.card_type_id = row.card_type_id
        this.infoVisible = true
        this.fetchData()
      },
      close() {
        this.queryForm.card_type_id = ''
        this.infoVisible = false
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNum = val
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        const { data } = await getInfoList(this.queryForm)
        this.list = data.items
        this.total = data.records
        this.listLoading = false
      },
    },
  }
</script>
