// 工作统计
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    topBarH: app.globalData.CustomBar,
    currentIndex: 0,
    tab: [],
    list: [],
    // startTime: '',
    // endTime: '',
    page: 1,
    pageSize: 10,
    userInfo:{},
    search:"",
    TypeData: [],
    showTypePop: false,
    postData:{
      orgcode:''
    },
    typeId:'',

     //树结构
     areaCode:'',
     areaPathName:'',
    unitTreepros: {
      key: 'orgname',
      val: 'orgcode'
    },
    unitTreelist: [],


    // 地区数据
    areamultiIndex: [0, 0, 0],
    areamultiIds: [],
    areanewArr: [],
    area:[],

  //   value:'',
	// tree: [
  //     {
  //       id: 1,
  //       name: '001',
  //       open: true,
  //       children: [{
  //         id: 2,
  //         name: '001-01',
  //         children: [{
  //           id: 3,
  //           name: '001-01-01',
  //           children: []
  //         }]
  //       }]
  //     }
  //   ]
  },

  /**
   * 切换tab
   */
  changeItem: function (params) {
    this.data.currentIndex = params.currentTarget.dataset.index;
    this.data.page = 1;
    this.data.list = [];
    this.setData(this.data);
    this.data.typeId = params.currentTarget.dataset.item.typeId;
    this.getList(params.currentTarget.dataset.item.typeId);
  },

  handleselectunit(e) {
    // e.detail  选中的id数组
    this.data.page = 1;
    this.data.list = [];
    let postData = this.data.postData; // 获取当前的 postData 对象  
    postData.orgcode = e.detail.item.orgcode;
    postData.areaPathName = e.detail.item.orgname;
    this.setData({
      postData: postData,
      showTypePop:false
    }); // 设置更新后的 postData 对象
    this.getList(this.data.typeId);
  },
    /**
   * 查看详情
   */
  toDetail: function (params) {
    wx.navigateTo({
      url: `/pages/statistic/j-b-x-x/detail/index?peopleId=${params.currentTarget.dataset.item.peopleId}`,
    })
  },
    /**
   * 显示/关闭弹窗筛选
   */
  popModal: function (params) {
    const type = params.currentTarget.dataset.type;
    this.data[type] = !this.data[type];
    this.setData(this.data)
  },
   /**
   * 地址
   */
  typeChange: function (params) {
    this.data.page = 1;
    this.data.list = [];
    this.setData(this.data);
    const index = params.currentTarget.dataset.index;
    this.data.postData.type = index + 1;
    this.data.postData.orgcode = this.data.TypeData[index].orgcode;
    this.data.showTypePop = false;
    this.setData(this.data);
    this.getList(this.data.typeId);
  },
   /**
   * 获取地区
   */
  getAreaTree: function () {
    app.axios("GET", "common", "/sysorg/areaTree", {
      classify: 2
    }).then(res => {
      if (res.code == 1) {
        console.log(res,'res===>');
        this.data.TypeData = res.data[0].children;
        this.data.unitTreelist = res.data;
        this.setData(this.data)
      }
    })
  },
  // /**
  //  * 开始时间
  //  */
  // startTimeChange: function (e) {
  //   this.data.startTime = e.detail.value;
  //   this.timeGetList();
  // },
  // /**
  //  * 开始时间
  //  */
  // endTimeChange: function (e) {
  //   this.data.endTime = e.detail.value;
  //   this.timeGetList();
  // },
  // //时间请求
  // timeGetList: function () {
  //   this.data.page = 1;
  //   this.data.currentIndex = 0;
  //   this.data.list = [];
  //   this.setData(this.data);
  //   this.getList(this.data.tab[0].typeId);
  // },
  /**
   * 获取工作统计类型
   */
  getType: function () {
    app.axios("GET", "app", `/people/populationByType`).then(res => {
      if (res.code == 1) {
        this.data.tab = res.data;
        this.setData(this.data);
        this.data.typeId = res.data[0].typeId;
        this.getList(res.data[0].typeId);
      }
    })
  },
  /**
   * 上拉刷新
   */
  refresh: function () {
    this.data.page = 1;
    this.data.currentIndex = 0;
    this.data.list = [];
    this.setData(this.data);
    this.getList(this.data.tab[0].typeId);
  },
  dropDown: function (params) {
    this.data.page = this.data.page+1;
    this.setData(this.data);
    this.getList(this.data.tab[this.data.currentIndex].typeId);
  },
  /**
   * 获取工作统计每项数据列表
   */
  getList: function (id) {
    let params = {
      limit:this.data.pageSize,
      page:this.data.page,
      // endTime:this.data.endTime,
      // startTime:this.data.startTime,
      typeId:id,
      // userId:this.data.userInfo.userId,
      condition:this.data.search,
    };
    
    if (this.data.postData.orgcode) {
      params.orgCode = this.data.postData.orgcode
    }
    console.log(params,'params==>');
    app.axios("GET", "app", `/people/peopleInfoByType`, params).then(res => {
      if (res.code == 1) {
        this.setData({
          list:[...this.data.list,...res.data.list],
          freshIng:false,
        });
      }
    })
  },






  // 地区获取
// getAreaTree: function () {
//   app.axios("GET", "common", "/sysorg/areaTree").then(res => {
//     if (res.code == 1) {
//       this.data.areamultiArray = res.data
//       this.setData(this.data);
//       let state = {
//         arr: [],
//         arr1: [],
//         arr2: [],
//         arr3: [],
//         areamultiIds: []
//       }
//       this.data.areamultiArray.map((v, vk) => {
//         state.arr1.push(v.orgname);
//         if (this.data.areamultiIndex[0] === vk) {
//           state.areamultiIds[0] = v;
//         }
//         if (state.arr2.length <= 0) {
//           v.children.map((c, ck) => {
//             state.arr2.push(c.orgname);
//             if (this.data.areamultiIndex[1] === ck) {
//               state.areamultiIds[1] = c;
//             }
//             if (state.arr3.length <= 0) {
//               c.children.map((t, tk) => {
//                 state.arr3.push(t.orgname);
//                 if (this.data.areamultiIndex[2] === tk) {
//                   state.areamultiIds[2] = t;
//                 }
//               });
//             }
//           });
//         }
//       });
//       state.arr[0] = state.arr1;
//       state.arr[1] = state.arr2;
//       state.arr[2] = state.arr3;
//       this.setData({
//         areanewArr: state.arr,
//         areamultiIds: state.areamultiIds,
//       });
//     }
//   })
// },
    // 级联选择地区
    areabindMultiPickerChange(e) {
      console.log(this.data.areamultiIds,'级联选择地区==>');
      this.setData({
        area: e.detail.value,
        areatate:true
      });
    },
    areabindMultiPickerColumnChange(e) {
      let data = {
        areanewArr: this.data.areanewArr,
        areamultiIndex: this.data.areamultiIndex,
        areamultiIds: this.data.areamultiIds,
      };
      data.areamultiIndex[e.detail.column] = e.detail.value;
  
      let searchColumn = () => {
        let arr1 = [];
        let arr2 = [];
        this.data.areamultiArray.map((v, vk) => {
          if (data.areamultiIndex[0] === vk) {
            data.areamultiIds[0] = {
              ...v,
            };
            v.children.map((c, ck) => {
              arr1.push(c.orgname);
              if (data.areamultiIndex[1] === ck) {
                data.areamultiIds[1] = {
                  ...c,
                };
                c.children.map((t, vt) => {
                  arr2.push(t.orgname);
                  if (data.areamultiIndex[2] === vt) {
                    data.areamultiIds[2] = {
                      ...t,
                    };
                  }
                });
              }
            });
          }
        });
        data.areanewArr[1] = arr1;
        data.areanewArr[2] = arr2;
      };
      switch (e.detail.column) {
        case 0:
          // 每次切换还原初始值
          data.areamultiIndex[1] = 0;
          data.areamultiIndex[2] = 0;
          // 执行函数处理
          searchColumn();
          break;
        case 1:
          data.areamultiIndex[2] = 0;
          searchColumn();
          break;
      }
      this.setData(data);
    },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.data.freshIng = true;
    this.data.userInfo = app.globalData.userInfo;
    this.setData(this.data);
    this.getType();
    this.getAreaTree();
  },

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

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

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

  },

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

  },

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

  },

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

  },

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

  }
})