<template>
  <el-dialog
    append-to-body
    :title="__('店铺经营类目')"
    :visible.sync="dialogTableVisible"
    @close="close"
  >
    <div class="store-base-container">
      <vab-query-form>
        <vab-query-form-left-panel :span="24">
          <el-button
            icon="el-icon-plus"
            type="primary"
            @click="handleEdit(null, queryForm.store_id)"
          >
            {{ __('添加') }}
          </el-button>
        </vab-query-form-left-panel>
      </vab-query-form>
      <el-table v-loading="listLoading" border :data="list" height="300">
        <el-table-column align="center" :label="__('操作')" width="120">
          <template #default="{ row }">
            <el-button type="text" @click="handleEdit(row, null)">
              {{ __('编辑') }}
            </el-button>
            <el-button type="text" @click="handleDelete(row)">
              {{ __('删除') }}
            </el-button>
          </template>
        </el-table-column>
        <el-table-column
          v-if="false"
          align="center"
          :label="__('是否启用')"
          prop="store_business_category_enable"
          show-overflow-tooltip
        />
        <el-table-column
          align="center"
          :label="__('经营类目')"
          prop="category_name"
          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" />
    </div>
  </el-dialog>
</template>

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

  import {
    doCategoryDelete,
    getListCategory,
  } from '@/api/store/business/category'
  import Edit from './SetCategoryEdit'

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

        listLoading: true,
        title: '',
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        queryForm: {
          pageNum: 1,
          pageSize: 20,
        },
        dialogTableVisible: false,
      }
    },
    created() {},

    mounted() {
      this.$forceUpdate()
    },
    methods: {
      __,

      // 获取规格数据

      showTable(row) {
        this.queryForm.store_id = row.store_id
        this.dialogTableVisible = true
        this.fetchData()
      },
      handleEdit(row, store_id) {
        if (row != null) {
          this.$refs['edit'].showEdit(row)
        } else {
          this.$refs['edit'].showEdit(null, store_id)
        }
      },
      close() {
        this.form = this.$options.data().form
        this.dialogFormVisible = false
      },
      handleDelete(row) {
        this.$baseConfirm(this.__('你确定要删除当前项吗'), null, async () => {
          const { msg, status } = await doCategoryDelete({
            store_business_category_id: row.store_business_category_id,
          })
          if (200 == status) {
            this.$baseMessage(msg, 'success')
          } else {
            this.$baseMessage(msg, 'error')
          }
          await this.fetchData()
        })
      },
      async fetchData() {
        this.listLoading = true
        const { data } = await getListCategory(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()
      },
    },
  }
</script>
