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

const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        goosId      : '',        //商品id
        storyData   : '',        //商品详情
        content     : '',        //商品详情介绍
        storeCoupons: [],        //全场优惠券
        coupons     : [],        //商品优惠券
        couponShow  : false,     //优惠券弹出状态
        specsShow   : false,     //规格弹出状态
        goodsNumber : '1',       //input商品数量
        changeSku   : [],        //规格选择
        shareShow   : false,     //分享弹出
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.setData({
            goosId: options.id
        })
    },

    onShow() {
        // 获取商品详情
        this.storyInfo();
    },

    /**
     * 商品详情
     */
    storyInfo() {
        wx.$api.mall.details(this.data.goosId).then(res => {
            console.log(res)
            this.setData({
                storyData   : res,
                storeCoupons: res.store_coupons,       //全场优惠券数组
                coupons     : res.coupons,             //商品优惠券数组
                skus        : res.skus,                //规格数据
                changeSku   : res.skus.default_attr,   //规格默认
                prices      : res.skus.prices,         //规格所有组合
                content     : res.content.replace(/\<img/gi, '<img style="max-width:100%;height:auto;display:block;"')
            })

        })
    },

    /**
     * 优惠券弹出
     */
    couponTap() {
        this.setData({
            couponShow: !this.data.couponShow
        })
    },

    /**
     * 优惠券关闭
     */
    couponHide() {
        this.setData({
            couponShow: !this.data.couponShow
        })
    },

    /**
     * 规格弹出
     */
    specsTap() {
        this.setData({
            specsShow: !this.data.specsShow
        })
    },

    /**
     * 规格关闭
     */
    specsHide() {
        this.setData({
            specsShow: !this.data.specsShow
        })
    },

    /**
     * 规格选择
     */
    selectSku(e) {
        let name      = e.currentTarget.dataset.name,   //规格名称
            attr      = e.currentTarget.dataset.attr,   //规格标题
            changeSku = this.data.changeSku,            //规格默认
            prices    = this.data.prices                //规格所有组合
        changeSku.attr[name] = attr;

        for (let attr_id in prices) {
            let i = true;
            for (let name in changeSku.attr) {
                if (prices[attr_id][name] != changeSku.attr[name]) {
                    i = false;
                    break;
                }
            }
            if (i) {
                changeSku.sku_id = attr_id;
                changeSku.prices = prices[attr_id];
                break;
            }
        }

        this.setData({
            changeSku: changeSku
        })
    },

    /**
     * 优惠券领取
     */
    drawTap(e) {
        let id = e.currentTarget.dataset.id
        if(wx.getStorageSync("token") == ""){
            wx.navigateTo({
                url: '/pages/login/login'
            })
            return
        }
        
        wx.$api.mall.grant({
            coupon_id: id
        }).then(res => {
            wx.showToast({
                title: res,
                // title: '恭喜你抢到了',
                icon: 'none'
            })
        })
    },

    /**
     * 产品数量
     */
    goodsNumber(e){
        let type     = e.currentTarget.dataset.type,
            atNumber = this.data.goodsNumber

        if(type == "plus"){
            if(atNumber < this.data.changeSku.prices.stock){
                atNumber++
            }else{
                wx.showToast({
                    title: "商品数量不足",
                    icon : "none"
                })
            }
        }else if(type == "remove"){
            if(atNumber > 1){
                atNumber--
            }else{
                wx.showToast({
                    title: "商品数量不能小于1",
                    icon : "none"
                })
            }
        }

        this.setData({
            goodsNumber: atNumber
        })
    },

    /**
     * 输入产品数量
     */
    goodsNumberInput(e){
        let value    = e.detail.value,
            atNumber = this.data.goodsNumber

        if(value > 1){
            if(value <= this.data.changeSku.prices.stock){
                atNumber = value
            }else{
                atNumber = this.data.changeSku.prices.stock
                wx.showToast({
                    title: "商品数量不足",
                    icon : "none"
                })
            }
        }else{
            wx.showToast({
                title: "商品数量不能小于1",
                icon : "none"
            })
        }

        this.setData({
            goodsNumber: atNumber
        })
    },

    /**
     * 加入购物车
     */
    addBag(){
        if(wx.getStorageSync("token") == ""){
            wx.navigateTo({
                url: '/pages/login/login'
            })
            return
        }

        wx.$api.mall.jionCar({
            sku_id  : this.data.changeSku.sku_id,
            goods_id: this.data.goosId,
            number  : this.data.goodsNumber
        }).then(res => {
            this.setData({
                specsShow: !this.data.specsShow
            })
        })
    },

    /**
     * 结算
     */
    buy() {
        if(wx.getStorageSync("token") == ""){
            wx.navigateTo({
                url: '/pages/login/login'
            })
            return
        }

        if(this.data.changeSku.prices.stock != '') {
            if(this.data.changeSku.prices.stock < this.data.goodsNumber) {
                wx.showToast({
                    title   : '抱歉，库存不足',
                    icon    : 'none'
                })
            } else {
                wx.navigateTo({
                    url: '/pages/mall/mall_order_submit/mall_order_submit?skuId=' + this.data.changeSku.sku_id + '&qty=' + this.data.goodsNumber
                })

                this.setData({
                    specsShow: false
                })
            }
        }
    },

    /**
     * 分享
     */
    shareLay(e) {
        if(wx.getStorageSync("token") == ""){
            wx.navigateTo({
                url: '/pages/login/login'
            })
            return
        }

        this.setData({
            shareShow: !this.data.shareShow
        })
    },

    /**
     * 分享-关闭
     */
    shareLayEnd(e) {
        this.setData({
            shareShow: false
        })
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {
        console.log(this.data.storyData.cover)
        this.setData({
            shareShow: false
        })
        return({
            title   : "#" + this.data.storyData.title,
            path    : '/pages/mall/mall_details/mall_details?id=' + this.data.storyData.id + '&nameList=share',
            imageUrl: this.data.storyData.cover
        })

        // + '&share_user_id=' + app.globalData.userInfo.user_id || 0
    },
    
})