<!-- 视频列表 -->
<template>
    <div class="table-box">
        <div style="padding-bottom: 16px">
            <el-button type="primary" @click="handleAdd"> 添加 </el-button>
            <el-button type="primary" @click="handleExport"> 导出 </el-button>
        </div>
        <ProTable
            ref="proTableRef"
            :formData="dataStore.formData"
            :columns="dataStore.columns"
            :request-api="getVideoListApi"
            :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 #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>
        <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"
                >
                </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="videoListIndex">
import ProTable from "@/components/ProTable/index.vue";
import rulesForm from "@/components/rulesForm/index.vue";
import { messageBox } from "@/utils/messageBox";
import { useMsg } from "@/hooks/useMsg";

//列表接口
import {
    getVideoListApi,
    getVideoListExportApi,
    getVideoListDelApi,
    getVideoReadApi,
    getVideoUpdateApi,
    getVideoSaveApi,
    getVideoClassListApi
} from "@/api/modules/videoList";
import { recursiveCompare } from "@/utils/recursiveCompare";
// import { getVideoClassListApi } from "@/api/modules/videoClass";
//深拷贝方法
import { cloneDeep } from "lodash-es";
//表格和搜索條件
import { RULE_FORM, FORM_DATA, COLUMNS, EDIT_FORM_DATA, EDIT_RULE_FORM, RULES } from "./constant/index";
import { useExport } from "@/hooks/useExport";
// 获取 ProTable 元素，调用其获取刷新数据方法（还能获取到当前查询参数，方便导出携带参数）
const proTableRef = ref<any>(null);
const formRef: any = ref(null);
//图片地址
import { h } from "@/utils/url";
// 数据源
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 === "添加视频" ? getVideoSave() : getVideoUpdate();
        } else {
            console.log("error submit!");
            return false;
        }
    });
};
//重置验证状态
const resetFields = () => {
    if (!formRef.value!.ruleFormRef) return;
    formRef!.value!.ruleFormRef.resetFields();
};
//抽屉重置
const handleResetClick = () => {
    if (dataStore.title === "添加视频") {
        resetFields();
    } else {
        getVideoRead(dataStore.selectRow.id);
    }
};

//视频分类接口
const getVideoClassList = async () => {
    const result = await getVideoClassListApi();
    if (result?.code === 0) {
        let arr: any[] = [];
        if (result?.data?.length) {
            result?.data?.forEach((item: any) => {
                let obj = {
                    value: item.id,
                    label: item.name
                };
                arr.push(obj);
            });
            dataStore.formData[1].options = arr;
        }
    }
};
getVideoClassList();

const getVideoClassEditList = async () => {
    const result = await getVideoClassListApi({ is_show: 1 });
    if (result?.code === 0) {
        let arr: any[] = [];
        if (result?.data?.length) {
            result?.data?.forEach((item: any) => {
                let obj = {
                    value: item.id,
                    label: item.name
                };
                arr.push(obj);
            });
            dataStore.editFormData[1].options = arr;
        }
    }
};
getVideoClassEditList();

//添加
const handleAdd = () => {
    dataStore.visible = true;
    dataStore.title = "添加视频";
};
//抽屉关闭前的钩子
const handleBeforeClone = () => {
    dataStore.editRuleForm = cloneDeep(EDIT_RULE_FORM);
    resetFields();
    dataStore.visible = false;
};
//详情
const getVideoRead = async (id: any) => {
    const result = await getVideoReadApi(id);
    if (result?.code === 0) {
        dataStore.editRuleForm = result?.data;
        // let is = dataStore.editFormData[1].options.some((item: any) => {
        //     console.log(item.id);
        //     console.log(dataStore.editRuleForm.category_id);
        //     return item.value == dataStore.editRuleForm.category_id;
        // });
        let is = dataStore.editFormData[1].options.some((item: any) =>
            recursiveCompare(item, dataStore.editRuleForm.category_id)
        );
        dataStore.editRuleForm.category_id1 = is ? dataStore.editRuleForm.category_id : dataStore.editRuleForm.category_name;
    }
};

const handleSelectChangeEmits = (value: any) => {
    console.log(value, "========value=33========");
    if (value.prop === "category_id1") {
        dataStore.editRuleForm.category_id = value.id;
    }
};

//保存
const getVideoSave = async () => {
    const result = await getVideoSaveApi(dataStore.editRuleForm);
    if (result?.code === 0) {
        const { msg } = result;
        useMsg("success", msg);
        dataStore.visible = false;
        dataStore.editRuleForm = cloneDeep(EDIT_RULE_FORM);
        proTableRef?.value?.getTableList();
    }
};
//更新
const getVideoUpdate = async () => {
    const result = await getVideoUpdateApi(dataStore.editRuleForm);
    if (result?.code === 0) {
        const { msg } = result;
        useMsg("success", msg);
        dataStore.visible = false;
        dataStore.editRuleForm = cloneDeep(EDIT_RULE_FORM);
        proTableRef?.value?.getTableList();
    }
};
//导出接口
const getProductListExport = async () => {
    const result = await getVideoListExportApi({
        ...proTableRef?.value?.searchParam,
        ...proTableRef?.value?.pageable
    });
    await useExport(result);
};
//删除
const getVideoListDel = (id: any) => {
    messageBox("你确定要删除?", async () => {
        const result = await getVideoListDelApi(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 === "编辑") {
        dataStore.visible = true;
        dataStore.title = "编辑视频";
        getVideoRead(row.id);
        return;
    }
    //删除
    if (type === "删除") {
        getVideoListDel(row.id);
    }
};
//导出
const handleExport = () => {
    getProductListExport();
};
</script>

<style scoped></style>
