<!-- 视频列表 -->
<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="getAttachmentListApi"
            :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="scope.row.status == 1 ? 'success' : 'danger'" effect="dark">{{
                    scope.row.status == 1 ? "启用" : "禁用"
                }}</el-tag>
            </template>
            <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="primary" @click="handleBtnClick('编辑', scope.row)">编辑</el-button>

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

<script setup lang="ts" name="downloadListIndex">
import ProTable from "@/components/ProTable/index.vue";

//列表接口
import { getAttachmentListApi, getAttachmentListDelApi, getAttachmentSortApi } from "@/api/modules/downloadList";
import { getCategorysApi } from "@/api/modules/downloadClass";
import { integerRexg } from "@/utils/regexp/index";
import { messageBox } from "@/utils/messageBox";
import { useMsg } from "@/hooks/useMsg";
import { addLabelValue } from "@/utils/addLabelValue";

//深拷贝方法
import { cloneDeep } from "lodash-es";
//表格和搜索條件 FORM_DATA
import { RULE_FORM, COLUMNS, EDIT_FORM_DATA, EDIT_RULE_FORM, RULES, EDIT_TABLE_DATA, FORM_DATA } from "./constant/index";
// 获取 ProTable 元素，调用其获取刷新数据方法（还能获取到当前查询参数，方便导出携带参数）
const proTableRef = ref<any>(null);
const $router = useRouter();

//图片地址
import { h } from "@/utils/url";
// 数据源
const dataStore = reactive<any>({
    editTableData: cloneDeep(EDIT_TABLE_DATA), //添加|编辑里的表格数据
    editColumns: [
        {
            label: "文件",
            prop: "file_path",
            disabled: false,
            formType: "up",
            width: "250",
            align: "left"
        },
        {
            label: "文件格式",
            prop: "file_ext",
            disabled: false,
            formType: "input",
            maxLength: 50,
            width: "100"
        },
        //operation
        {
            label: "按钮名称",
            prop: "btn_name",
            disabled: false,
            formType: "input",
            maxLength: 50,
            width: "150"
        },
        {
            label: "操作",
            prop: "operation",

            width: "112"
        }
    ], //添加|编辑里的表格配置项
    formData: cloneDeep(FORM_DATA),
    columns: COLUMNS, //列表配置项
    rules: cloneDeep(RULES), //抽屉表单验证
    editRuleForm: cloneDeep(EDIT_RULE_FORM),
    editFormData: cloneDeep(EDIT_FORM_DATA), //抽屉表单配置项
    initParam: cloneDeep(RULE_FORM) // 初始化搜索条件|重置搜索条件
});

//按钮点击事件
const handleBtnClick = (type: any, row: any) => {
    //编辑
    if (type === "编辑") {
        $router.push({
            path: "/admin/downloadManagement/list/edit",
            query: {
                id: row.id,
                title: "编辑下载"
            }
        });
        return;
    }
    //删除
    if (type === "删除") {
        getAttachmentListDel(row.id);
    }
};

const handleAdd = () => {
    $router.push({
        path: "/admin/downloadManagement/list/edit",
        query: {
            title: "添加下载"
        }
    });
};

//删除
const getAttachmentListDel = (id: any) => {
    messageBox("你确定要删除?", async () => {
        const result = await getAttachmentListDelApi(id);
        if (result?.code === 0) {
            const { msg } = result;
            useMsg("success", msg);
            proTableRef?.value?.getTableList();
        }
    });
};
//排序
const getAttachmentSort = async (row: any) => {
    const result = await getAttachmentSortApi({ id: row.id, sort: row.sort });
    useMsg("success", result?.msg);
    if (result?.code === 0) {
        proTableRef?.value?.getTableList();
    }
};

//排序input框失焦
const handleBlur = (row: any) => {
    getAttachmentSort(row);
};
//排序input输入
const handleInput = (row: any) => {
    row.sort = integerRexg(row.sort);
};

const getCategorys = async () => {
    const result = await getCategorysApi();

    if (result?.code === 0) {
        // let arr: any[] = [];
        // result?.data?.forEach((item: any) => {
        //     let obj = {
        //         value: item.id,
        //         label: item.name
        //     };
        //     arr.push(obj);
        // });

        dataStore.formData[1].options = addLabelValue(result?.data);
    }
};
getCategorys();
</script>

<style scoped></style>
