// packageD/teamSales/sales_management/sales_management.js
let app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    active: 0,
    date: "",
    fieldsType: "month",
    dateBol: true,
    datestr: "",
    date_2: "",
    fieldsType_2: "month",
    dateBol_2: true,
    datestr_2: "",
    time_types: 1,
    year: "",
    month: "",
    year_2: "",
    month_2: "",
    time_types_2: 1,
    datas: "",
    page: 1, //分页数，当前页数
    isLoadMore: true, //判断是否要加载更多的标志
    total_page: 0, //总页数
    sales_type: "commonly",
    month_sales: "",
    year_sales: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      year: String(new Date().getFullYear()),
      month: this.data.month
        ? this.data.month
        : String(
          new Date().getMonth() + 1 > 9
            ? new Date().getMonth() + 1
            : "0" + String(new Date().getMonth() + 1)
        ),
      year_2: String(new Date().getFullYear()),
      month_2: this.data.month
        ? this.data.month
        : String(
          new Date().getMonth() + 1 > 9
            ? new Date().getMonth() + 1
            : "0" + String(new Date().getMonth() + 1)
        ),
    });
    this.getData();
  },

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

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

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

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

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

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

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
  _getMoreData() {
    let that = this;
    this.data.isLoadMore = false; // 防止多次请求分页数据
    if (that.data.page >= that.data.total_page) {
      return;
    } else {
      that.data.page += 1;
      let json = {};
      json.year_2 = this.data.year_2;
      json.page = this.data.page;
      if (this.data.time_types_2 == 1) {
        json.time_types_2 = 1;
        json.month_2 = this.data.month_2;
      } else if (this.data.time_types_2 == 2) {
        json.time_types_2 = 2;
      }
      let urlStr = app.getNetAddresss(
        "plugin.goods-sales.frontend.controllers.list.index"
      );
      app._postNetWork({
        url: urlStr,
        showToastIn: false,
        data: json,
        success: function (resdata) {
          var res = resdata.data;
          if (res.result == 1) {
            var nextPageData = res.data.subordinate.data;
            that.setData({
              datas: that.data.datas.concat(nextPageData),
            });
          } else {
            that.data.page = that.page - 1;
            that.data.isLoadMore = false;
            return;
          }
        },
        fail: function (res) {
          console.log(res.msg);
        },
      });
    }
  },
  bindDateChange: function (e) {
    console.log("picker发送选择改变，携带值为", e.detail.value);
    let val = e.detail.value;
    let str = "";
    if (val) {
      str = val.split("-");
      if (str[1]) {
        this.setData({
          year_2: str[0],
          month_2: str[1],
        });
        str = str[0] + "年" + str[1] + "月";
      } else {
        this.setData({
          year_2: str[0],
        });
        str = str[0] + "年";
      }
      this.getnext();
      this.setData({
        datestr: str,
      });
    }
  },
  dateBtn(e) {
    let datetype = e.currentTarget.dataset.datetype;
    this.setData({
      fieldsType: datetype,
    });
    if (datetype == "year") {
      this.setData({
        dateBol: false,
        time_types_2: 2,
      });
    } else {
      this.setData({
        dateBol: true,
        time_types_2: 1,
      });
    }
    this.getnext();
  },
  bindDateChange_2: function (e) {
    console.log("picker发送选择改变，携带值为", e.detail.value);
    let val = e.detail.value;
    let str = "";
    if (val) {
      str = val.split("-");
      if (str[1]) {
        this.setData({
          year: str[0],
          month: str[1],
        });
        str = str[0] + "年" + str[1] + "月";
      } else {
        this.setData({
          year: str[0],
        });
        str = str[0] + "年";
      }
      this.setData({
        datestr_2: str,
      });
      this.getData();
    }
  },
  dateBtn_2(e) {
    let datetype = e.currentTarget.dataset.datetype;
    this.setData({
      fieldsType_2: datetype,
    });
    if (datetype == "year") {
      this.setData({
        dateBol_2: false,
        time_types: 2,
      });
    } else {
      this.setData({
        dateBol_2: true,
        time_types: 1,
      });
    }
    this.getData();
  },
  getData() {
    let that = this;
    let json = {};
    if (this.data.time_types == 1) {
      json.time_types = 1;
      json.year = this.data.year;
      json.month = this.data.month;
    } else if (this.data.time_types == 2) {
      json.time_types = 2;
      json.year = this.data.year;
    }
    let urlStr = app.getNetAddresss(
      "plugin.goods-sales.frontend.controllers.list.index"
    );
    app._postNetWork({
      url: urlStr,
      showToastIn: false,
      data: json,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            month_sales: res.data.month_sales,
            year_sales: res.data.year_sales,
          });
        } else {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1500,
          });
        }
      },
      fail: function (res) {
        console.log(res.msg);
      },
    });
  },
  onChange(e) {
    let index = e.detail.index;
    if (index == 1) {
      this.data.sales_type = "subordinate";
      this.getnext();
    } else {
      this.data.sales_type = "commonly";
      this.getData();
    }
  },
  //获取下级数据
  getnext() {
    let that = this;
    let json = {};
    this.data.isLoadMore = true; //判断是否要加载更多的标志
    this.data.total_page = 0; //总页数
    json.year_2 = this.data.year_2;
    json.page = this.data.page;
    if (this.data.time_types_2 == 1) {
      json.time_types_2 = 1;
      json.month_2 = this.data.month_2;
    } else if (this.data.time_types_2 == 2) {
      json.time_types_2 = 2;
    }
    let urlStr = app.getNetAddresss(
      "plugin.goods-sales.frontend.controllers.list.index"
    );
    app._postNetWork({
      url: urlStr,
      showToastIn: false,
      data: json,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.data.total_page = res.data.subordinate.last_page;
          if (!that.data.total_page) {
            that.data.total_page = 0;
          }
          that.setData({
            datas: res.data.subordinate.data,
          });
        } else {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1500,
          });
        }
      },
      fail: function (res) {
        console.log(res.msg);
      },
    });
  },
});
