// pages/todayData/index.js
const app = getApp();
var dateTimePicker = require("../../utils/dateTimePicker.js");
var dateUtil = require("../../utils/dateUtil.js");
var { formatDateYMD, fullCopy } = require("../../utils/commonUtil.js");
import moment from 'moment'
import { customRequest } from '../../utils/request.js';
Page({
  /**
   * 页面的初始数据
   */
  data: {
    currentTime: formatDateYMD(new Date()),
    currentTime2: formatDateYMD(new Date()),
    start: formatDateYMD(new Date()),
    end: formatDateYMD(new Date()),
    endTime: formatDateYMD(new Date()),
    tabs: [{ title: "全部" }, { title: "审核中" }, { title: "待接待" }, { title: "待面试" }, { title: "待入职" }, { title: "在职中" }, { title: "已完成" }],
    activeTab: 0,
    dataList: [],
    isLoading: true,
    totalObj: {
      // 数据总计
      arrived: null,
      entry: null,
      obJob: null,
      passed: null,
      signUps: null,
    },
    dropdownShow: false,
    dateList: [
      {
        name: '昨日',
        value: 7
      },
      {
        name: '上周',
        value: 3
      },
      {
        name: '上月',
        value: 5
      },
      {
        name: '近7日',
        value: 0
      },
      {
        name: '近30日',
        value: 1
      },
    ],
    groupDataList: [],// 分组列表
    dateType: '',
    dateVal: 6,
    sortType: 0,
    totalObj: {},
    currentGroup: {},
    notCustom: true
  },

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

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

  /**
   * 生命周期函数--监听页面显示
   */
  onShow () {
    let that = this;
    // this.getDataList();
    console.log(that.data.endTime);
  },

  changeDateTime (e) {
    console.log(e);
    if (e.currentTarget.dataset.type == "start") {
      this.setData({
        currentTime: e.detail.value,
        start: e.detail.value
      });
    } else if (e.currentTarget.dataset.type == "end") {
      this.setData({
        currentTime2: e.detail.value,
        end: e.detail.value
      });
    }
    let time1 = new Date(this.data.currentTime).getTime();
    let time2 = new Date(this.data.currentTime2).getTime();
    console.log(time1);
    console.log(time2);
    if (time1 > time2) {
      wx.showToast({
        duration: 2000,
        title: "结束时间不能早于开始时间",
        mask: true,
        icon: "none",
      });
      return;
    }
    this.setData({
      dateVal: -1,
      notCustom: false
    })
    this.getGroupList(this.data.currentTime, this.data.currentTime2)
    // this.getDataList();
  },
  showToast () {
    wx.showToast({
      title: "仅显示当前在职数",
      icon: "none",
    });
  },
  /**
  * 获取团队列表
  */
  getGroupList () {
    let that = this;
    customRequest("/yishoudan/statistics/agency/teamWithGroup", {
      header: 'headers', method: 'post', data: {
        start: this.data.start,
        end: this.data.end,
      }
    }).then(({ data }) => {
      if (data.status == 200) {
        let totalObj = {
          arrived: null,
          entry: null,
          obJob: null,
          passed: null,
          signUps: null,
          finshedGPA: null,
          entryGPA: null,
        }
        data.data.list.forEach((item, index) => {
          totalObj.arrived += item.arrived
          totalObj.entry += item.entry
          totalObj.obJob += item.obJob
          totalObj.passed += item.passed
          totalObj.signUps += item.signUps
          totalObj.finshedGPA += item.finshedGPA
          totalObj.entryGPA += item.entryGPA

          totalObj.index = index + 2
        })
        that.data.totalObj = totalObj
        let newList = []
        data.data.list.forEach((item, index) => {
          newList = newList.concat(item.users)
          console.log(item)
          console.log(index)
        })
        console.log(newList)
        if (that.data.sortType != 0) {
          that.data.dataList = fullCopy(data.data.list)
          if (that.data.currentGroup) {
            that.selectGroup(that.data.currentGroup)
          }
        } else {
          newList.sort((a, b) => b.funnel - a.funnel);
          that.data.dataList = fullCopy(newList)
        }

        that.data.groupDataList = fullCopy(data.data.list)
        // that.data.totalDataList = fullCopy(newList)

        // that.isspinning = false

        that.data.groupDataList.forEach((item) => {
          console.log(item);
          item.title = item.agencyTeamName
        })
        that.data.groupDataList.unshift({
          title: '全部部门', agencyTeamId: 12, agencyTeamIdx: 2, arrived: 3, entry: 1, entryGPA: 0.03, finshedGPA: 1.86, funnel: 3.2, obJob: 71, packageIdx: 138, passed: 2, signUps: 8
        })

        that.setData({
          groupDataList: that.data.groupDataList,
          dataList: that.data.dataList,
          totalObj: that.data.totalObj
        })
      }
    })
    // wx.request({
    //   url: app.globalData.ip + "/yishoudan/statistics/agency/teamWithGroup",
    //   method: "post",
    //   header: app.globalData.headers,
    //   data: {
    //     start: this.data.start,
    //     end: this.data.end,
    //   },
    //   success: function ({ data }) {
    //     console.log(data);
    //     if (data.status == 200) {
    //       let totalObj = {
    //         arrived: null,
    //         entry: null,
    //         obJob: null,
    //         passed: null,
    //         signUps: null,
    //         finshedGPA: null,
    //         entryGPA: null,
    //       }
    //       data.data.list.forEach((item, index) => {
    //         totalObj.arrived += item.arrived
    //         totalObj.entry += item.entry
    //         totalObj.obJob += item.obJob
    //         totalObj.passed += item.passed
    //         totalObj.signUps += item.signUps
    //         totalObj.finshedGPA += item.finshedGPA
    //         totalObj.entryGPA += item.entryGPA

    //         totalObj.index = index + 2
    //       })
    //       that.data.totalObj = totalObj
    //       let newList = []
    //       data.data.list.forEach((item, index) => {
    //         newList = newList.concat(item.users)
    //         console.log(item)
    //         console.log(index)
    //       })
    //       console.log(newList)
    //       if (that.data.sortType != 0) {
    //         that.data.dataList = fullCopy(data.data.list)
    //         if (that.data.currentGroup) {
    //           that.selectGroup(that.data.currentGroup)
    //         }
    //       } else {
    //         that.data.dataList = fullCopy(newList)
    //       }

    //       that.data.groupDataList = fullCopy(data.data.list)
    //       // that.data.totalDataList = fullCopy(newList)

    //       // that.isspinning = false

    //       that.data.groupDataList.forEach((item) => {
    //         console.log(item);
    //         item.title = item.agencyTeamName
    //       })
    //       that.data.groupDataList.unshift({
    //         title: '全部部门', agencyTeamId: 12, agencyTeamIdx: 2, arrived: 3, entry: 1, entryGPA: 0.03, finshedGPA: 1.86, funnel: 3.2, obJob: 71, packageIdx: 138, passed: 2, signUps: 8
    //       })

    //       that.setData({
    //         groupDataList: that.data.groupDataList,
    //         dataList: that.data.dataList,
    //         totalObj: that.data.totalObj
    //       })
    //     }
    //   }
    // });
  },
  /**
  * 团队排序的分组点击
  */
  onTabClick (e) {
    console.log(e);
    if (e.detail.index != 0) {
      let val = this.data.groupDataList[e.detail.index]
      this.selectGroup(val)
    } else {
      this.selectGroup('all')
    }

  },
  selectGroup (val) {
    console.log(val)
    if (typeof val == 'object') {
      this.data.currentGroup = val
      this.data.groupDataList.forEach((item) => {
        console.log(item)
        console.log()
        if (item.agencyTeamId == val.agencyTeamId) {
          this.data.dataList = item.users
          this.data.totalObj.arrived = 0
          this.data.totalObj.entry = 0
          this.data.totalObj.obJob = 0
          this.data.totalObj.passed = 0
          this.data.totalObj.signUps = 0
          this.data.totalObj.finshedGPA = 0
          this.data.totalObj.entryGPA = 0
          this.data.dataList.forEach((item1) => {
            this.data.totalObj.arrived += item1.arrived
            this.data.totalObj.entry += item1.entry
            this.data.totalObj.obJob += item1.obJob
            this.data.totalObj.passed += item1.passed
            this.data.totalObj.signUps += item1.signUps
            this.data.totalObj.finshedGPA += item1.finshedGPA
            this.data.totalObj.entryGPA += item1.entryGPA
          })
        }
      })
      this.data.dataList.sort((a, b) => b.funnel - a.funnel);
      this.setData({
        dataList: this.data.dataList,
        totalObj: this.data.totalObj,
        currentGroup: this.data.currentGroup,

      })
      console.log(this.dataList)
    } else {
      this.data.currentGroup = {}
      this.setData({
        currentGroup: this.data.currentGroup,
      })
      this.getGroupList()
    }

  },
  /**
  * 获取数据
  */
  getDataList () {
    let that = this;
    wx.showLoading({
      title: "加载中...",
      mask: true,
    });
    that.setData({
      isLoading: true,
    });
    customRequest("/statistics/agency/team", {
      header: 'headers', method: 'post', data: {
        start: that.data.currentTime,
        end: that.data.currentTime2,
      }
    }).then((res) => {
      if (res.data.status == 200) {
        that.data.totalObj = {
          arrived: null,
          entry: null,
          obJob: null,
          passed: null,
          signUps: null,
        };
        res.data.data.list.forEach((item, index) => {
          that.data.totalObj.arrived += item.arrived;
          that.data.totalObj.entry += item.entry;
          that.data.totalObj.obJob += item.obJob;
          that.data.totalObj.passed += item.passed;
          that.data.totalObj.signUps += item.signUps;
          that.data.totalObj.index = index + 2;

        });
        that.setData({
          dataList: res.data.data.list,
          totalObj: that.data.totalObj,
          // dataList: [],
        });
      }
      wx.hideLoading();
      that.setData({
        isLoading: false,
      });
    })
    // wx.request({
    //   url: app.globalData.ip + "/statistics/agency/team",
    //   method: "post",
    //   data: {
    //     start: that.data.currentTime,
    //     end: that.data.currentTime2,
    //   },
    //   header: app.globalData.headers,
    //   success (res) {
    //     console.log(res);
    //     if (res.data.status == 200) {
    //       that.data.totalObj = {
    //         arrived: null,
    //         entry: null,
    //         obJob: null,
    //         passed: null,
    //         signUps: null,
    //       };
    //       // let newList = []
    //       // const len = 3;
    //       // for (let i = 0; i < res.data.data.list.length; i++) {  // 循环截取  每次截取三个
    //       //   const arr = res.data.data.list.slice(i, i + 3);
    //       //   const obj = { ...arr }
    //       //   newList.push(obj);
    //       //   i = i + len - 1;
    //       // }
    //       // newList.forEach((item) => {
    //       //   console.log(item);
    //       //   item.arrived = 0
    //       //   item.entry = 0
    //       //   item.obJob = 0
    //       //   item.passed = 0
    //       //   item.signUps = 0
    //       //   for (var k in item) {
    //       //     console.log(item[k]);
    //       //     console.log(item.arrived);
    //       //     console.log(item.arrived + item[k].arrived);
    //       //     item.arrived += item[k].arrived*1;
    //       //     item.entry += item[k].entry*1;
    //       //     item.obJob += item[k].obJob*1;
    //       //     item.passed += item[k].passed*1;
    //       //     item.signUps += item[k].signUps*1;
    //       //   }
    //       // })
    //       res.data.data.list.forEach((item, index) => {
    //         that.data.totalObj.arrived += item.arrived;
    //         that.data.totalObj.entry += item.entry;
    //         that.data.totalObj.obJob += item.obJob;
    //         that.data.totalObj.passed += item.passed;
    //         that.data.totalObj.signUps += item.signUps;
    //         that.data.totalObj.index = index + 2;

    //       });
    //       that.setData({
    //         dataList: res.data.data.list,
    //         totalObj: that.data.totalObj,
    //         // dataList: [],
    //       });
    //     }
    //     wx.hideLoading();
    //     that.setData({
    //       isLoading: false,
    //     });
    //   },
    // });
  },

  triggerGroup (e) {
    console.log(e);
    let type = e.currentTarget.dataset.type
    this.data.dataList.forEach((item, index) => {
      if (type == index) {
        item.checked = !item.checked
      }
    })
    // this.data.dataList[type].checked = !this.data.dataList[type].checked
    console.log(this.data.dataList[type].checked);
    this.setData({
      dataList: this.data.dataList
      // [type]: !this.data[type]
    })
  },
  setSortType (e) {
    this.setData({
      sortType: e.currentTarget.dataset.id
    })
    this.selectGroup('all')

    // this.getGroupList()
  },
  formatDate (tag) {
    let start, end
    console.log(tag);
    if (tag == 0) {
      start = moment().add(-6, 'd').format('YYYY-MM-DD')
      end = moment().format('YYYY-MM-DD')
    } else if (tag == 1) {

      start = moment().add(-29, 'd').format('YYYY-MM-DD')
      end = moment().format('YYYY-MM-DD')
    } else if (tag == 2) {
      start = moment().startOf('week').format('YYYY-MM-DD')
      end = moment().format('YYYY-MM-DD')
    } else if (tag == 3) {
      start = moment()
        .week(moment().week() - 1)
        .weekday(0)
        .format('YYYY-MM-DD')
      end = moment()
        .week(moment().week() - 1)
        .weekday(6)
        .format('YYYY-MM-DD')
      console.log(start)
      console.log(end)
      // return
    } else if (tag == 4) {
      start = moment().startOf('month').format('YYYY-MM-DD')
      end = moment().format('YYYY-MM-DD')
    } else if (tag == 5) {
      start = moment().subtract('month', 1).format('YYYY-MM') + '-01'
      end = moment().subtract(1, 'month').endOf('month').format('YYYY-MM-DD')
    } else if (tag == 6) {
      start = moment().add(-0, 'd').format('YYYY-MM-DD')
      end = moment().format('YYYY-MM-DD')
    } else if (tag == 7) {
      start = moment().add(-1, 'd').format('YYYY-MM-DD')
      end = moment().add(-1, 'd').format('YYYY-MM-DD')
    }
    console.log(start);
    console.log(end);
    this.setData({
      currentTime: start,
      currentTime2: end,
      start,
      end
    })
    this.getGroupList()
  },
  /**
  * 时间筛选
  */
  setOuterDate (e) {
    console.log(e);
    this.setData({
      dateType: '',
      dateVal: e.currentTarget.dataset.type,
      notCustom: true
    })
    console.log(this.data.dateVal);
    this.formatDate(this.data.dateVal)
  },
  setDate (e) {
    let that = this
    console.log(e);
    that.data.dateList.forEach((item) => {
      if (item.value == e.currentTarget.dataset.type) {
        console.log(item.value);
        that.setData({
          dateType: item.name,
          dateVal: item.value,
          dropdownShow: false,
          notCustom: true

        })
        setTimeout(() => {
          console.log(that.data.dateVal);

        }, 10);
      }
    })
    console.log(that.data.dateVal);
    that.formatDate(that.data.dateVal)

  },
  hideDefinedDropdown () {
    this.setData({
      definedDropdownShow: false
    })
  },
  showDefinedDropdown () {
    this.setData({
      definedDropdownShow: true,
      dropdownShow: false
    })
    console.log(this.data.definedDropdownShow);
  },
  toDataDetail (e) {
    console.log(e);
    let info = e.currentTarget.dataset.info;
    let type = e.currentTarget.dataset.type;
    console.log(type);
    // console.log(e.currentTarget.dataset.info);
    // console.log(JSON.parse(e.currentTarget.dataset.info));
    let data = { userId: info.id, type, start: this.data.currentTime, end: this.data.currentTime2, agencyTeamId: info.agencyTeamId }
    console.log(data);

    wx.navigateTo({
      url: "/pages/dataDetail/index?info=" + JSON.stringify(data),
    });
  },
  hideDropdown () {
    this.setData({
      dropdownShow: false
    })
  },
  showDropdown () {
    this.setData({
      dropdownShow: true
    })
  },
  /**
* 阻止滑动穿透
*
*
*/
  modalMove () {
    return false;
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide () { },

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

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

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

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