import {mallApi} from "@/utils/request.ts";


export interface PriceDetailList {
    id: string
    maxQuantity: number
    minQuantity: number
    pricePerItem: number
    productId: string
    unit: string
}

export interface StoreInfo {
    id: string;
    name: string;
    logo: string;
}

export interface AttrValueItem {
    name: string;
    imgUrl: string;
}

export interface ProductSku {
    attributeName: string
    price: number;
    stock: number;
    val:string;
    value: string | string [];
    list: AttrValueItem[]
    id: string;
    selectIndex: number;
    productAttribute: Object,
    activeAttr: Boolean
}

/**
 * @Description: 构建商品详情
 * @author：汪明
 * @date 2024/4/1 11:10
 */
export class ProductInfo {
    readonly id = '';
    readonly name = ''; // 商品名称
    readonly msrp: string = '0'; // 建议售价
    readonly pic: string = ''; // 商品主图
    readonly albumPic: string[] = [];
    readonly detailMobile: string[] = [];
    readonly bindModelStatus:boolean = false
    readonly store: StoreInfo = { // 商品所在店铺信息
        id: '',
        name: '',
        logo: ''
    }
    readonly productCommodityPriceDetailList: PriceDetailList[] = []; // 商品区间价格
    readonly productSkuList: ProductSku[] = []
    readonly productThreeDSkuList: ProductSku[] = []
    readonly modelId = '';

    constructor(data?: any) {
        if (data) {
            console.log(data);
            this.id = data.id;
            this.name = data.name || '';
            this.msrp = (data.msrp || 0).toFixed(2);
            this.albumPic = [data.pic, ...data.albumPic] || []
            this.store = data.store || this.store;
            this.productCommodityPriceDetailList = data.productCommodityPriceDetailList || [];
            this.detailMobile = data.detailMobile ? this.stringToArray(data.detailMobile) : [];
            this.bindModelStatus = data.bindModelStatus;
            this.productSkuList = this.transformSku(data.productAttributeValueList) || []
            this.productThreeDSkuList = this.transformSku(data.product3dAttributeValueList) || []
            this.modelId = data.modelId
            console.log('this.productSkuList=', this.productSkuList);
        }
    }

    private stringToArray(str: string, symbol: string = ','): string [] {
        if (str) {
            return str.split(symbol)
        } else {
            return []
        }
    }

    private transformSku(data: ProductSku[]): ProductSku[] {
        const limitedData = data.slice(0, 3); // 只取前三个元素
        return limitedData.map(item => {
            const values = this.stringToArray(item.value as string);
            let transformedValue: AttrValueItem[] = values.map(val => {
                const strList: string[] = this.stringToArray(val, '_');
                return {
                    name: strList[0],
                    imgUrl: strList[1] ? strList[1] : ''
                }
            });
            return {
                ...item,
                selectIndex: 0,
                val:transformedValue[0].name,
                activeAttr: true,
                list: transformedValue
            };
        });
    }
}


/**
 * 获取产品分类
 * @returns 产品分类的 Promise
 */
export interface CateList {
    id: string,
    name: string
}

export const getCateList = (): Promise<CateList[]> => {
    try {
        return mallApi.get<CateList[]>({
            url: '/public/getAll',
            config: {requireToken: false}
        })
    } catch (error) {
        throw error
    }
};


/**
 * 获取全部周边产品
 * @returns 全部周边产品的 Promise
 */
export interface GetAroundData {
    count: string,
    list: AroundList[]
}

export interface AroundList {
    id: string,
    name: string,
    pic: string,
    price: string
}
export const getAroundList = (cateId: string, pageNo: number, pageSize: number): Promise<GetAroundData> => {
    try {
        return mallApi.get<GetAroundData>({
            url: `/public/getAllList`,
            config: {requireToken: false},
            params: {
                categoryId: cateId,
                pageNo,
                pageSize
            }
        })
    } catch (error) {
        throw error
    }
}

// 获取3d设计页面的商品分类数据
export const getThreeDAroundList = (params: any) :Promise<GetAroundData> => {
   try {
       return mallApi.get<GetAroundData>({
           url: `/public/getAllList`,
           config: {requireToken: false},
           params
       })
   } catch (error) {
       throw error
   }
}

/**
 * 获取爆款推荐
 * @returns 爆款推荐的 Promise
 */
export interface RecomendationList {
    id: string,
    name: string,
    pic: string,
    url: string
    price: number,
    storeId: string
}

export const getRecommendationList = (): Promise<RecomendationList[]> => {
    try {
        return mallApi.get<RecomendationList[]>({
            url: '/public/getHomeList',
            config: {requireToken: false}
        })
    } catch (error) {
        throw error;
    }
};

/**
 * 获取商品详情页内容
 * @returns 爆款推荐的 Promise
 */
export const getProductDetail = (productId: string): Promise<ProductInfo> => {
    try {
        return mallApi.get<ProductInfo>({
            url: `/product/${productId}`,
            config: {requireToken: false}
        })
    } catch (error) {
        throw error;
    }
};

/**
 * @Description: 商品详情页商品推荐
 * @author：翁庚荣
 * @date 2024/3/19 14:52
 * @returns Promise
 */
export const getProductRecommend = (productId: string): Promise<AroundList[]> => {
    try {
        return mallApi.get<AroundList[]>({
            url: `/product/recommend/${productId}`,
            config: {requireToken: false}
        })
    } catch (error) {
        throw error;
    }
}

/**
 * @Description: 根据商品名称获取商品列表数据
 * @author：陈宇颢
 * @date 2024/4/2 17:02
 * @returns Promise
 */
export interface SearchQueryKey {
    pageNo: number,
    pageSize: number,
    total: number,
    key: string
}

export const getProductByName = (data: SearchQueryKey): Promise<GetAroundData> => {
    try {
        return mallApi.get<GetAroundData>({
            url: `/product/list`,
            config: {requireToken: false},
            params: {
                pageNo: data.pageNo,
                key: data.key,
                pageSize: data.pageSize
            }
        })
    } catch (error) {
        throw error;
    }
}

/**
 * @Description: 根据商品ID,获取3d模型数据
 * @author：汪明
 * @date 2024/4/19 9:21
 * @param {string}  productId 商品id
 * @param {string}  storeId 店铺id
 * @returns Promise<GetAroundData>
 */
export const getModelDetail = (productId: string, storeId: string) => {
    try {
        return mallApi.get({
            url: `/product/model/${productId}`,
            config: {
                requireToken: false,
                Tenant: storeId
            }
        })
    } catch (error) {
        throw error
    }
}

/**
 * @Description: 查询设计界面我的图库
 * @author：翁庚荣
 * @date 2024/5/21 15:54
 */
export const getLibraryImages = (data: any) => {
    try {
        return mallApi.get({
            url: `/album/pic/list`,
            params: data
        })
    } catch (error) {

    }
}
/**
 * @Description: 删除我的图库的图片
 * @author：翁庚荣
 * @date 2024/5/21 15:54
 * @param {string}  id 图片id
 */

export const deleteLibraryImageById = (id: string) => {
    try {
        return mallApi.get({url: `/album/pic/${id}`})
    } catch (error) {
        throw error
    }
}

