var productDetail = new Vue({
    el: "#productDetail",
    data: {
        productId: 0,
        userId: 0,
        userIdentity: '', //2-会员机构；3-供应商；4-普通机构
        number: 0,
        productStock: 0,
        images: [],
        priceObj: {
            actStatus: 0,
            ladderPriceFlag: 0,
            minBuyNumber: 1,
            price: 0,
            costPrice: 0,
            priceFlag: 3,
            productId: 0,
            promotions: '',
            step: 1,
            supplierId: 0,
            userIdentity: 0
        },
        ladderList:[],//阶梯价
        promotions:{},//促销活动信息
        promotionsId:0,//活动id
        addStatus: true,
        recommendType: 0,//相关推荐类型 0自动选择; 1手动推荐
        recommendPage: 1,
        recommends: [],
        parameters: []
    },
    computed: {},
    methods: {
        getImages: function(){
            var _self = this;
            if(!this.productId){return;}
            $.getJSON(spiServer + "/product/detail/images",{productId: this.productId}).done(function (r) {
                if (r.code === 0 && r.data) {
                    _self.images = r.data;
                    setTimeout(function(){
                        if (isPC) {
                            var magnifier = new ImageMagnifier(
                                '#imgShown .smallImage li'
                                ,'#imgShown .bigImage'
                                ,'#imgShown .zoomImage'
                                ,'#imgShown .mask'
                                ,'#imgShown .zoom'
                                ,"on"
                            ).init();
                        } else {
                            var swiper = new Swiper('#swiperImage', {
                                loop : true,
                                autoplay: {
                                    delay: 2000,
                                    disableOnInteraction: false
                                },
                                pagination: {
                                    el: '.swiper-pagination',
                                    type: 'fraction'
                                }
                            });
                        }
                    },500);
                }
            });
        },
        getProductPrice:function(){
            var _self = this;
            if(_self.productId ===0){return;}
            $.getJSON(spiServer + "/product/detail/price",{
                userId: GLOBAL_USER_ID,
                productId: _self.productId
            }).done(function (r) {
                if (r.code === 0 && r.data) {
                    _self.priceObj = r.data;
                    _self.number = r.data.minBuyNumber;
                    if(r.data.ladderPriceFlag===1){
                        _self.ladderPrice();
                    }
                    if(r.data.actStatus==1){
                        _self.promotions =  r.data.promotions;
                        _self.promotionsId = r.data.promotions.id;
                    }
                }
            });
        },
        ladderPrice:function(){
            var _self = this;
            if(!this.productId){return;}
            $.getJSON(spiServer + "/product/ladderPrice",{
                productId: this.productId
            }).done(function (r) {
                if (r.code === 0 && r.data) {
                    _self.ladderList =r.data;
                    this.numberResize();
                }
            });
        },
        getParameters: function(){
            var _self = this;
            if(!this.productId){return;}
            $.getJSON(spiServer + "/product/detail/parameter",{
                productId: this.productId
            }).done(function (r) {
                if (r.code === 0 && r.data) {
                    _self.parameters = r.data;
                }
            });
        },
        toggleThisLadder: function(event){
            var el = event.currentTarget;
            if($(el).hasClass("on")){
                $(el).removeClass("on").siblings('.mFixed').hide();
                if(!isPC){looseBody();}
            }else{
                $(el).addClass("on").siblings('.mFixed').show();
                if(!isPC){fixedBody();}
            }
        },
        hideThisLadder: function(event){
            var el = event.currentTarget;
            $(el).parents('.priceTag').find('.tag').removeClass("on").siblings('.mFixed').hide();
            if(!isPC){looseBody();}
        },
        numberSub: function(){
            this.number -= this.priceObj.step;
            this.numberResize();
        },
        numberAdd: function(){
            this.number += this.priceObj.step;
            this.numberResize();

        },
        numberChange: function(){
            this.number = Math.ceil(this.number/this.priceObj.step)*this.priceObj.step;
            this.numberResize();

        },
        numberResize: function(){
            var _self = this;
            if (this.number<this.priceObj.minBuyNumber){
                this.number = this.priceObj.minBuyNumber;
            }
            if (this.number>this.productStock){
                this.number = this.productStock;
            }
            if(this.priceObj.ladderPriceFlag){
                this.ladderList.forEach.forEach(function(ladder){
                    if(_self.number>=ladder.buyNum){
                        _self.priceObj.price = ladder.buyPrice;
                    }
                });
            }
        },
        toLogin: function() {
            location.href ='/login.html';
        },
		addShopCart: function(){ //加入购物车
		    var _self = this;
		    if(this.addStatus){
		        _self.addStatus = false;
                addShoppingCart(this.userId, this.productId, this.number,function(){
                    _self.addStatus = true;
                });
		    }
		},
        buyNowSubmit: function(){
            if(this.productId && this.number){
                // type:（1购物车提交[对应表cm_cart]，2直接购买提交, 3协销下单）
                window.location.href = '/shopping/confirm.html?type=2&id='+this.productId+'&count='+this.number;
            }
        },
        getRecommends: function(){
            var _self = this;
            if(!this.productId){return;}
            $.getJSON(spiServer + "/product/detail/recommend",{
                productId: this.productId,
                recommendType: this.recommendType
            }).done(function (r) {
                if (r.code === 0 && r.data) {
                    if(r.data.length > 0){
                        _self.recommends = r.data;
                        _self.recommendPage = isPC ? Math.ceil(r.data.length / 7) : Math.ceil(r.data.length / 3);
                        setTimeout(function(){
                            if (isPC) {
                                $('#productRecommend').slide({
                                    mainCell:".swiper-wrapper"
                                    ,titCell:".swiper-pagination span"
                                    ,effect: "leftLoop"
                                    ,interTime: 3000
                                    ,autoPlay: true
                                    ,scroll:7
                                    ,vis:7
                                    ,trigger: "mouseover"
                                });
                            } else {
                                var swiper = new Swiper('#productRecommend', {
                                    slidesPerView: 3,
                                    spaceBetween: 0,
                                    autoplay: {
                                        delay: 3000,
                                        disableOnInteraction: false
                                    },
                                    pagination: {
                                        el: '.swiper-pagination'
                                    }
                                });
                            }
                        },500);
                    }
                }
            });
        }
    },
    created: function () {
        this.productId = $("#productId").val();
        this.productStock = $("#productStock").val();
        this.getImages();
    },
    mounted: function () {
        this.userId = GLOBAL_USER_ID;
        this.userIdentity = GLOBAL_USER_IDENTITY;
        this.getProductPrice();
        this.getParameters();
        this.getRecommends();
        $('.productInfo').slide({
            mainCell:".tabCon"
            ,titCell:".tabTit span"
            ,trigger: "click"
        });
    }
});
