<template>
  <el-dialog
    append-to-body
    custom-class="center-dialog"
    :title="title"
    top="0vh"
    :visible.sync="dialogFormVisible"
    width="800px"
    @close="close"
  >
    <vab-query-form>
      <vab-query-form-right-panel :span="24">
        <el-button
          icon="el-icon-plus"
          type="primary"
          @click="handleEdit(null, queryForm.activity_id)"
        >
          {{ __('添加') }}
        </el-button>
      </vab-query-form-right-panel>
    </vab-query-form>
    <el-table v-loading="listLoading" border :data="list" height="400">
      <el-table-column align="center" fixed :label="__('操作')" width="120">
        <template #default="{ row }">
          <el-button
            type="text"
            @click="handleEdit(row, queryForm.activity_id)"
          >
            {{ __('编辑') }}
          </el-button>
          <el-button
            type="text"
            @click="handleDelete(row, queryForm.activity_id)"
          >
            {{ __('删除') }}
          </el-button>
        </template>
      </el-table-column>

      <el-table-column
        align="center"
        :label="__('奖品类型')"
        prop="awards_type"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('奖品名称')"
        prop="awards_name"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('奖品数量')"
        prop="awards_quantity"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('奖品图片')"
        prop="awards_image"
        show-overflow-tooltip
        width="150px"
      >
        <template slot-scope="scope">
          <el-image
            v-show="scope.row.awards_image"
            :preview-src-list="[scope.row.awards_image]"
            :src="scope.row.awards_image"
            style="width: 35px; height: 35px"
          />
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="__('奖品权重')"
        prop="awards_probability"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :formatter="formatEnble"
        :label="__('是否有效')"
        prop="awards_enable"
        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"
    />
    <Edit ref="edit" @fetch-data="fetchData" />
  </el-dialog>
</template>

<script>
  import { translateTitle as __ } from '@/utils/i18n'

  import {
    removeLotteryItem,
    getListsLotteryItems,
  } from '@/api/store/activity/base'
  import Edit from './components/StoreActivityBaseEdit'
  import { URL } from '@/config'

  export default {
    name: 'SetAward',
    components: { Edit },
    data() {
      return {
        list: [],

        listLoading: true,
        form: {},
        action: URL.action,
        title: '',
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        data: {},
        queryForm: {
          pageNum: 1,
          pageSize: 20,
        },
        dialogFormVisible: false,
      }
    },
    created() {},

    mounted() {
      this.$forceUpdate()
    },
    methods: {
      __,
      handleEdit(row, activity_id) {
        if (row != null) {
          console.log(row.index)
          this.$refs['edit'].showEdit(row, activity_id)
        } else {
          this.$refs['edit'].showEdit(null, activity_id)
        }
      },
      // 获取规格数据
      formatEnble(row) {
        return row.awards_enable == 1 ? this.__('是') : this.__('否')
      },
      showTable(row) {
        ;(this.queryForm.activity_id = row.activity_id),
          (this.dialogFormVisible = true)
        this.fetchData()
      },
      handleDelete(row, activity_id) {
        this.$baseConfirm(this.__('你确定要删除当前项吗'), null, async () => {
          const { msg, status } = await removeLotteryItem({
            activity_id: activity_id,
            index: row.index,
          })
          if (200 == status) {
            this.$baseMessage(msg, 'success')
          } else {
            this.$baseMessage(msg, 'error')
          }
          await this.fetchData()
        })
      },

      close() {
        this.form = this.$options.data().form
        this.dialogFormVisible = false
      },
      async fetchData() {
        this.listLoading = true
        const { data } = await getListsLotteryItems(this.queryForm)
        this.list = data.items
        this.total = data.records
        this.listLoading = false
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNum = val
        this.fetchData()
      },
      change(id) {
        this.queryForm.category_id = id
      },
    },
  }
</script>
