// packageE/others/case_library/new_mobile/caseLibraryNewMobile.js
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    show: false,
    page_type: '',
    get_url: '',
    order_by: 'new',
    province_list: [],
    city_list: [],
    title_word: '最新案例',
    list: [],
    province_id: '',
    city_id: '',
    province_text: '',
    area_text: '',
    city_text: '',
    page: 1, //分页数，当前页数
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.name) {
      this.setData({
        page_type: options.name
      });
    }
    this.initData();
    this._initAddressInfo();
  },
  initData() {
    console.log('123232332');
    let that = this;
    // this.show = false;
    this.data.city_list = [];
    // this.data.order_by = 'new'
    // this.title_word = '最新案例'
    // this.list = []
    this.data.province_id = '';
    this.data.city_id = '';
    this.data.province_text = '';
    this.data.city_text = '';
    that.setData({
      show:false,
      title_word:'最新案例',
      order_by:'new',
      list:[],
      page:1
    });
    // 区域列表
    if (that.data.page_type == 'caseLibraryAreaMobile') {
      // that.get_url = 'plugin.case-library.api.mobile.home.region-case'
      that.setData({
        get_url:app.getNetAddresss('plugin.case-library.api.mobile.home.region-case')
      });
      that.getList('area', 'area');
    }
    // 最新、热门列表
    else {
      that.setData({
        get_url:app.getNetAddresss('plugin.case-library.api.mobile.home.new-case-list')
      });
      // that.get_url = 'plugin.case-library.api.mobile.home.new-case-list'
      that.getList('new', that.data.order_by);
    }
  },
  openArea() {
    this.setData({
      show : !this.data.show
    });
  },
  getListOne(e){
    console.log(e);
    let fir = e.currentTarget.dataset.fir;
    let two = e.currentTarget.dataset.two;
    this.setData({
      page:1,
      list:[]
    });
    this.getList(fir,two);
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

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

  },

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

  },

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

  },

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

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if(this.data.current_page >= this.data.last_page){
      return;
    }else{
      let pages = this.data.page + 1 ;
      this.setData({
        page : pages
      });
      this.getMoreData();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  getMoreData(){
    let that = this;
    let json =[];
    if(that.data.page_type == 'caseLibraryAreaMobile'){
      json = {
        province_id: that.data.province_id,
        city_id: that.data.city_id,
        page: that.data.page
      };
    }else{
      json = {
        order_by: that.data.order_by,
        page: that.data.page
      };
    }
    app._postNetWork({
      url: that.data.get_url,
      data: json,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          var nextPageData = res.data.case.data;
          that.setData({
            list: that.data.list.concat(nextPageData),
            current_page: res.data.case.current_page,
            last_page: res.data.case.last_page
          });
        } else {
          wx.showToast({
            title: res.msg,
            icon: "none",
            duration: 1000,
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  getList(page_type, order_by) {
    let that = this;
    console.log(page_type, order_by,'sadddd');
    if (order_by) {
      // that.data.order_by = order_by
      that.setData({
        order_by:order_by
      });
      if (that.data.order_by == 'new') {
        // that.title_word = '最新案例'
        that.setData({
          title_word: '最新案例'
        });
      } else if (that.data.order_by == 'hot') {
        // that.title_word = '热门案例'
        that.setData({
          title_word: '热门案例'
        });
      } else if (that.data.order_by == 'area') {
        // that.title_word = '区域案例'
        that.setData({
          title_word: '区域案例'
        });
      }
    }
    let json = {};
    if (page_type == 'new') {
      json = {
        order_by: that.data.order_by
      };
    } else if (page_type == 'area') {
      // that.title_word = '区域案例'
      that.setData({
        title_word: '区域案例'
      });
      json = {
        province_id: that.data.province_id || '',
        city_id: that.data.city_id || ''
      };
    }
    app._postNetWork({
      url: that.data.get_url,
      data:json,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.list = [];
          that.setData({
            list: res.data.case.data,
            current_page: res.data.case.current_page,
            last_page: res.data.case.last_page
          });
          if(page_type == 'area'){
            that.setData({
              show : false
            });
          }
        } else {
          wx.showToast({
            title: res.msg,
            icon: "none",
            duration: 1000,
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  onClose(){
    this.setData({
      show : false
    });
  },
  changePro(e){
    let item = e.currentTarget.dataset.item;
    this.setData({
      provinceName:e.currentTarget.dataset.item.areaname
    });
    this._getCityData(item);
  },
  changeCity(e){
    console.log(e);
    let provinceId = e.currentTarget.dataset.item.parentid;
    let cityId = e.currentTarget.dataset.item.id;
    let cityName = e.currentTarget.dataset.item.areaname;
    this.setData({
      province_id:provinceId,
      city_id:cityId,
      cityName:cityName,
      area_text:`${this.data.provinceName}${cityName}`
    });
    this.getList('area','area');
  },
  //地址选择器数据初始化
  _initAddressInfo: function () {
    var that = this;
    var _keyName = 'provinceData';
    wx.getStorage({
      key: _keyName,
      success: function (res) {
        console.log("地址信息准备就绪");
        console.log(res);
        that._getProvinceData();
      },
      fail: function (res) {
        console.log("省fail");
        app._getNetWork({
          url: app.getNetAddresss("member.member-address.address"),
          success: function (res) {
            var response = res.data;
            var province = response.data.province;
            var city = response.data.city;
            var district = response.data.district;
            wx.setStorage({
              key: 'provinceData',
              data: province
            });
            wx.setStorage({
              key: 'cityData',
              data: city
            });
            wx.setStorage({
              key: 'districtData',
              data: district
            });
            that._initAddressInfo();
          },
          fail: function (res) {},
          state: true
        });
      }
    });
  },
  // 读取省key
  _getProvinceData: function () {
    var that = this;
    var _keyName = 'provinceData';
    wx.getStorage({
      key: _keyName,
      success: function (res) {
        console.log("省读取成功", res.data);
        // that._getCityData();
        that.setData({
          province_list:res.data
        });
      }
    });
  },
  // 读取城市key，筛选城市data
  _getCityData: function (item) {
    var _data = item.id;
    // console.log("城市", "_data", _data);
    var that = this;
    var _keyName = 'cityData';
    wx.getStorage({
      key: _keyName,
      success: function (res) {
        console.log("城市读取成功", res.data);
        console.log(_data);
        var _json = [];
        for (var i = 0; i < res.data.length; i++) {
          if (res.data[i].parentid == _data) {
            _json.push(res.data[i]);
          }
        }
        console.log("城市数据筛选", _json);
        that.setData({
          city_list:_json
        });
        // that._getDistrictData();
      }
    });
  },
});