var initNum = 7; //倒计时数
var spaceNum = 1000; //文字倒计时间隔
var space = 1000; //环倒计时间隔
var begin = -(1 / 2 * Math.PI);
var pai2 = 2 * Math.PI;
var app = getApp();
import {
  BluetoothManager,
  BtErrorCode,
  CMD
} from '../../service/BluWkm'

const bluM = new BluetoothManager;
const util = require('../../utils/utils.js')
Page({
  data: {
    progress_txt: 60,  
    count:0, // 设置 计数器 初始为0
    countTimer: null, // 设置 定时器 初始为null
    money:[],
    request_data:[],
    notice:false,
    order:'',
    screenHeight:''
  },
  closeLock: function (data) {
    var that = this;
    bluM.connectDeivece(that.data.box_no,CMD.lock)
    app.request('/bike/close-lock', data, 'POST').then(res => {
      console.log(res)
      if (res.statusCode == 200) {
        wx.reLaunch({
          url: '../payment/payment?order=' + res.data.no
        })
      } else if (res.statusCode == 450) {
        // that.coundDown()
      }
    }).catch(err => {
      console.log(err)
    })
  },
  rent_closeLock: function(data) {
    var that = this;
    bluM.connectDeivece(that.data.box_no,CMD.lock)
    app.request('/rent/close-order', data, 'POST').then(res => {
      console.log(res)
      if (res.statusCode == 200) {
        wx.reLaunch({
          url: '../payment/payment?order=' + res.data.no + '&rent=rent'
        })
      } else if (res.statusCode == 450) {
        that.coundDown()
      }
    }).catch(err => {
      console.log(err)
    })
  },
  onLoad: function (options) {
    console.log(options)
    wx.setNavigationBarTitle({
      title: app.globalData.title 
    })
    this.setData({
      screenHeight: app.globalData.screenHeight
    })
    wx.showLoading({
      title: '加载中...'
    })
    var data = {
      bike_no: options.bike_no,
      order_no: options.order_no,
      lat: options.lat,
      lng: options.lng
    }
    this.setData({request_data:data,order:options.order})
    if(options.order=='order'){
      app.request('/order/expect-order-money', data, 'POST').then(res => {
        console.log(res)
        if (res.statusCode == 200) {
          wx.hideLoading()
          this.setData({money:res.data})
        }
      })
    }else{
      app.request('/rent/expect-rent-order-money', data, 'POST').then(res => {
        console.log(res)
        if (res.statusCode == 200) {
          wx.hideLoading()
          this.setData({money:res.data})
        }
      })
    }
  },
  notice:function(){
    this.setData({notice:true})
  },
  notice_false:function(){
    this.setData({notice:false})
  },
  kefu:function(){
    wx.makePhoneCall({
      phoneNumber: wx.getStorageSync('home').customer_service_phone,
    })
  },
  close_bike:function(){
    if(this.data.order=='rent'){
      console.log('rent')
      this.rent_closeLock(this.data.request_data)
    }else{
      this.closeLock(this.data.request_data)
    }
  },
  onReady: function () {
    this.drawCircle(60 / (60/2))
    this.drawProgressbg(); 
    this.countInterval()
  },
  drawProgressbg: function(){
    // 使用 wx.createContext 获取绘图上下文 context
    var ctx = wx.createCanvasContext('canvasProgressbg')
    ctx.setLineWidth(6);// 设置圆环的宽度
    ctx.setStrokeStyle('#cccccc'); // 设置圆环的颜色
    ctx.setLineCap('round') // 设置圆环端点的形状
    ctx.beginPath();//开始一个新的路径
    ctx.arc(55, 55, 50, -Math.PI / 2, 60, false);
    //设置一个原点(100,100)，半径为90的圆的路径到当前路径
    ctx.stroke();//对当前路径进行描边
    ctx.draw();
  },
  drawCircle: function (step){  
    var context = wx.createCanvasContext('canvasProgress');
      // 设置渐变
      var gradient = context.createLinearGradient(100, 50, 50, 100);
      context.setStrokeStyle("#F95A29");
      
      context.setLineWidth(6);
      // context.setStrokeStyle(gradient);
      context.setLineCap('round')
      context.beginPath(); 
      // 参数step 为绘制的圆环周长，从0到2为一周 。 -Math.PI / 2 将起始角设在12点钟位置 ，结束角 通过改变 step 的值确定
      context.arc(55, 55, 50, -Math.PI / 2, step * Math.PI - Math.PI / 2, false);
      context.stroke(); 
      context.draw() 
      // this.drawCircle(60 / (60/2))
  },
  countInterval: function () {
    // 设置倒计时 定时器 每100毫秒执行一次，计数器count+1 ,耗时6秒绘一圈
    this.countTimer = setInterval(() => {
      if (this.data.count <= 60) {
        /* 绘制彩色圆环进度条  
        注意此处 传参 step 取值范围是0到2，
        所以 计数器 最大值 60 对应 2 做处理，计数器count=60的时候step=2
        */
       var time = this.data.progress_txt-1
         this.drawCircle(time / (60/2))
        this.data.count++;
        if(time<=0){
          clearInterval(this.countTimer);
          time = 0;
          wx.navigateBack()
        }
        this.setData({
          progress_txt: time
        }); 
      } else {
        clearInterval(this.countTimer);
      }
    }, 1000)
  },
  parkTip: function () {
    wx.navigateTo({
      url: '/pages/navigation/navigation',
    })
  },
  continue:function(){
    clearInterval(this.countTimer);
    wx.navigateBack()
  },
  onShow: function () {

  },
  onHide: function () {
    // clearInterval(this.countTimer);
  },
  onUnload: function () {
    clearInterval(this.countTimer);
  },
  onPullDownRefresh: function () {

  },
  onReachBottom: function () {

  },
  onShareAppMessage: function () {

  }
})