// pages/screen/index.js
const app = getApp();
import { customRequest } from '../../utils/request.js';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    searchParam: {
      search: 0,
      sortTag: 0,
      jobSpecialLabelIds: '',
      brandIds: '',
    },
    jobSpecialLabelList: [],
    brandList: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;

    try {
      var searchJobListParamStorage = wx.getStorageSync('searchJobListParamStorage')
      if (searchJobListParamStorage) {
        console.log("searchJobListParamStorage：======", searchJobListParamStorage);
        that.data.searchParam.sortTag = searchJobListParamStorage.sortTag;
        that.data.searchParam.jobSpecialLabelIds = searchJobListParamStorage.jobSpecialLabelIds;
        that.data.searchParam.brandIds = searchJobListParamStorage.brandIds;
      }
    } catch (e) {
      console.log("获取缓存设置的查询职位列表参数错误：", e);
    }


    this.setData({
      searchParam: that.data.searchParam
    });
    this.getListByTypeAndIndustry();
    this.getBrandNameAllList();
  },
  selectSort: function (e) {
    var that = this;
    var tag = e.currentTarget.dataset.tag;

    if (tag == 2) {//选择距离近

      wx.getLocation({
        type: 'gcj02',
        success (res1) {
          console.log("获取位置1");
          console.log(res1);
          app.globalData.lng = res1.longitude;
          app.globalData.lat = res1.latitude;

          that.data.searchParam.sortTag = tag;
          that.setData({
            searchParam: that.data.searchParam
          });
        },
        fail () {
          console.log('获取位置失败，打开位置设置界面');
          wx.openSetting({
            success (res) {
              console.log(res.authSetting);
              if (res.authSetting['scope.userLocation']) {
                wx.getLocation({
                  type: 'gcj02',
                  success (res1) {
                    console.log("获取位置2");
                    console.log(res1);
                    app.globalData.lng = res1.longitude;
                    app.globalData.lat = res1.latitude;

                    that.data.searchParam.sortTag = tag;
                    that.setData({
                      searchParam: that.data.searchParam
                    });
                  },
                  fail () {
                  }
                });
              }
            }
          })
        }
      });

    } else {
      that.data.searchParam.sortTag = tag;
      this.setData({
        searchParam: that.data.searchParam
      });
    }

  },
  reset: function (e) {
    var that = this;
    that.data.searchParam.sortTag = 0;
    that.data.jobSpecialLabelList.forEach(item => {
      if (item.checked) {
        item.checked = false;
      }
    })
    that.data.brandList.forEach(item => {
      if (item.checked) {
        item.checked = false;
      }
    })

    this.setData({
      searchParam: that.data.searchParam,
      jobSpecialLabelList: that.data.jobSpecialLabelList,
      brandList: that.data.brandList
    });

  },
  selectJobSpecialLabel: function (e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    if (that.data.jobSpecialLabelList[index].checked) {
      that.data.jobSpecialLabelList[index].checked = false;
    } else {
      that.data.jobSpecialLabelList[index].checked = true;
    }
    this.setData({
      jobSpecialLabelList: that.data.jobSpecialLabelList
    });
  },
  selectBrand: function (e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    if (that.data.brandList[index].checked) {
      that.data.brandList[index].checked = false;
    } else {
      that.data.brandList[index].checked = true;
    }
    this.setData({
      brandList: that.data.brandList
    });
  },
  getListByTypeAndIndustry: function () {
    var that = this;
    customRequest('/labels/getListByTypeAndIndustry',{header:'headers', method: 'GET', data: {  type: 90,
      industry: 2}}).then((res)=>{
        var jobSpecialLabelIdArray = that.data.searchParam.jobSpecialLabelIds.split(',');

        res.data.data.forEach(item => {
          item['checked'] = false;
          jobSpecialLabelIdArray.forEach(item1 => {
            if (item.id == item1) {
              item['checked'] = true;
            }
          });
        });



        that.setData({
          jobSpecialLabelList: res.data.data
        })

    })
    // wx.request({
    //   url: app.globalData.ip + '/labels/getListByTypeAndIndustry',
    //   data: {
    //     type: 90,
    //     industry: 2
    //   },
    //   header: app.globalData.headers,
    //   method: "GET",
    //   success: function (res) {
    //     console.log(res);

    //     var jobSpecialLabelIdArray = that.data.searchParam.jobSpecialLabelIds.split(',');

    //     res.data.data.forEach(item => {
    //       item['checked'] = false;
    //       jobSpecialLabelIdArray.forEach(item1 => {
    //         if (item.id == item1) {
    //           item['checked'] = true;
    //         }
    //       });
    //     });



    //     that.setData({
    //       jobSpecialLabelList: res.data.data
    //     })

    //   }
    // })
  },
  getBrandNameAllList: function () {
    var that = this;
    customRequest('/brand/getBrandNameAllList',{header:'headers', method: 'GET', data: {}}).then((res)=>{
      var brandIdArray = that.data.searchParam.brandIds.split(',');

      res.data.data.forEach(item => {
        item['checked'] = false;
        brandIdArray.forEach(item1 => {
          if (item.id == item1) {
            item['checked'] = true;
          }
        });
      });
      that.setData({
        brandList: res.data.data
      })
    })
    // wx.request({
    //   url: app.globalData.ip + '/brand/getBrandNameAllList',
    //   data: {
    //   },
    //   header: app.globalData.headers,
    //   method: "GET",
    //   success: function (res) {
    //     console.log(res);

    //     var brandIdArray = that.data.searchParam.brandIds.split(',');

    //     res.data.data.forEach(item => {
    //       item['checked'] = false;
    //       brandIdArray.forEach(item1 => {
    //         if (item.id == item1) {
    //           item['checked'] = true;
    //         }
    //       });
    //     });
    //     that.setData({
    //       brandList: res.data.data
    //     })

    //   }
    // })
  },
  loginOut: function () {
    var that = this;
    var jobSpecialLabelIdArray = [];
    that.data.jobSpecialLabelList.forEach(item => {
      if (item.checked) {
        jobSpecialLabelIdArray.push(item.id);
      }
    });

    if (jobSpecialLabelIdArray.length > 0) {
      that.data.searchParam.jobSpecialLabelIds = jobSpecialLabelIdArray.join(',');
    } else {
      that.data.searchParam.jobSpecialLabelIds = '';
    }

    var brandIdArray = [];
    that.data.brandList.forEach(item => {
      if (item.checked) {
        brandIdArray.push(item.id);
      }
    });

    if (brandIdArray.length > 0) {
      that.data.searchParam.brandIds = brandIdArray.join(',');
    } else {
      that.data.searchParam.brandIds = '';
    }

    that.data.searchParam.search = 1;

    try {
      wx.setStorageSync('searchJobListParamStorage', that.data.searchParam);
    } catch (e) {
      console.log("index-设置查询职位列表参数错误", e);
    }


    wx.reLaunch({
      url: "../index/index?fromSearchPage=1"
    })

  },

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

  },

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

  },

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

  },

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

  },

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

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

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

  // }
})