// pages/sign/sign.js
var myRequest = require("../../utils/api.js");
var utils = require("../../utils/utils.js");
var QQMapWX = require('../../utils/qqmap-wx-jssdk.min.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    latitude: '',
    longitude: '',
    deviceLat: '',
    deviceLong: '',
    address: '',
    label: '请选择站点',
    singleSelect: {
      value: '',
      options: [],
    },
    userInfo: [],
    keys: {
      value: 'code',
      label: 'name',
    },
    projectId: '',
    isSing: false,
    isSingOut: false,
    deviceCode: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getLocation();
    this.getTime();
    var projectId = wx.getStorageSync('projectId');
    this.setData({
      projectId: projectId,
    })
    
    
  },
  getTime() {
    var that = this;
    var interval = setInterval(function() {
      var time = utils.getTime();
      that.setData({
        time: time
      })
   }, 1000);
  },
  handleSingleSelect(e) {
    this.setData({
      'singleSelect.value': e.detail.value,
      deviceCode: e.detail.value
    });
    // wx.setStorageSync('deviceCode', e.detail.value)
    this.isUserSign();
    this.isUserSignOut();
    this.setSelectLabel(e.detail.value)
  },
  setSelectLabel(value) {
    var options = this.data.singleSelect.options;
    for (let i = 0; i < options.length; i++) {
      if(options[i].code == value) {
        this.setData({
          label: options[i].name,
          deviceLat: options[i].latitude,
          deviceLong: options[i].longitude,
        })
        // wx.setStorageSync('deviceName', options[i].name)
        // wx.setStorageSync('deviceLat', options[i].latitude)
        // wx.setStorageSync('deviceLong', options[i].longitude)
      }
    }
  },
  //获取距离
  // getDistance: function(latitude = '',longitude = '') {
  //   if (latitude == '' || longitude == '') {
  //     var lat1 = wx.getStorageSync('deviceLat')
  //     var lng1 = wx.getStorageSync('deviceLong')
  //   } else {
  //     var lat1 = latitude
  //     var lng1 = longitude
  //   }
    
  //   var lat2 = this.data.latitude;
  //   var lng2 = this.data.longitude;

  //   lat1 = lat1 || 0;
  //   lng1 = lng1 || 0;
  //   lat2 = lat2 || 0;
  //   lng2 = lng2 || 0;
  //   var rad1 = lat1 * Math.PI / 180.0;
  //   var rad2 = lat2 * Math.PI / 180.0;
  //   var a = rad1 - rad2;
  //   var b = lng1 * Math.PI / 180.0 - lng2 * Math.PI / 180.0;
  //   var r = 6378137;
  //   var distance = r * 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(rad1) * Math.cos(rad2) * Math.pow(Math.sin(b / 2), 2)));
   
  //   return distance;
  // },
  //签到
  singIn() {
    var that = this;
    if (!that.isSeleckDev) {
      wx.showToast({
        title: '请选择站点',
        icon: 'none',
      })
      return false;
    }
    if (that.data.deviceCode == '') {
      wx.showToast({
        title: '附近没有站点',
        icon: 'none',
      })
      return false;
    }
    var url = 'sign/in?deviceCode=' + that.data.deviceCode + '&location=' + that.data.address;
    myRequest.myRequest(url, {}, 'post').then(function(res){
      wx.showToast({
        title: '签到成功',
        icon: 'none',
      })
      that.setData({
        isSing: true
      })
      that.isUserSign();
      that.isUserSignOut();
    }).catch(function(res){
      console.log(res);
    })
  },
  //签退
  singOut() {
    var that = this;
    if (!that.isSeleckDev) {
      wx.showToast({
        title: '请选择站点',
        icon: 'none',
      })
      return false;
    }
    if (that.data.deviceCode == '') {
      wx.showToast({
        title: '附近没有站点',
        icon: 'none',
      })
      return false;
    }
    // if (this.getDistance() > 2000 ) {
    //   wx.showToast({
    //     title: '目前不在打卡范围',
    //     icon: 'none',
    //   })
    //   return false;
    // }
    var url = 'sign/out?deviceCode=' + that.data.deviceCode;
    myRequest.myRequest(url, {}, 'post').then(function(res){
      wx.showToast({
        title: '签退成功',
        icon: 'none',
      })
      that.setData({
        isSing: false
      })
      that.isUserSign();
      that.isUserSignOut();
    }).catch(function(res){
      console.log(res);
    })
  },
  isSeleckDev() {
    if (this.data.singleSelect.value == '') {
      return false;
    }
    return true;
  },
  // 计算两个坐标间的距离
  
  //获取站点
  getDevicesData: function()
  {
    var that = this;
    myRequest.myRequest('system/devicesLocation', {
      projectId: that.data.projectId,
      latitude: that.data.latitude,
      longitude: that.data.longitude,
    }, 'get').then(function(res){
      var devicesList = res.data.data;
      that.setData({
        'singleSelect.options': devicesList,
      });
      
      if (undefined != devicesList[0]) {
        that.setData({
          'singleSelect.value': devicesList[0].code,
          label: devicesList[0].name,
          deviceCode: devicesList[0].code
        })
      } else {
        that.setData({
          'singleSelect.value': '',
          label: '请选择站点',
          deviceCode: ''
        })
      }
      that.isUserSign();
      that.isUserSignOut();
    }).catch(function(res){
      console.log(res);
    })
  },

  //判断当日是否签到
  isUserSign() {
    var that = this;
    console.log(1, that.data.deviceCode)
    if (that.data.deviceCode == '') {
      wx.showToast({
        title: '附近没有站点',
        icon: 'none',
      })
      return false;
    }
    myRequest.myRequest('sign/isSign', {
      deviceCode: that.data.deviceCode
    }, 'get').then(function(res){
      that.setData({
        isSing: res.data.data
      })
    }).catch(function(res){
      console.log(res);
    })
  },
    //判断当日是否签退
    isUserSignOut() {
      var that = this;
      console.log(2, that.data.deviceCode)
      if (that.data.deviceCode == '') {
        wx.showToast({
          title: '附近没有站点',
          icon: 'none',
        })
        return false;
      }
      myRequest.myRequest('sign/isSignOut', {
        deviceCode: that.data.deviceCode
      }, 'get').then(function(res){
        that.setData({
          isSingOut: res.data.data
        })
      }).catch(function(res){
        console.log(res);
      })
    },
  //获取位置信息
  getLocation() {
    var that = this
    wx.getLocation({//调用API得到经纬度
      type: 'gcj02',
      isHighAccuracy: true,
      success: function (res) {
    
        var latitude = res.latitude  
        var longitude = res.longitude
        that.setData({
          latitude: res.latitude,
          longitude: res.longitude
        })   
        that.getDevicesData();
        //地址解析
        var demo = new QQMapWX({
          key: 'D6LBZ-45P3N-3V2FH-S2YBM-6ISH3-7LF7W'
        });
        demo.reverseGeocoder({//地址解析
          location: {
            latitude: latitude,
            longitude: longitude 
          },
          success: function (res) {
            console.log(res);
            //获得地址
            that.setData({
              address: res.result.address
            })
          },
          fail: function (res) {
            console.log(res);
          },
          complete: function (res) {
            console.log(res);
          }
        });
      }
 
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // this.getDevicesData();
    
  },

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

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

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

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

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






