<template>
  <el-dialog
    append-to-body
    custom-class="center-dialog"
    :title="title"
    top="0vh"
    :visible.sync="dialogFormVisible"
    width="1200px"
    @close="close"
  >
    <vab-query-form>
      <vab-query-form-left-panel :span="4">
        <el-button icon="el-icon-check" type="primary" @click="exportCsv(queryForm.activity_id)">
          {{ __('导出兑换码') }}
        </el-button>
      </vab-query-form-left-panel>
      <vab-query-form-right-panel :span="20">
        <el-button
          icon="el-icon-plus"
          type="primary"
          @click="handleEdit(queryForm.activity_id)"
        >
          {{ __('新增兑换码') }}
        </el-button>
      </vab-query-form-right-panel>
    </vab-query-form>
    <el-table
      v-loading="listLoading"
      border
      :data="list"
      height="400"
      width="60%"
    >
      <el-table-column align="center" :label="__('操作')" width="125">
        <template #default="{ row }">
          <el-button :disabled="row.user_id" type="text" @click="handleDelete(row)">
            {{ __('删除') }}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="__('兑换码')"
        prop="exchange_code"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('用户编号')"
        prop="user_id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('用户账号')"
        prop="user_account"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('生成时间')"
        prop="exchange_time"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('绑定时间')"
        prop="exchange_bind_time"
        show-overflow-tooltip
        width="150px"
      />
      <el-table-column
        v-if="false"
        align="center"
        :label="__('活动编号')"
        prop="activity_id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('订单编号')"
        prop="order_id"
        show-overflow-tooltip
        width="150px"
      />
      <el-table-column
        v-if="false"
        align="center"
        :label="__('店铺编号')"
        prop="store_id"
        show-overflow-tooltip
      />
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.pageNum"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
    <CodeEdit ref="CodeEdit" @fetch-data="fetchData" />
  </el-dialog>
</template>
<script>
  import { translateTitle as __ } from '@/utils/i18n'
  import { getCodeList, doCodeDelete, exportFile } from '@/api/store/activity/base'
  import CodeEdit from './StoreActivityCodeEdit'

  export default {
    name: 'StoreActivityCode',
    components: { CodeEdit },
    data() {
      return {
        list: [],
        form: {},
        listLoading: true,
        dialogFormVisible: false,
        title: '',
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        queryForm: {
          activity_id: '',
          pageNum: 1,
          pageSize: 20,
        },
      }
    },
    created() {
      this.title = this.__('兑换码列表')
    },
    methods: {
      __,
      showTable(row) {
        this.queryForm.activity_id = row.activity_id
        this.dialogFormVisible = true
        this.fetchData()
      },
      handleDelete(row) {
        this.$baseConfirm(
          this.__('你确定要删除当前兑换码吗'),
          null,
          async () => {
            const { msg, status } = await doCodeDelete({
              exchange_id: row.exchange_id,
            })
            if (200 == status) {
              this.$baseMessage(msg, 'success')
            } else {
              this.$baseMessage(msg, 'error')
            }
            await this.fetchData()
          }
        )
      },
      close() {
        this.queryForm = this.$options.data().queryForm
        this.dialogFormVisible = false
      },
      handleEdit(activity_id) {
        this.$refs['CodeEdit'].showTable(activity_id)
      },
      exportCsv(activity_id) {
        exportFile({ activity_id: activity_id }).then(
          (data) => {
            const blob = new Blob([data])
            const downloadElement = document.createElement('a')
            const href = window.URL.createObjectURL(blob) //创建下载的链接
            downloadElement.href = href
            downloadElement.download = decodeURIComponent('兑换码.csv') //下载后文件名
            document.body.appendChild(downloadElement)
            downloadElement.click() //点击下载
            document.body.removeChild(downloadElement) //下载完成移除元素
            window.URL.revokeObjectURL(href) //释放掉blob对象
          }
        )
      },
      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 getCodeList(this.queryForm)
        this.list = data.items
        this.total = data.records
        this.listLoading = false
      },
    },
  }
</script>
