// pages/recordChoice/index.js
let app = getApp();
const commonUtil = require("../../utils/commonUtil.js");
import { customRequest } from '../../utils/request.js';
Page({
  /**
   * 页面的初始数据
   */
  data: {
    pageType: "",
    pageFrom: 1,
    pageShow: false,
    hasMoreData: true,
    inputVal: "",
    inputAgencyVal: "",
    storeJobListSearchForm: {
      pageNum: 1,
      pageSize: 10,
      classify: 1,
      sex: -1,
      workTypeStr: "",
      lat: "",
      lng: "",
      jobClassify: "",
      sortTag: 0,
      jobSpecialLabelIds: "",
      cityName: "",
      brandIds: "",
      keys: ''
    },
    recordList: [],
    agencyList: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad (options) {
    console.log(options);
    this.setData({
      pageType: options.type,
      pageFrom: options.applyType
    });

  },

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

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

    if (this.data.pageType == "job" || this.data.pageType == "jobName") {
      wx.setNavigationBarTitle({
        title: '职位选择'
      })
      this.getJobList();

      // wx.
    } else if (this.data.pageType == "agency") {
      wx.setNavigationBarTitle({
        title: '成员选择'
      })
      this.getAgencyList();

    }


  },
  getJobList () {
    var that = this;
    return new Promise(function (resolve, reject) {
      wx.showLoading({
        title: "加载中...",
      });
      // /overall/store/job/list
      // /yishoudan/store/job/platform/list
      customRequest("/yishoudan/store/job/platform/list", { header: 'headers', method: 'POST', data: that.data.storeJobListSearchForm }).then((res) => {
        that.setData({
          loading: true,
        });
        setTimeout(function () {
          that.setData({
            triggered: false,
          });
        }, 1000);
        let recordList = res.data.data.pageBean.recordList
        if (app.isEmptyCheck(res.data.data.pageBean)) {
          that.setData({
            hasMoreData: false,
          });
        } else if (recordList == null || recordList.length == 0 || recordList.length < that.data.storeJobListSearchForm.pageSize) {
          // var jobListTemp = that.disposeJobListData(recordList);
          var jobListTemp = commonUtil.disposeJobListData(recordList);
          that.data.recordList = that.data.recordList.concat(jobListTemp);

          that.setData({
            recordList: that.data.recordList,
            hasMoreData: false,
          });
        } else {
          var jobListTemp = commonUtil.disposeJobListData(recordList);
          // var jobListTemp = that.disposeJobListData(recordList);
          that.data.recordList = that.data.recordList.concat(jobListTemp);

          that.setData({
            recordList: that.data.recordList,
          });
        }

        wx.hideLoading({
          success: (res) => { },
        });
        that.setData({
          pageShow: true,
          storeJobListSearchForm: that.data.storeJobListSearchForm,
        });
        resolve();

        console.log(that.data.recordList);


        that.data.recordList.forEach((item, index) => {
          console.log(item);
          // item.logoStr = item.jobName.slice(0,2)
          if (
            app.isNotEmptyCheck(item.returnFeeType) ||
            item.returnFeeType == "0" ||
            item.returnFee == "0"
          ) {
            item["fuWuFei"] = commonUtil.getReturnFeeTypeName1ById(
              item.returnFeeType,
              item.returnFee
            );
          } else {
            item["fuWuFei"] = "";
          }
        });

        that.setData({
          recordList: that.data.recordList,
        });
      })
      // wx.request({
      //   url: app.globalData.ip + "/overall/store/job/list",
      //   method: "POST",
      //   header: app.globalData.headers,
      //   data: that.data.storeJobListSearchForm,
      //   success: function (res) {
      //     console.log("职位列表↓↓↓↓");
      //     console.log(res);
      //     that.setData({
      //       loading: true,
      //     });
      //     setTimeout(function () {
      //       that.setData({
      //         triggered: false,
      //       });
      //     }, 1000);
      //     if (app.isEmptyCheck(res.data.data)) {
      //       that.setData({
      //         hasMoreData: false,
      //       });
      //     } else if (res.data.data.recordList == null || res.data.data.recordList.length == 0 || res.data.data.recordList.length < that.data.storeJobListSearchForm.pageSize) {
      //       var jobListTemp = that.disposeJobListData(res.data.data.recordList);
      //       that.data.recordList = that.data.recordList.concat(jobListTemp);

      //       that.setData({
      //         recordList: that.data.recordList,
      //         hasMoreData: false,
      //       });
      //     } else {
      //       var jobListTemp = that.disposeJobListData(res.data.data.recordList);
      //       that.data.recordList = that.data.recordList.concat(jobListTemp);

      //       that.setData({
      //         recordList: that.data.recordList,
      //       });
      //     }

      //     wx.hideLoading({
      //       success: (res) => { },
      //     });
      //     that.setData({
      //       pageShow: true,
      //       storeJobListSearchForm: that.data.storeJobListSearchForm,
      //     });
      //     resolve();

      //     console.log(that.data.recordList);


      //     that.setData({
      //       recordList: that.data.recordList,
      //     });
      //   },
      // });
    });
  },
  getAgencyList () {
    let that = this;
    wx.showLoading({
      title: "加载中...",
    });
    customRequest("/yishoudan/user/getAgencyCorpUsers", { header: 'headers', method: 'post', data: that.data.storeJobListSearchForm }).then((res) => {
      console.log('res', res);
      if (res.data.status == 200) {
        console.log(res);
        res.data.data.recordList.forEach((user) => {
          user.telStr = (user.tel ? user.tel.slice(-4) : "")

        })
        that.setData({
          agencyList: res.data.data.recordList,
          hasMoreData: false,
        });
      }
      wx.hideLoading();
    })
    // customRequest("/agency/getByPmdUserId", { header: 'headers', method: 'POST', data: that.data.storeJobListSearchForm }).then((res) => {
    //   if (res.data.status == 200) {
    //     console.log(res);
    //     that.setData({
    //       agencyList: res.data.data,
    //       hasMoreData: false,
    //     });
    //   }
    //   wx.hideLoading();
    // })
    // wx.request({
    //   url: app.globalData.ip + "/agency/getByPmdUserId",
    //   method: "POST",
    //   header: app.globalData.headers,
    //   data: that.data.storeJobListSearchForm,
    //   success: function (res) {
    //     console.log(res);
    //     if (res.data.status == 200) {
    //       console.log(res);
    //       that.setData({
    //         agencyList: res.data.data,
    //         hasMoreData: false,
    //       });
    //     }
    //     wx.hideLoading();
    //   },
    // });
  },
  /**
   * 处理后台返回的职位列表
   */
  disposeJobListData (recordList) {
    var that = this;
    recordList.forEach((item) => {
      item["workType"] = commonUtil.getWorkTypeById(item.workTypeMulti);

      if (app.isNotEmptyCheck(item.distance)) {
        item["distanceKm"] = commonUtil.getDistanceName(item.distance);
      }
      //年龄
      var ageStr = "";
      if (app.isNotEmptyCheck(item.minAge) && app.isNotEmptyCheck(item.maxAge)) {
        ageStr = item.minAge + "-" + item.maxAge + "岁";
      } else if (app.isNotEmptyCheck(item.minAge)) {
        ageStr = item.minAge + "岁以上";
      } else if (app.isNotEmptyCheck(item.maxAge)) {
        ageStr = item.maxAge + "岁以下";
      }
      item["age"] = ageStr;
      // console.log(item.salaryClassify,'=============', item.salaryClassifyValue);
      item["salaryClassifyValueHaibao"] = commonUtil.getSalaryClassifyValue1(item.salaryClassify, item.salaryClassifyValue);

      //月薪
      var monthlyPayStr = "";
      // if (hasHourlyPay) {
      if (app.isNotEmptyCheck(item.minMonthlyPay) && app.isNotEmptyCheck(item.maxMonthlyPay)) {
        if (item.minMonthlyPay == item.maxMonthlyPay) {
          monthlyPayStr = item.minMonthlyPay;
        } else {
          monthlyPayStr = item.minMonthlyPay + "-" + item.maxMonthlyPay;
        }
      } else if (app.isNotEmptyCheck(item.minMonthlyPay)) {
        monthlyPayStr = item.minMonthlyPay;
      } else if (app.isNotEmptyCheck(item.maxMonthlyPay)) {
        monthlyPayStr = item.maxMonthlyPay;
      }
      // }
      item["monthlyPay"] = monthlyPayStr;
      //地址深圳丨龙岗区丨
      let citys = commonUtil.setJobListInfoPosition(item.district)
      item["district"] = citys;
      //职位特色
      item.jobSpecialLabelNameArray = [];
      that.getJobSpecialLabelNamesArray(item.jobSpecialLabelNames).forEach((i, index) => {
        if (index <= 2) {
          item["jobSpecialLabelNameArray"].push(i);
        }
      });
      // item["jobSpecialLabelNames"] = ;
    });

    return recordList;
  },
  getJobSpecialLabelNamesArray (jobSpecialLabelNames) {
    if (app.isNotEmptyCheck(jobSpecialLabelNames)) {
      return jobSpecialLabelNames.split(", ");
    }
    return [];
  },
  onScrollToLower () {
    console.log("====================================================");
    var that = this;
    that.data.storeJobListSearchForm.pageNum = that.data.storeJobListSearchForm.pageNum + 1;
    if (that.data.hasMoreData) {
      that.getJobList();
    }
    // 根据实际数据加载情况设定 loadMore 的值即可，分别为 load 和 over

    // this.setData({
    // 	loadMore: "load",
    // loading:true
    // });
  },
  onScrollAgencyToLower () {
    var that = this;
    that.data.storeJobListSearchForm.pageNum = that.data.storeJobListSearchForm.pageNum + 1;
    that.getAgencyList();
  },
  inputTyping: function (e) {
    this.setData({
      inputVal: e.detail.value,
    });
    console.log(this.data.inputVal == "");
    if (this.data.inputVal == "") {
      this.data.recordList = [];
      this.data.storeJobListSearchForm.pageNum = 1;
      this.data.storeJobListSearchForm.keys = "";
      this.getJobList();
    }
  },
  inputAgency (e) {
    this.setData({
      inputAgencyVal: e.detail.value,
    });
    if (this.data.inputAgencyVal == "") {
      this.data.agencyList = [];
      this.data.storeJobListSearchForm.pageNum = 1;
      this.data.storeJobListSearchForm.keys = "";
      this.getAgencyList();
    }
  },
  searchKey: function (e) {
    console.log(e);
    console.log(this.data.inputVal);
    this.data.recordList = [];
    this.data.storeJobListSearchForm.pageNum = 1;
    this.data.storeJobListSearchForm.keys = this.data.inputVal;
    this.getJobList();
  },
  searchAgencyKey (e) {
    console.log(this.data.inputAgencyVal);
    this.data.agencyList = [];
    this.data.storeJobListSearchForm.pageNum = 1;
    this.data.storeJobListSearchForm.keys = this.data.inputAgencyVal;
    this.getAgencyList();
  },
  clearInput: function () {
    this.setData({
      inputVal: "",
      hasMoreData: true,
    });
    this.data.recordList = [];
    this.data.storeJobListSearchForm.pageNum = 1;
    this.data.storeJobListSearchForm.keys = "";
    this.getJobList();
  },
  clearAgencyInput () {
    this.setData({
      inputAgencyVal: "",
      hasMoreData: true,
    });
    this.data.agencyList = [];
    this.data.storeJobListSearchForm.pageNum = 1;
    this.data.storeJobListSearchForm.keys = "";
    this.getAgencyList();
  },
  getDetail (e) {
    var that = this;
    console.log('e', e);
    // return
    if (this.data.pageType == "job") {
      // wx.navigateBack();
      console.log(e.detail.dataset.info);
      // let argument = JSON.stringify(e.currentTarget.dataset.info).replace(/\=|\&/g, "3D");
      var pages = getCurrentPages();
      var prevPage = pages[pages.length - 2]; // 上一个页面
      prevPage.setData({
        jobInfo: e.detail.dataset.info,
      })
      wx.navigateBack({
        delta: 1,
      });
    } else {
      // wx.navigateBack();
      let argument = JSON.stringify(e.currentTarget.dataset.info).replace(/\=|\&/g, "3D");
      wx.redirectTo({
        url: `../billDetail/enrollJob/index?info=${argument}`,
      });
    }
  },
  getAgencyDetail (e) {
    var that = this;
    console.log(e);
    let info = e.currentTarget.dataset.info
    var pages = getCurrentPages();
    var prevPage = pages[pages.length - 2]; // 上一个页面
    prevPage.setData({
      agentName: info.aliasName,
      agentId: info.id,
    })
    wx.navigateBack({
      delta: 1,
    });

    // wx.redirectTo({
    //   url: `../newEnroll/enroll/index?agencyInfo=${JSON.stringify()}&applyType=${that.data.pageFrom}`,
    // });
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide () { },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload () {
    console.log('==============', 'isUnload');
  },

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

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

  /**
   * 用户点击右上角分享
   */
onShareAppMessage() {
  return app.sharePageImage()
},
});
