// packageE/community/community.js
var location = require("../../../mybehaviors/location/location");
var app = getApp();
Component({
  behaviors: [location],
  properties: {
    datas: {
      type: null,
    },
    component_id: {
      type: null,
    },
    U_tabcontrol: {
      type: null,
    },
    btnFlag: {
      type: null
    },
    bottomShowBolIndex: {
      type: null,
    },
    index: {
      type: null,
    },
    store_id: {
      type: null,
    },
    plugin_active: {
      // 会员中心商品加载
      type: null,
    },
    store_alone_temp: {
      // 门店独立判断
      type: null,
    }
  },
  // 私有数据，可用于模板渲染
  data: {
    emptyImage: "https://mini-app-img-1251768088.cos.ap-guangzhou.myqcloud.com/image.png",
    clientWidth: "375",
    language: "",

    active: 0,
    cate: [],
    cateList: [],
    newClassification: "",
    nearbyClassification: "",
    address: "",
    city: "",
    point: "",

    //more
    isLoadMore: true,
    page: 1,
    total_page: 0,
    getMoreDataType: "newClassification",
    cateMoreData: {},
    loading: false,
  },

  lifetimes: {
    // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
    attached() {
      let language = wx.getStorageSync("langIndex");
      this.setData({
        language: language.en
      });
    },
    moved() {},
    detached() {},
  },

  // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
  attached() {},
  // 此处attached的声明会被lifetimes字段中的声明覆盖
  ready() {
    this.setData({
      clientWidth: wx.getSystemInfoSync().windowWidth,
    });

    if (this.data.U_tabcontrol) {
      if (Number(this.data.datas.get_number) > 0) {
        this.getFirstData(this.data.datas.list);
      }
    } else if (this.data.datas.get_style == 2) {
      this.setData({
        newClassification: this.data.datas.list,
        isLoadMore: false
      });
    } else {

      if(this.data.datas.show_nearby) this._getLocation();
      this.getHomePageData(this.data.datas.cate);
      if (Number(this.data.datas.get_number) > 0) {
        this.getFirstData(this.data.datas.list);
      }
    }
  },

  pageLifetimes: {
    // 组件所在页面的生命周期函数
    show() {},
    hide() {},
    resize() {},
  },
  methods: {
    getHomePageData(data) {
      let that = this;
      that.setData({
        cate: data,
      });
      let cateList = that.data.cateList;
      for (let i = 0; i < that.data.cate.length; i++) {
        cateList.push([]);
      }
      that.setData({
        cateList: cateList,
      });
    },
    getFirstData(data) {
      if (data) {
        this.setData({
          total_page: data.last_page,
        });

        if (this.data.page < this.data.total_page) {
          this.setData({
            isLoadMore: true,
          });
        } else {
          this.setData({
            isLoadMore: false,
          });
        }

        if (data.data.length > this.data.datas.get_number) {
          data.data.splice(
            this.data.datas.get_number,
            data.data.length - this.data.datas.get_number
          );
          this.setData({
            isLoadMore: false,
          });
        }

        this.setData({
          newClassification: data.data,
        });
      }
    },
    getClassification(type, id, index) {
      let that = this;
      let urlStr = app.getNetAddresss(
        "plugin.micro-communities.api.index.getStick"
      );
      that.pageInit();
      let dataJson = {};
      if (id) {
        dataJson.cat_id = id;
      } else {
        if (type == "newClassification") {
          dataJson.trick_new = 1;
        } else if (type == "nearbyClassification") {
          dataJson.lng = this.data.point.lng;
          dataJson.lat = this.data.point.lat;
          dataJson.city_name = this.data.city;
          dataJson.address = this.data.city;
        }
      }

      if (this.data.store_id) {
        dataJson.store_id = this.data.store_id;
      }
      app._postNetWork({
        url: urlStr,
        showToastIn: false,
        data: dataJson,
        success: (resdata) => {
          var res = resdata.data;
          if (res.result == 1) {
            that.data.total_page = res.data.last_page;

            if (that.data.page < that.data.total_page) {
              that.setData({
                isLoadMore: true,
              });
            }

            if (id) {
              that.setData({
                ["cateList[" + index + "]"]: res.data.data,
              });
            } else {
              if (type == "newClassification") {
                if (res.data.data.length > this.data.datas.get_number) {
                  res.data.data.splice(
                    this.data.datas.get_number,
                    res.data.data.length - this.data.datas.get_number
                  );
                  this.setData({
                    isLoadMore: false,
                  });
                }

                that.setData({
                  newClassification: res.data.data,
                });
              } else if (type == "nearbyClassification") {
                if (res.data.data.length > this.data.datas.get_number) {
                  res.data.data.splice(
                    this.data.datas.get_number,
                    res.data.data.length - this.data.datas.get_number
                  );
                  this.setData({
                    isLoadMore: false,
                  });
                }

                that.setData({
                  nearbyClassification: res.data.data,
                });
              }
            }
          } else {
            wx.showToast({
              icon: "none",
              title: res.msg,
              duration: 1500,
            });
          }
        },
        fail: function (res) {},
      });
    },
    getClassificationBtn(e) {
      let index = e.detail.index;
      let title = e.detail.title;
      if (title == "最新") {
        this.getClassification("newClassification");
        this.data.getMoreDataType = "newClassification";
        this.setData({
          active: "0",
        });
      } else if (title == "附近") {
        this.getClassification("nearbyClassification");
        this.data.getMoreDataType = "nearbyClassification";
        this.setData({
          active: "near",
        });
      } else {
        let indexData = this.data.datas.show_nearby ? index - 2 : index - 1;
        this.getClassification("", this.data.cate[indexData].id, indexData);
        this.data.getMoreDataType = "cate";
        this.data.cateMoreData = {
          type: "",
          id: this.data.cate[indexData].id,
          index: indexData,
        };
        this.setData({
          active: this.data.cate[indexData].id,
        });
      }
    },
    pageInit() {
      this.setData({
        page: 1,
        total_page: 0,
        isLoadMore: false,
        loading: false,
      });
    },
    jumpDetails(e) {
      let stick_id = e.currentTarget.dataset.stickid;
      wx.navigateTo({
        url: "/packageC/micro_communities/microCommentDetails/microCommentDetails?stickId=" +
          stick_id,
      });
    },
    getMoreData() {
      const that = this;
      let json = {};
      if (this.data.getMoreDataType == "newClassification") {
        json.trick_new = 1;
      } else if (this.data.getMoreDataType == "nearbyClassification") {
        json.lng = this.data.point.lng;
        json.lat = this.data.point.lat;
        json.city_name = this.data.city;
        json.address = this.data.city;
      } else {
        json.cat_id = that.data.cateMoreData.id;
      }
      if (this.data.store_id) {
        json.store_id = this.data.store_id;
      }

      this.setData({
        isLoadMore: false,
      }); // 防止多次请求分页数据
      let urlStr = app.getNetAddresss(
        "plugin.micro-communities.api.index.getStick"
      );
      if (this.data.store_alone_temp == 1) {
        // 门店独立
        urlStr = app.getNetAddresss("plugin.store-alone-temp.frontend.member.nav-data");
        urlStr += '&code=' + that.data.plugin_active;
      }
      if (this.data.page >= this.data.total_page) {
        that.setData({
          loading: true,
        });
        return;
      } else {
        that.data.page = this.data.page + 1;
        json.page = this.data.page;
        app._postNetWork({
          url: urlStr,
          showToastIn: false,
          data: json,
          success: function (resdata) {
            var res = resdata.data;
            if (res.result == 1) {
              var myData = res.data.data;

              if (that.data.page < that.data.total_page) {
                that.setData({
                  isLoadMore: true,
                });
              }
              if (that.data.getMoreDataType == "newClassification") {
                if (myData.length > that.data.datas.get_number) {
                  myData.splice(
                    that.data.datas.get_number,
                    myData.length - that.data.datas.get_number
                  );
                  this.setData({
                    isLoadMore: false,
                  });
                }

                that.setData({
                  newClassification: that.data.newClassification.concat(myData),
                });
              } else if (that.data.getMoreDataType == "nearbyClassification") {
                if (myData.length > this.data.datas.get_number) {
                  myData.splice(
                    this.data.datas.get_number,
                    myData.length - this.data.datas.get_number
                  );
                  this.setData({
                    isLoadMore: false,
                  });
                }

                that.setData({
                  nearbyClassification: that.data.nearbyClassification.concat(
                    myData
                  ),
                });
              } else {
                let cateList = that.data.cateList[that.data.cateMoreData.index];

                that.setData({
                  ["cateList[" +
                    that.data.cateMoreData.index +
                    "]"
                  ]: cateList.concat(myData),
                });
              }
              that.triggerEvent("setLoadMore", {
                isLoadMore: that.data.isLoadMore,
              });
            } else {
              that.data.page = that.data.page - 1;
              this.setData({
                isLoadMore: false,
              });
            }
          },
          fail: function (res) {},
        });
      }
    },
  },
});