// pages/new-map/map.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentId: '',   //传值与list id 相同
    showPopup: false,
    bottom: "500rpx",
    showUp: false,
    longitude:'',
    latitude:'',
    markers: [],
    userLongitude:'',
    userLatitude:'',
    userMarkers:[],
    list:[],
    lists:[],
    userHatId:'',
    //页面跳转参数
    loginName:'',
    userName:'',
    projectId:'',
    projectName:'' ,
    index:'',
    userLocationList:{},
    active1:true,
    active2:false,
    active3:false,
    ryshow:false,
  },
  onClickShow() {
    this.setData({ ryshow: true });
  },
  onClickHide() {
    this.setData({ ryshow: false });
  },

  upward() {
    this.setData({ showUp: true });
    this.setData({ bottom:  "500rpx" });
    let userLocationList = this.data.userLocationList;
    this.selectUserHardHatById(userLocationList[0].id,userLocationList)
  },
  downward() {
    this.setData({ showUp: false });
    this.setData({ bottom:  "50rpx" });
  },

  showPopup() {
    this.setData({ showPopup: true });
  },

  onClose() {
    this.setData({ showPopup: false });
  },

  selectList(){
    this.setData({ showPopup: false });
  },

  showActive:function(event){
    this.onClickShow();
    var type = event.currentTarget.dataset.type;
    if(type =="1"){
      this.setData({
        active1:true,
        active2:false,
        active3:false,
        showUp: false,
        currentId:''
      })
      //查询当天工时排行
      this.selectUserAttendance(this.data.projectId,"day");
    }else if(type == "2"){
      this.setData({
        active1:false,
        active2:true,
        active3:false,
        showUp: false,
        currentId:''
      })
       //查询本周工时排行
       this.selectUserAttendance(this.data.projectId,"week");
    }else{
      this.setData({
        active1:false,
        active2:false,
        active3:true,
        showUp: false,
        currentId:''
      })
       //查询本月工时排行
       this.selectUserAttendance(this.data.projectId,"month");
    }
  },

  //页面初始化
  onLoad: function (options) {
    this.onClickShow();
    var that = this;
    //调用wx.getSystemInfo接口，然后动态绑定组件高度
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          height: res.windowHeight
        })
      }
    }),
    //获取缓存中数据
    wx.getStorage({
      key: 'userinfo',
      success:function(res){
        that.setData({
          loginName:res.data.loginName,
          userName:res.data.userName,
          projectId:app.globalData.projectId,
          projectName:app.globalData.projectName,
          index:options.index,
          userLocationList:JSON.parse(options.userLocationList),
          userHatId:options.id,
          currentId:options.id,
        })
      }
    })
    //页面初始化判断页面显示
    if(options.skipState =="1"){
        this.setData({ showUp: true })
        //查询单个人员安全帽信息
        this.selectUserHardHatById(options.id,JSON.parse(options.userLocationList));
    }else{
      this.setData({ showPopup:true })
    }   
    
    //判断列表今日、本周、本月显示
    that.decideData(options.state);
    //获取当天的工时排行
    that.selectUserAttendance(app.globalData.projectId,options.state,options.skipState);
},

  //根据id查询安全帽数据
  selectUserHardHatById:function(id,userLocationList){
      var that = this;
      wx.request({
        url: app.globalData.reqUrl+'/weixin/userinfo/selectUserHardhatById',
        data:{
          "id":id
        },
        method:"GET",
        success:function(res){
          that.onClickHide();
          if(res.data.length>0){
            var markers = [];
            for(var i=0;i<userLocationList.length;i++){              
                if(userLocationList[i].id == res.data[0].id){
                  var marker = {id:res.data[0].id,projectId:res.data[0].projectId,longitude:res.data[0].longitude,latitude:res.data[0].latitude,projectName:res.data[0].projectName,iconPath:"http://fileimg.makalu.cc/051dbbe42b9542568665033cd8a35aea.png",width: 40, height: 40};
                  markers.push(marker);  
                }else{
                  var marker = {id:userLocationList[i].id,projectId:userLocationList[i].projectId,longitude:userLocationList[i].longitude,latitude:userLocationList[i].latitude,projectName:userLocationList[i].projectName
                  }
                  markers.push(marker);  
                }
            }
            that.setData({
              list:res.data,
              markers:markers,
              longitude:res.data[0].longitude,
              latitude:res.data[0].latitude,
              userLongitude:res.data[0].longitude,
              userLatitude:res.data[0].latitude,
              userMarkers:markers,
            })
        }
     
        }
      })
  },

  //工时排行
  selectUserAttendance:function(projectId,state,skipState){
    var that = this;
    wx.request({
      url: app.globalData.reqUrl+'/weixin/userinfo/selectUserAttendance',
      data:{
        "projectId":projectId,
        "state":state
      },
      method:"GET",
      success:function(gsph){
        that.onClickHide();
        if(gsph.data.userLocationList.length >0){
          var markers = [];
          var userLocationList = gsph.data.userLocationList;
          for(var i=0;i<userLocationList.length;i++){
              if(userLocationList[i].id == that.data.userHatId){
                var marker = {id:userLocationList[0].id,projectId:userLocationList[0].projectId,longitude:userLocationList[0].longitude,latitude:userLocationList[0].latitude,projectName:userLocationList[0].projectName,iconPath:"http://fileimg.makalu.cc/051dbbe42b9542568665033cd8a35aea.png",width: 40, height: 40};
                markers.push(marker);  
              }else{
                  var marker = {id:userLocationList[i].id,projectId:userLocationList[i].projectId,longitude:userLocationList[i].longitude,latitude:userLocationList[i].latitude,projectName:userLocationList[i].projectName
                  }
                  markers.push(marker);  
              }
          }
          if(skipState == "1"){
            that.setData({
              lists:gsph.data.userLocationList,
              userLocationList:gsph.data.userLocationList,
            })
          }else{
            that.setData({
              lists:gsph.data.userLocationList,
              userLocationList:gsph.data.userLocationList,
              markers:markers,
              longitude:gsph.data.userLocationList[0].longitude,
              latitude:gsph.data.userLocationList[0].latitude,
            })
          }        
      }else{
        that.setData({
          lists:[],
          markers:[],
          longitude:'',
          latitude:'',
        })
      }
      }
    })
  },

  //判断列表显示
  decideData:function(event){
      if(event == "day"){
        this.setData({
          active1:true,
          active2:false,
          active3:false,
        })
      }else if(event == "week"){
        this.setData({
          active1:false,
          active2:true,
          active3:false,
        })
      }else{
        this.setData({
          active1:false,
          active2:false,
          active3:true,
        })
      }
  },

  ///向左切换人员数据
  changeLeftData:function(event){
    var userLocationList = this.data.userLocationList;
    if(this.data.index == "0"){
      app.toast("已经到头了，无法进行点击");
    }else{
      this.selectUserHardHatById(userLocationList[this.data.index-1].id,this.data.userLocationList);
      this.setData({
        currentId:userLocationList[this.data.index-1].id,
        index:this.data.index-1
      })
    }
  },

  //向右切换
  changeRightData:function(event){
    var userLocationList = this.data.userLocationList;
    if(this.data.index == (userLocationList.length-1)){
      app.toast("已经到头了，无法进行点击");
    }else{
      this.selectUserHardHatById(userLocationList[(this.data.index-1+2)].id,this.data.userLocationList);
      this.setData({
        currentId:userLocationList[(this.data.index-1+2)].id,
        index:this.data.index-1+2
      })
    }
  },

  //点击列表数据查看
  skipUserMap:function(event){
    //查询单个数据
    this.selectUserHardHatById(event.currentTarget.dataset.itemid,this.data.userLocationList);
    this.setData({
      currentId:event.currentTarget.dataset.itemid,
      index:event.currentTarget.dataset.index,
      showUp: true
    })
  },

  //获取人员定位
  userLocation:function(event){
    this.setData({
      longitude:this.data.userLongitude,
      latitude:this.data.userLatitude,
      markers:this.data.userMarkers
    })
  },

  //获取个人定位
  personalLocation:function(even){
    this.setData({
      longitude:app.globalData.initialLon,
      latitude:app.globalData.initialLat,
      markers:app.globalData.markers,
    })
  },

  //点击定位点查询数据
  getListById:function(event){
    //查询单个数据
    this.selectUserHardHatById(event.markerId,this.data.userLocationList);
    var markers = this.data.markers;
    var index = "";
    for(var i = 0;i<markers.length;i++){
      if(markers[i].id == event.markerId){
        index = i;
      }
    }
    this.setData({
      currentId:event.markerId,
      index:index,
      showUp: true
    })
  },

  //返回到人员管理页面
  goRYGL:function(){
    wx.redirectTo({
      url: '../renyuanguanli/renyuanguanli'
    })
  }
})