<!-- 文章分类 -->
<template>
    <div class="table-box">
        <div style="padding-bottom: 16px">
            <el-button type="primary" @click="handleAdd"> 添加 </el-button>
        </div>
        <div class="card table-main">
            <SearchForm :search="search" :reset="reset" :formData="dataStore.formData" :search-param="dataStore.ruleForm" />
            <el-table :data="dataStore.tableData" style="width: 100%; margin-bottom: 20px; font-size: 14px" row-key="id" border>
                <el-table-column prop="id" label="id" />
                <el-table-column prop="name" label="分类名称" />
                <el-table-column prop="sort" label="分类排序">
                    <template #default="{ row }">
                        <div @click.stop="">
                            <el-input v-model="row.sort" @blur="handleBlur(row)" @input="handleInput(row)" />
                        </div>
                    </template>
                </el-table-column>
                <el-table-column prop="is_show" label="是否显示">
                    <template #default="{ row }">
                        {{ row.is_show == 1 ? "✔️" : "❌" }}
                    </template>
                </el-table-column>
                <el-table-column prop="icon" label="图片">
                    <template #default="{ row }">
                        <el-image :src="row.icon ? h + row.icon : ''" style="width: 60px; height: 60px" />
                    </template>
                </el-table-column>

                <!--   <template #default="scope"> -->
                <el-table-column label="操作" :width="240">
                    <template #default="scope">
                        <el-button
                            size="small"
                            v-for="(item, index) in dataStore.btns"
                            :key="index"
                            :type="item.btnType"
                            @click="handleBtnClick(item.type, scope.row)"
                            >{{ item.name }}</el-button
                        >
                    </template>
                </el-table-column>
            </el-table>
        </div>

        <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"
                    @handleSelectChangeEmits="handleSelectChangeEmits"
                />
            </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="articleClassListIndex">
import rulesForm from "@/components/rulesForm/index.vue";
import SearchForm from "@/components/SearchForm/index.vue";
import { integerRexg } from "@/utils/regexp/index";
import { addLabelValue } from "@/utils/addLabelValue";
import { h } from "@/utils/url";
// import ProTable from "@/components/ProTable/index.vue";
import { messageBox } from "@/utils/messageBox";
import { useMsg } from "@/hooks/useMsg";
//列表接口
import {
    getArticleClassListApi,
    getArticleClassAddSaveApi,
    getArticleClassDelApi,
    getArticleClassEditUpApi,
    getArticleClassDetailsApi,
    getArticleClassApi,
    getArticleClassSortApi
} from "@/api/modules/articleClass";
//深拷贝方法
import { cloneDeep } from "lodash-es";
//表格和搜索條件
import { RULE_FORM, FORM_DATA, COLUMNS, EDIT_FORM_DATA, EDIT_RULE_FORM, RULES, BTNS } from "./constant/index";

const formRef: any = ref(null);
const $router = useRouter();
// 数据源
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,
    btns: BTNS,
    tableData: [],
    selectRow: {} //当前选择的row
});
const getArticleClassSort = async (row: any) => {
    const result = await getArticleClassSortApi(row);
    if (result?.code === 0) {
        useMsg("success", result?.msg);
        getArticleClassList();
    }
};
//排序input框失焦
const handleBlur = (row: any) => {
    getArticleClassSort(row);
};
//
const handleInput = (row: any) => {
    row.sort = integerRexg(row.sort);
};

const search = () => {
    getArticleClassList();
};
const reset = () => {
    for (let key in dataStore.ruleForm) {
        dataStore.ruleForm[key] = "";
    }
    getArticleClassList();
};

const handleSelectChangeEmits = (params: any) => {
    const { id } = params;
    dataStore.editRuleForm.pid = id;
};

const getArticleClass = async () => {
    const result = await getArticleClassApi();
    if (result?.code === 0) {
        dataStore.editFormData[3].options = addLabelValue(result?.data);
        dataStore.editFormData[3].options.unshift({ value: 0, label: "无" });
        console.log(result?.data);
    }
};
getArticleClass();
const getArticleClassList = async () => {
    const result = await getArticleClassListApi(dataStore.ruleForm);
    if (result?.code === 0) {
        dataStore.tableData = result?.data;
    }
};
getArticleClassList();
//新增文章接口
const getArticleClassAddSave = async () => {
    const result = await getArticleClassAddSaveApi(dataStore.editRuleForm);
    const { msg, code } = result;
    if (code === 0) {
        useMsg("success", msg);
        dataStore.visible = false;
        dataStore.editRuleForm = cloneDeep(EDIT_RULE_FORM);
        getArticleClassList();
    }
};
//文章编辑
const getArticleClassUpEdit = async () => {
    const result = await getArticleClassEditUpApi(dataStore.editRuleForm);
    const { msg, code } = result;

    if (code === 0) {
        useMsg("success", msg);
        dataStore.visible = false;
        dataStore.editRuleForm = cloneDeep(EDIT_RULE_FORM);
        getArticleClassList();
    }
};
//删除文章
const getArticleClassDel = (row: any) => {
    messageBox("你确定要删除?", async () => {
        const result = await getArticleClassDelApi(row.id);
        const { msg, code } = result;
        if (code === 0) {
            useMsg("success", msg);
            getArticleClassList();
        }
    });
};
//抽屉确认
const handleConfirmClick = () => {
    if (!formRef.value!.ruleFormRef) return;
    formRef!.value!.ruleFormRef!.validate((valid: any) => {
        if (valid) {
            dataStore.title === "添加文章分类" ? getArticleClassAddSave() : getArticleClassUpEdit();
        } else {
            console.log("error submit!");
            return false;
        }
    });
};
//重置验证状态
const resetFields = () => {
    if (!formRef.value!.ruleFormRef) return;
    formRef!.value!.ruleFormRef.resetFields();
    dataStore.editRuleForm = cloneDeep(EDIT_RULE_FORM);
};
//抽屉重置
const handleResetClick = () => {
    if (dataStore.title == "添加文章分类") {
        resetFields();
    } else {
        getArticleClassDetails(dataStore.selectRow);
    }
};

//添加
const handleAdd = () => {
    dataStore.title = "添加文章分类";
    dataStore.visible = true;
};
//弹窗关闭钩子
const handleBeforeClone = () => {
    dataStore.editRuleForm = cloneDeep(EDIT_RULE_FORM);
    resetFields();
    dataStore.visible = false;
};
//文章分类详情
const getArticleClassDetails = async (row: any) => {
    const result = await getArticleClassDetailsApi(row.id);
    const { code, data } = result;
    if (code === 0) {
        dataStore.editRuleForm = cloneDeep(data);
    }
};
//按钮点击事件
const handleBtnClick = (type: any, row: any) => {
    dataStore.selectRow = row;
    //添加
    if (type === "add") {
        $router.push({
            path: "/admin/articleManagement/list/edit",
            query: {
                type,
                title: "添加文章",
                category_id: row.id
            }
        });
    }
    //编辑
    if (type === "edit") {
        dataStore.visible = true;
        dataStore.title = "编辑文章分类";
        getArticleClassDetails(row);
        return;
    }
    //删除
    if (type === "del") {
        getArticleClassDel(row);
    }
};
</script>

<style scoped></style>
