Vue.config.devtools = true;
var v = new Vue({
    el:".index-box",
    data:{
        isLogin: 0,
        memToken: '',
        memHeadImg: '',
        userJf:-1,
        id:-1,
        goodInfo:{},
        info:{},
        name: '',
        phone: '',
        address: ''
    },
    created:function (){
        if (!checkWebLogin()) {
            // $('.navlogorebox').trigger('click')
            return false;
        }
        // if(!checkLogin()){
        //     goLogin();
        //     return false;
        // }
        // if($.cookie("memToken")){
        //     this.memToken = $.cookie("memToken");
        //     this.isLogin=1;
        // }
        // if($.cookie("memHeadImg")){
        //     this.memHeadImg = $.cookie("memHeadImg");
        // }
        this.id = sessionStorage.getItem('commodityId');
        // this.getUserInfo();
        this.findGoodDetails();
        // this.findUserIntegral();
    },
    mounted:function (){
        setTimeout(() => {
            $(".loading-item").fadeOut(200);
        },500)
    },
    methods: {
        gotoReceiptInformation(){
            if (this.info.memResidualPoints < this.goodInfo.goodsNeedPoints) {
                layer.msg("抱歉,您的积分不足");
                return;
            }else{
                layer.open({
                    type: 1,
                    area: ['640px', '430px'],
                    skin: 'layui-layer-molv',
                    shift: 0,
                    title:false,
                    closeBtn:0,
                    shadeClose: true,
                    content: $('#shdz')
                });
            }
        },
        getUserInfo (){
            var that = this;
            $.ajax({
                type : 'POST',
                url : serverHttp+"/user/getUserInfo",
                data : {
                    memToken:that.memToken
                },
                success:function (res) {
                    console.log(res);
                    if (res.code == 200) {
                        that.info = res.data;
                    }
                }
            })
        },
        findUserIntegral (){
            var that = this;
            $.ajax({
                type : 'POST',
                url : serverHttp+"/user/findUserIntegral",
                data : {
                    memToken:that.memToken
                },
                success : function(data) {
                    if (data.code == 200) {
                        that.userJf = data.data;
                    }
                },
                error : function(data) {
                }
            });
        },
        findGoodDetails (){
            var that = this;
            $.ajax({
                type : 'POST',
                url : "/user/findGoodDetailsWeb",
                data : {
                    id:that.id
                },
                success:function (res) {
                    if (res.code == 200) {
                        that.goodInfo = res.data;
                        //图片不显示问题
                        if(that.goodInfo.goodsImg){
                            if(that.goodInfo.goodsImg.indexOf('http') < 0){
                                that.goodInfo.goodsImg = 'http://files.qkyxzx.com/' + that.goodInfo.goodsImg;
                            }
                        }
                    }
                }
            })
        },
        gotoCommodityDetail (id){
            window.location.href = serverHttp + "/user/commodityDetail?from=H5&memToken="+this.memToken+"&id="+id;
        },
        saveReceiptInformation(){
            var that = this;
            if (that.name == undefined || that.name === '') {
                layer.msg("请填写姓名")
                return;
            }
            const regex = /^1[3456789]\d{9}$/;//使用此正则即可
            if (!regex.test(that.phone)) {
                layer.msg("请填写正确的手机号")
                return;
            }
            if (that.address == undefined || that.address === '') {
                layer.msg("请填写收货地址")
                return;
            }

            $.ajax({
                type : 'POST',
                url : "/user/saveReceiptInformationWeb",
                data : {
                    id:that.id,
                    name:that.name,
                    phone:that.phone,
                    adress:that.address
                },
                success:function (res) {
                    if (res.code == 200) {
                        layer.closeAll();
                        layer.msg("商品兑换成功")
                    }else if(res.code == 10023){
                        layer.msg(res.message);
                    } else {
                        layer.msg("商品兑换失败");
                    }
                }
            })
        }
    }
})
