var app = getApp()
var that=null
var sopenFrame=null
Page( {
  data: {
    cartImg: '../../images/cart-null.png',
    tipWords: '购物车空空如也',
    productHidden:false,
    noproductHidden:true,
    loadingHidden:false,
    footerHidden:false,
    ttype:0,
    cartproducts:[],
    cart_Total:"￥0.00",
    cart_Total_title:"总计（不包含运费）"
  },

  onLoad: function() {
      that = this
      that.rdata(0)
      sopenFrame=setInterval(function (){
          if(app.globalData.IFRefreshCart==1){
              that.rdata(0)
              app.globalData.IFRefreshCart=0
          }
      }, 1000);
    },onPullDownRefresh: function () {
      //在当前页面显示导航条加载动画
       wx.showNavigationBarLoading(); 
      //显示 loading 提示框。需主动调用 wx.hideLoading 才能关闭提示框
      wx.showLoading({
          title: '刷新中...',
      })
      that.rdata(1)
  },rdata: function(t) {
      var userId = wx.getStorageSync("userId")
      var LoginId = wx.getStorageSync("LoginId")
      var CartId = wx.getStorageSync("CartId")
      wx.request({
        url: app.globalData.apiurl + "/xapiajax.ashx", 
        data: { 
          action:"getshopcart",
           userId:(!userId?"":userId),
           LoginId:(!LoginId?"":LoginId),
           CartId:(!CartId?"":CartId)
        },
        method:"GET",
        dataType:"json", 
        header: { 
          "content-type": "application/json" // 默认值
        },
        success (res) { 
            if(res.data.status==1){
            that.setData({
                 ttype:res.data.ttype,
                 cartproducts:res.data.productlist,
                 loadingHidden:true,
                 cart_Total:"￥"+res.data.ZSubTotal
             })
             if(res.data.ttype==0){
              that.setData({
                cart_Total:"兑换券"+res.data.ZSubTotal
              })
             }
            if(res.data.productlist.length>0){
              that.setData({
                  productHidden:false, 
                  noproductHidden:true,
                  footerHidden:false
              })
            }else{
              that.setData({
                productHidden:true,
                noproductHidden:false,
                footerHidden:true
               })
            }
          }else{
              if(res.data.msg=="您未登录"||res.data.msg=="参数错误"){
                  wx.removeStorageSync('userId')
                  wx.removeStorageSync('LoginId')
                  that.rdata(0) 
              }
          }
          if(t==1){
            //隐藏loading 提示框
            wx.hideLoading();
            //隐藏导航条加载动画
            wx.hideNavigationBarLoading();
            //停止下拉刷新
            wx.stopPullDownRefresh();
         }
          }
      })
    },updatenum:function(e) {
      var datavalue = e.currentTarget.dataset.id.split(":")
      var t=datavalue[0]
      var id=datavalue[1]
      var index=datavalue[2]
      var prodNum=that.data.cartproducts[index].prodNum
      var TotalNum=that.data.cartproducts[index].TotalNum
      var userId = wx.getStorageSync("userId")
      var LoginId = wx.getStorageSync("LoginId")
      var CartId = wx.getStorageSync("CartId")
      var bb=true
      var a=prodNum
      if(t==1){
        var c=/^[0-9]*$/
        if (!c.test(a)) {
            a = 1
            wx.showToast({
                title: "输入的数量有误,应为[1-999]",
                icon: "none",
                duration: 2000
            })
            bb=false
        }
        if(bb){
             var b = parseInt(a) + 1
             if (b > 999) {
                  b = 999
             }
             var e = TotalNum;
             e = parseInt(e);
             if (b > e) {
                wx.showToast({
                    title: "库存不足",
                    icon: "none",
                    duration: 2000
                })
                b = e
                bb=false
             }
             a=b
        } 
      }else if(t==2){
        var c = /^[0-9]*$/;
        if (!c.test(a)) {
            a = 1
            wx.showToast({
                title: "输入的数量有误,应为[1-999]",
                icon: "none",
                duration: 2000
            })
            bb=false
        }
        if(bb){
            var b = parseInt(a) - 1
            var zkc = TotalNum
            if (b > 999) {
               b = 999
            }
            if (b > zkc) {
               b = zkc
            }
            if (b <= 0) {
                wx.showToast({
                    title: "输入的数量有误,应为[1-999]",
                    icon: "none",
                    duration: 2000
                })
                b = 1
                bb=false
            }
            a=b
        }  
      }else{
           var zkc=parseInt(TotalNum);
           if(e.detail.value!=""){
              a=parseInt(e.detail.value); 
           }else{
               bb=false
           }
           if (a > zkc) {
               a = zkc
           }
           if (a < 1) {
               a = 1
           }
      }
      if(bb){
      wx.request({
        url: app.globalData.apiurl + "/xapiajax.ashx", 
        data: { 
          action:"updateshopcart",
           userId:(!userId?"":userId),
           LoginId:(!LoginId?"":LoginId),
           CartId:(!CartId?"":CartId),
           num:a,
           id:id
        },
        method:"POST",
        dataType:"json", 
        header: { 
          "content-type": "application/x-www-form-urlencoded" // 默认值
        },
        success (res) { 
          if(res.data.status==1){
            that.setData({
                 ttype:res.data.ttype,
                 cartproducts:res.data.productlist,
                 cart_Total:(res.data.ttype==1?"￥":"")+res.data.ZSubTotal+(res.data.ttype==0?"积分":"")
                 
             })
            if(res.data.productlist.length>0){
              that.setData({
                  productHidden:false,
                  noproductHidden:true
              })
            }else{
              that.setData({
                productHidden:true,
                noproductHidden:false
               })
            }
          }else{
            wx.showToast({
              title: res.data.msg,
              icon: "none",
              duration: 3000
             })
          }
          }
      })
      }
    },delcart:function(e) {
      wx.showModal({
        title: '删除提示',
        content: '此操作不可恢复！您确认要删除吗？',
        success (res0) {
          if (res0.confirm) {
            var id = e.currentTarget.dataset.id
            var userId = wx.getStorageSync("userId")
            var LoginId = wx.getStorageSync("LoginId")
            var CartId = wx.getStorageSync("CartId")
            wx.request({
              url: app.globalData.apiurl + "/xapiajax.ashx", 
              data: { 
                action:"delshopcart",
                 userId:(!userId?"":userId),
                 LoginId:(!LoginId?"":LoginId),
                 CartId:(!CartId?"":CartId),
                 id:id
              },
              method:"POST",
              dataType:"json", 
              header: { 
                "content-type": "application/x-www-form-urlencoded" // 默认值
              },
              success (res) { 
                if(res.data.status==1){
                  that.setData({
                       ttype:res.data.ttype,
                       cartproducts:res.data.productlist,
                       cart_Total:(res.data.ttype==1?"￥":"")+res.data.ZSubTotal+(res.data.ttype==0?"积分":"")
                       
                   })
                  if(res.data.productlist.length>0){
                    that.setData({
                        productHidden:false,
                        noproductHidden:true
                    })
                  }else{
                    that.setData({
                      productHidden:true,
                      noproductHidden:false
                     })
                  }
                }else{
                  wx.showToast({
                    title: res.data.msg,
                    icon: "none",
                    duration: 3000
                   })
                }
                }
            })
          } else if (res0.cancel) {
               
          }
        }
      })
     
      
    },Shop_Pay:function(e) {
      var userId = wx.getStorageSync("userId")
      var LoginId = wx.getStorageSync("LoginId")
      if(userId&&LoginId){
          wx.navigateTo({  
            url: "../sell/index"
          })
      }else{
        wx.navigateTo({  
            url: "../login/index"
        })
     }
    },gotodetail:function(e) {
      var userId = wx.getStorageSync("userId")
      var LoginId = wx.getStorageSync("LoginId")
      if(userId&&LoginId){
           var id = e.currentTarget.dataset.id
           wx.navigateTo({  
               url: "../details/index?id="+id
           })
       }else{
           wx.navigateTo({  
               url: "../login/index"
            })   
        }
    }
})
