// pages/mine/listManagement/index.js
const app = getApp();

const commonUtil = require("../../../utils/commonUtil.js");
const dateUtil = require("../../../utils/dateUtil.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    chaShowed: false,
    inputVal: '',
    hasMoreData: true, //下拉是否还有更多数据
    navigatorBarHeight: 0, // 头部整体高度信息
    editJobList: [
      "复制内容",
      "编辑", "删除"],

    totalPage: 0,
    currPage: 0,
    isTrigger: 0,
    pageShow: false,
    storeJobListSearchForm: {
      pageNum: 1,
      pageSize: 40,
    },
    recordList: [],
    currIndex: 0,
    checked: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad (options) {
    this.getJobList()
  },

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

  },
  changecheck (e) {
    console.log(e);
    let id = e.detail.value.id;
    let ind = this.data.recordList.findIndex(item => item.id == id)
    console.log(id);
    console.log(ind);
    this.data.recordList[ind].checked = !this.data.recordList[ind].checked
    this.setData({
      recordList: this.data.recordList
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow () {

  },

  // 下拉加载更多
  onScrollToLower () {
    console.log("====================================================");
    var that = this;
    if (app.globalData.isLogin) {
      that.data.storeJobListSearchForm.pageNum = that.data.storeJobListSearchForm.pageNum + 1;
      if (that.data.hasMoreData) {
        that.getJobList();
      }

    } else {
    }

  },
  showDraw (e) {
    let that = this;
    let item = e.currentTarget.dataset.item
    wx.showActionSheet({
      itemList: that.data.editJobList,
      success: function (res) {
        if (res.tapIndex == 0) {
          that.getJobDetail(item.id);

        } else if (res.tapIndex == 1) {
          // uni.navigateTo({
          //   url: "/root/home/jobForm?id=" + e.jobId,
          // });
          // that.delJob(item.id);
        } else if (res.tapIndex == 2) {
          that.delJob(item.id);
        }
      },
      fail: function (res) { },
    });
  },
  /**
  * 获取职位详情
  */
  getJobDetail (id) {
    let that = this;
    wx.request({
      url: app.globalData.ip + "/assistant/custom/job/v2/getInfo/" + id,
      method: "get",
      header: app.globalData.headers,
      success: function (res) {
        console.log(res);
        if (!res.data.data) {
          wx.showToast({
            title: "该职位无内容",
            icon: "none",
          });
        } else {
        console.log('right',res);
        wx.setClipboardData({
            data: res.data.data,
            success (res) {
              wx.showToast({
                title: "内容已复制",
                icon: "none",
                duration: 1500,
              });
            },
            fail (err) { },
          });
        }

      }
    })
  },
  /**
   * 删除职位
   */
  delJob (id) {
    let that = this;
    wx.showModal({
      content: "是否确定删除该职位",
      success: function (res) {
        console.log(res);
        if (res.confirm) {
          wx.request({
            url: app.globalData.ip + "/yishoudan/custom/job/del/" + id,
            method: "get",
            header: app.globalData.headers,
            success: function (res) {
              console.log(res);
              if (res.data.status == 200) {
                that.getJobList();

              } else {
                wx.showToast({
                  title: "删除失败",
                  icon: "none",
                });
              }
            }
          })
        }
      }
    });
  },
  changeMenu (e) {
    var that = this;
    that.data.storeJobListSearchForm.pageNum = 1;
    that.data.storeJobListSearchForm.recruitment = e.currentTarget.dataset.id;

    this.setData({
      currIndex: e.currentTarget.dataset.id,
      recordList: [],
      storeJobListSearchForm: this.data.storeJobListSearchForm,
    });
    this.getJobList();
  },
  clearInput: function () {
    console.log('clearInput');
    this.setData({
      inputVal: "",
      chaShowed: false,
      hasMoreData: true,

    });
    // this.inputBlur();
    this.data.recordList = [];
    this.data.storeJobListSearchForm.pageNum = 1;
    this.data.storeJobListSearchForm.keys = "";
    this.getJobList();
  },
  searchKey: function (e) {
    console.log(e);

    if (this.data.inputVal.trim() == "") {
      wx.showToast({
        title: "请输入内容",
        icon: "none",
      });
    } else {
      this.setData({
        isWannaShow: false,
        recordList: [],
      });
      console.log(this.data.placeholderText);
      this.data.storeJobListSearchForm.pageNum = 1;
      this.data.storeJobListSearchForm.keys = this.data.inputVal || this.data.placeholderText;
      this.setData({
        inputVal: this.data.inputVal || this.data.placeholderText,
      });
      this.getJobList();
      // this.setHistoryList();
    }
  },
  inputTyping: function (e) {
    console.log(e);
    this.setData({
      inputVal: e.detail.value,
    });
    console.log(this.data.inputVal == "");
    if (this.data.inputVal == "") {
      this.setData({
        recordList: [],
      });
      this.data.storeJobListSearchForm.pageNum = 1;
      this.data.storeJobListSearchForm.keys = "";
      // this.getJobList();
    }
  },

  getJobList (e) {
    console.log(e);
    var that = this;
    // debugger
    return new Promise(function (resolve, reject) {


      console.log(that.data.inputVal);
      that.data.storeJobListSearchForm.keys = that.data.inputVal == "" ? "" : that.data.inputVal;
      // debugger;
      wx.showLoading({
        title: "加载中...",
      });
      console.log(that.data.storeJobListSearchForm);
      wx.request({
        url: app.globalData.ip + "/assistant/custom/job/v2/list",
        method: "POST",
        header: app.globalData.headers,
        data: that.data.storeJobListSearchForm,
        success: function (res) {
          console.log("职位列表↓↓↓↓");
          console.log(res);

          that.setData({
            totalPage: res.data.data.pageCount,
            currPage: res.data.data.currentPage,
          });

          setTimeout(function () {
            that.setData({
              triggered: false,
            });
          }, 1000);
          if (res.data.data.recordList == null || res.data.data.recordList.length == 0 || res.data.data.recordList.length < that.data.storeJobListSearchForm.pageSize) {
            var jobListTemp = commonUtil.disposeJobListData(res.data.data.recordList);
            that.data.recordList = that.data.recordList.concat(jobListTemp);

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

            that.setData({
              recordList: that.data.recordList,
              hasMoreData: true,
              isTrigger: false,
            });
          }

          that.data.recordList.forEach((item) => {
            item.update = dateUtil.timeShowXXX(item.updateTime)
            item.checked = false
          })
          that.setData({
            recordList: that.data.recordList,
          });
          wx.hideLoading({
            success: (res) => { },
          });
          that.setData({
            pageShow: true,
            storeJobListSearchForm: that.data.storeJobListSearchForm,
          });
          resolve();
          // that.getTag();

          that.data.recordList.forEach((item, index) => {
            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,
          });
          setTimeout(() => {
            var query = wx.createSelectorQuery();
            query.select(".navigator").boundingClientRect();
            query.exec(function (res) {
              //res就是 所有标签为v1的元素的信息 的数组
              console.log(res);
              //取高度
              // console.log(res[0].height);
              that.setData({
                navigatorBarHeight: res[0].height,
              });
              console.log(that.data.navigatorBarHeight);
            });
          }, 1000);
        },
      });
    });
  },
  onScrollRefresh: function () {
    var that = this;
    that.data.storeJobListSearchForm.pageNum = 1;
    that.setData({
      recordList: [],
    });
    that.getJobList();
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide () {

  },

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

  },

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

  },

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

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage () {

  }
})