const user = require('../../../utils/cache.js');
import staff from '../../../api/StaffApi'


// pages/groupmeallist/goodsManagement/goodsManagement.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentData: 0,
    storeObject: {},
    allgoods: [],
    onlinetrue: [],
    onlinefalse: [],
    page: 1,
    pageSize: 5,
    keyword: '',
    text: 0,
    allTop: 0,
    onLineTop: 0,
    offLineTop: 0
  },


  //点击切换，滑块index赋值
  checkCurrent: function (e) {
    if (this.data.currentData === e.target.dataset.current) {
      return false;
    } else {
      this.setData({
        currentData: e.target.dataset.current
      })
    }
  },

  //获取当前滑块的index
  bindchange: function (e) {
    const that = this;
    that.setData({
      currentData: e.detail.current
    })
    console.log("切换导航2")

    this.reloadData(e.detail.current)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  reloadData(index) {
    this.data.page = 1
    this.getGoodsList()
  },

  gotoStoreList() {
    wx.redirectTo({
      url: '../goodsManagementStore/goodsManagementStore?way=' + '1',
    })
  },

  bottomefficientList() {
    let _self = this;
    _self.setData({
      page: _self.data.page + 1
    })
    _self.getGoodsList()
  },

  toggleline: function (e) {
    let goodid = e.currentTarget.dataset.goodid;

    let idx = e.currentTarget.dataset.idx;

    this.togglelinechange(goodid);

    let currentData = this.data.currentData

    if (currentData == 0) {
      this.data.allgoods[idx].isOnline = this.data.allgoods[idx].isOnline == 1 ? '0' : 1

      this.setData({
        allgoods: this.data.allgoods
      })

    } else if (currentData == 1) {
      this.data.onlinetrue[idx].isOnline = this.data.onlinetrue[idx].isOnline == 1 ? '0' : 1
      this.setData({
        onlinetrue: this.data.onlinetrue
      })
    } else {
      this.data.onlinefalse[idx].isOnline = this.data.onlinefalse[idx].isOnline == 1 ? '0' : 1
      this.setData({
        onlinefalse: this.data.onlinefalse
      })
    }
  },

  async togglelinechange(goodid) {

    await staff.toggleline({
      mobile: user.getMobile(),
      storeCode: this.data.storeObject.storeNo,
      goodsId: goodid
    })


    // await this.getGoodsList();

  },

  listenInput: function (e) {
    console.log("进入确定")
    let keyword = e.detail.value;
    this.data.lock = false;
    this.data.page = 1;
    this.setData({
      keyword,
    })
    this.getGoodsList();
  },


  async getGoodsList() {
    await staff.getGoodsStaffList({
      mobile: user.getMobile(),
      storeCode: this.data.storeObject.storeNo,
      state: this.data.currentData,
      keyword: this.data.keyword,
      page: this.data.page,
      pageSize: this.data.pageSize
    }).then(res => {
      let all = res.data
      if (this.data.currentData == 0) {
        if (this.data.page == 1) {
          this.setData({
            allgoods: all,
            allTop: 0,
          })
        } else {
          this.setData({
            allgoods: this.data.allgoods.concat(all)
          })
        }
      } else if (this.data.currentData == 1) {
        if (this.data.page == 1) {
          this.setData({
            onlinetrue: all,
            onLineTop: 0,
          })
        } else {
          this.setData({
            onlinetrue: this.data.onlinetrue.concat(all)
          })
        }

      } else if (this.data.currentData == 2) {
        if (this.data.page == 1) {
          this.setData({
            onlinefalse: all,
            offLineTop: 0,
          })
        } else {
          this.setData({
            onlinefalse: this.data.onlinefalse.concat(all)
          })
        }
      }
    })
  },

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

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

    let storeObject = user.getChooseStore();
    if (storeObject != null) {
      this.setData({
        storeObject: storeObject
      })
      this.getGoodsList()
    }


  },

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

  },

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

  },

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

  },

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

  },

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

  }
})