// packageE/article/article.js
var app = getApp();
Component({
  properties: {
    datas: {
      type: null
    },
    component_id: {
      type: null
    },
    U_tabcontrol: {
      type: null
    },
    bottomShowBolIndex: {
      type: null
    },
    index: {
      type: null
    },
    plugin_active: String
  },
  // 私有数据，可用于模板渲染
  data: {
    emptyImage: 'https://mini-app-img-1251768088.cos.ap-guangzhou.myqcloud.com/image.png',
    clientWidth: '375',
    language: '',

    active: "0",
    class_id: "0",
    list: [],
    categories: [],
    isLoadMore: true,
    page: 1,
    total_page: 0,
    pageSize: 12,
    noNotice: false,
    allLoaded: 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.datas.get_style == 1 || this.data.datas.get_style == 3) {
      this.initData();
      //获取数据
      this.getNetData(this.data.datas);
    } else {
      this.setData({
        isLoadMore: false,
        allLoaded: true
      });
    }
  },

  pageLifetimes: {
    // 组件所在页面的生命周期函数
    show() {},
    hide() {},
    resize() {},
  },
  methods: {
    initData() {
      this.setData({
        page: 1,
        total_page: 0,
        isLoadMore: true,
        list: [],
      });
    },
    //获取数据
    getNetData(data) {
      let that = this;

      if (data) {
        this.setData({
          noNotice: false,
          total_page: data.list.last_page,
          pageSize: data.list.per_page,
          isLoadMore: true
        });

        if (that.data.total_page <= 1) {
          this.setData({
            isLoadMore: false,
            allLoaded: true
          });
        }

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

        this.setData({
          list: data.list.data
        });

        if (data.cate) {
          this.setData({
            categories: data.cate,
            class_id: data.cate[0] ? data.cate[0].id : "0",
            active: data.cate[0] ? data.cate[0].id : "0",
          });
          if (data.cate.length <= 0) {
            this.setData({
              noNotice: true
            });
          }
        }

        return;
      }

      let json = {
        category_id: this.data.class_id,
        page: 1,
        pageSize: 12
      };
      let urlStr = app.getNetAddresss("plugin.article.api.article.get-articles");
      app._getNetWork({
        url: urlStr,
        data: json,
        success: (resdata) => {
          var res = resdata.data;
          if (res.result == 1) {
            that.setData({
              noNotice: false,
              total_page: res.data.articles.last_page,
              pageSize: res.data.articles.per_page,
              isLoadMore: true
            });
            if (that.data.total_page <= 1) {
              that.setData({
                isLoadMore: false,
                allLoaded: true
              });
            }
            if (res.data.categories.length <= 0) {
              that.setData({
                noNotice: true
              });
            }

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

            that.setData({
              list: res.data.articles.data,
            });

          } else {
            that.setData({
              noNotice: true
            });
          }
        },
        fail: function (res) {
          console.log(res);
        }
      });

    },
    toNoticeInfo(e) {
      let item = e.currentTarget.dataset.item;
      wx.navigateTo({
        url: '/packageA/member/article/articleContent/articleContent?article_id=' + item.id
      });
    },
    onClick(e) {
      let index = e.detail.index;
      console.log(this.data.categories, '188');
      this.initData();
      if (index === 0) {
        if (this.data.datas.get_style == 1) {
          //分类的"全部"只在get_style == 1的时候才出现
          this.setData({
            class_id: '0',
            active: '0'
          });
        } else {
          this.setData({
            class_id: this.data.categories[index].id,
            active: String(this.data.categories[index].id)
          });
        }
      } else {
        this.setData({
          class_id: this.data.datas.get_style == 1 ? this.data.categories[index - 1].id : this.data.categories[index].id,
          active: this.data.datas.get_style == 1 ? String(this.data.categories[index - 1].id) : String(this.data.categories[index].id)
        });
      }
      this.getNetData();
    },
    getMoreData() {
      let that = this;
      that.setData({
        isLoadMore: false
      });
      if (this.data.page >= this.data.total_page || this.data.list.length > this.data.datas.get_number) {
        that.setData({
          isLoadMore: false
        });
      } else {
        that.setData({
          page: this.data.page + 1
        });
        let json = {
          category_id: this.data.class_id,
          page: this.data.page,
          pageSize: this.data.pageSize
        };
        let urlStr = app.getNetAddresss("plugin.article.api.article.get-articles");
        app._getNetWork({
          url: urlStr,
          data: json,
          success: (resdata) => {
            var res = resdata.data;
            if (res.result == 1) {
              that.setData({
                total_page: res.data.articles.last_page,
              });
              if (this.data.page < this.data.total_page) {
                that.setData({
                  isLoadMore: true
                });
              } else {
                that.setData({
                  allLoaded: true
                });
              }

              let data = that.data.list.concat(res.data.articles.data);
              if (data.length > this.data.datas.get_number) {
                data.splice(this.data.datas.get_number, data.length - this.data.datas.get_number);
                that.setData({
                  isLoadMore: false,
                  allLoaded: true
                });
              }
              that.triggerEvent("setLoadMore", {
                isLoadMore: that.data.isLoadMore,
              });
              that.setData({
                list: data
              });

            } else {
              that.setData({
                page: that.data.page - 1,
                isLoadMore: false,
                allLoaded: true
              });
            }
          },
          fail: function (res) {
            console.log(res);
          }
        });
      }
    }
  }
});