<template>
  <div>
    <el-card class="!border-none mt-4" shadow="never" v-loading="pager.loading">
      <div>
        <el-button v-perms="['goods:category:add']" type="primary" @click="handleAdd()">
          <template #icon>
            <icon name="el-icon-Plus"/>
          </template>
          新增
        </el-button>
      </div>
      <el-table class="mt-4" size="large" :data="pager.lists">
        <el-table-column type="selection" width="55"/>
        <el-table-column
            label="分类名称"
            prop="name"
            min-width="120"
            show-tooltip-when-overflow
        />
        <el-table-column label="商品数" prop="number" min-width="120"/>
<!--        <el-table-column label="状态" min-width="120">-->
<!--          <template #default="{ row }">-->
<!--            <el-switch-->
<!--                v-perms="['article:cate:change']"-->
<!--                v-model="row.isShow"-->
<!--                :active-value="1"-->
<!--                :inactive-value="0"-->
<!--                @change="changeStatus(row.id, row.isShow)"-->
<!--            />-->
<!--          </template>-->
<!--        </el-table-column>-->
        <el-table-column label="排序" prop="sort" min-width="120"/>
        <el-table-column label="操作" width="120" fixed="right">
          <template #default="{ row }">
            <el-button
                v-perms="['goods:category:edit']"
                type="primary"
                link
                @click="handleEdit(row)"
            >
              编辑
            </el-button>
            <el-button
                v-perms="['goods:category:delete']"
                type="danger"
                link
                @click="handleDelete(row.id)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="flex justify-end mt-4">
        <pagination v-model="pager" @change="getLists"/>
      </div>
    </el-card>
    <edit-popup v-if="showEdit" ref="editRef" @success="getLists" @close="showEdit = false"/>
  </div>
</template>
<script lang="ts" setup name="articleColumn">
import {usePaging} from '@/hooks/usePaging'
import feedback from '@/utils/feedback'
import EditPopup from './edit.vue'
import {goodsCategoryDelete, goodsCategoryEdit, goodsCategoryPage} from "@/api/goods/category";

const editRef = shallowRef<InstanceType<typeof EditPopup>>()
const showEdit = ref(false)

const {pager, getLists} = usePaging({
  fetchFun: goodsCategoryPage
})
const handleAdd = async () => {
  showEdit.value = true
  await nextTick()
  editRef.value?.open('add')
}

const handleEdit = async (data: any) => {
  showEdit.value = true
  await nextTick()
  editRef.value?.open('edit')
  editRef.value?.setFormData(data)
}

const handleDelete = async (id: number) => {
  await feedback.confirm('确定要删除？')
  await goodsCategoryDelete({id, isDelete: 1})
  feedback.msgSuccess('删除成功')
  getLists()
}

const changeStatus = async (id: number, isShow: number) => {
  try {
    await goodsCategoryEdit({id, isShow})
    feedback.msgSuccess('修改成功')
    getLists()
  } catch (error) {
    getLists()
  }
}

getLists()
</script>
