// packageD/o2o/recommendlist/recommendlist.js
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    language: "",
    inpvalue: "",
    left_show: false,
    right_show: false,
    testarr: ["一级标题", "二级标题", "三级标题", "四级标题"],
    openSelect: false,
    // 商品展示的数量
    show_num: "",
    name: 1,
    result: [],
    first_name: "",
    ind: "",
    twos: "",
    page: 1,
    json: {},
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      store_id: options.store_id,
    });
    this.getStoreInfo();
    this.getdata();
  },
  ischang(e) {
    console.log(e);
  },
  bindPickerChange: function (e) {
    var first_ind = e.detail.value;
    this.setData({
      first_name: this.data.category[first_ind].name,
      parentid: this.data.category[first_ind].id,
      twos_list: this.data.category[first_ind].childrens,
    });
    var ajson = {
      store_id: this.data.store_id,
      page: "1",
      category: {
        parentid: this.data.parentid,
        childid: "",
      },
    };
    this.setData({
      isjson: ajson,
    });
    this.flSearch(this.data.isjson);
  },
  bindPickerChange_two: function (e) {
    var two_ind = e.detail.value;
    console.log(two_ind);
    this.setData({
      twos_name: this.data.twos_list[two_ind].name,
      childid: this.data.twos_list[two_ind].id,
    });
    var ajson = {
      store_id: this.data.store_id,
      page: "1",
      category: {
        parentid: this.data.parentid,
        childid: this.data.childid,
      },
    };
    this.setData({
      isjson: ajson,
    });
    this.flSearch(this.data.isjson);
  },
  inpbtn(e) {
    console.log(e.detail.value);
    this.setData({
      inpvalue: e.detail.value,
    });
  },
  inpbtnclear(e) {
    console.log(e.detail.value);
  },
  tapOn(e) {
    console.log(e.currentTarget.dataset.tap);
    let tapname = e.currentTarget.dataset.tap;
    if (tapname == "left") {
      this.setData({
        left_show: !this.data.left_show,
      });
    } else if (tapname == "right") {
      this.setData({
        right_show: !this.data.right_show,
      });
    }
  },
  checkAll(e) {
    console.log(e);
  },
  rentAllSelectHandle(event) {
    console.log(event);
    this.setData({
      result: event.detail,
    });
  },
  tapfirst(e) {
    console.log(e.currentTarget.dataset.name);
    this.setData({
      first_name: e.currentTarget.dataset.name,
    });
  },
  btntap() {
    console.log(this.data.result);
    let json = {
      store_id: this.data.store_id,
      choose_goods: this.data.result,
    };
    this.tapPush(json);
  },
  goText() {
    wx.navigateTo({
      url:
        "/packageD/o2o/recommendtext/recommendtext?store_id=" +
        this.data.store_id +
        "&&show_num=" +
        this.data.show_num,
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    let language = wx.getStorageSync("langIndex");
    this.setData({ language: language.en });
  },

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

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

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

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

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log(this.data.current_page, this.data.last_page);
    console.log(this.data.json);
    console.log(app._isTextEmpty(this.data.json));
    if (this.data.current_page <= this.data.last_page) {
      wx.showToast({
        title: "没有更多",
        icon: "none",
        duration: 1500,
      });
    } else if (this.data.current_page > this.data.last_page) {
      let pageAge = this.data.page + 1;
      this.setData({
        page: pageAge,
      });
      if (!app._isTextEmpty(this.data.json)) {
        this.loadinginfo(this.data.json);
      } else {
        this.getStoreInfo();
      }
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
  getdata() {
    let that = this;
    let urlStr = app.getNetAddresss(
      "plugin.store-cashier.frontend.store.near-by-goods.get-store-goods"
    );
    app._postNetWork({
      url: urlStr,
      data: {
        store_id: that.data.store_id,
      },
      success: function (resdata) {
        var res = resdata.data;
        console.log(res);
        if (res.result == 1) {
          that.setData({
            show_num: res.data.show_num,
          });
        } else {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1500,
          });
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  getStoreInfo() {
    let that = this;
    let urlStr = app.getNetAddresss(
      "plugin.store-cashier.frontend.store.near-by-goods.index"
    );
    app._postNetWork({
      url: urlStr,
      data: {
        store_id: that.data.store_id,
        page: this.data.page,
      },
      success: function (resdata) {
        var res = resdata.data;
        console.log(res);
        if (res.result == 1) {
          that.setData({
            goods_data: res.data.goods_data.data,
            category: res.data.category,
            current_page: res.data.goods_data.current_page,
            last_page: res.data.goods_data.last_page,
          });
          console.log(res.data.goods_data);
        } else {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1500,
          });
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  flSearch(json) {
    let that = this;
    let urlStr = app.getNetAddresss(
      "plugin.store-cashier.frontend.store.near-by-goods.get-goods-list"
    );
    app._postNetWork({
      url: urlStr,
      data: json,
      success: function (resdata) {
        var res = resdata.data;
        console.log(res);
        if (res.result == 1) {
          that.setData({
            goods_data: res.data.list.data,
            current_page: res.data.list.current_page,
            last_page: res.data.list.last_page,
          });
          if (res.data.list.data.length == 0) {
            wx.showToast({
              icon: "none",
              title: "没有相关商品",
              duration: 1500,
            });
          }
        } else {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1500,
          });
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  loadinginfo(json) {
    let that = this;
    let urlStr = app.getNetAddresss(
      "plugin.store-cashier.frontend.store.near-by-goods.get-goods-list"
    );
    app._postNetWork({
      url: urlStr,
      data: json,
      success: function (resdata) {
        var res = resdata.data;
        console.log(res);
        if (res.result == 1) {
          that.setData({
            goods_data: res.data.list.data,
            current_page: res.data.current_page,
            last_page: res.data.last_page,
          });
          if (res.data.list.data.length == 0) {
            wx.showToast({
              icon: "none",
              title: "没有相关商品",
              duration: 1500,
            });
          }
        } else {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1500,
          });
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  tapPush(json) {
    let urlStr = app.getNetAddresss(
      "plugin.nearby-store-goods.frontend.controllers.goods.index"
    );
    app._postNetWork({
      url: urlStr,
      data: json,
      success: function (resdata) {
        var res = resdata.data;
        console.log(res);
        if (res.result == 1) {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1500,
            success: function () {
              wx.navigateBack({
                delta: 1,
              });
            },
          });
        } else {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1500,
          });
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
});
