// pages/applyMedince/applyMedince.js
const app = getApp()
import {
  cart,
  editCart,
  goodList
} from "../../../api/goodsCar"
import {throttle} from "../../../utils/util"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    product_keyword: '',
    totalNumber: 0,
    isFocus:false,
    isLock: false,
    show: true,
    toggle: false,
    hideInput: false,
    list: [],
    cart: [],
    toggle: false,
    medinceSearchList: [],
    img_host:'https://oss.prod.applets.igandanyiyuan.com/applet/patient/static'
  },
  HandleGoodList() {
    let {
      product_keyword
    } = this.data;
  
    goodList({
      product_keyword: product_keyword
    }).then(data => {
      this.setData({
        list: data
      })
    })
  },
  getCart() {
    cart().then(data => {
      let cart = 0;
      for (let i = 0; i < data.length; i++) {
        cart = cart + data[i].shopping_cart_num;
      };
      this.setData({
        totalNumber: cart
      })
    })
  },
  minusGoods(event) {
    if (!this.data.isLock) {
      this.setData({
        isLock:true
      })
      let index = event.currentTarget.dataset.id;
      let shopping_cart_num = this.data.list[index].shopping_cart_num;
      let product_id = this.data.list[index].product_id;
      if(shopping_cart_num==0){
        this.setData({
          isLock:false
        })
        return false
      }
      --shopping_cart_num;
      this.handleEditCart(product_id, shopping_cart_num, index, 'minus');
    }

  },
  addGoods(event) {
    if(!this.data.isLock){
      this.setData({
        isLock:true
      })
    let index = event.currentTarget.dataset.id;
    let shopping_cart_num = this.data.list[index].shopping_cart_num;
    let stock = this.data.list[index].stock;
    let product_id = this.data.list[index].product_id;
    if(shopping_cart_num>=5){
      wx.showToast({
        title: '每个药品购买数量不超过5个',
        icon: "none"
      })
      this.setData({
        isLock:false
      })
      return false
    }
    if (shopping_cart_num == stock) {
      this.setData({
        isLock:false
      })
      wx.showToast({
        title: '该商品库存已不足',
        icon: "none"
      })
      return false
    }
    ++shopping_cart_num;
    this.handleEditCart(product_id, shopping_cart_num, index, 'add');
    }
    
  },
  goBack(){
    wx.navigateBack({
      delta: 1,
    })
   },
   changeIpt(event){
    this.setData({
      product_keyword:event.detail.value
    })
   },
   goSearchByBtn(){
    this.handleSearch(this.data.product_keyword)
   },
  handleEditCart(product_id, shopping_cart_num, index, type) {
    let item = `list[${index}].shopping_cart_num`;
    editCart({
      product_id: product_id,
      shopping_cart_num: shopping_cart_num
    }).then(data => {
      let number = this.data.totalNumber;
      if (type == "add") {
        ++number
      } else {
        --number
      };
      this.setData({
         [item]: shopping_cart_num,
         totalNumber: number,
          isLock:false
      })
    })
  },
  goSearch(event) {
    this.setData({
       toggle: true,
      product_keyword: event.detail.value
    });
    this.handleSearch(event.detail.value)
  },
  handleSearch(value) {
    if(!value) {
      wx.showToast({
        title: '搜索内容不能为空',
        icon:"none"
      })
      return false
    }
    let serachList = wx.getStorageSync('medinceSearchList');
    let serachArr = [];
    if (serachList) {
      serachArr = JSON.parse(serachList);
    };

    var index = serachArr.indexOf(value);
    if (index != -1) {
      serachArr.splice(index, 1);
    };
    if(serachArr.length>=30){
      serachArr.pop();
    };
    serachArr.unshift(value);
    wx.setStorageSync('medinceSearchList', JSON.stringify(serachArr));
    this.setData({
      medinceSearchList: serachArr,
      product_keyword:value
    })
    this.HandleGoodList();
  },
  selectHistroy(event) {
    let value = event.currentTarget.dataset.id;
    this.setData({
      product_keyword: value,
      toggle: true
    });
    this.handleSearch(value)
  },
  delSearch() {
    wx.setStorageSync('medinceSearchList', '');
    this.getSearchList();
    wx.showToast({
      title: '搜索历史已清空',
      icon: 'none'
    })
  },
  getSearchList() {
    let serachList = wx.getStorageSync('medinceSearchList');
    let serachArr = [];
    if (serachList) {
      serachArr = JSON.parse(serachList);
    };
    this.setData({
      medinceSearchList: serachArr
    })
  },
  goMedinceList:throttle(function() {
    app.method.navigateTo({
      url: '/patient/pages/medinceList/medinceList',
    })
  }),
  handleFcous() {
    this.setData({
      show: false
    });
    setTimeout(()=>{
      this.setData({
        isFocus: true
      });
      this.HandleGoodList('');
    },600)
  },
  // handleIpt(event) {
  //   if (event.detail.value) {
  //     this.setData({
  //       hideInput: true
  //     })
  //   } else {
  //     this.setData({
  //       hideInput: false
  //     })
  //   }
  // },
  addNumber() {
    this.setData({
      totalNumber: ++this.data.totalNumber
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */


  onLoad(options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      show:true,
      img_host:app.hostConfig().imghost
    })
    this.getSearchList();
    this.getCart();
  },

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

  },

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

  },

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

  },

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

  },

  /**
   * 用户点击右上角分享
   */
  
})