// pages/order/placeOrder/placeOrder.js
const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        activeKey: 0,
        navList: [],
        height: '',
        show: false,
        detailsShow: false,
        carList: [

        ],
        carNum: 0,
        carPrice: '',
        shoppingList: [],
        shop: [],
        worke: "",
        status: ""
    },

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

        var worke = JSON.parse(options.parmise)
        console.log(worke)
        this.setData({
            worke: worke,
            status: app.globalData.status
        })
        this.getHeight()
        this.getCarAll()
        this.leftList()
    },

    shoppingMinus(e) {
        let items = e.currentTarget.dataset.item, list = this.data.shoppingList;
        list.map(item => {
            if (items.id == item.id) {
                item.num--
                console.log(item);

                this.changeCarList(item)
                var good_id = items.id;
                var money = items.price.sale_price;
                var stock = item.num;
                this.cartChang(good_id, money, stock);

            }
        })
        this.setData({
            shoppingList: list
        })
        this.getCarAll()
    },

    shoppingPlus(e) {
        let items = e.currentTarget.dataset.item, list = this.data.shoppingList;
        list.map(item => {
            if (items.id == item.id) {
                item.num++
                this.changeCarList(item)

                var good_id = items.id;
                var money = items.price.sale_price;
                var stock = item.num;
                this.cartChang(good_id, money, stock);


            }
        })
    


        this.setData({
            shoppingList: list
        })
        this.getCarAll()
    },

    changeCarList(item) {
        let carList = this.data.carList

        if (carList.length == 0) {
            carList.push(item)
            this.setData({
                carList
            })
            return
        }
        console.log(item);
        var type = false;
        carList.map((i, index) => {
            if (i.id === item.id) {
                i.num = item.num
                if (item.num == 0) {
                    carList.splice(index, 1)
                }
                this.setData({
                    carList
                })
                type = false
            } else {
                type = true
            }
        })
        if (type) {
            carList.push(item)
        }
        console.log(carList);
        this.setData({
            carList
        })

    },

    //修改购物车
    cartChang(good_id, money, stock) {
        var nid = this.data.worke.id;
        app.api.useApi(app.globalData.baseAppUrl + "api/change_cart", {
            network_id: nid,
            good_id: good_id,
            money: money,
            stock: stock
        }, "post").then(function (res) {
            console.log(res);
            if (res.code != 200) {
                wx.showLoading({
                    title: res.message.status,
                })
            } else {
                var data = res.message.data;
                var shop = [];
                for (var index in data) {
                    shop[data[index].id] = data[index].goods
                }


                that.setData({
                    navList: res.message.data,
                    shoppingList: res.message.data[0].goods,
                    shop: shop
                })
            }

            wx.hideLoading();

        }).catch(function (err) {
            console.log(222);
        })
    },

    changeShoppingList(item) {
        console.log(item);
        let shoppingList = this.data.shoppingList;
        shoppingList.map(i => {
            if (item.id == i.id) {
                i.num = item.num
            }
        })
        this.setData({
            shoppingList
        })

    },

    getCarAll() {
        var nid = this.data.worke.id;
        var that=this;
        app.api.useApi(app.globalData.baseAppUrl + "api/cart_info", {
            network_id: nid,
        }, "get").then(function (res) {
            console.log(res);
            if (res.code != 200) {
                wx.showLoading({
                    title: res.message.msg,
                })
            } else {
                console.log(res.message.data.carPrice)
                that.setData({
                    carList:res.message.data.cart.data, 
                    carNum: res.message.data.carNum,
                    carPrice: res.message.data.carPrice
                })
              
            }

            wx.hideLoading();

        }).catch(function (err) {
            console.log(222);
        })


        let carList = this.data.carList
        carList.map(item => {
            wx.nextTick(() => {
                this.setData({
                    carNum: this.data.carNum + item.num,
                    carPrice: this.data.carPrice + item.price.sale_price * item.num
                })

            })
        })

    },
    getOrder() {
        var nid = this.data.worke.id;
        app.api.useApi(app.globalData.baseAppUrl + "api/makeorder", {
            network_id: nid,
        }, "post").then(function (res) {
            console.log(res);
            if (res.code != 200) {
                wx.showLoading({
                    title: res.message.msg,
                })
                wx.setTimeout(() => {
                    wx.hideLoading();
                }, 1000);
            } else {
                var order_no = res.message.data.order_no;
                wx.navigateTo({
                    url: '/pages/order/confirmOreder/confirmOreder?order_no='+order_no,
                })
            }

           

        }).catch(function (err) {
            console.log(222);
        })



      
    },
    carPlus(e) {
        let items = e.currentTarget.dataset.item, list = this.data.carList
        console.log(items);
        list.map((item, index) => {
            if (items.id == item.id) {
                item.num++
                this.changeShoppingList(item)
            }
        })
        this.setData({
            carList: list
        })
        this.getCarAll()
    },

    carMinus(e) {
        let items = e.currentTarget.dataset.item, list = this.data.carList
        list.map((item, index) => {
            if (items.id == item.id) {
                item.num--
                this.changeShoppingList(item)
                if (items.num - 1 <= 0) {
                    list.splice(index, 1)
                }
            }
        })
        this.setData({
            carList: list
        })

        this.getCarAll()

    },
    clearCarAll() {
        this.setData({
            carList: []
        })
        let shoppingList = this.data.shoppingList
        shoppingList.map(item => {
            item.num = 0
        })
        this.setData({
            shoppingList
        })
        this.getCarAll()
        this.btnShow()
        this.getHeight()

    },

    closeNews() {
        console.log(this.data.height);
        this.setData({
            height: this.data.height + 40
        })
    },
    changeActive(e) {
        let { activekey } = e.currentTarget.dataset
        var id = e.target.dataset.id;

        this.setData({
            activeKey: activekey,
            shoppingList: this.data.shop[id]
        })
    },
    addressDetail() {
        this.setData({
            detailsShow: !this.data.detailsShow
        })
    },
    getHeight() {
        var clientHeight;
        wx.getSystemInfo({
            success: (res) => {
                clientHeight = res.windowHeight;
            }
        });
        var query1 = wx.createSelectorQuery()
        var query2 = wx.createSelectorQuery()
        var query3 = wx.createSelectorQuery()
        query1.select('#footerBar').boundingClientRect()
        query2.select('#topBox').boundingClientRect()
        query3.select('#scrollBox').boundingClientRect()
        query2.exec(result => {
            query3.exec(r => {

                query1.exec(res => {
                    if (res && res[0] != null) {
                        this.setData({
                            height: clientHeight - res[0].height - result[0].height - r[0].height - 40
                        })

                    } else {
                        this.setData({
                            height: clientHeight - result[0].height - r[0].height - 40
                        })
                    }
                    console.log(clientHeight, result[0].height, r[0].height);


                })
            })
        })
    },
    btnShow() {
        this.setData({
            show: !this.data.show
        })
    },
    leftList: function () {
        var that = this;
        var nid = this.data.worke.id;

        app.api.useApi(app.globalData.baseAppUrl + "api/good", {
            network_id: nid,
        }, "get").then(function (res) {
            console.log(res);
            if (res.code != 200) {
                wx.showLoading({
                    title: res.message.status,
                })
            } else {
                var data = res.message.data;
                var shop = [];
                for (var index in data) {
                    shop[data[index].id] = data[index].goods
                }


                that.setData({
                    navList: res.message.data,
                    shoppingList: res.message.data[0].goods,
                    shop: shop
                })
            }

            wx.hideLoading();

        }).catch(function (err) {
            console.log(222);
        })
    },


})