<!-- 购买链接 -->
<template>
    <div class="table-box">
        <div></div>
        <div style="display: flex; padding-bottom: 16px">
            <el-button type="primary" @click="handleImport"> 导入 </el-button>
            <el-button type="primary" @click="handleExport" style="margin-left: 20px"> 导出 </el-button>
        </div>

        <ProTable
            ref="proTableRef"
            :formData="dataStore.formData"
            :columns="dataStore.columns"
            :request-api="getProductBuypassListApi"
            :init-param="dataStore.initParam"
        >
            <template #status="scope">
                <el-tag type="success" effect="dark">{{ scope.row.status }}</el-tag>
            </template>
            <template #is_show="scope">
                <el-tag :type="scope.row.is_show === 1 ? 'success' : 'danger'" effect="dark">{{
                    scope.row.is_show === 1 ? "已上架" : "未上架"
                }}</el-tag>
            </template>
            <template #platform_name="scope">
                <div v-for="(it, itIndex) in scope.row.rowspan" :key="itIndex" style="margin-bottom: 10px">
                    <el-select
                        v-model="it.platform_name"
                        class="m-2"
                        placeholder="请选择"
                        @change="handleInputOrChange(it, scope, itIndex)"
                    >
                        <el-option
                            v-for="item in dataStore.options"
                            :key="item.id"
                            :label="item.platform"
                            :value="item.platform"
                            @click="handleOptionItemClick(item, scope)"
                        />
                    </el-select>
                </div>
            </template>
            <template #link="scope">
                <div v-for="(it, itIndex) in scope.row.rowspan" :key="itIndex" style="margin-bottom: 10px">
                    <el-input v-model="it.link" @blur="handleInputOrChange(it, scope, itIndex)"></el-input>
                </div>
            </template>
        </ProTable>
        <ImportExcel ref="importRef" />
    </div>
</template>

<script setup lang="ts" name="productLinkIndex">
import ProTable from "@/components/ProTable/index.vue";
import { useMsg } from "@/hooks/useMsg";
import { useExport } from "@/hooks/useExport";
// import type { UploadInstance } from "element-plus";
import ImportExcel from "@/components/ImportExcel/index.vue";
import { useUserStore } from "@/stores/modules/user";
//列表接口
//    getProductBuypassListImportApi
import {
    getProductBuypassListSaveApi,
    getProductBuypassListApi,
    getProductPlatformsListApi,
    getProductBuypassListExportApi,
    getProductBuypassUpdateApi,
    getProductBuypassListImportApi
} from "@/api/modules/productLink";
const importRef = ref();

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

// 数据源
const dataStore = reactive<any>({
    importUrl: import.meta.env.VITE_APP_API_BASE_UPLOAD_URL + "/product/buypass/import",
    uploadHeaders: {
        Authorization: "Bearer" + " " + userStore.token
    },
    fileType: [".xlsx", ".xls"],
    options: [],
    columns: COLUMNS, //列表配置项
    initParam: cloneDeep(RULE_FORM), // 初始化搜索条件|重置搜索条件
    ruleForm: cloneDeep(RULE_FORM), // 搜索參數
    formData: FORM_DATA //搜索配置项
});
//添加
const getProductBuypassListSave = async (params: any) => {
    const result = await getProductBuypassListSaveApi(params);
    if (result?.code === 0) {
        useMsg("success", result?.msg);
        // proTableRef?.value?.getTableList();
        // console.log(result?.data);
    }
};
//更新
const getProductBuypassUpdate = async (params: any) => {
    const result = await getProductBuypassUpdateApi(params);
    if (result?.code === 0) {
        useMsg("success", result?.msg);
        // console.log(result?.data);
    }
};

//输入框失焦
const handleInputOrChange = (it: any, scope: any, itIndex: any) => {
    const { link, platform_name, link_id } = it;
    let is = scope.row.rowspan.some((item: any, index: number) => {
        if (index !== itIndex && item.platform_name === platform_name) {
            return true;
        }
    });
    if (is) {
        useMsg("warning", "同型号平台不能重复 !");
        return;
    }
    if (!platform_name) {
        useMsg("warning", "平台不能为空 !");
        return;
    }
    if (!link) {
        useMsg("warning", "链接不能为空 !");
        return;
    }
    //获取ID
    let platform_id: any = null;
    dataStore.options.forEach((item: any) => {
        if (item.platform === platform_name) {
            platform_id = item.id;
        }
    });

    //添加
    if (!link_id) {
        getProductBuypassListSave({
            product_id: scope.row.id,
            platform_id,
            link
        });
    } else {
        // 更新
        getProductBuypassUpdate({
            id: link_id,
            param: {
                platform_id,
                link
            }
        });
    }
};
//平台
const getProductPlatformsList = async () => {
    const result = await getProductPlatformsListApi();
    if (result?.code === 0) {
        dataStore.options = result?.data;
    }
};
getProductPlatformsList();

const handleOptionItemClick = (item: any, scope: any) => {
    console.log(item, "=1=");
    console.log(scope, "=2=");
};

//导出
const handleExport = () => {
    getProductBuypassListExport();
};

//导出接口
const getProductBuypassListExport = async () => {
    const result = await getProductBuypassListExportApi({
        ...proTableRef?.value?.searchParam,
        ...proTableRef?.value?.pageable
    });
    await useExport(result);
};

const handleImport = () => {
    let params = {
        title: "数据",
        tempApi: "",
        importApi: getProductBuypassListImportApi,
        proTableRef: proTableRef
    };
    importRef.value.acceptParams(params);
};
</script>

<style scoped></style>
