// pages/clock/clock.js
const app=getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    wai:false,
    companyid:'',
    is_first: '',
    day: '',
    status: '',
    token:'',
    qrcode: '',
    address:'',
    first_scan: '',
    this_scan: '',
    // 二维码显示
    showcode:false,
    codeurl:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    this.setData({
      companyid: options.scene
    })
    this.clock()
  },

  // 显示二维码
  sub(){
    this.setData({
      showcode:true
    })
  },
  // 隐藏二维码
  closecode(){
    this.setData({
      showcode:false
    })
  },

  // 保存图片
  //点击保存到相册
  baocun: function () {
    console.log(999)
    var that = this
    wx.getSetting({
      success(res) {
        if (!res.authSetting['scope.writePhotosAlbum']) {
          wx.authorize({
            scope: 'scope.writePhotosAlbum',
            success() { //这里是用户同意授权后的回调


              wx.saveImageToPhotosAlbum({
                filePath: that.data.codeurl,
                success(res) {
                  wx.showModal({
                    content: '图片已保存到相册',
                    showCancel: false,
                    confirmText: '好的',
                    confirmColor: '#333',
                    success: function (res) {
                      if (res.confirm) {
                        console.log('用户点击确定');
                        /* 该隐藏的隐藏 */
                        that.setData({
                          maskHidden: false
                        })
                      }
                    },
                    fail: function (res) {

                    }
                  })
                }
              })



            },
            fail() { //这里是用户拒绝授权后的回调


              // wx.openSetting({
              //   success: function (data) {
              //     console.log(data)
              //     if (data.authSetting["scope.writePhotosAlbum"] === true) {
              //      console.log("是否授权成功")
              //     } else {
              //       applyApi.toast("授权失败");
              //     }
              //   }
              // })
              wx.showModal({
                title: '提示',
                content: '您取消授权,无法保存图片,点击确定打开权限',
                success(res) {
                  if (res.confirm) {
                    console.log('用户点击确定')
                    wx.openSetting({
                      success(res) {
                        console.log(res.authSetting)
                        // res.authSetting = {
                        //   "scope.userInfo": true,
                        //   "scope.userLocation": true
                        // }
                      }
                    })
                  } else if (res.cancel) {
                    console.log('用户点击取消')
                  }
                }
              })

            }
          })
        } else { //用户已经授权过了

          console.log(that.data.codeurl)
          wx.saveImageToPhotosAlbum({
            filePath: that.data.codeurl,
            success(res) {
              wx.showModal({
                content: '图片已保存到相册',
                showCancel: false,
                confirmText: '好的',
                confirmColor: '#333',
                success: function (res) {
                  if (res.confirm) {
                    console.log('用户点击确定');
                    /* 该隐藏的隐藏 */
                    that.setData({
                      maskHidden: false
                    })
                  }
                },
                fail: function (res) {

                }
              })
            }
          })
        }
      }
    })

  },

  savefilecode() {
    let that = this;
    console.log(that.data.codeurl)
    wx.downloadFile({
      url: that.data.codeurl,
      header: {},
      success: function (res) {
        console.log('下载图片下载图片下载图片', res)
        var tempFilePath = res.tempFilePath
        //console.log('临时文件地址是:' + tempFilePath)
        wx.saveFile({
          tempFilePath: tempFilePath,
          success: function (res) {
            console.log(res)
            var saveFilePath = res.savedFilePath;
            that.setData({
              codeurl: res.savedFilePath
            })

            console.log('123456855555555', that.data.qrcode)

          }, //可以将saveFilePath写入到页面数据中
          fail: function (res) { },
          complete: function (res) {
            console.log('complete后的res数据:')
          },
        }) //,
      },
      // fail: function (res) {
      //   wx.showModal({
      //     title: '下载失败',
      //     content: '请联系管理员',
      //   })
      // },
      complete: function (res) { },
    })
  },

  getuser() {
    let that = this;
    var url = '/api/user/index';
    var params = {


    }
    app.post(url, params).then((res) => {
      console.log(res);
     
        that.setData({
          codeurl: res.user_qrcode,
          
        })

        that.savefilecode()

      





    }).catch((err) => {

    })

  },
  clock(){
    let that = this;
    var url = '/api/user/check_in';
    var params = {
      company_id: that.data.companyid,

    }
    app.post(url, params,"post").then((res) => {
      console.log(res);
      that.setData({
        address: res.address,
        is_first: res.is_first,
        day: res.day,
        status:res.status,
        first_scan: res.first_scan,
        this_scan: res.this_scan
      })

      if(res.day<14&&res.status==2){
        
        wx.setNavigationBarColor({
          frontColor: '#ffffff', // 必写项
          backgroundColor: '#D4012D', // 必写项

        })
      }
    }).catch((err) => {

    })
  },
  clocklist(){
    wx.navigateTo({
      url: '/pages/clocklist/clocklist',
    })
  },

  dengji() {
    wx.redirectTo({
     

      url: '/pages/backenterdoor/backenterdoor?clock=' + 1 + '&scene=' + this.data.companyid,
    })
  },

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

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let token = wx.getStorageSync("token");
    this.setData({
      token:token
    })
   
    if (token == '') {
      wx.setNavigationBarTitle({
        title: '等待打卡'
      })
     
      wx.showToast({
        title: '请先授权登录',
        icon: "none"
      })

      setTimeout(function () {
        wx.redirectTo({
          url: '/pages/start/start',
        })
      }, 1000)



      let url = '/pages/clock/clock?scene='+this.data.companyid;
      wx.setStorageSync("url", url)
      wx.setStorageSync("nologin", true)
    }else {
      let that = this;
      var url = '/api/user/index';
      var params = {
      }
      app.post(url, params).then((res) => {
        console.log(res);
        that.setData({
          qrcode: res.qrcode
        })
        if (res.qrcode==''){
          wx.setNavigationBarTitle({
            title: '请登记'
          })
        }else{
          wx.setNavigationBarTitle({
            title: '打卡成功'
          })

          that.getuser()
        }
        
        




      }).catch((err) => {

      })

      
    }
  },

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

  },

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

  },

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

  },

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

  },

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

  }
})