// pages/callPhone/callPhone.js
let app = getApp();

const commonUtil = require("../../utils/commonUtil.js");
import { customRequest } from '../../utils/request.js';
import { timeShowXXX, getUserApplyOrderStatusById, formatDateYMDHM } from "../../utils/dateUtil";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    chaShowed: false,
    isTrigger: false,
    activeTab: 0,
    navlist: -1,
    statusmark: 1,
    inputVal: "搜索职位名",
    hasMoreData: false, // 获取更多数据是否显示

    // 状态栏高度
    statusBarHeight: wx.getStorageSync("statusBarHeight"),
    // 导航栏高度
    navigationBarHeight: wx.getStorageSync("navigationBarHeight"),
    // 胶囊按钮高度
    menuButtonHeight: wx.getStorageSync("menuButtonHeight"),
    // 胶囊信息
    menuButton: wx.getStorageSync("menuButtonInfo"),
    navigatorHeight: 60,
    searchParam: {
      pageSize: 20,
      pageNum: 1,
    },
    billList: [],
    underReviewList: [],
    underReviewNum: "",
    timer: null, // 防抖
    currIndex: 1,
    drawerShow: false
  },
  onTabClick (e) {
    const index = e.detail.index;
    console.log(index);

    let status = -1;
    if (index == 1) {
      status = 10;
    } else if (index == 2) {
      status = 20;
    } else if (index == 3) {
      status = 25;
    } else if (index == 4) {
      status = 30;
    } else if (index == 5) {
      status = 40;
    } else if (index == 6) {
      status = 999;
    } else {
      status = -1;
    }
    this.data.searchParam.pageNum = 1;
    this.data.searchParam.status = status;
    this.setData({
      //  navlist: e.mark.ind,
      activeTab: index,
      searchParam: this.data.searchParam,
      billList: [],
    });

    console.log(this.data.searchParam);
    this.getList();
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let index = options.status;

    let data = {
      mark: {
        ind: options.status,
      },
    };
    console.log(options);
    this.data.searchParam.status = 1
    if (options.currIndex || options.currIndex == 0) {
      this.data.searchParam.status = options.currIndex
      this.setData({
        currIndex: options.currIndex
      });
    }
    this.setData({
      activeTab: options.status,
      underReviewList: [],
      billList: [],

      searchParam: this.data.searchParam,
    });
    console.log(this.data.searchParam);
    // this.changeactive(data);
    // this.getBillNum();
  },
  hidedrawershow (e) {
    console.log(e);
    this.setData({
      [e.detail.type]: false
    })
    // console.log(this.data.drawerShow);
  },
  onScrollRefresh () {
    this.data.billList = [];
    this.data.searchParam.pageNum = 1;
    this.getList();
  },
  showDrawer (e) {
    console.log(e);
    this.setData({
      drawerShow: true,
      currentJobDrawer: e.currentTarget.dataset.item
    })
  },
  copyClose () {
    let that = this
    wx.setClipboardData({
      data: that.data.currentJobDrawer.jobDesp,
      success (res) {
        wx.getClipboardData({
          success (res) {
            console.log(res.data); // data
            that.setData({
              currentDesp: res.data
            })
            wx.showToast({
              title: "内容已复制",
              icon: "none",
              duration: 1500,
            });
          },
        });
      },
    });
  },

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

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // this.data.billList = [];
    this.data.searchParam.pageNum = 1;

    this.setData({
      billList: [],
    });
    this.getList();
  },

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

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

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

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

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    return app.sharePageImage()
  },
  goEdit (e) {
    console.log(e.currentTarget.dataset.item);
    let item = JSON.stringify(e.currentTarget.dataset.item)
    wx.navigateTo({
      url: '/pages/publishJob/index?isedit=1&item=' + item
    })
  },
  getBillNum () {
    let that = this;
    customRequest("/yishoudan/user/apply/order/listStatisticsNum", {
      header: 'headers', method: 'GET', data: {
        code: that.data.wxCode,
        // iv: iv,
        // encryptedData: encryptedData,
        type: "yishoudan",
      }
    }).then((res) => {
      if (res.data.status == 200) {
        that.setData({
          underReviewNum: res.data.data.num10,
        });
      }
    })
    // wx.request({
    //   url: app.globalData.ip + "/yishoudan/user/apply/order/listStatisticsNum",
    //   header: app.globalData.headers,
    //   data: {
    //     code: that.data.wxCode,
    //     // iv: iv,
    //     // encryptedData: encryptedData,
    //     type: "yishoudan",
    //   },
    //   success (res) {
    //     console.log(res);
    //     if (res.data.status == 200) {
    //       that.setData({
    //         underReviewNum: res.data.data.num10,
    //       });
    //     }
    //   },
    // });
  },
  // changeactive: function (e) {
  // 	this.data.searchParam.pageNum = 1;
  // 	this.setData({
  // 		navlist: e.mark.ind,
  // 		searchParam: this.data.searchParam,
  // 		["searchParam.status"]: e.mark.ind,
  // 		billList: [],
  // 	});
  // 	this.getList();
  // },
  onScrollToLower () {
    console.log(this.data.searchParam);
    if (this.data.hasMoreData) {
      this.data.searchParam.pageNum = this.data.searchParam.pageNum + 1;
      this.getList();
    }
  },
  getList: function (under) {
    var that = this;
    console.log(that.data.searchParam);

    that.data.isLoading = true;
    wx.showLoading({
      title: "加载中",
    });
    that.data.searchParam.keys = that.data.inputVal == "搜索职位名" ? "" : that.data.inputVal;
    // debugger;
    customRequest("/yishoudan/custom/job/list", { header: 'headers', method: 'post', data: that.data.searchParam }).then((res) => {
      let data = res.data;
      if (res.data.status == 200) {

        if (res.data.data.pageBean.recordList != null && res.data.data.pageBean.recordList.length > 0) {
          res.data.data.pageBean.recordList.forEach((item) => {
            var minAge
            var maxAge
            if (item.minAgeMan && item.minAgeWoman) {
              item.sex = '男女不限'
              minAge = (item.minAgeMan < item.minAgeWoman ? item.minAgeMan : item.minAgeWoman) || ''
              maxAge = (item.maxAgeMan < item.maxAgeWoman ? item.maxAgeMan : item.maxAgeWoman) || ''
            } else if (item.minAgeMan && !item.minAgeWoman) {
              item.sex = '男'
              minAge = item.minAgeMan || ''
              maxAge = item.maxAgeMan || ''
            } else if (!item.minAgeMan && item.minAgeWoman) {
              item.sex = '女'
              minAge = item.minAgeWoman || ''
              maxAge = item.maxAgeWoman || ''
            } else {
              minAge = ''
              maxAge = ''
            }
            item.minAge = minAge
            item.maxAge = maxAge
            item["fuWuFei"] = commonUtil.getReturnFeeTypeName1ById(
              item.returnFeeType,
              item.returnFee / 100
            );
          });
        }
        // var jobListTemp = commonUtil.disposeJobListData(
        //   res.data.data.pageBean.recordList
        // );
        if (res.data.data.pageBean.recordList != null && res.data.data.pageBean.recordList.length < 20) {
          var recordListTemp = commonUtil.disposeJobListData(
            res.data.data.pageBean.recordList
          );;
          that.data.billList = that.data.billList.concat(recordListTemp);
          that.setData({
            hasMoreData: false,
          });
        } else {
          var recordListTemp = commonUtil.disposeJobListData(
            res.data.data.pageBean.recordList
          );;
          that.data.billList = that.data.billList.concat(recordListTemp);
          that.setData({
            hasMoreData: true,
          });
        }
        console.log(that.data.billList);
        that.setData({
          billList: that.data.billList,
          isTrigger: false,
        });

        // }
        wx.hideLoading();
      } else {
        app.showTips(that, res.data.msg);
      }
      that.data.isLoading = false;
    }).catch(() => {
      that.data.isLoading = false;

    })
    // wx.request({
    //   url: app.globalData.ip + "/yishoudan/custom/job/list",
    //   data: that.data.searchParam,
    //   header: app.globalData.headers,
    //   method: "post",
    //   success: function (res) {
    //     // debugger;
    //     console.log(res.data);
    //     let data = res.data;
    //     if (res.data.status == 200) {

    //       if (res.data.data.pageBean.recordList != null && res.data.data.pageBean.recordList.length > 0) {
    //         res.data.data.pageBean.recordList.forEach((item) => {
    //           var minAge
    //           var maxAge
    //           if (item.minAgeMan && item.minAgeWoman) {
    //             item.sex = '男女不限'
    //             minAge = (item.minAgeMan < item.minAgeWoman ? item.minAgeMan : item.minAgeWoman) || ''
    //             maxAge = (item.maxAgeMan < item.maxAgeWoman ? item.maxAgeMan : item.maxAgeWoman) || ''
    //           } else if (item.minAgeMan && !item.minAgeWoman) {
    //             item.sex = '男'
    //             minAge = item.minAgeMan || ''
    //             maxAge = item.maxAgeMan || ''
    //           } else if (!item.minAgeMan && item.minAgeWoman) {
    //             item.sex = '女'
    //             minAge = item.minAgeWoman || ''
    //             maxAge = item.maxAgeWoman || ''
    //           } else {
    //             minAge = ''
    //             maxAge = ''
    //           }
    //           item.minAge = minAge
    //           item.maxAge = maxAge
    //           item["fuWuFei"] = commonUtil.getReturnFeeTypeName1ById(
    //             item.returnFeeType,
    //             item.returnFee / 100
    //           );
    //         });
    //       }
    //       // var jobListTemp = commonUtil.disposeJobListData(
    //       //   res.data.data.pageBean.recordList
    //       // );
    //       if (res.data.data.pageBean.recordList != null && res.data.data.pageBean.recordList.length < 20) {
    //         var recordListTemp = commonUtil.disposeJobListData(
    //           res.data.data.pageBean.recordList
    //         );;
    //         that.data.billList = that.data.billList.concat(recordListTemp);
    //         that.setData({
    //           hasMoreData: false,
    //         });
    //       } else {
    //         var recordListTemp = commonUtil.disposeJobListData(
    //           res.data.data.pageBean.recordList
    //         );;
    //         that.data.billList = that.data.billList.concat(recordListTemp);
    //         that.setData({
    //           hasMoreData: true,
    //         });
    //       }
    //       console.log(that.data.billList);
    //       that.setData({
    //         billList: that.data.billList,
    //         isTrigger: false,
    //       });

    //       // }
    //       wx.hideLoading();
    //     } else {
    //       app.showTips(that, res.data.msg);
    //     }
    //     that.data.isLoading = false;
    //   },
    //   fail: function (res) {
    //     console.log(res);
    //     that.data.isLoading = false;
    //   },
    // });
  },
  goSearch () {
    wx.navigateTo({
      url: "../search/index?from=mineJob",
    });
  },
  changeMenu (e) {
    var that = this;
    // wx.showLoading({
    // 	title: "加载中...",
    // });
    that.data.searchParam.pageNum = 1;
    // that.data.searchParam.searchTag = e.currentTarget.dataset.id;
    that.data.searchParam.status = e.currentTarget.dataset.id;
    console.log(e);

    this.setData({
      currIndex: e.currentTarget.dataset.id,
      billList: [],
      searchParam: this.data.searchParam,
    });
    this.getList();
  },
  inputTyping: function (e) {
    this.setData({
      inputVal: e.detail.value,
    });
    console.log(this.data.inputVal == "");
    if (this.data.inputVal == "") {
      this.data.billList = [];
      this.data.searchParam.pageNum = 1;
      this.data.searchParam.keys = "";
      this.getList();
    }
  },
  searchKey: function (e) {
    console.log(e);
    this.data.billList = [];
    this.data.searchParam.pageNum = 1;
    this.data.searchParam.keys = this.data.inputVal;
    this.getList();
  },
  // searchinput: function (e) {
  // 	let that = this;
  // 	this.setData({
  // 		inputVal: e.detail.value,
  // 	});
  // 	this.data.searchParam.keys = e.detail.value;
  // 	this.data.searchParam.pageNum = 1;
  // 	that.setData({
  // 		billList: [],
  // 		searchParam: this.data.searchParam,
  // 	});
  // 	if (that.data.timer) {
  // 		clearTimeout(that.data.timer);
  // 	}
  // 	that.data.timer = setTimeout(() => {
  // 		this.getList();
  // 	}, 500);
  // },
  addJob () {
    wx.navigateTo({
      url: '/pages/publishJob/index'
    })
  },
  clearInput () {
    let that = this;
    this.data.billList = [];
    this.setData({
      inputVal: "搜索职位名",
      chaShowed: false,
    });

    this.data.searchParam.keys = "";
    this.data.searchParam.pageNum = 1;
    this.setData({
      searchParam: this.data.searchParam,
    });
    this.getList();
  },
  toDetail (e) {
    console.log(e.mark.id);
    console.log(this.data.statusmark);
    wx.navigateTo({
      url: `/pages/billDetail/index?id=${e.mark.id}`,
    });
  },
  goBack () {
    wx.navigateBack({
      delta: 1,
    });
  },
  tounderReview () {
    let that = this;

    this.setData({
      ["searchParam.status"]: 10,
      ["searchParam.pageSize"]: 100,
    });
    // that.data.searchParam.status = 10
    that.getList(true);

    // setTimeout(() => {

    // 		that.tounderReview();

    // }, 200);
  },

  makePhoneCall (e) {
    console.log(e);
    wx.makePhoneCall({
      phoneNumber: e.currentTarget.dataset.tel,
    });
  },
});
