<template>
  <div class="article-lists" v-perms="['goods:list:query']">
    <el-card class="!border-none" shadow="never">
      <el-form ref="formRef" class="mb-[-16px]" :model="queryParams" :inline="true">
        <el-form-item label="商品名称">
          <el-input
              class="w-[280px]"
              v-model="queryParams.title"
              clearable
              @keyup.enter="resetPage"
          />
        </el-form-item>
        <el-form-item label="商品分类">
          <el-select class="w-[280px]" v-model="queryParams.categoryId">
            <el-option label="全部" value/>
            <el-option
                v-for="item in optionsData.goodsCategory"
                :key="item.id"
                :label="item.name"
                :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="商品状态">
          <el-select class="w-[280px]" v-model="queryParams.auditStatus">
            <el-option label="全部" value/>
            <el-option label="待审核" :value="0"/>
            <el-option label="已通过" :value="1"/>
            <el-option label="已驳回" :value="2"/>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="resetPage">查询</el-button>
          <el-button @click="resetParams">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card class="!border-none mt-4" shadow="never">
      <div>
        <router-link v-perms="['goods:list:add', 'goods:list:add/edit']" :to="{ path: getRoutePath('goods:list:add/edit') }">
          <el-button class="mr-3" type="primary">
            <template #icon>
              <icon name="el-icon-Plus"/>
            </template>
            添加商品
          </el-button>
        </router-link>
        <!-- <multi-del-btn></multi-del-btn> -->
      </div>
      <el-table class="mt-4" size="large" v-loading="pager.loading" :data="pager.lists">
        <el-table-column label="ID" type="index" min-width="80"/>
        <el-table-column label="图片" min-width="100">
          <template #default="{ row }">
            <image-contain
                v-if="row.image"
                :src="row.image"
                :width="60"
                :height="45"
                :preview-src-list="[row.image]"
                preview-teleported
                fit="contain"
            />
          </template>
        </el-table-column>
        <el-table-column
            label="商品名称"
            prop="title"
            min-width="160"
            show-tooltip-when-overflow
        />
        <el-table-column label="商品分类" prop="categoryName" min-width="150" show-tooltip-when-overflow/>
        <el-table-column label="售价/元" prop="price" min-width="70"/>
        <el-table-column
            label="审核状态"
            prop="auditStatus"
            min-width="100"
            show-tooltip-when-overflow
        >
          <template #default="{ row }">
            <span v-if="row.auditStatus === 0" class="text-yellow-500">待审核</span>
            <span v-else-if="row.auditStatus === 1" class="text-green-500">已通过</span>
            <span v-else-if="row.auditStatus === 2" class="text-red-500">已驳回</span>
          </template>
        </el-table-column>
        <el-table-column
            label="库存"
            prop="stock"
            min-width="100"
            show-tooltip-when-overflow
        />
        <el-table-column label="排序" prop="sort" min-width="80"/>
        <el-table-column label="添加时间" prop="createTime" min-width="120"/>
        <el-table-column label="操作" width="120" fixed="right">
          <template #default="{ row }">
            <el-button v-perms="['goods:list:edit', 'goods:list:add/edit']" type="primary" link>
              <router-link :to="{ path: getRoutePath('goods:list:add/edit'), query: { id: row.id } }">
                编辑
              </router-link>
            </el-button>
            <el-button
                v-perms="['goods:list: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>
  </div>
</template>
<script lang="ts" setup name="goodsPage">
import {goodsPage} from '@/api/goods/list'
import {goodsCategoryPage} from '@/api/goods/category'
import {useDictOptions} from '@/hooks/useDictOptions'
import {usePaging} from '@/hooks/usePaging'
import {getRoutePath} from '@/router'
import { getShopId } from '@/utils/auth'
import feedback from '@/utils/feedback'

const queryParams = reactive({
  title: '',
  cid: '',
  isShow: '',
  shopId: getShopId()
})
const {pager, getLists, resetPage, resetParams} = usePaging({
  fetchFun: goodsPage,
  params: queryParams
})

const {optionsData} = useDictOptions<{
  goodsCategory: any[]
}>({
  goodsCategory: {
    api: goodsCategoryPage,
    params: {pageNo: 1, pageSize: 100},
    transformData(data: any) {
      return data.lists
    }
  }
})

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

onActivated(() => {
  getLists()
})

getLists()
</script>
<style lang="scss" scoped>
.remark-text {
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
  overflow: hidden;
  text-overflow: ellipsis;
  word-break: break-all;
  line-height: 1.5;
  max-height: 3em; // 2行的高度
}

.text-yellow-500 {
  color: #f59e0b;
}

.text-red-500 {
  color: #ef4444;
}

.text-green-500 {
  color: #10b981;
}
</style>
