var WxParse = null;
var app = getApp()
var viewId=0
var ttype=0
var ttype0=0
var yunfeis=[]
var selectedguige=""
var selectedguigeid=""  
var sopenFrame=null 
var sopenFrame0=null
var that;
var updateing=0;
var poid=0
Page({
    data: {
        indicatorDots: true,
        indicatorDots0: false,
        vertical: false,
        vertical0: true,
        autoplay: true,
        interval: 6000,
        duration: 1200, 
        goodsPicsInfo:[],
        spell_list:[],
        title:"",
        oldpricetitle:"",
        oldprice:"0.00",
        currentprice:"0.00",
        currentprice0:"0.00",
        currenttitle:"",
        yjptnum:0,
        body:"",
        qybtnHidden:true,
        serverHidden:true,
        servermsg:"",
        favorite:"收藏",
        addcartHidden:true,
        guigesHidden:false,
        colorHidden:true,
        selguige_font:"请选择：",
        selguige_title:"规格或颜色",
        selguiges:[],
        currentindex:0,
        minprice:0.00,
        showindex:0,
        showimage:"",
        showimages:"",
        sharepic:"",
        cartmsgHidden:true,
        qyaddcart:"确认",
        cartcount:0,
        ttype:0,
        addtype:0,
        tguid:0,
        IFPD:0,
        IFPD0:0,
        IFDJPD:0,
        yydptgz:0,
        yqpdname:"",
        dowloadpicHidden:false,
        authorizeHidden0:true,
        onauthorizetitle0:"获取微信手机号",
        getcode0:"获取验证码",
        savebutton0:"确认绑定",
        ptgzHidden:true,
        ptgzHidden0:true,
        ptgzbutton:"确认并下单",
        ptgzbutton0:"我已阅读",
        ptgztitle:"",
        ptgzbody:"",
        ptgztitle0:"",
        ptgzbody0:"",
        sharetitle:"",
        checked:0,
        vipqx:0,
        loadingHidden:false
    }, 

    onLoad: function(options) {
        that = this
        WxParse = require('../../wxParse/wxParse.js');
        viewId=options.id;
       
        if(options.poid){
                that.setData({
                    IFPD:options.poid
                })
                
        }
        
        
        that.rdata(0)
    },onPullDownRefresh: function () {
        //在当前页面显示导航条加载动画
         wx.showNavigationBarLoading(); 
        //显示 loading 提示框。需主动调用 wx.hideLoading 才能关闭提示框
        wx.showLoading({
            title: '刷新中...',
        })
        that.rdata(1)
    },
    rdata:function(t) {
        var servermsg = wx.getStorageSync("servermsg")
        if(servermsg){
             WxParse.wxParse('servermsg', 'html', servermsg, that)
        }
       
        var userId = wx.getStorageSync("userId")
        var LoginId = wx.getStorageSync("LoginId")
        var tguid0 = wx.getStorageSync("tguid0")
        var CartId = wx.getStorageSync("CartId")
        if(!userId||!LoginId){
            wx.hideShareMenu()
        }else{
            wx.showShareMenu()
        }
        wx.request({
          url: app.globalData.apiurl + "/xapiajax.ashx", 
          data: {   
            action:"getshopview",
             userId:(!userId?"":userId),
             LoginId:(!LoginId?"":LoginId),
             CartId:(!CartId?"":CartId),
             tguid:(!tguid0?"":tguid0),
             poid:that.data.IFPD,
             viewId:viewId 
          },
          method:"GET",
          dataType:"json", 
          header: { 
            "content-type": "application/json" // 默认值
          },
          success (res) {  
            if(res.data.status==1){
                yunfeis=res.data.yunfeis;
               
                that.setData({
                    goodsPicsInfo: res.data.pic,
                    selguiges:res.data.guige,
                    spell_list:res.data.spell_list,
                    title: res.data.name,         
                    oldprice:res.data.scje,
                    showindex:0,
                    showimage:res.data.showpic,
                    showimages: res.data.pic,
                    ttype:res.data.ttype,
                    ttype0:res.data.ttype0,
                    cartcount:res.data.cartcount,
                    tguid:res.data.tguid, 
                    IFPD0:res.data.IFPD, 
                    ptgztitle:res.data.ptgztitle,
                    ptgztitle0:res.data.ptgztitle0,
                    sharetitle:res.data.sharetitle,
                    sharepic:res.data.sharepic,
                    yjptnum:res.data.yjptnum
                })
                that.getselguige();
                if(that.data.IFPD==0){
                       that.setData({
                         yqpdname:"请点击下方按钮参与拼单"
                       })
                }else{
                   that.setData({
                    yqpdname:res.data.yqpdname + "邀请您参与拼单，点击下方按钮参与"
                    })
                 }
                if(res.data.IFfavorite>0){
                    that.setData({
                       favorite: "已收藏"
                    })
                }
                if(res.data.minprice==res.data.maxprice){
                    that.setData({
                        minprice: (ttype==0?"":"￥")+res.data.maxprice
                     })
                }else{  
                    that.setData({
                        minprice: (ttype==0?"":"￥")+res.data.minprice + "-" +res.data.maxprice
                     })
                }
                ttype=res.data.ttype
                that.setData({
                  oldpricetitle:"零售价：",
                  currenttitle:"会员价",
                  currentprice:res.data.hyje,
                  currentprice0:res.data.hyje0
               })
                    if(ttype==0){
                         
                    }else if(ttype==1){
                        
                    }else if(ttype==2){
                        
                    }else if(ttype==5){
                        
                    }else if(ttype==6){
                        
                    }else if(ttype==7){
                        
                    }else if(ttype==8){
                        
                    }
                    
                    that.setRemainTimeto();
                    clearInterval(sopenFrame0);
                    sopenFrame0=setInterval(function (){
                         that.setRemainTimeto();
                    }, 1000);

                var body = res.data.body;
                WxParse.wxParse('body', 'html', body, that);
                body = res.data.ptgzbody;
                WxParse.wxParse('ptgzbody', 'html', body, that);
                body = res.data.ptgzbody0;
                WxParse.wxParse('ptgzbody0', 'html', body, that);
                var zkc=0;
                if(res.data.guige.length>0){
                   
                    for(var i=0;i<res.data.guige.length;i++){
                        var kc=parseInt(res.data.guige[i].kc);
                        if(kc>0){
                            zkc+=kc; 
                        }
                    
                    }
                    that.setData({
                        guigesHidden:false
                    })
                }else{
                    that.setData({
                         guigesHidden:true
                    })
                }
                
                that.setData({
                    loadingHidden:true
                })
                if(t==1){
                    //隐藏loading 提示框
                    wx.hideLoading();
                    //隐藏导航条加载动画
                    wx.hideNavigationBarLoading();
                    //停止下拉刷新
                    wx.stopPullDownRefresh();
               } 
              }else{
                wx.showToast({
                    title: res.data.msg,
                    icon: "none",
                    duration: 2000
                  })
              }
           }
        })

     
    },setRemainTimeto:function() {
     
        var spell_list=that.data.spell_list;
        for(var i=0;i<spell_list.length;i++){
          var SJC=spell_list[i].SJC;
             var SysSecond=parseInt(SJC);
             if (SysSecond > 0) {
                 SysSecond = SysSecond - 1;
                 var second = Math.floor(SysSecond % 60).toString();
                 var minite = Math.floor((SysSecond / 60) % 60).toString();
                 var hour = Math.floor((SysSecond / 3600) % 24).toString();
                 var day = Math.floor((SysSecond / (3600*24))).toString();
                 var timesto="";
                 if(day>0){
                  timesto=day + "天 ";
                 }
                 if(hour>0){
                  timesto+=hour + "时 ";
                 }
                 if(minite>0){
                  timesto+=minite + "分 ";
                 }
                 if(second>0){
                  timesto+=second + "秒 ";
                 }
                 spell_list[i].SJC=SysSecond;
                 spell_list[i].times=timesto;
            }
          
        }
        that.setData({
            spell_list: spell_list
         })
     },
    onShareAppMessage: function (res) {
      /*const promise = new Promise(resolve => {
        setTimeout(() => {
          resolve({
            title: "[" + that.data.currentprice + "]" + (that.data.sharetitle==""?that.data.title:that.data.sharetitle)
          })
        }, 2000)
      })
      return {
        title: "[" + that.data.currentprice + "]" + (that.data.sharetitle==""?that.data.title:that.data.sharetitle) ,
        path: "/pages/index/index?viewid=" + viewId + (that.data.tguid>0?"&tguid="+that.data.tguid:"") + (that.data.IFPD0>0?"&poid="+that.data.IFPD0:""),
        promise 
      }*/
          return {
              title: "[" + that.data.currentprice + "]" + (that.data.sharetitle==""?that.data.title:that.data.sharetitle) ,
              imageUrl: that.data.sharepic,
              path: "/pages/index/index?viewid=" + viewId + (that.data.tguid>0?"&tguid="+that.data.tguid:"") + (that.data.IFPD0>0?"&poid="+that.data.IFPD0:"")
           }
       
    },
    showserver:function(e) {
      wx.navigateTo({  
        url: "../mykf/index"
    })
       /* that.setData({
            serverHidden: false
        })*/
     
    },
    closeserver:function(e) {
        that.setData({
            serverHidden: true
        })
    },inserver:function(e) {
   
    },closeIFPD:function() {
        that.setData({
            IFPD: 0
        })
    },showcolor:function(e) {
        var index = e.currentTarget.dataset.id;
        that.setData({
            currentindex:index,
            colorHidden: false
        })
     
    },
    closecolor:function(e) {
        that.setData({
            colorHidden: true
        })
    },incolor:function(e) {
   
    },closeptgz:function(e) {
        that.setData({
            ptgzHidden: true
        })
    },yydptgz:function(e) {
        if(that.data.checked==1){
        that.setData({
            ptgzHidden: true,
            yydptgz:1,
        })
        if(that.data.IFDJPD==1){
            that.showpd();
        }else{
            that.showaddcart();
        }
      }else{
          wx.showToast({
            title: "请点击我已阅读并同意",
            icon: "none",
            duration: 2000
          })  
      }
    },onchecked:function(e) {
        if(that.data.checked==0){
            that.setData({
               checked:1
            })
        }else{
            that.setData({
                checked:0
             })
        }
    },openptgz:function(e) {
        that.setData({
            ptgzHidden0: false
        })
    },closeptgz0:function(e) {
        that.setData({
            ptgzHidden0: true
        })
    },yydptgz0:function(e) {
        that.setData({
            ptgzHidden0: true
        })
    },
    addfavorite:function(e) {
        var userId = wx.getStorageSync("userId")
        var LoginId = wx.getStorageSync("LoginId")
        if(userId&&LoginId){
        wx.request({
          url: app.globalData.apiurl + "/xapiajax.ashx", 
          data: { 
            action:"addfavorite",
             userId:(!userId?"":userId),
             LoginId:(!LoginId?"":LoginId),
             viewId:viewId 
          },
          method:"GET",
          dataType:"json", 
          header: { 
            "content-type": "application/json" // 默认值
          },
          success (res) { 
              if(res.data.status==1){
                  that.setData({
                     favorite: "已收藏"
                  })
                  wx.showToast({
                    title: res.data.msg,
                    icon: "success",
                    duration: 2000
                  })
              }else{
                 wx.showToast({
                    title: res.data.msg,
                    icon: "none",
                    duration: 2000
                  })
              }
              
          }
        })
    }else{
        wx.navigateTo({  
            url: "../login/index"
        })   
     }
    },
    onpd:function(e) {
       
        that.setData({
            IFDJPD:1,
            addtype:1
        })
        poid=e.currentTarget.dataset.id
        that.showpd();
    },
    showpd:function() {
        var userId = wx.getStorageSync("userId")
        var LoginId = wx.getStorageSync("LoginId")
        var userInfodata = wx.getStorageSync("userInfo")
        var ifdl=true;
        if(!userId||!LoginId){
              ifdl=false;
              wx.navigateTo({  
                url: "../login/index"
               })
        }else{
  
            var userInfo = JSON.parse(userInfodata);
             if(userInfo.Phone==""&&userInfo.FHEnable4=="0"){
                ifdl=false;
                wx.showToast({
                    title: "您还没有绑定手机号",
                    icon: "none",
                    duration: 2000
                })
                wx.navigateTo({  
                    url: "../myphone/index"
                })
            }else{
                if(that.data.yydptgz==0){
                    var IFYYD = wx.getStorageSync("IFYYD");
                    if(!IFYYD){
                       ifdl=false;
                       that.setData({
                            ptgzHidden: false
                       })
                       wx.setStorageSync("IFYYD",1);
                    }
                }
            }
        }
        if(ifdl){
            that.setData({
                addcartHidden: false
            })
            that.data.selguiges[0].selectednum=1;
            that.setData({
                selguiges:that.data.selguiges
            })
            that.getselguige();
        }
    },
    addcart:function(e) {
        var t = e.currentTarget.dataset.id
        that.setData({
            IFDJPD:0,
            addtype:t
        })
        if(that.data.ttype==6){
            var aguige = that.data.selguiges;
            for(var i=0;i<aguige.length;i++){
                 aguige[i].selectednum=0;
            } 
            that.setData({
                selguiges:that.data.selguiges
            })
            
        }
        if(t==0){
            poid=0
        }else{
           if(that.data.ttype==6&&that.data.IFPD>0){
                poid=that.data.IFPD
           }else{
                poid=0
           }
        }
        that.showaddcart();
        
    },
    showaddcart:function() {
        var userId = wx.getStorageSync("userId")
        var LoginId = wx.getStorageSync("LoginId")
        var userInfodata = wx.getStorageSync("userInfo")
        var ifdl=true;
       
        if(!userId||!LoginId){
              ifdl=false;
              wx.navigateTo({  
                url: "../login/index"
               })
        }else{
                var userInfo = JSON.parse(userInfodata);
                 if(userInfo.Phone==""&&userInfo.FHEnable4=="0"){
                    ifdl=false;
                    wx.showToast({
                        title: "您还没有绑定手机号",
                        icon: "none",
                        duration: 2000
                    })
                    that.setData({
                        authorizeHidden0: false
                    })
                }else{
                    if(that.data.ttype==6&&that.data.addtype==1){
                       var IFYYD = wx.getStorageSync("IFYYD");
                       if(that.data.yydptgz==0 && !IFYYD){
                         ifdl=false;
                          that.setData({
                              ptgzHidden: false
                          })
                          wx.setStorageSync("IFYYD",1);
                       }
                    }
                } 
        } 
        if(ifdl){
            that.setData({
                addcartHidden: false
            })
            if(that.data.ttype==6&&that.data.addtype==1){
               that.data.selguiges[0].selectednum=1;
               that.setData({
                     selguiges:that.data.selguiges
               })
               that.getselguige();
            }
        }
    },
    closeaddcart:function(e) {
        that.setData({
            addcartHidden: true
        })
    },inaddcart:function(e) {
   
    },viewimages:function(e) {
        var showimages=that.data.showimages; 
        var imgList=new Array(); 
        for(var i=0;i<showimages.length;i++){
            imgList[i]=showimages[i].img;
        }
        wx.previewImage({
            urls: imgList,
            current:imgList[that.data.showindex] 
        })
    },viewimages0:function(e) {
        var index = e.currentTarget.dataset.index;
        var showimages=that.data.showimages; 
        var imgList=new Array(); 
        for(var i=0;i<showimages.length;i++){
            imgList[i]=showimages[i].img;
        }
        wx.previewImage({
            urls: imgList,
            current:imgList[index] 
        })
    },
    selcolor:function(e) {
        var datavalue = e.currentTarget.dataset.id;
        var selectedcolor=that.data.selguiges[that.data.currentindex].colors[datavalue].color
        that.data.selguiges[that.data.currentindex].selectedcolor=selectedcolor;
        var strimages=that.data.selguiges[that.data.currentindex].images;
        var showimage="";
        var showindex=0;  
        if(strimages!=""){
            var aimages=strimages.split('|');
             var strshowimages="[";
             for(var i=0;i<aimages.length-1;i++){
                   if(aimages[i]!=""){
                      var images = aimages[i].split(',');
                      var image0=app.globalData.apiurl + "/GetFiles.ashx?image=" + images[0] + "&itsize=720x460&itmode=cut";
                                var image1=app.globalData.apiurl + "/GetFiles.ashx?image=" + images[0] + "&itsize=150x150&itmode=cut";
                                if(strshowimages=="["){
                                 strshowimages+="{\"img\":\""+image0+"\",\"minimg\":\""+image1+"\",\"label\":\"\",\"yanse\":\""+(images.length>1?images[1]:"")+"\"}";
                               }else{
                                 strshowimages+=",{\"img\":\""+image0+"\",\"minimg\":\""+image1+"\",\"label\":\"\",\"yanse\":\""+(images.length>1?images[1]:"")+"\"}";
                               }
                   }
             }
             strshowimages+="]";
             var showimages=JSON.parse(strshowimages);
            for(var j=0; j< showimages.length;j++){
                 if (showimages[j].yanse == selectedcolor){  
                      showimage= showimages[j].minimg;
                      showindex=j;
                 }
            } 
            if(showimage==""){
                showimage= showimages[0].minimg;
                showindex=0;
            }
            that.setData({
                showimage:showimage,
                showimages:showimages,
                showindex:showindex,
            })  
       }
        that.setData({
            selguiges:that.data.selguiges,     
            colorHidden: true
        }) 
       
        that.getselguige();
    },increment:function(e) {
        var index = e.currentTarget.dataset.id;
        var kc=that.data.selguiges[index].kc
        var num=that.data.selguiges[index].selectednum
        var c = /^[0-9]*$/;
        if (!c.test(num)) {
            wx.showToast({
                title: "输入的数量有误",
                icon: "none",
                duration: 2000
            })
        }else{
           kc = parseInt(kc);
           num=parseInt(num);
           var b = num+1;
           if (b > kc) {
               wx.showToast({
                   title: "库存不足",
                   icon: "none",
                   duration: 2000
                })
                b=num
            }
            
            that.data.selguiges[index].selectednum=b;
            that.setData({
                selguiges:that.data.selguiges
             })
             var strshowimages="";
             if(that.data.selguiges[index].images!=""){
             var aimages=that.data.selguiges[index].images.split('|');
             strshowimages="[";
             for(var i=0;i<aimages.length-1;i++){
                   if(aimages[i]!=""){
                      var images = aimages[i].split(',');
                      var image0=app.globalData.apiurl + "/GetFiles.ashx?image=" + images[0] + "&itsize=720x460&itmode=cut";
                                var image1=app.globalData.apiurl + "/GetFiles.ashx?image=" + images[0] + "&itsize=150x150&itmode=cut";
                                if(strshowimages=="["){
                                 strshowimages+="{\"img\":\""+image0+"\",\"minimg\":\""+image1+"\",\"label\":\"\",\"yanse\":\""+(images.length>1?images[1]:"")+"\"}";
                               }else{
                                 strshowimages+=",{\"img\":\""+image0+"\",\"minimg\":\""+image1+"\",\"label\":\"\",\"yanse\":\""+(images.length>1?images[1]:"")+"\"}";
                               }
                   }
             }
             strshowimages+="]";
             }
           
             var showimages;
             if(strshowimages!=""&&strshowimages!="[]"){
                showimages=JSON.parse(strshowimages);
             }
             else{
                showimages=that.data.goodsPicsInfo;
             }
             var strimages="";
             if(showimages.length>0){
                 strimages=showimages[0].minimg;
             }
             that.setData({
                showimage:strimages,
                showindex:0,
                showimages:showimages
             })
            
        }
        that.getselguige();
    },inputOnlyNum:function(e) {
        var num=e.detail.value;
        var index = e.currentTarget.dataset.id;
        var kc=that.data.selguiges[index].kc
        var c = /^[0-9]*$/;
        if (!c.test(num)) {
            wx.showToast({
                title: "输入的数量有误",
                icon: "none",
                duration: 2000
            })
        }else{
           kc = parseInt(kc);
           num=parseInt(num);
           var b = num;
           if(that.data.ttype==6 && that.data.addtype==1){
            var aguige = that.data.selguiges;
            var IFNum=0;
            for(var i=0;i<aguige.length;i++){
                if(i!=index){
                 var num = parseInt(aguige[i].selectednum);
                 if(num > 0){
                    IFNum+=1;
                 }
                }
            }
            if (IFNum > 0) {
                wx.showToast({
                    title: "拼单只能选择一种规格",
                    icon: "none",
                    duration: 2000
                 })
                 b=0
            }
            if (b > 1) {
                wx.showToast({
                    title: "拼单不能大于0",
                    icon: "none",
                    duration: 2000
                 })
                 b=1
             }
        }
           if (b > kc) {
            wx.showToast({
                title: "库存不足",
                icon: "none",
                duration: 2000
             })
             b=num
         }
           if (b < 0) {
               wx.showToast({
                   title: "不能小于0",
                   icon: "none",
                   duration: 2000
                })
                b=num
            }
            that.data.selguiges[index].selectednum=b;
            that.setData({
                selguiges:that.data.selguiges
             })
             if(b>0){
                  if(that.data.selguiges[index].images!=""){
                    var aimages=that.data.selguiges[index].images.split('|');
                    var strshowimages="[";
                    for(var i=0;i<aimages.length-1;i++){
                          if(aimages[i]!=""){
                            var images = aimages[i].split(',');
                            var image0=app.globalData.apiurl + "/GetFiles.ashx?image=" + images[0] + "&itsize=720x460&itmode=cut";
                                var image1=app.globalData.apiurl + "/GetFiles.ashx?image=" + images[0] + "&itsize=150x150&itmode=cut";
                                if(strshowimages=="["){
                                 strshowimages+="{\"img\":\""+image0+"\",\"minimg\":\""+image1+"\",\"label\":\"\",\"yanse\":\""+(images.length>1?images[1]:"")+"\"}";
                               }else{
                                 strshowimages+=",{\"img\":\""+image0+"\",\"minimg\":\""+image1+"\",\"label\":\"\",\"yanse\":\""+(images.length>1?images[1]:"")+"\"}";
                               }
                          }
                    }
                    strshowimages+="]";
                    var showimages=JSON.parse(strshowimages);
                    var strimages=showimages[0].minimg;
                    that.setData({
                       showimage:strimages,
                       showindex:0,
                       showimages:showimages
                    })
                   }
             }else{
               
                 var strimages="";
                 var showimages=[];
                 var aguige = that.data.selguiges;
                 for(var i=0;i<aguige.length;i++){
                      var num = parseInt(aguige[i].selectednum);
                      if(num > 0){
                        var aimages=that.data.selguiges[index].images.split('|');
                        var strshowimages="[";
                        for(var i=0;i<aimages.length-1;i++){
                              if(aimages[i]!=""){
                                var images = aimages[i].split(',');
                                var image0=app.globalData.apiurl + "/GetFiles.ashx?image=" + images[0] + "&itsize=720x460&itmode=cut";
                                var image1=app.globalData.apiurl + "/GetFiles.ashx?image=" + images[0] + "&itsize=150x150&itmode=cut";
                                if(strshowimages=="["){
                                 strshowimages+="{\"img\":\""+image0+"\",\"minimg\":\""+image1+"\",\"label\":\"\",\"yanse\":\""+(images.length>1?images[1]:"")+"\"}";
                               }else{
                                 strshowimages+=",{\"img\":\""+image0+"\",\"minimg\":\""+image1+"\",\"label\":\"\",\"yanse\":\""+(images.length>1?images[1]:"")+"\"}";
                               }
                              }
                        }
                        strshowimages+="]";
                        showimages=JSON.parse(strshowimages);
                        strimages=showimages[0].minimg;
                      }
                  }
                  if(strimages==""){
                    showimages=that.data.goodsPicsInfo;
                    strimages=showimages[0].minimg;
                  }
                  that.setData({
                    showimage:strimages,
                    showindex:0,
                    showimages:showimages
                  })
              
             }
        }
        that.getselguige();
    },decrement:function(e) {
        var index = e.currentTarget.dataset.id;
        var kc=that.data.selguiges[index].kc
        var num=that.data.selguiges[index].selectednum
        var c = /^[0-9]*$/;
        if (!c.test(num)) {
            wx.showToast({
                title: "输入的数量有误",
                icon: "none",
                duration: 2000
            })
        }else{
           kc = parseInt(kc);
           num=parseInt(num);
           var b = num-1;
           if (b < 0) {
               wx.showToast({
                   title: "不能小于0",
                   icon: "none",
                   duration: 2000
                })
                b=num
            }
            that.data.selguiges[index].selectednum=b;
            that.setData({
                selguiges:that.data.selguiges
             })
             if(b>0){
                if(that.data.selguiges[index].images!=""){
                  var aimages=that.data.selguiges[index].images.split('|');
                  var strshowimages="[";
                  for(var i=0;i<aimages.length-1;i++){
                        if(aimages[i]!=""){
                            var images = aimages[i].split(',');
                            var image0=app.globalData.apiurl + "/GetFiles.ashx?image=" + images[0] + "&itsize=720x460&itmode=cut";
                                var image1=app.globalData.apiurl + "/GetFiles.ashx?image=" + images[0] + "&itsize=150x150&itmode=cut";
                                if(strshowimages=="["){
                                 strshowimages+="{\"img\":\""+image0+"\",\"minimg\":\""+image1+"\",\"label\":\"\",\"yanse\":\""+(images.length>1?images[1]:"")+"\"}";
                               }else{
                                 strshowimages+=",{\"img\":\""+image0+"\",\"minimg\":\""+image1+"\",\"label\":\"\",\"yanse\":\""+(images.length>1?images[1]:"")+"\"}";
                               }
                        }
                  }
                  strshowimages+="]";
                  var showimages=JSON.parse(strshowimages);
                  var strimages=showimages[0].minimg;
                  that.setData({
                     showimage:strimages,
                     showindex:0,
                     showimages:showimages
                  })
                 }
           }else{
             
               var strimages="";
               var showimages=[];
               var aguige = that.data.selguiges;
               for(var i=0;i<aguige.length;i++){
                    var num = parseInt(aguige[i].selectednum);
                    if(num > 0){
                      var aimages=that.data.selguiges[index].images.split('|');
                      var strshowimages="[";
                      for(var i=0;i<aimages.length-1;i++){
                            if(aimages[i]!=""){
                                var images = aimages[i].split(',');
                                var image0=app.globalData.apiurl + "/GetFiles.ashx?image=" + images[0] + "&itsize=720x460&itmode=cut";
                                var image1=app.globalData.apiurl + "/GetFiles.ashx?image=" + images[0] + "&itsize=150x150&itmode=cut";
                                if(strshowimages=="["){
                                 strshowimages+="{\"img\":\""+image0+"\",\"minimg\":\""+image1+"\",\"label\":\"\",\"yanse\":\""+(images.length>1?images[1]:"")+"\"}";
                               }else{
                                 strshowimages+=",{\"img\":\""+image0+"\",\"minimg\":\""+image1+"\",\"label\":\"\",\"yanse\":\""+(images.length>1?images[1]:"")+"\"}";
                               }
                            }
                      }
                      strshowimages+="]";
                      showimages=JSON.parse(strshowimages);
                      strimages=showimages[0].minimg;
                    }
                }
                if(strimages==""){
                  showimages=that.data.goodsPicsInfo;
                  strimages=showimages[0].minimg;
                }
                that.setData({
                  showimage:strimages, 
                  showindex:0,
                  showimages:showimages
                })
            
           }
        }
        that.getselguige();
    },getselguige:function(e) {
        var guiges=""
        var aguige = that.data.selguiges;
        for(var i=0;i<aguige.length;i++){
              var color = aguige[i].selectedcolor;
              var num = parseInt(aguige[i].selectednum);
              if(num > 0){
                      if(guiges==""){
                           guiges=(aguige[i].gg + color + "X"+ num+"单");
                      }else{
                           guiges+=("\n" + aguige[i].gg + color + "X"+ num+"单");
                      }
                      
             }
        }
        if(guiges==""){
            that.data.selguige_font="请选择：";
            that.data.selguige_title="规格或颜色";
        }else{
            that.data.selguige_font="已选择：";
            that.data.selguige_title=guiges;
        }
        that.setData({
            selguige_font:that.data.selguige_font,
            selguige_title:that.data.selguige_title
         })
    },
    qyaddcart:function(e) {
        var userId = wx.getStorageSync("userId")
        var LoginId = wx.getStorageSync("LoginId")
        var userInfodata = wx.getStorageSync("userInfo")
        var bb=true;
        if(!userId||!LoginId){
              ifdl=false;
              wx.navigateTo({  
                url: "../login/index"
               })
        }else{
           /* var userInfo = JSON.parse(userInfodata);
             if(userInfo.Phone==""&&userInfo.FHEnable4=="0"){
                bb=false;
                wx.showToast({
                    title: "您还没有绑定手机号",
                    icon: "none",
                    duration: 2000
                })
                that.setData({
                    authorizeHidden0: false
                })
            }*/
        }
        if(bb){
        var guiges="";
        
            var aguige = that.data.selguiges;
            for(var i=0;i<aguige.length;i++){
                  var color = aguige[i].selectedcolor;
                  var num = parseInt(aguige[i].selectednum);
                  if(num > 0){
                      if(aguige[i].colors.length>0){
                           if(color==""){
                               wx.showToast({
                                  title: aguige[i].gg+"的颜色必须选择",
                                  icon: "none",
                                  duration: 3000
                               })
                               bb = false;
                           }
                      }
                      if(bb){
                          if(guiges==""){
                               guiges=(aguige[i].id + "|" + color + "|" + num);
                          }else{
                               guiges+=(","+aguige[i].id + "|" + color + "|" + num);
                          }
                      }
                  }
            }
           
        }
        if(bb){
            if(guiges==""){
               wx.showToast({
                title: "请填写购买数量",
                icon: "none",
                duration: 3000
               })
               bb = false;
            }
        }
        if(bb){
            if(that.data.qyaddcart=="请稍候..."){
                wx.showToast({
                    title: "请不要重复点击",
                    icon: "none",
                    duration: 3000
                })
                bb = false;
            }
        }
        if(bb){
            var userId = wx.getStorageSync("userId")
            var LoginId = wx.getStorageSync("LoginId")
            var CartId = wx.getStorageSync("CartId")
            that.setData({
                qyaddcart:"请稍候..."
            })
            wx.request({
              url: app.globalData.apiurl + "/xapiajax.ashx", 
              data: { 
                 action:"addcart",
                 userId:(!userId?"":userId),
                 LoginId:(!LoginId?"":LoginId),
                 CartId:(!CartId?"":CartId),
                 guiges:guiges,
                 poid:poid,
                 at:that.data.addtype,
                 viewId:viewId
              },
              method:"POST",
              dataType:"json", 
              header: { 
                "content-type": "application/x-www-form-urlencoded" // 默认值
              },
              success (res) { 
                 that.setData({
                    qyaddcart:"确认"
                 })
                  if(res.data.status==1){
                      that.setData({
                        cartcount:res.data.cartcount
                      })
                      app.globalData.IFRefreshCart=1
                      wx.setStorageSync('CartId', res.data.CartId)
                      if(that.data.ttype==6&&that.data.IFPD>0){
                           wx.removeStorageSync("viewid")
                           wx.removeStorageSync("tguid0")
                      }
                      /*wx.showToast({
                        title: res.data.msg,
                        icon: "success",
                        duration: 2000
                      })*/
                      if(that.data.addtype==0){
                        if(that.data.ttype==6){
                            wx.navigateTo({  
                                url: "../sell/index"
                             })
                        }else{
                         that.setData({
                            cartmsgHidden:false,
                            addcartHidden:true
                          })
                        }
                         /**wx.switchTab({
                            url: "../cart/index"
                         })*/
                      }else{
                         wx.navigateTo({  
                            url: "../sell/index"
                         })
                      }
                  }else{
                     wx.showToast({
                        title: res.data.msg,
                        icon: "none",
                        duration: 2000
                      })
                  }
                  
              }  
            })
        } 
    },gotocart:function(e) {
         /*wx.switchTab({
            url: '../cart/index'
          })*/
          wx.navigateTo({  
            url: "../cart/index"
         })
    },showcart:function(e) {
        that.setData({
            cartmsgHidden:false
        })
    },closemsg:function(e) {
        that.setData({
            cartmsgHidden:true
        })
    },
    closeauth0: function (e) {
      that.setData({
        authorizeHidden0:true
       })
       updateing=0
    }, 
    getPhoneNumber: function (e) {
     
      if (e.detail.errMsg == "getPhoneNumber:ok") {
        that.setData({
            onauthorizetitle0:"获取中..."
        })
        var userId = wx.getStorageSync("userId")
        var LoginId = wx.getStorageSync("LoginId")
        if(updateing==0){
          updateing=1
        wx.request({
          url: app.globalData.apiurl + "/xapiajax.ashx", 
          data: {
            action:"getwxphone",
            encryptedData: e.detail.encryptedData,
            iv: e.detail.iv,
            userId:(!userId?"":userId),
            LoginId:(!LoginId?"":LoginId)
          },
          method:"POST",
          dataType:"json", 
          header: { 
              "content-type": "application/x-www-form-urlencoded" // 默认值
          },
          success: function (res2) {
             updateing=0
              wx.showToast({
                 title: "获取成功",
                 icon: "success",
                 duration: 2000
              }) 
              that.setData({
                phone:res2.data.Phone,
                onauthorizetitle0:"获取微信手机号"
             })  
          }
        })
      } 
      }
        
    },getcode0:function(e) {
        var userId = wx.getStorageSync("userId")
        var LoginId = wx.getStorageSync("LoginId")
        var b=true
       
        if(b){
          if(updateing==0){
            updateing=1
          wx.request({
            url: app.globalData.apiurl + "/xapiajax.ashx", 
            data: { 
                 action:"sendphonecode2",
                 userId:(!userId?"":userId),
                 LoginId:(!LoginId?"":LoginId),
                 phone:that.data.phone
            },
            method:"POST",
            dataType:"json", 
            header: { 
              "content-type": "application/x-www-form-urlencoded" // 默认值
            },
            success (res) { 
              updateing=0
              if(res.data.status==1){
                wx.showToast({
                  title: res.data.msg,
                  icon: "success",
                  duration: 3000
                 })
                 wait = 120;
                 that.gettime0()
              }else{
                 wx.showToast({
                  title: res.data.msg,
                  icon: "none",
                  duration: 3000
                 })
              }
              }
          })
          }
        }
      },  
      gettime0:function() {
          if (wait == 0) {
              that.setData({
                  getcode0:"获取验证码"
              })
              wait = 120;
              updateing=0
          } else {
              that.setData({
                   getcode0: "重新发送(" + wait + ")"
               })
              wait--;
              setTimeout(function() {
                  that.gettime0()
              }, 1000)
          }
  
      },inputphone:function(e) {
         that.setData({
             phone:e.detail.value
         })
      },bindPhone:function(e) {
        var phone=that.data.phone
        var code = e.detail.value.code
        var userId = wx.getStorageSync("userId")
        var LoginId = wx.getStorageSync("LoginId")
        var b=true
        
         
        if(b){
        if(code==null || code==""){
          b=false
          wx.showToast({
            title: "手机验证码不能为空！",
            icon: "none",
            duration: 3000
           })
        }
      }
        if(b){
          if(updateing==0){
            updateing=1
          that.setData({
            savebutton0:"请稍候..."
        })
        wx.request({
         
          url: app.globalData.apiurl + "/xapiajax.ashx", 
          data: { 
             action:"bindphone",
             userId:(!userId?"":userId),
             LoginId:(!LoginId?"":LoginId),
             phone:phone,
             code:code
          },
          method:"POST",
          dataType:"json", 
          header: { 
            "content-type": "application/x-www-form-urlencoded" // 默认值
          },
          success (res) { 
            updateing=0
            wx.showToast({
              title: res.data.msg,
              icon: "none",
              duration: 3000 
             })
            if(res.data.status==1){
               wx.showToast({
                title: res.data.msg,
                icon: "none",
                duration: 3000 
               })
               wx.setStorageSync('userInfo', JSON.stringify(res.data.userInfo)) 
               app.globalData.IFRefreshindex=0,
               app.globalData.IFRefreshmy=1
               that.setData({
                  authorizeHidden0:true
                })
                if(that.data.IFDJPD==1){
                    that.onpd();
                }else{
                    that.addcart();
                }
                
            }else{
               wx.showToast({
                title: res.data.msg,
                icon: "none",
                duration: 3000 
               })
            }
            that.setData({
              savebutton0:"确认解绑"
          })
            }
        })
        }
      }
      }
})  
