// pages/callPhone/callPhone.js
let app = getApp();
import { timeShowXXX, getUserApplyOrderStatusById, formatDateYMDHM } from "../../utils/dateUtil";
import { customRequest } from '../../utils/request.js';
Page({
  /**
   * 页面的初始数据
   */
  data: {
    chaShowed: false,
    tabs: [{ title: "全部" }, { title: "审核中" }, { title: "待接待" }, { title: "待面试" }, { title: "待入职" }, { title: "在职中" }, { title: "已完成" }],
    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, // 防抖
  },
  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) {
    console.log(options.status);
    let index = options.status;
    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.status = status;
    let data = {
      mark: {
        ind: options.status,
      },
    };

    this.setData({
      activeTab: options.status,
      underReviewList: [],
      billList: [],
      searchParam: {
        pageSize: 20,
      },
      searchParam: this.data.searchParam,
    });
    this.getList();

    console.log(this.data.searchParam);
    // this.changeactive(data);
    // this.getBillNum();
  },
  onScrollRefresh () {
    this.data.billList = [];
    this.data.searchParam.pageNum = 1;
    this.getList();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () { },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this
    // this.data.billList = [];
    console.log(wx.getStorageSync('MYBILL_PAGE_CHANGE'));
    if (typeof this.getTabBar === "function" && this.getTabBar()) {
      this.getTabBar().setData({
        isShow: true,
        selected: 1,
      });
    }
    if (wx.getStorageSync('MYBILL_PAGE_CHANGE') || that.data.inputVal != "搜索姓名/手机号") {
      this.data.searchParam.pageNum = 1;
      this.setData({
        billList: [],
      });
      this.getList();
      wx.removeStorageSync('MYBILL_PAGE_CHANGE')
    }

  },

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

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

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

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

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    return app.sharePageImage()
  },
  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;
    that.setData({
      isLoading: true
    })
    wx.showLoading({
      title: "加载中",
    });
    that.data.searchParam.keys = that.data.inputVal == "搜索姓名/手机号" ? "" : that.data.inputVal;
    // debugger;
    // /yishoudan/user/apply/order/list
    // /yishoudan/user/apply/order/web/list
    customRequest("/yishoudan/user/apply/order/web/list", { header: 'headers', method: 'post', data: that.data.searchParam }).then((res) => {
      let data = res.data;
      console.log('res', res);
      if (res.data.status == 200) {
        console.log('data', data);
        if (res.data.data.recordList != null && res.data.data.recordList.length > 0) {
          console.log('res.data.data.recordList', res.data.data.recordList);
          res.data.data.recordList.forEach((item) => {
            console.log('item', item);
            if (item.nation.substr(item.nation.length - 1, 1) != "族" && item.nation.substr(item.nation.length - 1, 1) != "") {
              item.nation = item.nation + "族";
            }

            if (item.status == 10 || item.status == 20 || item.status == 21 || item.status == 26) {
              item["updateTimeStr"] = formatDateYMDHM(item.receptionTime);
            } else if (item.status == 25 || item.status == 35 || item.status == 45) {
              item["updateTimeStr"] = formatDateYMDHM(item.interviewTime);
            } else if (item.status == 30) {
              item["updateTimeStr"] = formatDateYMDHM(item.willEntryTime);
            } else if (item.status == 40 || item.status == 48) {
              item["updateTimeStr"] = formatDateYMDHM(item.updateTime);
            }
            // else if(item.status ==48){
            //   item["updateTimeStr"] = formatDateYMDHM(item.updateTime)
            // }
            else if (item.status == 50) {
              item["updateTimeStr"] = formatDateYMDHM(item.leaveTime);
            } else {
              item["updateTimeStr"] = formatDateYMDHM(item.updateTime);
            }
            let timeSrc = item["updateTimeStr"];
            // console.log(timeSrc.slice(0, 4));
            // console.log(timeSrc);
            // console.log(new Date().getFullYear());
            if (item["updateTimeStr"].slice(0, 4) == new Date().getFullYear()) {
              item["updateTimeStr"] = item["updateTimeStr"].slice(5);
            }
            item["statusText"] = getUserApplyOrderStatusById(item.status);

            item["statusText"] = getUserApplyOrderStatusById(item.status);
            if (app.isNotEmptyCheck(item.userServeRecordDespLatest)) {
              item.userServeRecordDespLatest = item.userServeRecordDespLatest.replace(/「/g, "<span style='color:#4DB54B;margin-right:4px;'>").replace(/」/g, "</span>");
            }
          });
        }
        // if (under) {
        // 	that.setData({
        // 		underReviewList: JSON.parse(JSON.stringify(res.data.data.recordList)),
        // 	});
        // 	let list = JSON.stringify(that.data.underReviewList);
        // 	wx.navigateTo({
        // 		url: "/pages/underReview/index?list=" + list,
        // 	});
        // 	// wx.hideLoading();
        // } else {
        // let list = that.data.billList;
        // 	that.setData({
        //     billList: list.concat(res.data.data.recordList),
        //     isTrigger:false
        // 	});
        // }
        // else {
        // 	that.setData({
        // 		billList: res.data.data.recordList,
        // 	});
        // }
        // if (res.data.data.recordCount > that.data.billList.length) {
        // 	that.setData({
        // 		hasMoreData: true,
        // 	});
        // } else {
        // 	that.setData({
        // 		hasMoreData: false,
        // 	});
        // }

        if (res.data.data.recordList != null && res.data.data.recordList.length < 20) {
          var recordListTemp = res.data.data.recordList;
          that.data.billList = that.data.billList.concat(recordListTemp);
          that.setData({
            hasMoreData: false,
          });
        } else {
          var recordListTemp = res.data.data.recordList;
          that.data.billList = that.data.billList.concat(recordListTemp);
          that.setData({
            hasMoreData: true,
          });
        }

        that.setData({
          billList: that.data.billList,
          isTrigger: false,
        });

        // }
        wx.hideLoading();
      } else {
        app.showTips(that, res.data.msg);
      }
      that.setData({
        isLoading: false
      })
    }).catch((err) => {
      console.log('err',err);
      that.setData({
        isLoading: false
      })
    })
    // wx.request({
    //   url: app.globalData.ip + "/yishoudan/user/apply/order/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.recordList != null && res.data.data.recordList.length > 0) {
    //         res.data.data.recordList.forEach((item) => {
    //           if (item.nation.substr(item.nation.length - 1, 1) != "族" && item.nation.substr(item.nation.length - 1, 1) != "") {
    //             item.nation = item.nation + "族";
    //           }

    //           if (item.status == 10 || item.status == 20 || item.status == 21 || item.status == 26) {
    //             item["updateTimeStr"] = formatDateYMDHM(item.receptionTime);
    //           } else if (item.status == 25 || item.status == 35 || item.status == 45) {
    //             item["updateTimeStr"] = formatDateYMDHM(item.interviewTime);
    //           } else if (item.status == 30) {
    //             item["updateTimeStr"] = formatDateYMDHM(item.willEntryTime);
    //           } else if (item.status == 40 || item.status == 48) {
    //             item["updateTimeStr"] = formatDateYMDHM(item.updateTime);
    //           }
    //           // else if(item.status ==48){
    //           //   item["updateTimeStr"] = formatDateYMDHM(item.updateTime)
    //           // }
    //           else if (item.status == 50) {
    //             item["updateTimeStr"] = formatDateYMDHM(item.leaveTime);
    //           } else {
    //             item["updateTimeStr"] = formatDateYMDHM(item.updateTime);
    //           }
    //           let timeSrc = item["updateTimeStr"];
    //           // console.log(timeSrc.slice(0, 4));
    //           // console.log(timeSrc);
    //           // console.log(new Date().getFullYear());
    //           if (item["updateTimeStr"].slice(0, 4) == new Date().getFullYear()) {
    //             item["updateTimeStr"] = item["updateTimeStr"].slice(5);
    //           }
    //           item["statusText"] = getUserApplyOrderStatusById(item.status);

    //           item["statusText"] = getUserApplyOrderStatusById(item.status);
    //           if (app.isNotEmptyCheck(item.userServeRecordDespLatest)) {
    //             item.userServeRecordDespLatest = item.userServeRecordDespLatest.replace(/「/g, "<span style='color:#4DB54B;margin-right:4px;'>").replace(/」/g, "</span>");
    //           }
    //         });
    //       }
    //       // if (under) {
    //       // 	that.setData({
    //       // 		underReviewList: JSON.parse(JSON.stringify(res.data.data.recordList)),
    //       // 	});
    //       // 	let list = JSON.stringify(that.data.underReviewList);
    //       // 	wx.navigateTo({
    //       // 		url: "/pages/underReview/index?list=" + list,
    //       // 	});
    //       // 	// wx.hideLoading();
    //       // } else {
    //       // let list = that.data.billList;
    //       // 	that.setData({
    //       //     billList: list.concat(res.data.data.recordList),
    //       //     isTrigger:false
    //       // 	});
    //       // }
    //       // else {
    //       // 	that.setData({
    //       // 		billList: res.data.data.recordList,
    //       // 	});
    //       // }
    //       // if (res.data.data.recordCount > that.data.billList.length) {
    //       // 	that.setData({
    //       // 		hasMoreData: true,
    //       // 	});
    //       // } else {
    //       // 	that.setData({
    //       // 		hasMoreData: false,
    //       // 	});
    //       // }

    //       if (res.data.data.recordList != null && res.data.data.recordList.length < 20) {
    //         var recordListTemp = res.data.data.recordList;
    //         that.data.billList = that.data.billList.concat(recordListTemp);
    //         that.setData({
    //           hasMoreData: false,
    //         });
    //       } else {
    //         var recordListTemp = res.data.data.recordList;
    //         that.data.billList = that.data.billList.concat(recordListTemp);
    //         that.setData({
    //           hasMoreData: true,
    //         });
    //       }

    //       that.setData({
    //         billList: that.data.billList,
    //         isTrigger: false,
    //       });

    //       // }
    //       wx.hideLoading();
    //     } else {
    //       app.showTips(that, res.data.msg);
    //     }
    //     that.setData({
    //       isLoading: false
    //     })
    //     // that.data.isLoading = false;
    //   },
    //   fail: function (res) {
    //     console.log(res);
    //     that.setData({
    //       isLoading: false
    //     })
    //     // that.data.isLoading = false;
    //   },
    // });
  },
  goSearch () {
    wx.navigateTo({
      url: "../search/index?from=process",
    });
  },
  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);
  // },
  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',e.mark);
    console.log(e.mark.id);
    console.log(this.data.statusmark);
    wx.navigateTo({
      url: `/pages/billDetail/index?id=${e.mark.id}&relationId=${e.mark.item.relationId}`,
    });
  },
  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,
    });
  },
});
