// pages/gouwu/gouwu.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
<<<<<<< HEAD
    'iscart':false,
    'goodList': [
      {
=======
    'iscart': false,
    'checked': [],
<<<<<<< HEAD
    'goodList': [{
>>>>>>> f23362a433f70f1876587a01c643e157b7e8b3ae
        'cover': '/pages/index/image/suanfa.jpg',
        'isbn': '9787535482051',
        'desc': '计算机算法设计',
        'price': 20,
        'count': 1,
        'checked': false
      },
      {
        'cover': '/pages/index/image/jichu.jpg',
        'isbn': '9787540455958',
        'desc': '计算机基础',
        'price': 30,
        'count': 1,
        'checked': false
      },
      {
        'cover': '/pages/index/image/rjgcdl.jpg',
        'isbn': '9787539982830',
        'desc': '软件工程导论',
        'price': 25,
        'count': 1,
        'checked': false
      },
      {
        'cover': '/pages/index/image/java.jpg',
        'isbn': '9787550013247',
        'desc': 'Java Web',
        'price': 17,
        'count': 1,
        'checked': false
      },
      {
        'cover': '/pages/index/image/byyl.jpg',
        'isbn': '9787208061644',
        'desc': '编译原理',
        'price': 15,
        'count': 1,
        'checked': false
      }
    ],
=======
    // 'goodList': [{
    //     'cover': '/pages/index/image/suanfa.jpg',
    //     'isbn': '9787535482051',
    //     'desc': '计算机算法设计',
    //     'price': 20,
    //     'count': 1,
    //     'checked': false
    //   },
    //   {
    //     'cover': '/pages/index/image/jichu.jpg',
    //     'isbn': '9787540455958',
    //     'desc': '计算机基础',
    //     'price': 30,
    //     'count': 1,
    //     'checked': false
    //   },
    //   {
    //     'cover': '/pages/index/image/rjgcdl.jpg',
    //     'isbn': '9787539982830',
    //     'desc': '软件工程导论',
    //     'price': 25,
    //     'count': 1,
    //     'checked': false
    //   },
    //   {
    //     'cover': '/pages/index/image/java.jpg',
    //     'isbn': '9787550013247',
    //     'desc': 'Java Web',
    //     'price': 17,
    //     'count': 1,
    //     'checked': false
    //   },
    //   {
    //     'cover': '/pages/index/image/byyl.jpg',
    //     'isbn': '9787208061644',
    //     'desc': '编译原理',
    //     'price': 15,
    //     'count': 1,
    //     'checked': false
    //   }
    // ],
>>>>>>> 1b33c170e75d0094eab32be499d7f94124a3b4ef
    'bookList': [],
    'checkAll': false,
    'totalCount': 0,
    'totalPrice': 0,
    'consignee': "杨盼成",
    'phone': '12345678912'
  },

  /**
   * 删除购物车当前商品
   */
  deleteList(e) {
    var that = this
    const index = e.currentTarget.dataset.index;
    let bookList = this.data.bookList;

    wx.request({
      url: "https://hunjianghu.xuanjis.com/web/AddCart.do?action=Delete&BookID=" + that.data.bookList[index].BookID + "&UserID=1",
      header: {
        "content-type": "json"
      },
      success: function(res) {

        if (res.statusCode == 200) {
          that.setData({
            book: res.data,
          })
          //wx.hideNavigationBarLoading()
        }
      }
    })

    bookList.splice(index, 1);
    this.setData({
      bookList: bookList
    });
    if (!bookList.length) {
      this.setData({
        iscart: true
      });
    } else {
      this.calculateTotal();
    }
  },

  /**
   * 计算商品总数
   */
  calculateTotal: function() {
    var bookList = this.data.bookList;
    var totalCount = 0;
    var totalPrice = 0;
    for (var i = 0; i < bookList.length; i++) {
      var book = bookList[i];
      if (book.checked) {
        totalCount += book.Count * 1;
        totalPrice += book.Count * book.Price;
      }
    }
    totalPrice = totalPrice.toFixed(2);
    this.setData({
      'totalCount': totalCount,
      'totalPrice': totalPrice
    })
  },

  /**
   * 用户点击商品减1
   */
  subtracttap: function(e) {
    var that = this
    var index = e.target.dataset.index;
    var bookList = this.data.bookList;
    var count = bookList[index].Count;
    if (count <= 1) {
      return;
    } else {
      bookList[index].Count--;
      this.setData({
        'bookList': bookList
      });
      this.calculateTotal();

      wx.request({
        url: "https://hunjianghu.xuanjis.com/web/AddCart.do?action=Modify&BookID=" + bookList[index].BookID + "&UserID=1&Count=" + bookList[index].Count,
        header: {
          "content-type": "json"
        },
        success: function(res) {

          if (res.statusCode == 200) {
            that.setData({
              book: res.data,
            })
            //wx.hideNavigationBarLoading()
          }
        }
      })

    }

  },

  /**
   * 用户点击商品加1
   */
  addtap: function(e) {
    var that = this
    var index = e.target.dataset.index;
    var bookList = this.data.bookList;
    var count = bookList[index].Count;
    bookList[index].Count++;
    this.setData({
      'bookList': bookList
    });
    this.calculateTotal();

    wx.request({
      url: "https://hunjianghu.xuanjis.com/web/AddCart.do?action=Modify&BookID=" + bookList[index].BookID + "&UserID=1&Count=" + bookList[index].Count,
      header: {
        "content-type": "json"
      },
      success: function(res) {

        if (res.statusCode == 200) {
          that.setData({
            book: res.data,
          })
          //wx.hideNavigationBarLoading()
        }
      }
    })

  },
  /**
   * 用户选择购物车商品
   */
  checkboxChange: function(e) {
    //console.log('checkbox发生change事件,携带value值为:', e.detail.value);
    var checkboxItems = this.data.bookList;
    var values = e.detail.value;
    for (var i = 0; i < checkboxItems.length; ++i) {
      checkboxItems[i].checked = false;
      for (var j = 0; j < values.length; ++j) {
        if (checkboxItems[i].BookID == values[j]) {
          checkboxItems[i].checked = true;
          break;
        }
      }
    }

    var checkAll = false;
    if (checkboxItems.length == values.length) {
      checkAll = true;
    }

    this.setData({
      'bookList': checkboxItems,
      'checkAll': checkAll
    });
    this.calculateTotal();
  },

  /**
   * 用户点击全选
   */
  selectalltap: function(e) {
    // console.log('用户点击全选,携带value值为:', e.detail.value);
    var value = e.detail.value;
    var checkAll = false;
    if (value && value[0]) {
      checkAll = true;
    }

    var bookList = this.data.bookList;
    for (var i = 0; i < bookList.length; i++) {
      var book = bookList[i];
      book['checked'] = checkAll;
    }

    this.setData({
      'checkAll': checkAll,
      'bookList': bookList
    });
    this.calculateTotal();
  },
  todetail:function(){
    wx.navigateTo({
      url:"/pages/detail/detail"
    })
  },


  todetail: function(e) {
    var id = e.target.dataset.id
    //console.log(id)
    wx.navigateTo({
      url: "/pages/detail/detail?id=" + id
    })
  },

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

  },

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

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function(options) {
    var that = this
    wx.request({
      url: "https://hunjianghu.xuanjis.com/web/AddCart.do?action=getinfo&UserID=1",
      header: {
        "content-type": "json"
      },
      success: function(res) {
        for (var i = 0; i < res.data.length; i++) {
          res.data[i].checked = false;
        }
        if (res.statusCode == 200) {
          that.setData({
            bookList: res.data,
            iscart: false
          })
          if (res.data.length == 0) {
            that.setData({
              iscart: true
            })
          }
          //wx.hideNavigationBarLoading()
        }
      }
    })
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
<<<<<<< HEAD
  newAddress: function () {
    if (this.data.totalCount >0)
    {wx.navigateTo({
      url: '/pages/order/order'
    })}
    else
    {
      wx.showToast({
        title: '没有选择商品',
        icon: 'success',
        duration: 2000
      })
    }
  }
})
=======

  /**
   * 点击结算触发
   */
  newAddress: function() {
    var that = this
    var checkList = [];
    // var flag = false

    if (this.data.totalCount > 0) {
      if (!this.iscart) { //购物车不空
        var bookLists = this.data.bookList
        for (var i = 0; i < bookLists.length; i++) {
          if (bookLists[i].checked) {
            var bookid = bookLists[i].BookID
            var count = bookLists[i].Count
            wx.request({
              url: 'https://hunjianghu.xuanjis.com/web/Buy.do?UserID=1&BookID=' + bookid + '&Count=' + count + '&MessageID=' + 1,
              header: {

              },
              success: function(res) {
                if (res.statusCode == 200) {
                  // that.setData({
                  //   // book: res.data,
                  //   flag :true
                  // })
                  //wx.hideNavigationBarLoading()
                  wx.showToast({
                    title: '订单生成成功!',
                    icon: 'success',
                    duration: 2000
                  })
                }
              }
            })

            wx.request({
              url: "https://hunjianghu.xuanjis.com/web/AddCart.do?action=Delete&BookID=" + bookid + "&UserID=1",
              header: {
                "content-type": "json"
              },
              success: function (res) {

                if (res.statusCode == 200) {
                  // that.setData({
                  //   book: res.data,
                  // })
                  //wx.hideNavigationBarLoading()
                }
              }
            })

          }
        }
      }
      
      

      // if (flag) {
        setTimeout(function() {
          //要延时执行的代码
          wx.navigateTo({
            url: '/pages/order/order?typeId=0'
          })
        }, 3000)
      // } else {
      //   wx.showToast({
      //     title: '订单创建失败',
      //     icon: 'success',
      //     duration: 2000
      //   })
      // }
    } else {
      wx.showToast({
        title: '没有选择商品',
        icon: 'success',
        duration: 2000
      })
    }
  }

})
>>>>>>> f23362a433f70f1876587a01c643e157b7e8b3ae