<template>
  <div class="card-type-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="4">
        <el-button icon="el-icon-plus" type="primary" @click="handleEdit">
          {{ __('添加') }}
        </el-button>
      </vab-query-form-left-panel>
      <vab-query-form-right-panel :span="20">
        <el-button icon="el-icon-refresh" type="primary" @click="queryData">
          {{ __('刷新') }}
        </el-button>
      </vab-query-form-right-panel>
    </vab-query-form>

    <el-table
      v-loading="listLoading"
      border
      :data="list"
      :height="tableConfig.height"
    >
      <el-table-column align="center" :label="__('操作')" width="180">
        <template #default="{ row }">
          <el-button type="text" @click="handleEdit(row)">
            {{ __('修改') }}
          </el-button>
          <el-button type="text" @click="handleAdd(row)">
            {{ __('添加') }}
          </el-button>
          <el-button type="text" @click="handleInfo(row)">
            {{ __('详情') }}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="__('充值卡名称')"
        prop="card_type_name"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :formatter="formatPrize"
        :label="__('充值卡内容')"
        prop="card_type_prize"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('类型描述')"
        prop="card_type_desc"
        show-overflow-tooltip
        width="180"
      />
      <el-table-column
        align="center"
        :formatter="formatStarttime"
        :label="__('有效开始时间')"
        prop="card_type_starttime"
        show-overflow-tooltip
        width="160"
      />
      <el-table-column
        align="center"
        :formatter="formatEndtime"
        :label="__('有效结束时间')"
        prop="card_type_endtime"
        show-overflow-tooltip
        width="160"
      />
      <el-table-column
        align="center"
        :label="__('卡片Logo')"
        prop="card_type_image"
        show-overflow-tooltip
        width="100"
      >
        <template slot-scope="scope">
          <el-popover placement="top-start" title="" trigger="hover">
            <img
              v-if="scope.row.card_type_image"
              alt=""
              :src="scope.row.card_type_image"
              style="width: 150px; height: 150px"
            />
            <img
              v-if="scope.row.card_type_image"
              slot="reference"
              :src="scope.row.card_type_image"
              style="width: 30px; height: 30px"
            />
          </el-popover>
        </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"
    />
    <edit ref="edit" @fetch-data="fetchData" />
    <Add ref="add" />
    <Info ref="info" />
  </div>
</template>

<script>
  import { translateTitle as __ } from '@/utils/i18n'
  import { getList } from '@/api/card/type'
  import { renderTime } from '@/utils/date'
  import Edit from './components/CardTypeEdit'
  import Add from './components/CardInfoAdd'
  import Info from './components/CardInfo'

  export default {
    name: 'CardType',
    components: { Edit, Add, Info },
    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: '',
          pay_type: 1,
        },
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      renderTime,
      formatEndtime(row) {
        const cardTypeEndtime = row.card_type_endtime
        if (!cardTypeEndtime) return ''
        return renderTime(cardTypeEndtime)
      },
      formatStarttime(row) {
        const cardTypeStarttime = row.card_type_starttime
        if (!cardTypeStarttime) return ''
        return renderTime(cardTypeStarttime)
      },
      formatPrize(row) {
        const cardTypePrize = row.card_type_prize
        if (!cardTypePrize) return ''
        const prize = JSON.parse(cardTypePrize)
        return '额度：' + prize.c + ' 积分：' + prize.p + ' 金额：' + prize.m
      },
      __,
      handleEdit(row) {
        if (row.card_type_id) {
          this.$refs['edit'].showEdit(row)
        } else {
          this.$refs['edit'].showEdit()
        }
      },
      handleAdd(row) {
        if (row.card_type_id) {
          this.$refs['add'].showEdit(row)
        }
      },
      handleInfo(row) {
        if (row.card_type_id) {
          this.$refs['info'].showEdit(row)
        }
      },
      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>
