<!-- 回收站 -->
<template>
    <div class="table-box">
        <ProTable
            ref="proTableRef"
            :formData="dataStore.formData"
            :columns="dataStore.columns"
            :request-api="getProductTrashListApi"
            :init-param="dataStore.initParam"
        >
            <template #image="scope">
                <el-image :src="scope.row.image ? h + scope.row.image : ''" style="width: 60px; height: 60px" />
            </template>
            <template #status="scope">
                <el-tag type="danger" effect="dark"> {{ scope.row.status === 1 ? "删除" : "删除" }}</el-tag>
            </template>

            <template #operation="scope">
                <el-button size="small" type="danger" @click="handleBtnClick('删除', scope.row)">删除</el-button>
                <el-button size="small" type="primary" @click="handleBtnClick('恢复', scope.row)">恢复</el-button>
            </template>
        </ProTable>
    </div>
</template>

<script setup lang="ts" name="productRecycleIndex">
import ProTable from "@/components/ProTable/index.vue";
import { messageBox } from "@/utils/messageBox";
import { h } from "@/utils/url";
//列表接口
import { getProductTrashListApi, getProductTrashDelApi, getProductTrashRestoreApi } from "@/api/modules/productRecycle";
//深拷贝方法
import { cloneDeep } from "lodash-es";
//表格和搜索條件
import { RULE_FORM, FORM_DATA, COLUMNS } from "./constant/index";
import { useMsg } from "@/hooks/useMsg";
import { getProductCategoryListApi } from "@/api/modules/productClass";
// 获取 ProTable 元素，调用其获取刷新数据方法（还能获取到当前查询参数，方便导出携带参数）
const proTableRef = ref<any>(null);

// 数据源
const dataStore = reactive<any>({
    columns: COLUMNS, //列表配置项
    initParam: cloneDeep(RULE_FORM), // 初始化搜索条件|重置搜索条件
    ruleForm: cloneDeep(RULE_FORM), // 搜索參數
    formData: FORM_DATA //搜索配置项
});

//删除
const getProductTrashDel = async (id: any) => {
    messageBox("你确定要删除?", async () => {
        const result = await getProductTrashDelApi(id);
        if (result?.code === 0) {
            useMsg("success", result?.msg);
            proTableRef?.value?.getTableList();
        }
    });
};
//恢复
const getProductTrashRestore = async (id: any) => {
    const result = await getProductTrashRestoreApi(id);
    if (result?.code === 0) {
        useMsg("success", result?.msg);
        proTableRef?.value?.getTableList();
    }
};
//按钮点击事件
const handleBtnClick = (type: any, row: any) => {
    //删除
    if (type === "删除") {
        getProductTrashDel(row.id);
    }
    if (type === "恢复") {
        getProductTrashRestore(row.id);
    }
};

const addLabelValue = (arr: any) => {
    return arr.map((item: any) => {
        // 为当前对象添加 label 和 value 属性
        const newItem = { ...item };
        newItem.label = newItem.name;
        newItem.value = newItem.id;

        // 如果有子对象，递归调用 addLabelValue 处理子对象
        if (newItem.children && Array.isArray(newItem.children)) {
            newItem.children = addLabelValue(newItem.children);
        }
        return newItem;
    });
};
//产品分类(后端大佬说直接掉列表接口)
const getProductCategoryList = async () => {
    const result = await getProductCategoryListApi({ page: 1, size: 500 });
    if (result?.code === 0) {
        // let data:any = []
        let dataClone: any = cloneDeep(result?.data);
        dataStore.formData[2].options = addLabelValue(dataClone);
        console.log(result?.data);
    }
};
getProductCategoryList();
</script>

<style scoped></style>
