var app = getApp()
var url = ''
import {
  BluetoothManager,
  BtErrorCode,
  CMD
} from '../../service/BluWkm'
var intervar;
const bluM = new BluetoothManager
Page({
  data: {
    order: '',
    date: [],
    balance: '', //当前余额
    bolear: '', //判断当前余额够不够本次消费 够为true 不够为false
    check1: '',
    check2: '',
    index: '', //记录当前选择的是余额还是微信支付
    disable: false,
    timing: '',
    rent: '',
    screenHeight: '',
    code: '',
    chooseSize: false,
    animationData1: {},
    couponIndex: 0,
    coupon_user_bags_id: 0,
    coupon: [],
    payment_type: false,
    notice: false,
    setting: wx.getStorageSync('setting'),
    img: app.globalData.imgUrl
  },
  catchtouchmove: function () {
    return;
  },
  tap_coupon: function (e) {
    this.setData({
      couponIndex: e.currentTarget.dataset.index
    })
  },
  payment_type: function () {
    this.setData({
      payment_type: true
    })
  },
  cancel: function () {
    console.log(this.data.index)
    this.setData({
      index: this.data.index,
      payment_type: false
    })
    if (this.data.index == 2) {
      this.setData({
        check1: true,
        check2: false
      })
    }
    if (this.data.index == 1) {
      this.setData({
        check2: true,
        check1: false
      })
    }
  },
  queding: function () {
    console.log(this.data.check2)
    console.log(this.data.check1)
    console.log(this.data.index)
    if (this.data.check1) {
      //判断当前是不是余额支付
      this.setData({
        check1: true,
        check2: false,
        index: 2,
      })
    } else {
      this.setData({
        check1: false,
        check2: true,
        index: 1,
      })
    }
    this.setData({
      payment_type: false
    })
  },
  to_recharge: function () {
    // console.log('去充值')
    wx.navigateTo({
      url: '/pages/personal/recharge/recharge',
    })
  },
  notice: function () {
    this.setData({
      notice: true
    })
  },
  notice_false: function () {
    this.setData({
      notice: false
    })
  },
  onLoad: function (options) {
    console.log(options)
    var that = this;
    this.setData({
      screenHeight: app.globalData.screenHeight
    })
    if (options.rent == 'rent') {
      //说明是日租订单
      url = '/rent'
      this.setData({
        rent: false
      })
    } else {
      url = '/order'
      this.setData({
        rent: true
      })
    }
    this.setData({
      order: options.order
    })
    console.log(options.order)
  },
  home: function () {
    //点击返回首页
    var that = this;
    if (app.globalData.compatible) {
      wx.reLaunch({
        url: '/pages/compatible/index/index',
      })
    } else {
      if (that.data.index != '') {
        var data = {
          pay_type: that.data.index,
          order_no: that.data.order,
          coupon_user_bags_id: that.data.coupon_user_bags_id
        }
        if (that.data.disable == true) {
          return;
        }
        that.setData({
          disable: true
        })
        app.request(url + '/pay', data, 'POST').then(res => {
          console.log(res)
          //获取支付参数
          if (res.statusCode == 200) {
            if (res.data.pay_order_status == true) {
              wx.reLaunch({
                url: '/pages/index/index',
              })
            } else {
              if (that.data.index == 1) {
                wx.requestPayment({ //调用微信支付
                  timeStamp: res.data.timeStamp.toString(),
                  nonceStr: res.data.nonceStr,
                  package: res.data.package,
                  signType: res.data.signType,
                  paySign: res.data.paySign,
                  success(resp) {
                    console.log(resp)
                    wx.reLaunch({
                      url: '/pages/index/index',
                    })
                  },
                  fail(err) {
                    console.log(err)
                    wx.showToast({
                      title: '支付失败',
                      icon: 'none'
                    })
                    if (res.data.order_no != that.data.order) {
                      that.setData({
                        order: res.data.order_no
                      })
                    }
                  }
                })
                that.setData({
                  disable: false
                })
              } else {
                wx.reLaunch({
                  url: '/pages/index/index',
                })
              }
            }
          }
        })
      }
    }
  },
  retryClose: function () {
    //点击关锁失败
    var that = this;
    wx.showLoading({
      title: '关锁中...',
      mask: true
    })
    clearInterval(intervar)
    wx.getLocation({
      type: 'gcj02',
      success: function (res) {
        console.log(res)
        bluM.connectDeivece(that.data.box_no, CMD.lock)
        app.request('/order/retry-close-lock?order_no=' + that.data.date.orders.no + '&bike_no=' + that.data.date.orders.bike_no, '', 'GET').then(res => {
          console.log(res)
          if (res.statusCode == 200) {
            wx.hideLoading()
            if (res.data.status == 1) {
              wx.showToast({
                title: '关锁成功',
                icon: 'none'
              })
            }
          }
        })
      },
    })
  },
  check1: function () {
    //选择余额支付
    this.setData({
      check1: true,
      check2: false
    })
  },
  check2: function () {
    //选择微信支付
    this.setData({
      check1: false,
      check2: true
    })
  },
  check4: function () {
    //点击去购买骑行卡
    wx.navigateTo({
      url: '/pages/recharge/recharge',
    })
  },
  check3: function () {
    var that = this;
    // 创建一个动画实例
    console.log(that.data.coupon)
    app.request('/coupon/getCouponsByOrder?order_no=' + that.data.order, '', 'GET').then(res => {
      console.log(res)
      if (res.statusCode == 200) {
        that.setData({
          coupon: res.data
        })
        var coupon = res.data.dataYes;
        for (var i = 0; i < coupon.length; i++) {
          if (coupon[i].id == that.data.date.user_coupon.coupon_user_bags_id) {
            coupon[i].selected = true;
          } else {
            coupon[i].selected = false;
          }
        }
        var couponYes = 'coupon.dataYes';
        that.setData({
          [couponYes]: coupon
        })
      }
    })
    var animation = wx.createAnimation({
      // 动画持续时间
      duration: 400,
      // 定义动画效果，当前是匀速
      timingFunction: 'linear'
    })
    // 将该变量赋值给当前动画
    that.animation = animation
    // 先在y轴偏移，然后用step()完成一个动画
    animation.translateY(300).step()
    // 用setData改变当前动画
    that.setData({
      // 通过export()方法导出数据
      animationData1: animation.export(),
      // 改变view里面的Wx：if
      chooseSize: true
    })
    // 设置setTimeout来改变y轴偏移量，实现有感觉的滑动
    setTimeout(function () {
      animation.translateY(0).step()
      that.setData({
        animationData1: animation.export()
      })
    }, 100)

  },
  hideModal: function (e) {
    var that = this;
    var animation = wx.createAnimation({
      duration: 400,
      timingFunction: 'linear'
    })
    that.animation = animation
    animation.translateY(360).step()
    that.setData({
      animationData1: animation.export()

    })
    setTimeout(function () {
      var query = wx.createSelectorQuery();
      query.select('.choose').boundingClientRect();
      query.exec(function (res) {
        console.log(res)
        // that.setData({arrowDown:res[0].height+15})
        animation.translateY(res[0].height).step()
      })
      that.setData({
        animationData1: animation.export(),
        chooseSize: false
      })
    }, 400)
  },
  coupons: function (e) {
    var id = e.currentTarget.dataset.id;
    var index = e.currentTarget.dataset.index;
    var coupons = this.data.coupon.dataYes;
    var select = e.currentTarget.dataset.selected;
    if (coupons[index].selected == true) {
      for (var i = 0; i < coupons.length; i++) {
        coupons[i].selected = false;
      }
    } else {
      for (var i = 0; i < coupons.length; i++) {
        coupons[i].selected = false;
        coupons[index].selected = true
      }
    }
    var couponList = 'coupon.dataYes';
    this.setData({
      coupon_user_bags_id: id,
      // chooseSize: false,
      [couponList]: coupons
    })
  },
  srue: function () {
    var that = this;
    var coupon_yes = that.data.coupon.dataYes;
    var id = ''
    for (var i = 0; i < coupon_yes.length; i++) {
      if (coupon_yes[i].selected == true) {
        id = coupon_yes[i].id
      }
    }
    wx.showLoading({
      title: '加载中...',
      mask: true
    })
    app.request(url + '/pay-show?order_no=' + that.data.order + '&coupon_user_bags_id=' + id, '', 'GET').then(res => {
      console.log(res)
      if (res.statusCode == 200) {
        wx.hideLoading()
        that.setData({
          date: res.data,
          balance: res.data.wallet_money,
          bolear: res.data.wallet_pay_status,
          chooseSize: false
        })
        if (res.data.wallet_pay_status == false) {
          that.setData({
            check1: false,
            check2: true,
            index: 1
          })
        } else {
          that.setData({
            check1: true,
            check2: false,
            index: 2
          })
        }
        console.log(that.data.index)
        if (res.data.user_coupon.is_coupon) {
          that.setData({
            coupon_user_bags_id: res.data.user_coupon.coupon_user_bags_id
          })
        }
      }
    })
  },
  payment: function () {
    //点击立即支付
    var that = this;
    console.log(that.data.index)
    if (that.data.index != '') {
      var data = {
        pay_type: that.data.index,
        order_no: that.data.order,
        coupon_user_bags_id: that.data.coupon_user_bags_id
      }
      if (that.data.disable == true) {
        return;
      }
      that.setData({
        disable: true
      })
      app.request(url + '/pay', data, 'POST').then(res => {
        console.log(res)
        //获取支付参数
        if (res.statusCode == 200) {
          if (that.data.index == 1) {
            wx.requestPayment({ //调用微信支付
              timeStamp: res.data.timeStamp.toString(),
              nonceStr: res.data.nonceStr,
              package: res.data.package,
              signType: res.data.signType,
              paySign: res.data.paySign,
              success(resp) {
                console.log(resp)
                wx.reLaunch({
                  url: '/pages/personal/rechSuce/rechSuce?order=' + that.data.date.orders.no + '&rent=' + that.data.rent,
                })
              },
              fail(err) {
                console.log(err)
                wx.showToast({
                  title: '支付失败',
                  icon: 'none'
                })
                if (res.data.order_no != that.data.order) {

                  that.setData({
                    order: res.data.order_no
                  })
                }
              }
            })
            that.setData({
              disable: false
            })
          } else {
            wx.reLaunch({
              url: '/pages/personal/rechSuce/rechSuce?order=' + that.data.order + '&rent=' + that.data.rent,
            })
          }
        } else {
          that.setData({
            disable: false
          })
        }
      }).catch(err => {
        console.log(err)
      })

    } else {
      wx.showToast({
        title: '请选择支付方式',
        icon: 'none'
      })
    }
  },
  repair: function () {
    wx.navigateTo({
      url: '/pages/repair/repair?bike_id=' + this.data.code,
    })
  },
  phone: function (e) {
    wx.makePhoneCall({
      phoneNumber: wx.getStorageSync('home').customer_service_phone,
    })
  },
  onReady: function () {
    // console.log(this.data.coupon.dataYes)
  },
  onShow: function () {
    this.refresh()
    this.setData({
      setting: wx.getStorageSync('setting')
    })
  },
  refresh: function () {
    wx.showNavigationBarLoading();
    var that = this;
    wx.showLoading({
      title: '加载中...',
      mask: true
    })
    app.request(url + '/pay-show?order_no=' + that.data.order + '&is_first=true', '', 'GET').then(res => {
      console.log(res)
      if (res.statusCode == 200) {
        wx.hideLoading({
          complete: (res) => {},
        })
        that.setData({
          date: res.data,
          balance: res.data.wallet_money,
          bolear: res.data.wallet_pay_status,
          code: res.data.orders.bike_no
        })
        if (res.data.wallet_pay_status == false) {
          that.setData({
            check1: false,
            check2: true,
            index: 1
          })
        } else {
          that.setData({
            check1: true,
            check2: false,
            index: 2
          })
        }
        if (res.data.user_coupon.is_coupon) {
          that.setData({
            coupon_user_bags_id: res.data.user_coupon.coupon_user_bags_id
          })
        }
        wx.hideNavigationBarLoading()
        wx.stopPullDownRefresh()
      }
    })
  },
  onHide: function () {

  },
  onUnload: function () {

  },
  onPullDownRefresh: function () {
    this.refresh()
  },
  onReachBottom: function () {

  },
  onShareAppMessage: function () {

  }
})