var flag = true,
    param = null,
    pStatus = true,
    goodStatus = true,
    colorId='',
    shareOpenid = '';
var yuanjia = '';
mui.ready(function() {
    var nowUrl = location.href;
    var productId = getParam("productId");
    colorId = getParam("colorId");
    param = nowUrl.split("?")[1];
    //springFestivalPop();
    marquee_Show();
    selectProductInfo(productId);
    selectProductImageText(productId);
    mui('body').on('tap', '#add-cart-div', function() {
        if(!pStatus) {
            mui.alert("只有在售商品才可以添加购物车!");
            return false
        }
        if(!goodStatus) {
            mui.alert("该商品暂时缺货,不能购买!");
            return false
        }
        var btnArray = ['否', '是'];
        mui.ajax(base_path + '/cart/addProductToCart?' + param + '&dates=' + new Date().getTime(), {
            dataType: 'json',
            type: 'GET',
            timeout: 15000,
            success: function(dt) {
                if(dt.status) {
                    getCartNum()
                } else {
                    mui.confirm(dt.message + '是否刷新页面？', '提示', btnArray, function(e) {
                        if(e.index == 1) {
                            location.reload(true)
                        }
                    })
                }
            },
            error: function(xhr, type, errorThrown) {
                console.log(xhr)
            }
        })
    });
    mui('body').on('tap', '#click-product-desc', function() {
        if(flag) {
            selectProductImageText(productId);
            flag = false
        }
    });
    mui('body').on('tap', '.video-right', function() {
        mui.openWindow({
            url: base_path + '/indexVideo'
        })
    });
    mui('body').on('tap', '.icon-guanbi', function() {
        document.querySelector('.go-video').style.display = 'none'
    });
    mui('body').on('tap', '.go_gwc', function() {
        mui.openWindow({
            url: base_path + '/wxCart/goCart'
        })
    });
    mui('body').on('tap', '#shopp-div', function() {
        if(!pStatus) {
            mui.alert("该商品已经下架,请重新挑选商品购买!");
            return false
        }
        if(!goodStatus) {
            mui.alert("该商品暂时缺货,不能购买!");
            return false
        }
        var btnArray = ['否', '是'];
        mui.ajax(base_path + '/cart/addProductToCart?' + param + '&dates=' + new Date().getTime(), {
            dataType: 'json',
            type: 'GET',
            timeout: 15000,
            success: function(dt) {
                if(dt.status) {
                    window.location.href = '/watero/pay/settlement?cartId=' + dt.data + "&tiemstamp=" + new Date().getTime()
                } else {
                    mui.confirm(dt.message, '爱贝源-提示', btnArray, function(e) {
                        if(e.index == 1) {}
                    })
                }
            },
            error: function(xhr, type, errorThrown) {
                console.log(xhr)
            }
        })
    });
    var url = "https:" + domanUrl + base_path + "//product/desc?productId=" + productId +'&colorId='+colorId + "&shareOpenid=" + shareOpenid;
    window.repleFlag = false;
    window.wxFriend = {
        "appId": "",
        "imgUrl": domanUrl + '/watero/common/shop/images/index.jpg',
        "link": url,
        "desc": "美国标准，4级净化，5档水温，废水比3:1，水质TDS智能显示。",
        "title": "Watero，美国高端净水专家，4级净化，5档水温，免安装饮水一体机。",
        "netError": "您的网络异常，请刷新重试!如多次刷新无效，请向我们反应 ~",
        "shareTimeSuccess": "分享成功",
        "shareTimeCancel": "已取消分享",
        "shareAppSuccess": "分享成功!",
        "shareAppCancel": "已取消分享"
    };
    getConfig();
    getCartNum();
    document.querySelector('.loading-bg').style.display = 'none'
});

function selectProductInfo(productId) {
    var btnArray = ['否', '是'];
    mui.ajax(base_path + '/product/getProductByProductId?productId=' + productId +'&colorId='+colorId+'&dates=' + new Date().getTime(), {
        dataType: 'json',
        async: false,
        type: 'GET',
        timeout: 15000,
        success: function(dt) {
            if(dt.status) {
                console.log(dt.data.product);
                if(dt.data.product.productId == 28) {
                    goodStatus = false
                }
                shareOpenid = dt.data.shareOpenid;
                var imageNote = "<div class='mui-slider-item mui-slider-item-duplicate'><a href='#'><img src='" + dt.data.product.pictureList[0].pictureUrl + "'></a></div>";
                for(var i = 0; i < dt.data.product.pictureList.length; i += 1) {
                    imageNote += "<div class='mui-slider-item'><a href='#'><img src='" + dt.data.product.pictureList[i].pictureUrl + "'></a></div>";
                    if(i == 0) {
                        $("#active-div").append("<div class='mui-indicator mui-active'></div>")
                    } else {
                        $("#active-div").append("<div class='mui-indicator'></div>")
                    }
                }
                imageNote += "<div class='mui-slider-item mui-slider-item-duplicate'><a href='#'><img src='" + dt.data.product.pictureList[0].pictureUrl + "'></a></div>";
                $("#group-images-div").append(imageNote);
                var banner = mui('#detail-slider');
                banner.slider({
                    interval: 5000
                });
                $("#product-name-font").html(dt.data.product.productName);
                if(marquee != '' && dt.data.product.productDiscount == '328000') {
                    $('<span class="mui-h6">购买即赠3年滤芯</span>').insertAfter('.price-sp')
                }
                $("#product-price-span").html("¥" + accDiv(dt.data.product.productDiscount, 100));
                if(yuanjia == '') {
                    yuanjia = '<s>原价¥' + accDiv(dt.data.product.productPrice, 100) + '</s>'
                }
                $("#product-price-s").html(yuanjia);
                $("#product-access-span").html(dt.data.product.productSalesnum + "人已购买");
                if(dt.data.product.productStatus == 1) {
                    pStatus = true
                } else {
                    pStatus = false
                }
            } else {
                mui.confirm(dt.message + '是否重新加载页面？', '提示', btnArray, function(e) {
                    if(e.index == 1) {
                        location.reload(true)
                    }
                })
            }
        },
        error: function(xhr, type, errorThrown) {
            console.log(xhr)
        }
    })
};

function selectProductImageText(productId) {
    var btnArray = ['否', '是'];
    mui.ajax(base_path + '/product/getPictureByProductId?productId=' + productId, {
        dataType: 'json',
        type: 'POST',
        timeout: 15000,
        success: function(dt) {
            if(dt.status) {
                if(dt.data != null) {
                    $("#product-param-img").html(dt.data.imagetextContext)
                } else {
                    $("#item2").empty();
                    mui.alert('商品详情获取失败')
                }
            } else {
                mui.confirm(dt.message + '是否刷新页面？', '提示', btnArray, function(e) {
                    if(e.index == 1) {
                        location.reload(true)
                    }
                })
            }
        },
        error: function(xhr, type, errorThrown) {
            mui.confirm('您的网络开小差了，是否重新加载页面？', '提示', btnArray, function(e) {
                if(e.index == 1) {
                    location.reload(true)
                }
            })
        }
    })
};
var mask = mui.createMask(function() {});
mui('body').on('tap', '.share_wechat', function() {
    mask.show();
});
$(window).on("scroll", function() {
    if($(window).scrollTop() > 120) {
        $(".return").show()
    } else {
        $(".return").hide()
    }
});