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

  /**
   * 页面的初始数据
   */
  data: {
    cityText: '',
    cityValue: [],
    dateText: '',
    dateValue: [],
    fileList: [],
    label: '请选择站点',
    singleSelect: {
      value: '',
      options: [],
    },
    keys: {
      value: 'code',
      label: 'name',
    },
    projectId: '',
    isSing: false,
    isSingOut: false,
    deviceCode: '',
    title: '签到',
    img: '',
    address: '',
    remark: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getLocation();
    var projectId = wx.getStorageSync('projectId');
    this.setData({
      projectId: projectId,
    })
  },
  getRemark(e) {
    this.setData({
      remark: e.detail.value
    })
  },
  handleAdd(e) {
    var that = this;
    const { files } = e.detail;
    var fileLists = [];
    myRequest.updateImg({file:files[0].url}).then(function(res){
      fileLists.push({url:files[0].url,type:'image',percent:100});
      that.setData({
        img: res,
        fileList: fileLists,
      })
    }).catch(function(res){
      console.log(res);
    })
  },
  handleRemove(e) {
    var filed = e.currentTarget.dataset.filed;
    const { index } = e.detail;
    const { fileList } = this.data;
    fileList[filed].splice(index, 1);
    this.setData({
      fileList,
    });
  },
  singIn() {
    var img = this.data.img;
    var location = this.data.address;
    var deviceCode = this.data.deviceCode;
    var remark = this.data.remark;
    if (deviceCode == '') {
      wx.showToast({
        title: '请选择站点',
        icon: 'none'
      })
      return false;
    }
    if (img == '') {
      wx.showToast({
        title: '请上传图片',
        icon: 'none'
      })
      return false;
    }
    myRequest.myRequest('outSign/in', {
      deviceCode: deviceCode,
      location: location,
      img: img,
      remark: remark,
    }, '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 deviceCode = this.data.deviceCode;
    if (deviceCode == '') {
      wx.showToast({
        title: '请选择站点',
        icon: 'none'
      })
      return false;
    }
    var url = 'outSign/out?deviceCode=' + 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);
    })
  },
   //获取站点
   getDevicesData: function()
   {
     var that = this;
     myRequest.myRequest('outSign/deviceList', {
       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;
    if (that.data.deviceCode == '') {
      wx.showToast({
        title: '附近没有站点',
        icon: 'none',
      })
      return false;
    }
    myRequest.myRequest('outSign/isSign', {
      deviceCode: that.data.deviceCode
    }, 'get').then(function(res){
      var isSing = res.data.data
      that.setData({
        isSing: isSing,
        title: isSing == false ? '签到' : '签退'
      })
    }).catch(function(res){
      console.log(res);
    })
  },
    //判断当日是否签退
    isUserSignOut() {
      var that = this;
      if (that.data.deviceCode == '') {
        wx.showToast({
          title: '附近没有站点',
          icon: 'none',
        })
        return false;
      }
      myRequest.myRequest('outSign/isSignOut', {
        deviceCode: that.data.deviceCode
      }, 'get').then(function(res){
        var isSingOut = res.data.data
        that.setData({
          isSingOut: isSingOut,
          title: isSingOut == false ? '签退' : '签到',
        })
      }).catch(function(res){
        console.log(res);
      })
    },
    onWithoutTitlePicker() {
      this.setData({ city2Visible: true, city2Title: '' });
    },
  //获取位置信息
  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: 'RQJBZ-6ZOCX-3QI4R-Z3JUH-GFDOS-YYBJ4'
        });
        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 () {
    
  },

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

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

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

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

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