<!-- 视频列表 -->
<template>
    <div class="table-box">
        <div style="padding-bottom: 16px">
            <el-button type="primary" @click="handleAdd('add')"> 添加 </el-button>
            <el-button type="primary" @click="handleExport"> 导出 </el-button>
        </div>
        <ProTable
            ref="proTableRef"
            :formData="dataStore.formData"
            :columns="dataStore.columns"
            :request-api="getArticleListApi"
            :init-param="dataStore.initParam"
        >
            <template #image="scope">
                <el-image :src="scope.row.image ? h + scope.row.image : '--'" style="width: 60px; height: 60px" />
            </template>
            <template #enabled="scope">
                <el-tag type="success" effect="dark">{{ scope.row.enabled === 1 ? "启用" : "禁用" }}</el-tag>
            </template>
            <template #operation="scope">
                <el-button size="small" type="primary" @click="handleBtnClick('edit', scope.row)">编辑</el-button>
                <el-button size="small" type="danger" @click="handleBtnClick('del', scope.row)">删除</el-button>
            </template>
        </ProTable>
    </div>
</template>

<script setup lang="ts" name="articleListIndex">
import { useExport } from "@/hooks/useExport";
import { messageBox } from "@/utils/messageBox";
import { useMsg } from "@/hooks/useMsg";
import ProTable from "@/components/ProTable/index.vue";
//列表接口
import {
    getArticleListApi,
    getArticleClassDataApi,
    getArticleListExportApi,
    getArticleListDelApi
} from "@/api/modules/articleList";
// import { useSearchInfoArray } from "@/hooks/useSearch";
//深拷贝方法
import { cloneDeep } from "lodash-es";
//表格和搜索條件
import { RULE_FORM, FORM_DATA, COLUMNS } from "./constant/index";

import { addLabelValue } from "@/utils/addLabelValue";

//图片地址
import { h } from "@/utils/url";
// 获取 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, //搜索配置项
    selectRow: {} //当前选择的row
});

//文章分类(搜索条件)
const getArticleClassData = async () => {
    const result = await getArticleClassDataApi();
    if (result?.code === 0) {
        const { data } = result;
        dataStore.formData[1].options = addLabelValue(data);
    }
};
getArticleClassData();
//添加
const handleAdd = (type: any) => {
    $router.push({
        path: "/admin/articleManagement/list/edit",
        query: {
            type,
            title: "添加文章"
        }
    });
};

//导出接口
const getArticleListExport = async () => {
    const result = await getArticleListExportApi({
        ...proTableRef?.value?.searchParam,
        ...proTableRef?.value?.pageable
    });
    await useExport(result);
};
//导出
const handleExport = () => {
    getArticleListExport();
};
//删除
const getArticleListDel = async (id: any) => {
    messageBox("你确定要删除?", async () => {
        const result = await getArticleListDelApi(id);
        if (result?.code === 0) {
            const { msg } = result;
            useMsg("success", msg);
            proTableRef?.value?.getTableList();
        }
    });
};
//按钮点击事件
const handleBtnClick = (type: any, row: any) => {
    dataStore.selectRow = row;
    //编辑
    if (type === "edit") {
        $router.push({
            path: "/admin/articleManagement/list/edit",
            query: {
                id: row.id,
                type
            }
        });
        return;
    }
    //删除
    if (type === "del") {
        getArticleListDel(row.id);
    }
};
</script>

<style scoped></style>
