const product = require('../../utils/product.js')

Page({
  data: {
    toastAddProduct: true,
    title: '',
    id: 0,
    quantity: 1,
    product: {}
  },

  onLoad (params) {
    var id = params.id
    var product = wx.getStorageSync('products').find(function(i){
      return i.id === id
    })

    this.setData({
      id:id,
      product:product
    })
  },

  onReady () {
    wx.setNavigationBarTitle({ title: this.data.title })
  },

  bindAddToCart (e) {
    var that = this
    var cartItems = wx.getStorageSync('cartItems') || []

    var exist = cartItems.find(function(ele){
      return ele.id === that.data.id
    })

    if (exist) {
      exist.quantity = parseInt(exist.quantity) + 1
    } else {
      cartItems.push({
        id: this.data.id,
        quantity: this.data.quantity,
        product: this.data.product
      })
    }
    this.setData({ toastAddProduct:false });
    wx.setStorage({
      key: 'cartItems',
      data: cartItems
    })
  },

  bindQuantityInput (e) {
    this.setData({'quantity': e.detail.value})
  },

  toastChange: function(){
    this.setData({ toastAddProduct:true });
  }
})

@@ -323,16 +323,17 @@
      id:
+ 
 id,%0A    
@@ -342,23 +342,51 @@
 product:
+ 
 product
+,%0A      title: product.name
 %0A    %7D)%0A
@@ -400,17 +400,16 @@
  onReady
- 
 () %7B%0A   

Fix navigation bar title display.