// pages/member/CommodityManagement/CommodityManagement.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    language: '',
    store: '',
    store_id: '',
    commodity_data: [],
    goods_length: '',
    type: '',
    // more
    isLoadMore: true,
    page: 1,
    total_page: 0,
    goods_audit: 0,
    goods_name:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.store) {
      this.setData({
        store: options.store,
        type: options.store
      });
    }
    if (options.store_id) {
      this.setData({
        store_id: options.store_id
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    let language = wx.getStorageSync('langIndex');
    this.setData({ 'language': language.en });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.initData();
    this._getJson();
    if (this.data.store == 'store') {
      this.getGoodsAaudit();
    }
  },

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

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

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

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.isLoadMore) {
      this.getMoreData();
    } else {
      console.log('没有更多数据');
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () { },
  // 商品数据获取
  _getJson() {
    let that = this;
    let urls = '';
    let json = {};
    if (this.data.store == 'store') {
      urls = 'plugin.store-cashier.frontend.store.goods.get-goods-list';
      json = {
        store_id: this.data.store_id
      };
    } else if (this.data.store == 'supplier') {
      urls = 'plugin.supplier.frontend.goods.get-goods-list';
      json = {
        sid: this.data.store_id
      };
    }
    let urlStr = app.getNetAddresss(urls);
    app._postNetWork({
      url: urlStr,
      showToastIn: false,
      data: json,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            commodity_data: []
          });
          if (that.data.store == 'store') {
            for (let i = 0; i < res.data.goods_data.length; i++) {
              res.data.goods_data[i].edit_active = false;
            }
            console.log(res.data, '数组的合并');

            that.setData({
              goods_length: res.data.count,
              total_page: res.data.goods_data.last_page,
              commodity_data: res.data.goods_data.data
            });
            if (app.data.host == 'https://dev3.yunzshop.com/') {
              that.setData({
                commodity_data: res.data.goods_data
              });
            }
          } else if (that.data.store == 'supplier') {
            for (let i = 0; i < res.data.list.length; i++) {
              res.data.list[i].edit_active = false;
            }
            console.log(res.data, '数组的格式');

            that.setData({
              total_page: res.data.list.last_page,
              goods_length: res.data.list.data.length,
              commodity_data: res.data.list.data
            });
          }
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function (res) { }
    });
  },
  // 更多
  moreEvent(e) {
    let index = e.currentTarget.dataset.index;
    if (this.data.commodity_data[index].edit_active == false) {
      this.emptyEditActive();
      this.setData({
        ['commodity_data[' + index + '].edit_active']: true
      });
    } else {
      this.emptyEditActive();
    }
  },
  // 清空编辑删除状态
  emptyEditActive() {
    for (var i = 0; i < this.data.commodity_data.length; i++) {
      this.setData({
        ['commodity_data[' + i + '].edit_active']: false
      });
    }
  },
  // 路由跳转
  routeTake() {
    wx.navigateTo({
      url: '/packageA/member/supplier/CommodityRelease/CommodityRelease?store_id=' + this.data.store_id + '&store=' +this.data.store
    });
    this.emptyEditActive();
  },
  // 商品删除
  commodityDelete(e) {
    let that = this;
    let index = e.currentTarget.dataset.index;
    this.emptyEditActive();
    wx.showModal({
      title: '提示',
      content: '此操作将永久删除该商品, 是否继续?',
      success(res) {
        if (res.confirm) {
          let urlStr = '';
          let json = {};
          if (that.data.store == 'store') {
            urlStr = app.getNetAddresss('plugin.store-cashier.frontend.store.goods.destroy-goods');
            json = {
              goods_id: that.data.commodity_data[index].id
            };
          } else if (that.data.store == 'supplier') {
            urlStr = app.getNetAddresss('plugin.supplier.frontend.goods.del-goods');
            json = {
              goods_id: that.data.commodity_data[index].id
            };
          }
          app._postNetWork({
            url: urlStr,
            showToastIn: false,
            data: json,
            success: function (resdata) {
              var res = resdata.data;
              if (res.result == 1) {
                wx.showToast({
                  icon: 'none',
                  title: '商品删除成功',
                  duration: 1500
                });
                let commodity_data = that.data.commodity_data;
                commodity_data.splice(index, 1);
                that.setData({
                  commodity_data: commodity_data,
                  goods_length: Number(that.data.goods_length) - 1
                });
              } else {
                wx.showToast({
                  icon: 'none',
                  title: res.msg,
                  duration: 1500
                });
              }
            },
            fail: function (res) { }
          });
        } else if (res.cancel) {
          wx.showToast({
            icon: 'none',
            title: '已取消删除',
            duration: 1500
          });
        }
      }
    });
  },
  editJumpEvent(e) {
    let index = e.currentTarget.dataset.index;
    console.log(this.data.commodity_data[index].id, index, '11111222');
    // return
    wx.navigateTo({
      url: '/packageA/member/supplier/CommodityRelease/CommodityRelease?store_id=' + this.data.store_id + '&goods_id=' +
        this.data.commodity_data[index].id + '&store=' + this.data.store
    });
  },
  // editJumpEvent(e) {
  // 	let index = e.currentTarget.dataset.index
  // 	wx.navigateTo({
  // 		url: '/packageA/member/supplier/CommodityEditor/CommodityEditor?store_id=' + this.data.store_id + '&goods_id=' +
  // 			this.data.commodity_data[index].id + '&store=' + this.data.store
  // 	})
  // },
  initData() {
    this.setData({
      commodity_data: [],
      goods_length: '',
      isLoadMore: true,
      page: 1,
      total_page: 0,
      goods_audit: 0
    });
  },
  changeGoodsStatus(e) {
    let item = e.currentTarget.dataset.item;
    let index = e.currentTarget.dataset.index;
    if (this.data.store === 'supplier') {
      return;
    }
    let status = 0;
    if (item.status !== 1) {
      status = 1;
    }
    let that = this;
    let urlStr = app.getNetAddresss('plugin.store-cashier.frontend.store.goods.changeGoodsStatus');
    app._getNetWork({
      url: urlStr,
      data: {
        goods_id: item.id,
        status: status
      },
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          if (status === 1) {
            that.setData({
              ['commodity_data[' + index + '].status']: 1
            });
            wx.showToast({
              title: '上架成功',
              icon: 'none',
              duration: 1500
            });
          } else {
            that.setData({
              ['commodity_data[' + index + '].status']: 0
            });
            wx.showToast({
              title: '下架成功',
              icon: 'none',
              duration: 1500
            });
          }
        } else {
          if (status === 1) {
            wx.showToast({
              title: '上架失败',
              icon: 'none',
              duration: 1500
            });
          } else {
            wx.showToast({
              title: '下架失败',
              icon: 'none',
              duration: 1500
            });
          }
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  // 获取更多数据
  getMoreData() {
    const that = this;
    let urls = '';
    let json = {};
    if (that.data.store == 'store') {
      urls = app.getNetAddresss('plugin.store-cashier.frontend.store.goods.get-goods-list');
      json = {
        store_id: that.data.store_id
      };
    } else if (that.data.store == 'supplier') {
      urls = app.getNetAddresss('plugin.supplier.frontend.goods.get-goods-list');
      json = {
        sid: that.data.store_id
      };
    }
    that.setData({
      isLoadMore: false
    });
    if (this.data.page >= this.data.total_page) {
      return;
    } else {
      that.setData({
        page: this.data.page + 1
      });
      json.page = that.data.page;
      app._getNetWork({
        url: urls,
        data: json,
        success: function (resdata) {
          var res = resdata.data;
          if (res.result == 1) {
            if (that.data.store == 'store') {
              for (var i = 0; i < res.data.goods_data.data.length; i++) {
                res.data.goods_data.data[i].edit_active = false;
              }
              that.setData({
                commodity_data: that.data.commodity_data.concat(res.data.goods_data.data)
              });
            } else if (that.data.store == 'supplier') {
              for (var j = 0; j < res.data.list.data.length; j++) {
                res.data.list.data[j].edit_active = false;
              }
              that.setData({
                commodity_data: that.data.commodity_data.concat(res.data.list.data)
              });
            }
            that.setData({
              isLoadMore: true
            });
          } else {
            that.setData({
              page: that.data.page - 1,
              isLoadMore: false
            });
          }
        },
        fail: function (res) {
          console.log(res);
        }
      });
    }
  },
  onCancel(){
    this.setData({goods_name:""})
  },
  onChange(event){
    console.log(event)
    this.setData({
      goods_name:event.detail
    })
  },
  onSearch(){
    let url = '';
    let json = {}
    if(this.data.store == 'supplier'){
      url = 'plugin.supplier.frontend.goods.get-goods-list',
      json = {
        goods_name:this.data.goods_name,
        sid: this.data.store_id
      }
    }else{
      url = 'plugin.store-cashier.frontend.store.goods.get-goods-list'
      json = {
        goods_name:this.data.goods_name,
        store_id: this.data.store_id
      }
    }
    let urlr = app.getNetAddresss(url);
    app._getNetWork({
      url: urlr,
      data: json,
      success:  (resdata)=> {
        console.log(resdata)
        if(resdata.data.result == 1){
          if(this.data.store == 'supplier'){
            this.setData({
              commodity_data:resdata.data.data.list.data,
              page:resdata.data.data.list.current_page,
              total:resdata.data.data.list.last_page
            })
          }else{
            this.setData({
              commodity_data:resdata.data.data.goods_data.data,
              page:resdata.data.data.goods_data.current_page,
              total:resdata.data.data.goods_data.last_page
            })

          }
        }else{
          wx.showToast({
            icon:"none",
            title: resdata.data.msg,
          })
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  getGoodsAaudit() {
    let that = this;
    let urlStr = app.getNetAddresss('plugin.store-cashier.frontend.store.setting.get-goods-audit');
    app._getNetWork({
      url: urlStr,
      data: {},
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            goods_audit: res.data.goods_audit
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
});
