// pages/groupmeallist/goodDetails/goodDetails.js
import storeGoods from '../../../api/storeGoods'
const user = require('../../../utils/user.js');
const cache = require('../../../utils/cache.js');
import base from '../../../utils/base'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        bannerList: [{}, {}],
        isShowSpecPannel: false,
        activeTab: 0,
        totalPrice: 2,
        isShowCartPannel: false,
        cartSkuMapData: [{}],
        comBoFoodsList: [{}],
        comBoObject: {},
        comBoObjectCopy: {},
        shopCarList: [],
        allnum: '',
        chooseNum: 0,
        currSpecGoodsSpec: {},

        childrenindex: '',
        idx: '',
        setsId: ''

    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

        let goodsJson = JSON.parse(options.goodsJson)
        this.setData({
            comBoObject: goodsJson,
            comBoObjectCopy: goodsJson
        })

        console.log(goodsJson)

    },

    // 商品添加
    comBoFoodsListAdd: async function (e) {
        let childrenindex = e.currentTarget.dataset.childrenindex;
        let comBoObject = this.data.comBoObject
        let idx = e.currentTarget.dataset.idx;
        let setsId = comBoObject.productComboSetsList[idx].setsId
        let productObject = e.currentTarget.dataset.productlist;
        let allnum = 0;
        let chooseNum = this.data.chooseNum;

        let setObeject = comBoObject.productComboSetsList[idx]
        let chooseGood = comBoObject.productComboSetsList[idx].comboSku.productList[childrenindex]


        if (comBoObject.productComboSetsList[idx].shopCarList == null || comBoObject.productComboSetsList[idx].shopCarList.length <= 0) {
            console.log('進入if')
            comBoObject.productComboSetsList[idx].shopCarList = []

            comBoObject.productComboSetsList[idx].shopCarList.push({
                productName: productObject.productName,
                subtitle: '',
                num: 1,
                childrenindex: childrenindex,
                idx: idx,
                setsId: setsId,
                skuId: productObject.skuList[0].skuId

            })


            //列表显示数值
            chooseGood.num = 1

            this.data.shopCarList.push(comBoObject.productComboSetsList[idx].shopCarList[comBoObject.productComboSetsList[idx].shopCarList.length - 1])
            console.log(this.data.shopCarList)

            this.data.shopCarList.forEach(v => {

                allnum += v.num
            })
            this.setData({
                allnum: allnum,
                shopCarList: this.data.shopCarList,
                comBoObject: comBoObject
            })
            console.log(this.data.shopCarList)

        } else {
            console.log(this.data.shopCarList)
            console.log('else')
            let exist = 0
            //为添加购物车总数

            comBoObject.productComboSetsList[idx].shopCarList.forEach((v) => {
                chooseNum += v.num

            })


            // //判难是否超过最大数量
            if (chooseNum < setObeject.optionNum) {
                console.log(this.data.shopCarList)

                let shopCartList = JSON.parse(JSON.stringify(this.data.shopCarList))
                shopCartList.forEach((item, i) => {
                    console.log(item)
                    if (productObject.productName == item.productName) {
                        console.log("qian" + item.num)
                        item.num = item.num + 1

                    }
                })


                this.data.comBoObject.productComboSetsList[idx].shopCarList.forEach((v) => {
                    //名称一致 并且分类允许重复选择
                    if (v.productName == productObject.productName && setObeject.repeatSelect == 1) {
                        console.log("发现商品一样")
                        v.num = v.num + 1
                        exist = 1;
                        chooseGood.num = chooseGood.num + 1


                    } else if (v.productName == productObject.productName && setObeject.repeatSelect == 0) {
                        wx.showToast({
                            title: '此分类不可重复选择',
                            icon: 'none',
                            duration: 3000
                        })
                        exist = 2;
                        return
                    }
                })

                console.log(this.data.shopCarList)
                //列表显示数值
                console.log("执行列表显示值")
                // chooseGood.num = chooseGood.num + 1

                if (exist == 0) {
                    console.log("进入0")
                    comBoObject.productComboSetsList[idx].shopCarList.push({
                        productName: productObject.productName,
                        subtitle: '',
                        num: 1,
                        childrenindex: childrenindex,
                        idx: idx,
                        setsId: setsId,
                        skuId: productObject.skuList[0].skuId
                    })
                    chooseGood.num = 1


                    shopCartList.push(comBoObject.productComboSetsList[idx].shopCarList[comBoObject.productComboSetsList[idx].shopCarList.length - 1])

                } else if (exist == 2) {
                    return
                }




                // 购物车总数累加 根据分类分成多组分类购物车


                shopCartList.forEach((v) => {
                    allnum += v.num
                })

                this.setData({
                    allnum: allnum,
                    shopCarList: shopCartList,
                    comBoObject: comBoObject,
                    chooseNum: 0

                })
                console.log(comBoObject.productComboSetsList[idx].shopCarList)

            } else {
                wx.showToast({
                    title: '此分类选择数量最大为' + setObeject.optionNum,
                    icon: 'none',
                    duration: 3000
                })

            }




        }

    },
    comBoFoodsListReduce: function (e) {


        let childrenindex = e.currentTarget.dataset.childrenindex;
        let idx = e.currentTarget.dataset.idx;
        let setsId = e.currentTarget.dataset.setsId;
        let productObject = e.currentTarget.dataset.productlist;
        let allnum = 0;

        let comBoObject = this.data.comBoObject
        let setObeject = comBoObject.productComboSetsList[idx]
        let chooseGood = comBoObject.productComboSetsList[idx].comboSku.productList[childrenindex]


        chooseGood.num = chooseGood.num - 1
        comBoObject.productComboSetsList[idx].shopCarList.forEach((item, i) => {
            if (productObject.productName == item.productName) {
                item.num = item.num - 1
                if (item.num <= 0) {
                    comBoObject.productComboSetsList[idx].shopCarList.splice(i, 1)
                }
            }

        })



        this.data.shopCarList.forEach((item, i) => {
            if (productObject.productName == item.productName) {
                console.log("qian" + item.num)
                item.num = item.num - 1
                console.log("hou" + item.num)
                if (item.num <= 0) {
                    this.data.shopCarList.splice(i, 1)
                }
            }
        })




        this.data.shopCarList.forEach(v => {
            allnum += v.num
        })

        this.setData({
            allnum: allnum,
            shopCarList: this.data.shopCarList,
            comBoObject: comBoObject

        })
    },

    comBoFoodsListReduceCar: function (e) {
        let productObject = e.currentTarget.dataset.item;
        let childrenindex = productObject.childrenindex;
        let idx = productObject.idx;
        let setsId = productObject.setsId;

        let allnum = 0;

        let comBoObject = this.data.comBoObject
        let setObeject = comBoObject.productComboSetsList[idx]
        let chooseGood = comBoObject.productComboSetsList[idx].comboSku.productList[childrenindex]


        chooseGood.num = chooseGood.num - 1
        comBoObject.productComboSetsList[idx].shopCarList.forEach((item, i) => {
            if (productObject.productName == item.productName) {

                if ((item.num = productObject.num - 1) <= 0) {
                    comBoObject.productComboSetsList[idx].shopCarList.splice(i, 1)
                }
            }

        })



        this.data.shopCarList.forEach((item, i) => {
            if (productObject.productName == item.productName) {

                if ((item.num = productObject.num - 1) <= 0) {
                    this.data.shopCarList.splice(i, 1)
                }
            }
        })




        this.data.shopCarList.forEach(v => {
            allnum += v.num
        })

        this.setData({
            allnum: allnum,
            shopCarList: this.data.shopCarList,
            comBoObject: comBoObject

        })
    },

    comBoFoodsListAddCar: async function (e) {

        let productObject = e.currentTarget.dataset.item;
        let childrenindex = productObject.childrenindex;
        let idx = productObject.idx;
        let setsId = productObject.setsId;
        // let productObject = e.currentTarget.dataset.productlist;
        let allnum = 0;



        let chooseNum = this.data.chooseNum;
        let comBoObject = this.data.comBoObject
        let setObeject = comBoObject.productComboSetsList[idx]
        let chooseGood = comBoObject.productComboSetsList[idx].comboSku.productList[childrenindex]

        console.log("循环之前")
        console.log(comBoObject.productComboSetsList[idx].shopCarList)

        // return

        //为添加购物车总数
        comBoObject.productComboSetsList[idx].shopCarList.forEach(v => {
            chooseNum += v.num
        })
        //判难是否超过最大数量
        if (chooseNum < setObeject.optionNum) {
            let exits = true
            comBoObject.productComboSetsList[idx].shopCarList.forEach(v => {
                //名称一致 并且分类允许重复选择
                if (v.productName == productObject.productName && setObeject.repeatSelect == 1) {
                    console.log("发现商品一样")
                    v.num = productObject.num + 1

                } else if (v.productName == productObject.productName && setObeject.repeatSelect == 0) {
                    wx.showToast({
                        title: '此分类不可重复选择',
                        icon: 'none',
                        duration: 3000
                    })
                    exits = false
                    return
                }
            })

            if (!exits) {
                return
            } else {
                //列表显示数值

                chooseGood.num = chooseGood.num + 1


                this.data.shopCarList.forEach(item => {

                    if (item.productName == productObject.productName) {
                        console.log("发现商品一样")
                        item.num = productObject.num + 1

                    }
                })


                // 购物车总数累加 根据分类分成多组分类购物车
                this.data.shopCarList.forEach(v => {
                    allnum += v.num
                })

                this.setData({
                    allnum: allnum,
                    shopCarList: this.data.shopCarList,
                    comBoObject: comBoObject,
                    chooseNum: 0

                })

            }
        } else {
            wx.showToast({
                title: '此分类选择数量最大为' + setObeject.optionNum,
                icon: 'none',
                duration: 3000
            })

        }





    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },

    //关闭购物车面板
    closeCartPannel: function () {
        this.setData({
            isShowCartPannel: false
        })
    },

    //关闭选规格面板
    closeSpecPannel: function () {
        this.setData({
            isShowSpecPannel: false
        })
    },
    //显示对话框
    showCartPannel: function () {
        // 显示遮罩层
        var animation = wx.createAnimation({
            duration: 100,
            timingFunction: "linear",
            delay: 0
        })
        this.animation = animation
        animation.translateY(300).step()
        this.setData({
            animationData: animation.export(),
            isShowCartPannel: true
        })
        setTimeout(function () {
            animation.translateY(0).step()
            this.setData({
                animationData: animation.export()
            })
        }.bind(this), 100)
    },

    //显示选规格
    showSpecPannel: function () {
        // 显示遮罩层
        var animation = wx.createAnimation({
            duration: 100,
            timingFunction: "linear",
            delay: 0
        })
        this.animation = animation
        animation.translateY(300).step()
        this.setData({
            animationData: animation.export(),
            isShowSpecPannel: true
        })
        setTimeout(function () {
            animation.translateY(0).step()
            this.setData({
                animationData: animation.export()
            })
        }.bind(this), 100)
    },
    // 弹窗购物车面板
    popCartPannel: function () {
        if (this.data.isShowCartPannel) {
            this.closeCartPannel()
            return
        }
        if (this.data.allNum == 0) {
            return
        }
        this.showCartPannel();
    },
    submitCat(e) {
        let childrenindex = this.data.childrenindex;
        let comBoObject = this.data.comBoObject;
        let idx = this.data.idx;
        let specObject = this.data.currSpecGoodsSpec;
        let setsId = this.data.setsId;


        let productObject = e.currentTarget.dataset.productlist;
        let allnum = 0;
        let chooseNum = this.data.chooseNum;

        let setObeject = comBoObject.productComboSetsList[idx]
        let chooseGood = comBoObject.productComboSetsList[idx].comboSku.productList[childrenindex]

        if (comBoObject.productComboSetsList[idx].shopCarList == null || comBoObject.productComboSetsList[idx].shopCarList.length <= 0) {
            comBoObject.productComboSetsList[idx].shopCarList = []

            comBoObject.productComboSetsList[idx].shopCarList.push({
                productName: specObject.productName,
                subtitle: '',
                text: specObject.currSelectedSpecName,
                num: 1,
                childrenindex: childrenindex,
                idx: idx,
                setsId: setsId,
                skuId: specObject.currSku.skuId

            })

            this.data.shopCarList.push(comBoObject.productComboSetsList[idx].shopCarList[comBoObject.productComboSetsList[idx].shopCarList.length - 1])


            this.data.shopCarList.forEach(v => {

                allnum += v.num
            })
            this.setData({
                allnum: allnum,
                shopCarList: this.data.shopCarList,
                comBoObject: comBoObject
            })

        } else {
            console.log('else')
            let exist = 0
            //为添加购物车总数
            console.log(chooseNum + '上面')
            comBoObject.productComboSetsList[idx].shopCarList.forEach(v => {
                chooseNum += v.num

            })
            let shopCartList = JSON.parse(JSON.stringify(this.data.shopCarList))
            shopCartList.forEach((item, i) => {
                console.log(item)
                if (specObject.productName == item.productName) {
                    console.log("qian" + item.num)
                    item.num = item.num + 1

                }
            })


            console.log(chooseNum + '下面')
            //判难是否超过最大数量
            if (chooseNum < setObeject.optionNum) {
                console.log("进入数量小于")
                comBoObject.productComboSetsList[idx].shopCarList.forEach(v => {
                    //名称一致 并且分类允许重复选择
                    if (v.productName == specObject.productName && setObeject.repeatSelect == 1) {
                        console.log("发现商品一样")
                        v.num = v.num + 1
                        exist = 1;
                    } else if (v.productName == specObject.productName && setObeject.repeatSelect == 0) {
                        wx.showToast({
                            title: '此分类不可重复选择',
                            icon: 'none',
                            duration: 3000
                        })
                        exist = 2;
                        return
                    }
                })
                //列表显示数值
                console.log("执行列表显示值")
                // chooseGood.num = chooseGood.num + 1

                if (exist == 0) {
                    comBoObject.productComboSetsList[idx].shopCarList.push({
                        productName: specObject.productName,
                        subtitle: '',
                        num: 1,
                        childrenindex: childrenindex,
                        text: specObject.currSelectedSpecName,
                        idx: idx,
                        setsId: setsId,
                        skuId: specObject.currSku.skuId
                    })
                    // chooseGood.num = 1
                } else if (exist == 2) {
                    return
                }
                let repeatSelect = false
                shopCartList.forEach(item => {

                    if (item.productName == specObject.productName) {
                        console.log("发现商品一样")
                        item.num = item.num + 1
                        repeatSelect = true
                    }
                })
                if (!repeatSelect) {
                    shopCartList.push(comBoObject.productComboSetsList[idx].shopCarList[comBoObject.productComboSetsList[idx].shopCarList.length - 1])
                }

                // 购物车总数累加 根据分类分成多组分类购物车
                shopCartList.forEach(v => {
                    allnum += v.num
                })

                this.setData({
                    allnum: allnum,
                    shopCarList: shopCartList,
                    comBoObject: comBoObject,
                    chooseNum: 0

                })
                console.log(comBoObject.productComboSetsList[idx])

            } else {
                wx.showToast({
                    title: '此分类选择数量最大为' + setObeject.optionNum,
                    icon: 'none',
                    duration: 3000
                })

            }




        }


    },

    comBoselectSpecNode(e) {
        let specId = e.currentTarget.dataset.specid
        let specKey = e.currentTarget.dataset.speckey
        console.log("specId" + specId + "----" + "specKey" + specKey)

        let currSpecGoodsSpec = this.data.currSpecGoodsSpec
        console.log(currSpecGoodsSpec)

        // 选中的规格id列表
        let specIdList = []
        let specNameList = []

        currSpecGoodsSpec.productSpecList.forEach(function (spec, index) {
            // currSpecGoods.productSpecList.forEach(spec => {
            spec.specValueList.forEach(specValue => {
                console.log("循环内specId" + spec.specId)
                if (spec.specId == specKey) {
                    console.log("specValue.specId" + specValue.specId + "specId" + specId)
                    if (specValue.specId == specId) {
                        console.log("Yes俩个条件全部命中")
                        specValue.active = true
                        console.log(specValue)
                        specIdList.push(specId)
                        specNameList.push(specValue.specValue)
                    } else {
                        console.log("No俩个条件全部未命中")
                        specValue.active = false
                    }
                } else {
                    if (specValue.active) {
                        specIdList.push(specValue.specId)
                        specNameList.push(specValue.specValue)
                    }
                }
            })
        })

        currSpecGoodsSpec.currSelectedSpecName = specNameList.join('/') || ""
        currSpecGoodsSpec.specNameList = specNameList
        currSpecGoodsSpec.currSku = null

        // 根据选中的规格找到skuId
        currSpecGoodsSpec.skuList.forEach(function (sku, index) {
            let dA = base.diffArr(specIdList, sku.specIdList)
            console.log("命中")
            console.log(dA)
            if (dA.length == 0) {
                // 命中
                currSpecGoodsSpec.currSku = sku
                console.log("命中")
                console.log(currSpecGoodsSpec.currSku)
            }
        })

        this.setData({
            currSpecGoodsSpec
        })

    },

    // 弹窗购物车面板
    popSpecPannel: function (e) {

        let childrenindex = e.currentTarget.dataset.childrenindex;

        let idx = e.currentTarget.dataset.idx;
        let setsId = this.data.comBoObject.productComboSetsList[idx].setsId

        this.setData({
            childrenindex: childrenindex,
            idx: idx,
            setsId: setsId

        })


        let productlist = e.currentTarget.dataset.productlist;
        this.setData({

            // 规格赋值
            currSpecGoodsSpec: e.currentTarget.dataset.productlist,
            opacityStyle: "opacity0"
        })


        if (this.data.isShowCartPannel) {
            this.closeSpecPannel()
            return
        }
        if (this.data.allNum == 0) {
            return
        }
        this.showSpecPannel();
    },


    // 详情和收起
    changeCollapseOrDetail: function (e) {
        let idx = e.currentTarget.dataset.idx;
        var that = this;
        that.data.cartSkuMapData[idx].show = !that.data.cartSkuMapData[idx].show
        that.setData({
            cartSkuMapData: that.data.cartSkuMapData
        });
        console.log("购物车详情");
        console.log(that.data.cartSkuMapData);
    },

    // 加入购物车返回商品列表
    gotoStoreGoods() {
        let allnum = 0
        let subitNum = 0
        let obeject = this.data.comBoObject.productComboSetsList
        this.data.shopCarList.forEach(v => {
            allnum += v.num
        })
        this.data.comBoObject.productComboSetsList.forEach(item => {
            subitNum += item.optionNum
        })

        if (allnum == subitNum) {

            console.log("符合加入购物车条件")

            let comboSkuList = []

            obeject.forEach(item => {
                item.comboSkuList = []
                item.shopCarList.forEach(v => {
                    item.comboSkuList.push(v)

                })
            })

            obeject.forEach(item => {
                comboSkuList.push({
                    setsId: item.setsId,
                    skuList: item.comboSkuList
                })


            })
            console.log(obeject)
            console.log(comboSkuList)


            storeGoods.comBoaddToCart({
                storeId: cache.getStoreId(),
                mobile: cache.getMobile(),
                skuId: this.data.comBoObject.skuId,
                num: 1,
                comboSkuList: comboSkuList,
            }).then(res => {
                if (res.code == 200) {
                    wx.switchTab({
                        url: '/pages/groupmeallist/storeGoods/storeGoods'
                    });
                }
            }).catch(_ => { })
        } else {
            wx.showToast({
                title: '请选择您喜欢的商品哟，亲！',
                icon: 'none',
                duration: 3000
            })
        }




    },
    //清空购物车
    onChangeShowStateDelFalse() {
        //购物车清空
        this.data.shopCarList = []
        //列表显示清空
        this.data.comBoObject.productComboSetsList.forEach(item => {
            item.comboSku.productList.forEach(v => {
                v.num = 0

            }),
                item.shopCarList = []
        })


        this.setData({
            shopCarList: this.data.shopCarList,
            comBoObject: this.data.comBoObject,
            allnum: 0,
            chooseNum: 0
        })

    }
})