<!-- 产品列表 -->
<template>
    <div class="table-box">
        <div style="padding-bottom: 16px">
            <el-button type="primary" @click="handleExport"> 导出 </el-button>
        </div>
        <ProTable
            ref="proTableRef"
            :formData="dataStore.formData"
            :columns="dataStore.columns"
            :request-api="getProductListApi"
            :init-param="dataStore.initParam"
        >
            <template #cover_image="scope">
                <el-image :src="scope.row.cover_image ? h + scope.row.cover_image : ''" style="width: 60px; height: 60px" />
            </template>
            <template #status="scope">
                <el-tag effect="dark" :type="scope.row.status == 1 ? 'success' : 'danger'">{{
                    scope.row.status == 1 ? "启用" : "禁用"
                }}</el-tag>
            </template>
            <template #is_show="scope">
                <el-tag :type="scope.row.is_show == 1 ? 'success' : 'danger'" effect="dark">{{
                    scope.row.is_show == 1 ? "已上架" : "未上架"
                }}</el-tag>
            </template>
            <!-- sort -->
            <template #sort="scope">
                <el-input v-model="scope.row.sort" @blur="handleBlur(scope.row)" @input="handleInput(scope.row)"></el-input>
            </template>
            <template #operation="scope">
                <el-button
                    size="small"
                    :type="scope.row.is_show == 1 ? 'danger' : 'primary'"
                    @click="handleBtnClick('上下架', scope.row)"
                    >{{ scope.row.is_show == 1 ? "下架" : "上架" }}</el-button
                >
                <el-button size="small" type="info" style="cursor: not-allowed" @click="handleBtnClick('添加SKU', scope.row)"
                    >添加SKU</el-button
                >
                <el-button size="small" type="primary" @click="handleBtnClick('编辑', scope.row)">编辑</el-button>
                <el-button size="small" type="info" style="cursor: not-allowed" @click="handleBtnClick('删除', scope.row)"
                    >删除</el-button
                >
            </template>
        </ProTable>
    </div>
</template>

<script setup lang="ts" name="productListIndex">
import ProTable from "@/components/ProTable/index.vue";
import { integerRexg } from "@/utils/regexp/index";
import { useMsg } from "@/hooks/useMsg";
import { useExport } from "@/hooks/useExport";
import { h } from "@/utils/url";
//列表接口
//getProductDelApi
import {
    getProductListApi,
    getProductUpOrShelvesApi,
    getProductListSortApi,
    getProductListExportApi,
    getProductCategoryListApi
} from "@/api/modules/productList";
// import { getProductCategoryListApi } from "@/api/modules/productClass";
import { addLabelValue } from "@/utils/addLabelValue";

//深拷贝方法
import { cloneDeep } from "lodash-es";
//表格和搜索條件
import { RULE_FORM, FORM_DATA, COLUMNS } from "./constant/index";
// 获取 ProTable 元素，调用其获取刷新数据方法（还能获取到当前查询参数，方便导出携带参数）
const proTableRef = ref<any>(null);
const $router = useRouter();
// 数据源
const dataStore = reactive<any>({
    columns: COLUMNS, //列表配置项
    initParam: cloneDeep(RULE_FORM), // 初始化搜索条件|重置搜索条件
    ruleForm: cloneDeep(RULE_FORM), // 搜索參數
    formData: FORM_DATA //搜索配置项
});

//导出
const handleExport = () => {
    getProductListExport();
};
//导出接口
const getProductListExport = async () => {
    const result = await getProductListExportApi({
        ...proTableRef?.value?.searchParam,
        ...proTableRef?.value?.pageable
    });
    await useExport(result);
};
//上下架
const getProductUpOrShelves = async (id: any) => {
    const result = await getProductUpOrShelvesApi(id);
    if (result?.code === 0) {
        useMsg("success", result?.msg);
        proTableRef?.value?.getTableList();
    }
};

//排序
const getProductListSort = async (row: any) => {
    const result = await getProductListSortApi({ id: row.id, sort: row.sort });

    if (result?.code === 0) {
        useMsg("success", result?.msg);
        proTableRef?.value?.getTableList();
    }
};

//产品分类(后端大佬说直接掉列表接口)
const getProductCategoryList = async () => {
    const result = await getProductCategoryListApi();
    if (result?.code === 0) {
        let dataClone: any = cloneDeep(result?.data);
        dataStore.formData[2].options = addLabelValue(dataClone);
        console.log(result?.data);
    }
};
getProductCategoryList();
//排序input框失焦
const handleBlur = (row: any) => {
    getProductListSort(row);
};
//
const handleInput = (row: any) => {
    row.sort = integerRexg(row.sort);
};

//按钮点击事件
const handleBtnClick = (type: any, row: any) => {
    //添加SUK和删除暂时无法操作
    if (type == "添加SUK" || type === "删除") {
        return false;
    }
    if (type === "上下架") {
        getProductUpOrShelves(row.id);
    }

    if (type === "编辑") {
        $router.push({
            path: "/admin/productManagement/list/edit",
            query: {
                id: row.id
            }
        });
        return;
    }
};
</script>

<style scoped></style>
