// customized/pages/made/car/car.js
let app = getApp();
let host = app.globalData.servsers; // 请求的url
Page({

  /**
   * 页面的初始数据
   */
  data: {
    carList:[],// 购物车列表数组
    host: app.globalData.servsers,
    amount:0, //总价
    checkAll:false, // 全选
    checkStatic:false, //默认一个都没有选择
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onShow: function () {
    let that = this;
    that.getCarlist(that)
  },

  /**
   * 生命周期函数--监听页面显示
   */

  subtraction(e){ // 减少
  let that = this;
    console.log(e)
    let index = e.target.dataset.index,carList = that.data.carList;
    if (carList[index].customGoodsNum > 1){
      // 表示最小数量，不可减少
      carList[index].customGoodsNum--;
      that.setData({
        carList: carList
      })
    }
    that.totalPrice(that)
  },
  add(e) { // 减少
    let that = this;
    console.log(e)
    let index = e.target.dataset.index, carList = that.data.carList;
      carList[index].customGoodsNum++;
      that.setData({
        carList: carList
      })
    that.totalPrice(that)
  },
  checkboxChange(e){
    let index = e.target.dataset.index, that = this, carList = that.data.carList;
    if (e.detail.value[0]){ // 表示价格增加
      that.data.carList[index].checked = true; // 表示选中
    } else {
      that.data.carList[index].checked = false; // 表示选中
    }
    that.setData({
      carList: carList
    })
    that.totalPrice(that)

  },
  checkboxChangeAll(e){ // 全选
  console.log(e)
    let that = this, checkAll= that.data.checkAll , carList=that.data.carList;
    if (e.detail.value[0]) { 
      checkAll = true; // 表示选中
    } else {
      checkAll = false;
    }
    carList.forEach(res => {
      res.checked = checkAll;
    })
    that.setData({
      carList:carList
    })
    that.totalPrice(that)
  },
  getCarlist(that){
    wx.showLoading({
      title: '加载中...',
    })
    wx.request({
      url: host +'/queryShopCar',
      method:'post',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data:{
        userId: app.globalData.user_id   //app.globalData.user_id //'oovpNwjlsY6xx8ceCebFa1dOLd9E'  //app.globalData.user_id,
      },
      success:function(res){
        console.log('购物车列表数据',res)
        let carList = [],imgArray=[];
        res.data.forEach((el,index)=>{
          imgArray =el.customGoodsImg.split(',');
          carList.push(el);
          carList[index]['imgArray'] = imgArray;
          carList[index]['checked'] = false;
        })
        that.setData({
          carList: carList
        })
        console.log(that.data.carList,"carListcarList")
      },
      complete(){
        wx.hideLoading()
      }
    })
  },
  carSubmit(){ // 提交订单
    let that = this, carList=[];
    if (that.data.checkStatic){
      that.data.carList.forEach(el=>{
        if(el.checked){
          carList.push(el)
        }
      })
      wx.setStorageSync('goodsList', carList);
      wx.setStorageSync('amount', that.data.amount);
      wx.navigateTo({
        url: '/customized/pages/accounts/accounts',
      })
    } else {
      wx.showToast({
        title: '您还没有选择商品',
        icon:'none'
      })
    }
  },
  totalPrice(that){
    let amount = 0, checkAll = true, checkStatic= false;
    that.data.carList.forEach(res=>{
      if(res.checked){
        amount += res.customgoodsPriceNow * res.customGoodsNum;
        checkStatic = true
       
      }
      if (!res.checked) {
        checkAll = false
      }
    })
    that.setData({
      amount:amount.toFixed(2),
      checkAll: checkAll,
      checkStatic: checkStatic
    })
  },
  previewImage(e){ // 点击图片放大
    let urls = [],that = this;
    console.log(e)
    e.target.dataset.srcarray.forEach(el=>{
      urls.push(that.data.host + el)
    })
    wx.previewImage({
      urls: urls,
      current:e.target.dataset.src,
      success(res){
        console.log(res,"放大成功")
      }
    })
  }
})