<template>
  <div class="base-product-category-nav-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="8">
        <el-button icon="el-icon-plus" type="primary" @click="handleEdit">
          {{ __('添加') }}
        </el-button>
      </vab-query-form-left-panel>
      <vab-query-form-right-panel :span="16">
        <el-form :inline="true" :model="queryForm" @submit.native.prevent>
          <el-form-item>
            <el-input
              v-model.trim="queryForm.category_name"
              clearable
              :placeholder="__('请输入分类名称')"
              style="width: 200px"
            />
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" type="primary" @click="queryData">
              {{ __('查询') }}
            </el-button>
            <el-button icon="el-icon-refresh" type="primary" @click="queryData">
              {{ __('刷新') }}
            </el-button>
          </el-form-item>
        </el-form>
      </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="150">
        <template #default="{ row }">
          <el-button type="text" @click="handleEdit(row)">
            {{ __('编辑') }}
          </el-button>
          <el-button type="text" @click="handleDelete(row)">
            {{ __('删除') }}
          </el-button>
          <el-button type="text" @click="showSpecEdit(row)">
            {{ __('分类') }}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="__('分类名称')"
        prop="category_name"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('分类别名')"
        prop="category_alias"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :formatter="formatType"
        :label="__('模板分类')"
        prop="temp_type"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('分类图片')"
        prop="category_image"
        show-overflow-tooltip
        width="120"
      >
        <template slot-scope="scope">
          <el-image
            v-show="scope.row.category_image"
            :preview-src-list="[scope.row.category_image]"
            :src="scope.row.category_image"
            style="width: 35px; height: 35px"
          />
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="__('排序')"
        prop="category_order"
        show-overflow-tooltip
        width="100"
      />
    </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" />
    <SpecEdit ref="specEdit" @fetch-data="fetchData" />
  </div>
</template>

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

  import { doDelete, getList } from '@/api/base/product/category/nav'
  import Edit from './components/BaseProductCategoryNavEdit'
  import SpecEdit from './components/BaseProductCategorySpecEdit'

  export default {
    name: 'BaseProductCategoryNav',
    components: { Edit, SpecEdit },
    data() {
      return {
        tableConfig: {
          height: window.innerHeight - 220 - 90,
        },
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        tempTypes: [
          { value: 1, label: this.__('分类模板') },
          { value: 2, label: this.__('商品模板') },
        ],
        queryForm: {
          category_name: "",
          pageNum: 1,
          pageSize: 20,
        },
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      formatType(row) {
        let temp_type = row.temp_type
        const obj = this.tempTypes.find((item) => item.value === temp_type)
        return obj ? obj.label : ''
      },
      __,
      handleEdit(row) {
        if (row.category_id) {
          this.$refs['edit'].showEdit(row)
        } else {
          this.$refs['edit'].showEdit()
        }
      },
      showSpecEdit(row) {
        this.$refs['specEdit'].showEdit(row)
      },
      handleDelete(row) {
        if (row.category_id) {
          this.$baseConfirm(this.__('你确定要删除当前项吗'), null, async () => {
            const { msg, status } = await doDelete({
              category_id: row.category_id,
            })
            if (200 == status) {
              this.$baseMessage(msg, 'success')
            } else {
              this.$baseMessage(msg, 'error')
            }
            await this.fetchData()
          })
        }
      },
      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>
