//index.js
import {
  getDeviceScreen,
  CalcMethod
} from '../../../utils/comm.js'
const Calc = new CalcMethod(),
  app = getApp(),
  {
    hostname
  } = app.globalData
Page({
  data: {
    ppNum: 1,
    activeIndex: 0,
    proindex: 0,
    flag: 0,
    totalPrice: 0,
    cartFlag: false,
    goodsFlag: false,
    // gtp:[
    //   { id: '龟田一号1',img:[{name:'龟田一号1'},{name:'龟田一号2'}],},
    //   { id: '龟田二号', img: [{ name: '龟田二号1' }, { name: '龟田二号2' }], },
    //   { id: '龟田三号', img: [{ name: '龟田三号1' }, { name: '龟田三号2' }], },
    // ],
    // gtd: [
    //   {  '一级分类': [{ name: '龟田一号1' }, { name: '龟田一号2' }], },
    //   { '一级分类': [{ name: '龟田二号1' }, { name: '龟田二号2' }], },
    //   { '一级分类': [{ name: '龟田三号1' }, { name: '龟田三号2' }], },
    // ],
    pushincar: false
  },
  onLoad: function(options) {
    this.getGoodsOther()
    this.getReduction()
    this.checkHack()
    this.getmoshi()
    
    if (options.business_id) {
      app.globalData.business_id = options.business_id
      app.globalData.layer = options.layer
      app.globalData.table = options.table
      // wx.setStorageSync('business_id', options.business_id)
      wx.setStorageSync('layer', options.layer)
      wx.setStorageSync('table', options.table)
    }
    
    let that = this,
      {
        windowHeight,
        pixelRatio
      } = getDeviceScreen(),
      scrollHeight = 0
    scrollHeight = windowHeight - (45 + 125) // 获取滚动区域高度
    this.setData({
      scrollHeight
    })
    // 传到授权登录接口
    var u_openid = options.u_openid;
    app.getUserInfo(function(cb, userInfo) {
      that.setData({
        userInfo: userInfo,
      })
    }, u_openid)
    // 保存AppData
    app.getUserInfo(function(userInfo) {
      that.setData({
        userInfo: userInfo
      }) //更新数据
    })
   
  },
  onShow: function(options) {
    var that = this
    this.getGoodsInfo() // 获取商品信息
    this.getStoreInfo() // 获取商家信息
    this.syncCart();
    // that.getGoodsProperty() //获取商品属性

  },
  // 选择分类
  selectCate(e) {
    let {
      index
    } = e.currentTarget.dataset
    this.setData({
      activeIndex: index
    })
  },



  // 获取特价商品
  // getBargainGoods() {
  //   let that = this
  //   var business_id = app.globalData.business_id
  //   wx.request({
  //     url: hostname + 'GoodsOther/special_goods_list_api',
  //     data: { page },
  //     header: { 'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8' },
  //     method: 'POST',
  //     dataType: 'JSON',
  //     success: function (res) {

  //     }
  //   })
  // },


  // 选择商品分类
  chooseGoodsType(e) {
    let id = e.currentTarget.dataset.id
    let idx = e.currentTarget.dataset.idx
    this.setData({
      activeIndex: idx,
      goodsId: id
    })
  },



  // 获取商品分类
  getGoodsOther() {
    let that = this
    wx.request({
      url: hostname + 'GoodsOther/cat_list',
      data: {
        business_id: app.globalData.business_id
      },
      header: {
        'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8'
      },
      method: 'POST',
      dataType: 'JSON',
      success: function(res) {
        var goodsOther = JSON.parse(res.data).data;

        var catid = [];
        for(var i=0;i<goodsOther.length;i++){
          catid.push(goodsOther[i].id)
        }

        that.setData({
          goodsOther: goodsOther,
          catid: catid
        })
        // console.log(goodsOther, catid, "goodsOther")
      },
      fail: function(res) {},
    })
  },

  // 获取消费模式
  getmoshi() {
    let that = this
    wx.request({
      url: hostname + 'Kaiguan/getConsum',
      data: {
        business_id: app.globalData.business_id,
        users_id: wx.getStorageSync('users_id')
      },
      header: {
        'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8'
      },
      method: 'POST',
      dataType: 'JSON',
      success: function (res) {
        // console.log(res,"获取消费模式")
      },
      fail: function (res) { },
    })
  },

  //获取商品属性规格
  getGoodsProperty(goods_id,cat_id) {
    let that = this
    var business_id = app.globalData.business_id
    wx.request({
      url: hostname + 'Goods/get_cateattr',
      data: {
         cat_id , 
         goods_id
         },
      header: { 'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8' },
      method: 'POST',
      dataType: 'JSON',
      success: function (res) {
        res.data = JSON.parse(res.data)
        if(res.data.status == "1"){
          var propertyData = res.data.data;
          var cateAttr = [];

          for (var i = 0; i < propertyData.length;i++){
            cateAttr.push(propertyData[i].attr)
          }

          for (var k = 0; k < cateAttr.length; k++) {
            cateAttr[k].cateTitle = null,
            cateAttr[k].cateAttrId = null;
          }

          that.setData({
            propertyData: propertyData,
            cateAttr: cateAttr
          })

        }

        // console.log(propertyData, cateAttr,"propertyDatapropertyDatapropertyData")

      }
          })
        },


  // 获取商品信息
  getGoodsInfo() {
    let that = this
    var business_id = app.globalData.business_id
    wx.request({
      url: hostname + 'Goods/goods_list',
      data: {
        business_id
      },
      header: {
        'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8'
      },
      method: 'POST',
      dataType: 'JSON',
      success: function(res) {

        res.data = JSON.parse(res.data)
        if (res.data.status === 1) {
          let goodsData = res.data.data,
            temp = []
          // console.log(goodsData, "goodsData1")
          // var cateAttr = [];

          // for (var i = 0; i < goodsData.length; i++) {
          //   cateAttr.push(goodsData[i].attr)
          // }

          // for (var k = 0; k < cateAttr.length; k++) {
          //   cateAttr[k].cateAttrId = null;
          // }

          // that.setData({
          //   cateAttr: cateAttr
          // })

          // console.log(goodsData, "goodsData2")
          // 给商品添加默认数量
          goodsData.forEach((value) => {
            for (let i in value) {
              // console.log(value[i].reverse())
              if (value[i].length) {
                value[i].forEach((goods) => {
                  goods.count = 0
                })
                temp.push(value)
              }
            }
          })


          // console.log(temp, temp, "temptemptemp")
          that.setData({
            catesArr: temp
          })
          // that.syncCart() // 同步购物车数量
          // console.log(temp,"商品信息")
        }
      },
      fail: function(res) {},
      complete: function(res) {
        setTimeout((value) => {
          that.setData({
            onLoad: true
          })
        })
      },
    })
  },
  // 获取商店信息
  getStoreInfo() {
    let that = this

    var business_id = app.globalData.business_id;
    wx.request({
      url: hostname + 'Goods/get_business_info',
      data: {
        business_id
      },
      header: {
        'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8'
      },
      method: 'POST',
      dataType: 'JSON',
      success: function(res) {
        let storeInfo = JSON.parse(res.data).data
        that.setData({
          storeInfo
        })
        // console.log(storeInfo,"storeInfostoreInfostoreInfo")
      },
      fail: function(res) {},
      complete: function(res) {},
    })
  },

  // 显示隐藏购物车
  showCart() {
    let {
      cart,
      cartFlag
    } = this.data
    if (cartFlag) {
      // console.log(1)
      let that = this
      this.setData({
        cartHeight: 0
      })
      setTimeout(() => {
        that.setData({
          cartFlag: false
        })
      }, 200)

    } else {
      // console.log(2)
      this.setData({
        cartHeight: 0,
        cartFlag: true
      })
      this.syncCart()
    }
  },
  // 窗口监听器
  windowTouchListener(e) {
    let {
      attr
    } = e.currentTarget.dataset;
    // console.log(e)
    if (attr != 'content') {
      // console.log(3)
      let that = this
      this.setData({
        cartHeight: 0
      })
      setTimeout(() => {
        that.setData({
          cartFlag: false
        })
      }, 200)
    }
    return false
  },
  // 跳转至确认订单页面
  payNav() {

    var business_id = app.globalData.business_id

    let {
      cart
    } = this.data
    if (cart.length > 0) {
      let final = this.data.finalPrice ? this.data.finalPrice : this.data.totalPrice
      wx.navigateTo({
        url: '../../confirmOrder/confirmOrder?business_id=' + business_id + '&finalPrice=' + final,
      })
    } else {
      wx.showToast({
        title: '请选择商品',
        image: '../../../images/toast/tips_icon.png',
        duration: 1500
      })
    }
  },
  // 增加数量
  addCount(e) {
    let {
      activeIndex,
      catesArr
    } = this.data, {
        id,
        index
      } = e.currentTarget.dataset,
      that = this
    for (let item in catesArr[activeIndex]) {
      if (catesArr[activeIndex][item][index].max_buy <= 0) {
        catesArr[activeIndex][item][index].count++
          this.setData({
            catesArr
          })
        // this.saveCart()
      } else {
        var business_id = app.globalData.business_id
        wx.request({
          url: hostname + 'Goods/get_user_goods_num',
          data: {
            business_id,
            users_id: wx.getStorageSync('users_id'),
            goods_id: catesArr[activeIndex][item][index].id
          },
          header: {
            'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8;'
          },
          method: 'POST',
          dataType: 'JSON',
          success: (res) => {
            res.data = JSON.parse(res.data)
            if (res.data.status && (catesArr[activeIndex][item][index].count + 1) <= catesArr[activeIndex][item][index].max_buy - res.data.buy_num) {
              catesArr[activeIndex][item][index].count++
                that.setData({
                  catesArr
                })
              // that.saveCart()
            } else if (catesArr[activeIndex][item][index].max_buy == res.data.buy_num) {
              wx.showModal({
                title: '提示',
                content: `已达购买上限`,
                showCancel: false,
                confirmColor: '#067655'
              })
            } else {
              let num = catesArr[activeIndex][item][index].max_buy - res.data.buy_num
              if (typeof num == 'NaN' || num <= 0 || res.data.buy_num == undefined) {
                num = 0
              }
              wx.showModal({
                title: '提示',
                content: `已达购买上限`,
                showCancel: false,
                confirmColor: '#067655'
              })
            }
          },
          fail: (error) => {},
          complete: (res) => {}
        })
      }
    }
  },
  // 减少数量
  reduceCount(e) {
    let {
      activeIndex,
      catesArr
    } = this.data, {
      id,
      index
    } = e.currentTarget.dataset
    for (let item in catesArr[activeIndex]) {
      if ((catesArr[activeIndex][item][index].count - 1) >= 0) catesArr[activeIndex][item][index].count--
    }
    this.setData({
      catesArr
    })
    // this.saveCart()
  },
  // 同步购物车本地数据
  syncCart() {
    let that = this,
      {
        catesArr
      } = this.data,
      cart = wx.getStorageSync('cart') || [],
      catesArrs = catesArr,
      totalPrice = 0,
      cartCount = 0;
      // console.log(cart)
      if(cart.length){
        cart.forEach(v=>{
          cartCount = Calc.add(cartCount, v.count);
          totalPrice = Calc.add(totalPrice, Calc.mul(v.count, v.price));
        })
      }
      // console.log(cartCount, totalPrice)
      that.setData({
        cartCount, totalPrice
      })

 // 清除无用数据
    let tempArr = [],
      cartHeight;
    cart.forEach((value) => {
      if (value.count > 0) tempArr.push(value)
    })
    if (tempArr.length < 5) {
      cartHeight = ((tempArr.length * 65) + 41) + 'px'
    } else {
      cartHeight = (5 * 65 + 41) + 'px'
    }
    this.setData({
      cartHeight,
      cart
    })
    // console.log(cartHeight)
     
    // if (that.data.ppPrice) {
    //   that.setData({
    //     totalPrice: that.data.ppPrice
    //   })
    // }
    // if (cart.length) {
    //   cart.forEach((cartGoods) => {
    //     let exist = false
    //     catesArrs.forEach((value) => {
    //       for (let i in value) {
    //         value[i].forEach((goods) => {
  
    //           if (goods.id == cartGoods.id) {
    //             console.log(goods)
    //             exist = true
    //             goods.count = cartGoods.count;
    //             goods.price = cartGoods.price;
    //             console.log(goods)
    //             // if(that.data.ppPrice){
    //             //   totalPrice = Calc.add(ppPrice, Calc.mul(goods.price, goods.count))
    //             // }
    //             // totalPrice = Calc.add(totalPrice, Calc.mul(goods.price, goods.count))
    //             // totalPrice = totalPrice+cartGoods.price
    //             totalPrice = Calc.add(totalPrice, Calc.mul(goods.price, goods.count))
    //             cartCount = Calc.add(cartCount, goods.count)

    //           }  
    //         })
    //       }
    //     })
    //     exist && tempArr.push(cartGoods)
    //   })
    // } else {
    //   totalPrice = 0
    //   cartCount = 0
    //   catesArrs.forEach((value) => {
    //     for (let i in value) {
    //       value[i].forEach((goods) => {
    //         goods.count = 0
    //       })
    //     }
    //   })
    // }
    // console.log(cart, catesArr, catesArrs,"catesArrcatesArrcatesArr")

    // 保存本地购物车数据
    // wx.setStorage({
    //   key: 'cart',
    //   data: tempArr,
    //   success: () => {
    //     that.setData({
    //       catesArrs,
    //       cart: tempArr,
    //       totalPrice: totalPrice.toFixed(2),
    //       cartCount
    //     })
    //     setTimeout(() => {
    //       wx.stopPullDownRefresh() // 停止刷新
    //     }, 150)
    //   }
    // })
    this.getFinalPrice(totalPrice)
  },


  // 新版更新保存购物车数据
  // newSaveCar:function(){
  //   let that = this,
  //     {
  //       catesArr
  //     } = this.data,
  //     cart = wx.getStorageSync('cart') || [],
  //     tempArr = [],
  //     catesArrs = catesArr,
  //     totalPrice = 0,
  //     cartCount = 0;
  //     console.log(cart)
  //   if (that.data.ppPrice) {
  //     that.setData({
  //       totalPrice: that.data.ppPrice
  //     })
  //   }

  //     if (cart.length) {
  //     cart.forEach((cartGoods) => {
  //       let exist = false
  //       catesArrs.forEach((value) => {
  //         for (let i in value) {
  //           value[i].forEach((goods) => {

  //             if (goods.id == cartGoods.id) {
  //               console.log(goods)
  //               exist = true
  //               goods.count = cartGoods.count;
  //               goods.price = cartGoods.price;
  //               totalPrice = Calc.add(totalPrice, Calc.mul(goods.price, goods.count))
  //               cartCount = Calc.add(cartCount, goods.count)
  //             }  
  //           })
  //         }
  //       })
  //       exist && tempArr.push(cartGoods)
  //     })
  //   } else {
  //     totalPrice = 0
  //     cartCount = 0
  //     catesArrs.forEach((value) => {
  //       for (let i in value) {
  //         value[i].forEach((goods) => {
  //           goods.count = 0
  //         })
  //       }
  //     })
  //   }


  // },


  // 更新价格
  getFinalPrice(totalPrice) {
    // console.log(totalPrice,'totalPrice')
    let list = this.data.reductionList
    for (let i in list) {
      if (totalPrice >= list[i].full_money) {
        // console.log(list[i].reduction_money,547565656565656565656)
        this.setData({
          finalPrice: totalPrice - list[i].reduction_money
        })
        return
      } else {
        this.setData({
          finalPrice: totalPrice
        })
      }
    }
  },
  // 保存本地购物车数据
  // saveCart() {
  //   let {
  //     catesArr
  //   } = this.data,
  //     cart = wx.getStorageSync('cart') || [];
  //   if(e){
  //     console.log(e)
  //   }
    
  //   catesArr.forEach((value) => {
  //     for (let i in value) {
  //       value[i].forEach((goods) => {
  //         let exist = false
  //         cart.length > 0 && cart.forEach((value) => {
  //           console.log(value, goods)
  //           if (value.id == goods.id && value.price==goods.price) {
  //             value.count = goods.count
  //             exist = true
  //           }
  //         })
  //         if (!exist && goods.count > 0) {
  //           cart.push({
  //             id: goods.id,
  //             count: goods.count,
  //             title: goods.title,
  //             price: goods.price
  //           })
  //         }
  //       })
  //     }
  //   // })
  //   // 清除无用数据
  //   let tempArr = [],
  //     cartHeight
  //   cart.forEach((value) => {
  //     if (value.count > 0) tempArr.push(value)
  //   })
  //   if (tempArr.length < 5) {
  //     cartHeight = ((tempArr.length * 65) + 41) + 'px'
  //   } else {
  //     cartHeight = (5 * 65 + 41) + 'px'
  //   }
  //   this.setData({
  //     cartHeight
  //   })
  //   // 保存缓存
  //   wx.setStorage({
  //     key: 'cart',
  //     data: tempArr,
  //     success: () => {
  //       this.syncCart() // 同步购物车数据
  //     }
  //   })
  // },
  // 购物车数量增删
  cartManager(e) {
    let {attr,id,index} = e.currentTarget.dataset;
    let cart = wx.getStorageSync('cart');
    // console.log(cart[index])
    if (attr === 'add') {
        cart[index].count++
      } else if (attr === 'reduce') {
        cart[index].count--
        if (cart[index].count==0){
          cart.splice(index,1);
        }
      }
      wx.setStorageSync("cart", cart)
  //  let { catesArrs} = this.data
    // catesArrs.forEach((value) => {
    //   for (let i in value) {
    //     value[i].forEach((goods) => {
    //       console.log(goods)
    //       if (goods.id == id && goods.price==price) {
    //         console.log(goods,1232132)
    //         if (attr === 'add') {

    //           goods.count++
    //         } else if (attr === 'reduce') {
    //           goods.count--
    //         }
    //       }
    //     })
    //   }
    // })
    // this.setData({
    //   catesArrs
    // })
    this.setData({
      cart
    })
    this.syncCart() // 保存本地购物车数据
  },
  // 清空购物车
  clearCart() {
    wx.showModal({
      title: '提示',
      content: '确定要清空购物车吗? ',
      confirmColor: '#067655',
      success: (res) => {
        let that = this
        if (res.confirm) {
          wx.removeStorage({
            key: 'cart',
            success: function(res) {
              that.syncCart() // 同步本地数据
              that.showCart() // 关闭购物车
            },
          })
        } else if (res.cancel) {}
      }
    })
  },
  // checkHack
  checkHack() {
    let that = this
    wx.request({
      url: hostname + 'Goods/get_info',
      success: function(res) {
        that.setData({
          checkHack: res.data
        })
        wx.setNavigationBarTitle({
          title: res.data.pageName
        })
      }
    })
  },
  // 下拉刷新
  onPullDownRefresh() {
    this.getGoodsInfo() // 获取商品信息
    this.getStoreInfo() // 获取商家信息
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function(res) {
    var userid = wx.getStorageSync('openId')
    // var userid = 1

    if (res.from === 'button') {
      // 来自页面内转发按钮
    }
    return {
      title: app.globalData.share_title,
      path: '/pages/ManyStore/subStore/subStore?u_openid=' + userid,
      success: function(res) {
        var pages = getCurrentPages() //获取加载的页面
        var currentPage = pages[pages.length - 1] //获取当前页面的对象
        //点击分享传进userid 截取userid 从onload拿到
        var url = currentPage.route + '?u_openid=' + userid; //当前页面url
      },
      fail: function(res) {
        // 转发失败
      },
    }
  },
  //查看订单
  view_order: function() {
    wx.navigateTo({
      url: '../../Suborder/Suborder?business_id=' + app.globalData.business_id,
    })
  },

  // 收藏商品
  getCollect: function(e) {
    let {
      activeIndex,
      catesArr
    } = this.data, {
        id,
        index
      } = e.currentTarget.dataset,
      that = this
    wx.request({
      url: hostname + 'UserOther/collect_goods',
      header: {},
      method: 'POST',
      data: {
        goods_id: catesArr[activeIndex][item][index].id,
        openid: wx.getStorageSync("openId")
      },
      success: function(res) {}
    })
  },
  // 满减列表
  getReduction() {
    let that = this
    wx.request({
      url: hostname + 'Order/reduction',
      header: {
        'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8'
      },
      method: 'POST',
      data: {
        business_id: app.globalData.business_id
      },
      success: function(res) {
        let data = res.data.data ? res.data.data.sort(sortJ) : null
        that.setData({
          reductionList: data
        })
      },complete(){
        that.getFinalPrice(that.data.totalPrice)
      }
    })
  },
  // 价格满减
  orderReduction(that) {
    wx.request({
      url: hostname + 'Order/reduction',
      header: {
        'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8'
      },
      method: 'POST',
      data: {
        business_id: app.globalData.business_id
      },
      success: function(res) {

        let allself = res.data.data,

          goods_price = that.data.totalPrice,
          newallself = [];

        // 对数组进行排序
        function compare(property) {
          return function(a, b) {
            var value1 = a[property];
            var value2 = b[property];
            return value1 - value2;
          }
        }
        allself = allself.sort(compare('full_money')).reverse();
        allself.forEach((v) => {
          if (Number(goods_price) > Number(v.full_money)) {
            goods_price = goods_price - v.reduction_money
            that.setData({
              selfprice: goods_price
            })
            // console.log(that.data.selfprice, 112)
            return
          }

        })
      }
    })
  },
  //显示蒙板
  showgm(e) {
    // console.log(e)
    let {
      cateIndex,
      itemIndex
    } = e.currentTarget.dataset,
      goodsDetails,
      catesArr = this.data.catesArr;
    // console.log(catesArr[cateIndex])
    for (let i in catesArr[cateIndex]) {
      // console.log(catesArr[cateIndex][i])
      goodsDetails = catesArr[cateIndex][i][itemIndex];
      // console.log(catesArr[cateIndex][i][itemIndex])

    }
    // console.log(goodsDetails)
    this.setData({
      goodsFlag: true,
      goodsDetails
    })
  },
  // 隐藏蒙板
  hiddengm() {
    this.setData({
      goodsFlag: false
    })
  },

  closeCar: function() {
    this.setData({
      pushincar: false,

    })
  },

  //加入购物车
  pushCar(e) {
    // console.log(e, 121312, this.data.pushPrice)
    let that = this;
    let {
      price
    } = e.currentTarget.dataset || this.data.pushPrice;
    let cgoodsid = e.currentTarget.dataset.cgoodsid;
    let ctitle = e.currentTarget.dataset.ctitle;
    let cateAttrId = that.data.cateAttrId;
    let catid = e.currentTarget.dataset.catid;
    let ppNum = that.data.ppNum;
    let ppPrice = that.data.ppPrice;
    let {
      catesArr
    } = this.data;
    let currentGoods;
    catesArr.forEach((v) => {
      console.log(v)
      for (let i in v) {
        v[i].forEach((x) => {
          if (x.id == cgoodsid) {
            console.log(x)
            currentGoods = x;
          }
        })

      }
    })
    //       var currentArr = currentGoods.attr;
    //       var cateAttr = [];


    //       for (var i = 0; i < currentArr.length;i++){
    //         cateAttr.push(currentArr[i].attr)
    //       }

    //       for (var k = 0; k < cateAttr.length; k++) {
    //         cateAttr[k].cateAttrId = null;
    //       }

    // console.log(cateAttr, currentGoods[k],"currentGoodscurrentGoods")

    this.setData({
      pushincar: true,
      pushPrice: price,
      cgoodsid: cgoodsid,
      ctitle: ctitle,
      currentGoods,
      catid,
      ppNum:1,
      ppPrice:''
      // cateAttr
    })


    that.getGoodsProperty(cgoodsid,catid);



    // console.log(currentGoods,that.data.cateAttrId,"cgoodsid")
  },

  //选中属性规格
  chooseProperty(e) {
    var that = this;
    var ppNum = that.data.ppNum;
    var cateTitleIndex = e.currentTarget.dataset.catetitleindex;
    var cateAttrId = e.currentTarget.dataset.cateattrid;
    var cateTitle = e.currentTarget.dataset.attitle;
    var propertyData = that.data.propertyData;
    let attrprice = [];
    var cateAttr = that.data.cateAttr;
    let ppPrice;
    let price = that.data.pushPrice;
    cateAttr[cateTitleIndex].cateTitle = cateTitle;
    cateAttr[cateTitleIndex].cateAttrId = cateAttrId;
    propertyData[cateTitleIndex].cateAttrId = cateAttrId;
    propertyData[cateTitleIndex].cateTitle = cateTitle;
    this.setData({
      cateAttr: cateAttr,
      propertyData,
      attrprice
    })
    
    //获取规格价格
    cateAttr.forEach((v) => {
      v.forEach((x) => {
        // console.log(x)
        propertyData.forEach((s) => {
          if (x.id == s.cateAttrId) {
            attrprice.push(x.attr_price)
          }
        })
      })
    })
    // 动态计算价格
    ppPrice = Number(price);
    attrprice.forEach((v) => {
      // console.log(ppPrice, v)
      ppPrice = ppPrice + Number(v);
    })
    that.setData({
      ppPrice: ppPrice
    })
    // console.log(ppPrice)
  },

  //属性加减号函数
  // addOne: function (e) {
  //   let that = this;
  //   ppNum = that.data.ppNum + 1;
  //   that.setData({
  //     ppNum: ppNum
  //   })
  // },

  carAdd: function() {
    
    let that = this,
      ppPrice = that.data.ppPrice;
      if(!ppPrice){
        wx.showModal({
          title: '温馨提示',
          content: '请选择相应规格',
          showCancel:false
        })
      }else{
        var ppNum = Number(that.data.ppNum) + 1;
        that.setData({
          ppNum: ppNum
        })
        // console.log(ppNum, "ppNumppNum")
        // if (ppNum>=1){
        //   ppPrice = Calc.mul(ppPrice, ppNum) 
        // }
        that.setData({
          ppPrice: ppPrice
        })
    // console.log(PPprice,"PPpricePPprice")
      }
    
  },

  carOff: function() {
    let that = this,
      ppNum = Number(that.data.ppNum) - 1;
    if (that.data.ppNum == 0) {
      that.setData({
        ppNum: 0
      })
    } else {
      that.setData({
        ppNum: ppNum
      })
    }



    // console.log(ppNum, "ppNumppNum")
  },

  //加入购物车计算属性价格 
  calcppPrice: function(e) {
    let that = this,     
      ppid = e.currentTarget.dataset.ppid,
      pushincar = that.data.pushincar,
      ppPrice = that.data.ppPrice,
      cgoodsid = that.data.cgoodsid,
      ctitle = that.data.ctitle,
      attr = [],
      atitle = [],
      ppNum = that.data.ppNum,
    propertyData = that.data.propertyData;
    propertyData.forEach(v=>{
      attr.push(v.cateAttrId),
      atitle.push(v.cateTitle);
    })
    // console.log(e)
    if (!ppPrice) {
       wx.showModal({
         title: '温馨提示',
         content: '请选择相应规格',
         showCancel: false
       }) 
    }else{
      that.setData({
        pushincar: false,
        ppPrice: ppPrice,
        cgoodsid,
        ctitle,
        ppid,
        attr,
        atitle
      })
      // console.log(attr, atitle, "加入购物车id等")

      let cart = wx.getStorageSync('cart') || [];
      cart.push({
        id: that.data.cgoodsid,
        count: that.data.ppNum,
        title: that.data.ctitle,
        price: that.data.ppPrice,
        attr,
        atitle
      })
      // console.log(attr.join(','));
      that.setData({
        cart: cart
      })
      wx.setStorageSync("cart", cart)

      that.syncCart()
    }
    
    
  },






})

function sortJ(a, b) {
  return b.full_money - a.full_money
}