<!-- 导航栏分类 -->
<template>
    <div class="table-box">
        <div style="padding-bottom: 16px">
            <el-button type="primary" @click="handleAdd"> 添加分类 </el-button>
        </div>
        <ProTable
            ref="proTableRef"
            :formData="dataStore.formData"
            :columns="dataStore.columns"
            :request-api="getNavClassListIndexApi"
            :init-param="dataStore.initParam"
        >
            <template #status="scope">
                <el-tag type="success" effect="dark">{{ scope.row.status }}</el-tag>
            </template>
            <template #operation="scope">
                <el-button size="small" type="primary" @click="getNavClassListRead(scope.row.id)">编辑</el-button>
                <el-button size="small" type="danger" @click="getNavClassListDel(scope.row.id)">删除</el-button>
            </template>
        </ProTable>
        <el-drawer
            v-model="dataStore.visible"
            :show-close="true"
            :size="600"
            :close-on-click-modal="false"
            :close-on-press-escape="false"
            :before-close="handleBeforeClone"
            destroy-on-close
        >
            <template #header="{ titleId, titleClass }">
                <h4 :id="titleId" :class="titleClass">{{ dataStore.title }}</h4>
            </template>
            <div>
                <rulesForm
                    :ruleForm="dataStore.editRuleForm"
                    :formData="dataStore.editFormData"
                    :rules="dataStore.rules"
                    ref="formRef"
                >
                </rulesForm>
            </div>
            <template #footer>
                <div style="flex: auto">
                    <el-button @click="handleResetClick">重置</el-button>
                    <el-button type="primary" @click="handleConfirmClick">确认</el-button>
                </div>
            </template>
        </el-drawer>
    </div>
</template>

<script setup lang="ts" name="webNavClassListIndex">
import rulesForm from "@/components/rulesForm/index.vue";
import ProTable from "@/components/ProTable/index.vue";
//列表接口
import {
    getNavClassListIndexApi,
    getNavClassListReadApi,
    getNavClassListEditUpApi,
    getNavClassListSaveApi,
    getNavClassListDelApi
} from "@/api/modules/navClass";
//深拷贝方法
import { cloneDeep } from "lodash-es";
//表格和搜索條件
import { RULE_FORM, FORM_DATA, COLUMNS, EDIT_FORM_DATA, EDIT_RULE_FORM, RULES } from "./constant/index";
import { messageBox } from "@/utils/messageBox";
import { useMsg } from "@/hooks/useMsg";
// 获取 ProTable 元素，调用其获取刷新数据方法（还能获取到当前查询参数，方便导出携带参数）
const proTableRef = ref<any>(null);
const formRef: any = ref(null);
// 数据源
const dataStore = reactive<any>({
    title: "添加导航栏分类",
    columns: COLUMNS, //列表配置项
    rules: cloneDeep(RULES), //抽屉表单验证
    editRuleForm: cloneDeep(EDIT_RULE_FORM),
    editFormData: cloneDeep(EDIT_FORM_DATA), //抽屉表单配置项
    initParam: cloneDeep(RULE_FORM), // 初始化搜索条件|重置搜索条件
    ruleForm: cloneDeep(RULE_FORM), // 搜索參數
    formData: FORM_DATA, //搜索配置项
    visible: false,
    selectRow: {} //当前选择的row
});

//抽屉确认
const handleConfirmClick = () => {
    if (!formRef.value!.ruleFormRef) return;
    formRef!.value!.ruleFormRef!.validate((valid: any) => {
        if (valid) {
            dataStore.title === "编辑导航栏分类" ? getNavClassListEditUp() : getNavClassListSave();
        } else {
            console.log("error submit!");
            return false;
        }
    });
};

//删除
const getNavClassListDel = async (id: any) => {
    messageBox("你确定要删除?", async () => {
        const result = await getNavClassListDelApi(id);
        if (result?.code === 0) {
            const { msg } = result;
            useMsg("success", msg);
            proTableRef?.value?.getTableList();
        }
    });
};

//添加
const getNavClassListSave = async () => {
    const result = await getNavClassListSaveApi(dataStore.editRuleForm);
    if (result?.code === 0) {
        useMsg("success", result?.msg);
        dataStore.visible = false;
        resetForm();
        proTableRef?.value?.getTableList();
    }
};
//更新
const getNavClassListEditUp = async () => {
    const result = await getNavClassListEditUpApi(dataStore.editRuleForm);
    if (result?.code === 0) {
        useMsg("success", result?.msg);
        dataStore.visible = false;
        resetForm();
        proTableRef?.value?.getTableList();
    }
};
//详情
const getNavClassListRead = async (id: any) => {
    dataStore.visible = true;
    dataStore.title = "编辑导航栏分类";
    const result = await getNavClassListReadApi(id);
    if (result?.code === 0) {
        dataStore.editRuleForm = result?.data;
    }
};

//重置验证状态
const resetFields = () => {
    if (!formRef.value!.ruleFormRef) return;
    formRef!.value!.ruleFormRef.resetFields();
};
//抽屉重置
const handleResetClick = () => {
    if (dataStore.title === "添加导航栏分类") {
        resetFields();
    } else {
        getNavClassListRead(dataStore.editRuleForm.id);
    }
};
//添加
const handleAdd = () => {
    dataStore.title = "添加导航栏分类";
    dataStore.visible = true;
};
const resetForm = () => {
    dataStore.editRuleForm = cloneDeep(EDIT_RULE_FORM);
};
//抽屉关闭前的钩子
const handleBeforeClone = () => {
    resetFields();
    resetForm();
    dataStore.visible = false;
};
</script>

<style scoped></style>
