/*
 * 手太欠
 * 星光网-商城
 */

const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        carArr          : [],      //购物车列表
        allCheckbox     : false,   //全选
        bagNumber       : 0,       //合计多少件商品
        allPrice        : '0.00',  //合计金额
        number          : 0,
        total           : '0',      //价格
        cartId          : 0,        //购物车id
        paramsCart      : [],
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad (options) {
    },
    
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        // 获取购物车列表
        this.carInfo();
    },

    /**
     * 购物车列表
     */
    carInfo() {
        if(wx.getStorageSync("token") == ""){
            wx.navigateTo({
                url: '/pages/login/login'
            })
            return
        }
        wx.$api.mall.carKist().then(res => {
            console.log(res)
            this.setData({
                carArr: res.lists
            })
            
        })
    },

    /**
     * 单选
     */
    checkbox(e){
        let goodsIndex   = e.currentTarget.dataset.goods,
            goodsList    = this.data.carArr,
            checkbox     = goodsList[goodsIndex].is_check,
            sellerLength = 0
            goodsList[goodsIndex].is_check = !checkbox
        for(let i of goodsList){
            if(i.is_check){
                sellerLength++
                if(sellerLength == goodsList.length){
                    goodsList[goodsIndex].is_check = true
                    // this.setData({
                    //     allCheckbox: true
                    // })
                }
            }else{
                goodsList[goodsIndex].is_check = false
                this.setData({
                    allCheckbox: false
                })
            }
        }
        this.setData({
            carArr  : goodsList
        })

        this.allCheckbox('checkbox')

        // 获取计算价格
        this.totalPrice()
    },

    /**
     * 全选
     */
    allCheckbox(type){
        let goodsList   = this.data.carArr,
            goodsLenght = 0,
            allCheckbox = this.data.allCheckbox
        
        if (type == 'checkbox'){
            for (let j in goodsList) {
                if (goodsList[j].is_check) {
                    goodsLenght++
                    if (goodsLenght == goodsList.length) {
                        allCheckbox = true
                    }
                }
            }
        }else{
            allCheckbox = !allCheckbox

            for (var i in goodsList){
                goodsList[i].is_check = allCheckbox
            }
        }


        this.setData({
            allCheckbox : allCheckbox,
            carArr      : goodsList
        })


        // 获取计算价格
        this.totalPrice()
    },

    /**
     * 商品数量加减
     */
    goodsNumber(e){
        let goodsId     = e.currentTarget.dataset.id,
            goodsIndex  = e.currentTarget.dataset.goods,
            goodsNew    = this.data.carArr,
            goodsNumber = goodsNew[goodsIndex].number

        if (e.currentTarget.dataset.type == 'plus'){
            wx.$api.mall.increment(goodsId).then(res => {
                goodsNew[goodsIndex].number = res.number
                goodsNew[goodsIndex].total  = res.total
                this.setData({
                    carArr      : goodsNew
                })
                // 获取计算价格
                this.totalPrice()
            })
        }else{
            if (goodsNumber > 1){
                wx.$api.mall.decrement(goodsId).then(res => {
                    goodsNew[goodsIndex].number = res.number
                    goodsNew[goodsIndex].total  = res.total
                    this.setData({
                        carArr      : goodsNew
                    })
                    // 获取计算价格
                    this.totalPrice()
                })
            }else{
                wx.showToast({
                    title   : '商品数量不能小于1',
                    icon    : 'none'
                })
            }
        }
    },

    /**
     * 计算价格
     */
    totalPrice() {
        let bagNumber = 0,
            allPrice  = 0,
            goodsList = this.data.carArr

        for (let i of goodsList){
            console.log(goodsList)
            if(i.is_check){
                bagNumber = bagNumber + i.number
                allPrice  = allPrice  + i.total
            }
        }

        this.setData({
            bagNumber : bagNumber,
            allPrice  : allPrice
        })
    },

    /**
     * 菜单
     */
    actionSheet(e){
        let goodIndex   = e.currentTarget.dataset.goods,
            goodRowid   = e.currentTarget.dataset.rowid,
            goodsList   = this.data.carArr,
            allCheckbox = this.data.allCheckbox

        wx.showActionSheet({
            itemList: ['删除'],
            success : res=>{
                if(res.tapIndex == 0){
                    wx.$api.mall.carRemove({ids: goodRowid}).then(res=>{
                        goodsList.splice([goodIndex],1)
                        if (goodsList.length == 0){
                            allCheckbox = false
                        }

                        this.setData({
                            allCheckbox : allCheckbox,
                            carArr      : goodsList
                        })
                        
                        // 获取计算价格
                        this.totalPrice()
                    })
                }
            }
        })
    },

    /**
     * 结算
     */
    bagOrder() {
        let arrCart   = this.data.carArr,
            params    = new Array

        for (var i of arrCart){
            if(i.is_check == true) {
                params.push(i.id)
            }
        }

        let listData  = params.join(",")

        if(wx.getStorageSync("token") == ""){
            wx.navigateTo({
                url: '/pages/login/login'
            })
            return
        }
        wx.navigateTo({
            url: '/pages/mall/mall_order_submit/mall_order_submit?cart_ids=' + listData + '&type=arrCart'
        })
    },
})