<template>
  <el-dialog
    append-to-body
    custom-class="center-dialog"
    :title="title"
    top="0vh"
    :visible.sync="dialogFormVisible"
    width="1000px"
    @close="close"
  >
    <vab-query-form>
      <vab-query-form-left-panel :span="24">
        <el-button
          icon="el-icon-plus"
          type="primary"
          @click="handleEdit(queryForm.activity_id)"
        >
          {{ __('新增') }}
        </el-button>
      </vab-query-form-left-panel>
    </vab-query-form>
    <el-table
      v-loading="listLoading"
      border
      :data="list"
      height="500"
      width="60%"
    >
      <el-table-column align="center" :label="__('操作')" width="125">
        <template #default="{ row }">
          <el-button type="text" @click="handleDelete(row)">
            {{ __('删除') }}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="__('商品编号')"
        prop="item_id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('商品名称')"
        prop="product_item_name"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('价格')"
        prop="item_unit_price"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('商品图片')"
        prop="product_image"
        show-overflow-tooltip
        width="150px"
      >
        <template slot-scope="scope">
          <el-image
            v-show="scope.row.product_image"
            :preview-src-list="[scope.row.product_image]"
            :src="scope.row.product_image"
            style="width: 35px; height: 35px"
          />
        </template>
      </el-table-column>
    </el-table>
    <TableEdit ref="TableEdit" @fetch-data="refresh" />
  </el-dialog>
</template>
<script>
  import { translateTitle as __ } from '@/utils/i18n'

  import { listItem } from '@/api/store/activity/base'
  import { removeItem } from '@/api/store/activity/base'
  import TableEdit from '@/components/activity/ProductItemTable'

  export default {
    name: 'StoreActivityBuyItemTable',
    components: { TableEdit },
    data() {
      return {
        list: [],
        form: {},
        listLoading: true,
        dialogFormVisible: false,
        title: '',
        layout: 'total, sizes, prev, pager, next, jumper',
        queryForm: {
          pageNum: 1,
          pageSize: 20,
        },
      }
    },
    created() {},
    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 removeItem({
            activity_id: this.queryForm.activity_id,
            item_id: row.item_id,
          })
          if (200 == status) {
            this.$baseMessage(msg, 'success')
          } else {
            this.$baseMessage(msg, 'error')
          }
          await this.refresh()
        })
      },
      async refresh() {
        this.fetchData()
        this.$emit('fetch-data')
      },
      close() {
        this.dialogFormVisible = false
      },

      handleEdit(activity_id) {
        this.$refs['TableEdit'].showTable(activity_id)
      },

      async fetchData() {
        this.listLoading = true
        const { data } = await listItem(this.queryForm)
        this.list = data.items
        this.total = data.records
        this.listLoading = false
      },
    },
  }
</script>
