// pages/search/index.js
const app = getApp();
import page from "../firstBill/index";
console.log(page);
Page({
  /**
   * 页面的初始数据
   */
  data: {
    inputVal: "",
    isShowCha: false,
    inputPlaceholder: "",
    fromProject: false,
    from: "",
    historyList: [],
    wannaList: [
      {
        name: "大龄工",
        id: 1,
      },
      {
        name: "16-18岁",
        id: 2,
      },
      {
        name: "纹身",
        id: 3,
      },
      {
        name: "烟疤",
        id: 4,
      },
      {
        name: "新能源",
        id: 5,
      },
      {
        name: "电子厂",
        id: 6,
      },
    ],
    agencyRoleList: [
      {
        userName: "房景会",
        tel: 15603716868,
      },
      {
        userName: "房景会1",
        tel: 15603716868,
      },
      {
        userName: "房景会2",
        tel: 15603716868,
      },
    ],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad (options) {
    var that = this;
    console.log(options);
    if (options.from == "project") {
      wx.setNavigationBarTitle({
        title: "职位管理",
      });
      wx.setNavigationBarColor({
        // frontColor: "#ffffff",
        // backgroundColor: "#1890ff",
      });
      that.setData({
        fromProject: true,
        from: "project",
        inputPlaceholder: "搜索工作",
      });
    } else if (options.from == "townMan") {
      wx.setNavigationBarTitle({
        title: "我的老乡",
      });
      that.setData({
        // fromProject: false,
        from: "townMan",
        inputPlaceholder: "搜索姓名",
      });
    } else if (options.from == "process") {
      wx.setNavigationBarTitle({
        title: "报名工单",
      });
      that.setData({
        // fromProject: false,
        from: "process",
        inputPlaceholder: "搜索姓名/手机号",
      });
    } else if (options.from == "mineJob") {
      wx.setNavigationBarTitle({
        title: "我的职位",
      });
      that.setData({
        // fromProject: false,
        from: "mineJob",
        inputPlaceholder: "搜索职位名",
      });
    } else if (options.from == "myProject") {
      wx.setNavigationBarTitle({
        title: "我的项目",
      });
      that.setData({
        // fromProject: false,
        from: "myProject",
        inputPlaceholder: "搜索我的项目",
      });
    } else if (options.from == "mobile") {
      wx.setNavigationBarTitle({
        title: "通讯录",
      });
      that.setData({
        // fromProject: false,
        from: "mobile",
        inputPlaceholder: "搜索姓名、手机号",
      });
    } else if (options.from == "announce") {
      wx.setNavigationBarTitle({
        title: "我的通告",
      });
      that.setData({
        // fromProject: false,
        from: "announce",
        inputPlaceholder: "搜索企业名称",
      });
    } else if (options.from == "wodeBill") {
      wx.setNavigationBarTitle({
        title: "我的账单",
      });
      that.setData({
        // fromProject: false,
        from: "wodeBill",
        inputPlaceholder: "搜索账单",
      });
    } else if (options.from == "wodeBillDetail") {
      wx.setNavigationBarTitle({
        title: "我的账单",
      });
      that.setData({
        // fromProject: false,
        from: "wodeBillDetail",
        inputPlaceholder: "搜索姓名",
      });
    } else if (options.from == "agencyRole") {
      wx.setNavigationBarTitle({
        title: "请选择用户",
      });
      that.setData({
        // fromProject: false,
        from: "agencyRole",
        inputPlaceholder: "请输入用户手机号",
      });
    }
  },
  inputTyping: function (e) {
    this.setData({
      inputVal: e.detail.value,
      isShowCha: true,
    });
    console.log(this.data.inputVal == "");
    if (this.data.inputVal == "") {
      this.setData({
        // inputVal: e.detail.value,
        isShowCha: false,
      });
      // this.data.billList = [];
      // this.data.searchParam.pageNum = 1;
      // this.data.searchParam.keys = "";
      // this.getList();
      // this.getRead();
    }
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow () {
    var that = this;
    if (app.isNotEmptyCheck(wx.getStorageSync("projectSearch")) && that.data.from == "project") {
      that.setData({
        historyList: wx.getStorageSync("projectSearch").reverse(),
      });
    } else if (app.isNotEmptyCheck(wx.getStorageSync("townManSearch")) && that.data.from == "townMan") {
      that.setData({
        historyList: wx.getStorageSync("townManSearch").reverse(),
      });
    } else if (app.isNotEmptyCheck(wx.getStorageSync("processSearch")) && that.data.from == "process") {
      that.setData({
        historyList: wx.getStorageSync("processSearch").reverse(),
      });
    } else if (app.isNotEmptyCheck(wx.getStorageSync("mineJobSearch")) && that.data.from == "mineJob") {
      that.setData({
        historyList: wx.getStorageSync("mineJobSearch").reverse(),
      });
    } else if (app.isNotEmptyCheck(wx.getStorageSync("myProjectSearch")) && that.data.from == "myProject") {
      that.setData({
        historyList: wx.getStorageSync("myProjectSearch").reverse(),
      });
    } else if (app.isNotEmptyCheck(wx.getStorageSync("mobileSearch")) && that.data.from == "mobile") {
      that.setData({
        historyList: wx.getStorageSync("mobileSearch").reverse(),
      });
    } else if (app.isNotEmptyCheck(wx.getStorageSync("announceSearch")) && that.data.from == "announce") {
      that.setData({
        historyList: wx.getStorageSync("announceSearch").reverse(),
      });
    } else if (app.isNotEmptyCheck(wx.getStorageSync("wodeBillSearch")) && that.data.from == "wodeBill") {
      that.setData({
        historyList: wx.getStorageSync("wodeBillSearch").reverse(),
      });
    } else if (app.isNotEmptyCheck(wx.getStorageSync("wodeBillDetailSearch")) && that.data.from == "wodeBill") {
      that.setData({
        historyList: wx.getStorageSync("wodeBillDetailSearch").reverse(),
      });
    }
    that.setData({
      historyList: that.data.historyList.slice(0, 10),
    });
  },
  searchKey: function (e) {
    var that = this;
    console.log(that.data.inputVal);
    let currText = "";
    if (that.data.from == "project") {
      // currText = "搜索工作";
      // if (app.isNotEmptyCheck(wx.getStorageSync("projectSearch"))) {
      // 	that.data.historyList = wx.getStorageSync("projectSearch");
      // }
      // that.data.historyList.push(that.data.inputVal);
      // wx.setStorageSync("projectSearch", that.funQC(that.data.historyList));
    } else if (that.data.from == "townMan") {
      currText = "搜索姓名";
      if (app.isNotEmptyCheck(wx.getStorageSync("townManSearch"))) {
        that.data.historyList = wx.getStorageSync("townManSearch");
      }
      that.data.historyList.push(that.data.inputVal);
      wx.setStorageSync("townManSearch", that.funQC(that.data.historyList));
      console.log(that.funQC(wx.getStorageSync("townManSearch")));
    } else if (that.data.from == "process") {
      currText = "搜索姓名";
      if (app.isNotEmptyCheck(wx.getStorageSync("processSearch"))) {
        that.data.historyList = wx.getStorageSync("processSearch");
      }
      that.data.historyList.push(that.data.inputVal);
      wx.setStorageSync("processSearch", that.funQC(that.data.historyList));
      console.log(that.funQC(wx.getStorageSync("processSearch")));
    } else if (that.data.from == "mineJob") {
      currText = "搜索姓名";
      if (app.isNotEmptyCheck(wx.getStorageSync("mineJobSearch"))) {
        that.data.historyList = wx.getStorageSync("mineJobSearch");
      }
      that.data.historyList.push(that.data.inputVal);
      wx.setStorageSync("mineJobSearch", that.funQC(that.data.historyList));
      console.log(that.funQC(wx.getStorageSync("mineJobSearch")));
    } else if (that.data.from == "myProject") {
      currText = "搜索我的项目";
      if (app.isNotEmptyCheck(wx.getStorageSync("myProjectSearch"))) {
        that.data.historyList = wx.getStorageSync("myProjectSearch");
      }
      that.data.historyList.push(that.data.inputVal);
      wx.setStorageSync("myProjectSearch", that.funQC(that.data.historyList));
      console.log(that.funQC(wx.getStorageSync("myProjectSearch")));
    } else if (that.data.from == "mobile") {
      currText = "搜索姓名、手机号";
      if (app.isNotEmptyCheck(wx.getStorageSync("mobileSearch"))) {
        that.data.historyList = wx.getStorageSync("mobileSearch");
      }
      that.data.historyList.push(that.data.inputVal);
      wx.setStorageSync("mobileSearch", that.funQC(that.data.historyList));
      console.log(that.funQC(wx.getStorageSync("mobileSearch")));
    } else if (that.data.from == "announce") {
      currText = "搜索企业名称";
      if (app.isNotEmptyCheck(wx.getStorageSync("announceSearch"))) {
        that.data.historyList = wx.getStorageSync("announceSearch");
      }
      that.data.historyList.push(that.data.inputVal);
      wx.setStorageSync("announceSearch", that.funQC(that.data.historyList));
      console.log(that.funQC(wx.getStorageSync("announceSearch")));
    } else if (that.data.from == "wodeBill") {
      currText = "搜索账单";
      if (app.isNotEmptyCheck(wx.getStorageSync("wodeBillSearch"))) {
        that.data.historyList = wx.getStorageSync("wodeBillSearch");
      }
      that.data.historyList.push(that.data.inputVal);
      wx.setStorageSync("wodeBillSearch", that.funQC(that.data.historyList));
      console.log(that.funQC(wx.getStorageSync("wodeBillSearch")));
    } else if (that.data.from == "wodeBillDetail") {
      currText = "搜索姓名";
      if (app.isNotEmptyCheck(wx.getStorageSync("wodeBillDetailSearch"))) {
        that.data.historyList = wx.getStorageSync("wodeBillDetailSearch");
      }
      that.data.historyList.push(that.data.inputVal);
      wx.setStorageSync("wodeBillDetailSearch", that.funQC(that.data.historyList));
      console.log(that.funQC(wx.getStorageSync("wodeBillDetailSearch")));
    }

    // wx.redirectTo({
    //   url: '../myProject/index?searchData='+that.data.inputVal,
    // })
    var pages = getCurrentPages();
    var currPage = pages[pages.length - 1]; // 当前页面
    var prevPage = pages[pages.length - 2]; // 上一个页面
    console.log(prevPage.data);

    if (that.data.inputVal == "") {
      prevPage.setData({
        inputVal: currText,
        chaShowed: false,
      });
    } else {
      this.setPrevData(prevPage);
      // var option = "status"
      prevPage.setData({
        inputVal: that.data.inputVal,
        chaShowed: true,
      });
    }

    wx.navigateBack({
      delta: 1,
    });

    // this.data.billList = [];
    // this.data.searchParam.pageNum = 1;
    // this.data.searchParam.keys = this.data.inputVal;
    // this.getList();
    // this.getRead();
  },
  /**
   * 设置列表的请求状态到全部
   */
  setPrevData (prevPage) {
    // if (prevPage.data.searchParam) {
    //   prevPage.data.searchParam.status = 0;
    //   prevPage.setData({
    //     activeTab: 0,
    //     currIndex: 0,
    //     searchParam: prevPage.data.searchParam,
    //   });
    // }
  },

  historySearchKey (e) {
    var that = this;
    console.log(e.currentTarget.dataset.text);
    var pages = getCurrentPages();
    var currPage = pages[pages.length - 1]; // 当前页面
    var prevPage = pages[pages.length - 2]; // 上一个页面
    this.setPrevData(prevPage);
    prevPage.setData({
      inputVal: e.currentTarget.dataset.text,
      chaShowed: true,
    });

    wx.navigateBack({
      delta: -1,
    });
  },
  // 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);
  // },
  cle () {
    wx.removeStorage({
      key: "myProjectSearch",
    });
  },
  clearInput () {
    let that = this;
    // this.data.billList = [];
    this.setData({
      inputVal: "",
      isShowCha: false,
    });

    // this.data.searchParam.keys = "";
    // this.data.searchParam.pageNum = 1;
    // this.setData({
    //     searchParam: this.data.searchParam,
    // });
    // this.getList();
    // this.getRead();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  funQC (arr) {
    let newsArr = [];
    for (let i = 0; i < arr.length; i++) {
      if (app.isEmptyCheck(arr[i])) {
        continue;
      }
      if (newsArr.indexOf(arr[i]) === -1) {
        newsArr.push(arr[i]);
      }
    }
    return newsArr;
  },
  /**
   * 点击搜索到的用户
   *
   *
   */
  selectRole (e) {
    console.log(e);
    var pages = getCurrentPages();
    var prevPage = pages[pages.length - 2]; // 上一个页面
    prevPage.setData({
      memberInfo: { ...prevPage.data.memberInfo, ...e.currentTarget.dataset.item },
    });
    console.log(prevPage.data.memberInfo);
    wx.navigateBack({
      delta: -1,
    });
  },
  goCity () {
    let that = this;
    wx.navigateTo({
      url: "../city/index",
    });
  },
  onReady () { },

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

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

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

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

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