import { useMsg } from "@/hooks/useMsg";
import { getProductEditUpApi } from "@/api/modules/productList";
import { cloneDeep } from "lodash-es";
const WARN: any = {
    name: "产品名称不能为空 !",
    spu: "型号不能为空 !",
    category_id: "产品分类不能为空 !",
    sort: "产品排序不能为空 !"
};
//警告
const warnFunction = (data: any) => {
    if (!data.name) {
        useMsg("warning", WARN["name"]);
        return false;
    }
    if (!data.spu) {
        useMsg("warning", WARN["spu"]);
        return false;
    }
    if (!data.category_id) {
        useMsg("warning", WARN["category_id"]);
        return false;
    }
    if (!data.name) {
        useMsg("warning", WARN["sort"]);
        return false;
    }
    return true;
};

//更新
const getProductEditUp = async (params: any) => {
    const result: any = await getProductEditUpApi(params);
    if (result?.code === 0) {
        useMsg("success", result?.msg);
        // montageImg(imgInfoRef);
    }
};
export const handleSubmit = async (infoRef: any, imgInfoRef: any, dataStore: any) => {
    let is = await warnFunction(infoRef.ruleForm);
    if (!is) {
        return false;
    }
    //相关信息及下载(过滤掉没有id的对象)
    let relatedData = dataStore.relatedTableData.filter((item: any) => {
        return item.related_product_id;
    });

    const { video_img, video_url, cover_image, skus } = imgInfoRef.data;

    //不要直接去修改skus的数据类型,这里的skus是和表格绑定的
    let skusClone = cloneDeep(skus);
    skusClone.forEach((item: any) => {
        let arr: any = [];
        item.photo_album_clone.forEach((it: any) => {
            arr.push(it.url);
        });
        item.photo_album = arr;
        item.attrs = item.attrs;
        // delete item.photo_albumClone;
    });
    console.log(skusClone, "=skusClone=");
    let skusCloneStr = JSON.stringify(skusClone);
    console.log(typeof skusCloneStr);

    const params = {
        ...infoRef.ruleForm,
        cover_image,
        video_url,
        video_img,
        skus: skusCloneStr,
        detail: dataStore.detail,
        related: JSON.stringify(relatedData) || []
    };

    console.log(params, "===========params=============");
    getProductEditUp(params);
};
