<template>
  <div class="department-management-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.spec_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="120">
        <template #default="{ row }">
          <el-button type="text" @click="handleEdit(row)">
            {{ __('编辑') }}
          </el-button>
          <el-button
            :disabled="row.spec_buildin === 1"
            type="text"
            @click="handleDelete(row)"
          >
            {{ __('删除') }}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="__('规格类别编号')"
        prop="spec_id"
        show-overflow-tooltip
      />
      <el-table-column
        v-if="isAdmin"
        align="center"
        label="店铺名称"
        prop="store_name"
        width="200"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('规格类别名称')"
        prop="spec_name"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('显示类型')"
        prop="spec_format"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('排序')"
        prop="spec_order"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('规格分类编号')"
        prop="spec_category_id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :formatter="formatBuildin"
        :label="__('系统内置')"
        prop="spec_buildin"
        show-overflow-tooltip
      />
      <template #empty>
        <el-image
          class="vab-data-empty"
          :src="require('@/assets/empty_images/data_empty.png')"
        />
      </template>
    </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>
</template>

<script>
  import { doDelete, getList } from '@/api/base/product/spec'
  import Edit from './components/specManagementEdit'
  import { translateTitle as __ } from '@/utils/i18n'
  import { mapState } from "vuex";
  export default {
    name: 'SpecManagement',
    components: { Edit },
    data() {
      return {
        tableConfig: {
          height: window.innerHeight - 220 - 90,
        },
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        queryForm: {
          pageNum: 1,
          pageSize: 20,
          spec_name: '',
        },
      }
    },
    computed:{
      ...mapState('user',['isAdmin'])
    },
    created() {
      this.fetchData()
    },
    methods: {
      __,
      formatBuildin(row) {
        return row.spec_buildin === 1 ? this.__('是') : this.__('否')
      },

      handleEdit(row) {
        if (row.spec_id) {
          this.$refs['edit'].showEdit(row)
        } else {
          this.$refs['edit'].showEdit()
        }
      },
      handleDelete(row) {
        if (row.spec_id) {
          this.$baseConfirm(this.__('你确定要删除当前项吗'), null, async () => {
            const { msg, status } = await doDelete({ spec_ids: row.spec_id })
            if (200 == status) {
              this.$baseMessage(msg, 'success')
            } else {
              this.$baseMessage(msg, 'error')
            }

            await this.fetchData()
          })
        } else {
          if (this.selectRows.length > 0) {
            const spec_ids = this.selectRows.map((item) => item.spec_id).join()
            this.$baseConfirm(
              this.__('你确定要删除选中项吗'),
              null,
              async () => {
                const { msg, status } = await doDelete({ spec_ids })

                if (200 == status) {
                  this.$baseMessage(msg, 'success')
                } else {
                  this.$baseMessage(msg, 'error')
                }

                await this.fetchData()
              }
            )
          } else {
            this.$baseMessage(__('未选中任何行'), 'error')
            return false
          }
        }
      },
      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)
        if(data && data.items.length > 0){
          this.list = data.items
          this.total = data.records
        }
        
        this.listLoading = false
      },
    },
  }
</script>
