const app = getApp();
const util = require('../../../utils/utils.js');
let videoAd = null;
Page({
  data: {
    index: 0, //0  缴纳押金 1退回押金  2退押金状态
    pay: 0, // 1 支付押金   0 支付/缴纳/退回
    popShow: true, //是否显示弹窗
    money: '',
    depoSuce: true,
    imgUrl: 'http://resource.weilaibike.com/xiaobanma/moneyBg.png',
    depShow: true, //是否选中缴纳押金   true选中  false 未选中
    curShow: '', //是否选中购买免押金卡  undefined 未选中  有值选中
    cardList: [], //免押金卡列表
    state: [],
    deposit_status: '',
    phone: '',
    is_deposit_ad: wx.getStorageSync('setting').is_return_deposit_ad_video,
    img: app.globalData.imgUrl,
    userState: wx.getStorageSync('userState'),
    setting: wx.getStorageSync('setting')
  },
  //获取免押金卡列表
  getCard() {
    let data = {
      area_id: wx.getStorageSync('home').id
    }
    if (wx.getStorageSync('home').id == undefined) {
      wx.showToast({
        title: '您附近没有运营区域',
        icon: 'none'
      })
    } else {
      app.request('/deposit_card/index', data, 'GET').then(res => {
        console.log(res.data);
        this.setData({
          cardList: res.data.data
        })
        wx.hideLoading({
          complete: (res) => {},
        })
        console.log(this.data.cardList)
      })
    }

  },
  student() {
    wx.navigateTo({
      url: '/pages/student_certification/student_certification',
    })
  },
  // 切换免押金卡
  cut(e) {
    let that = this;
    let id = e.currentTarget.dataset.id;
    if (that.data.state.is_deposit == 1 && that.data.state.deposit_type == 1 && id != undefined) {
      wx.showToast({
        title: '您已缴纳押金无需购买免押金卡',
        icon: 'none'
      })
      return;
    }
    if (id == undefined) {
      that.setData({
        depShow: true,
        curShow: "text_undefined",
        depshow: true
      })
    } else {
      that.setData({
        curShow: id,
        depShow: false,
        depshow: false
      })
    }
  },
  explain() {
    wx.navigateTo({
      url: '/pages/explain_card_free/explain_card_free',
    })
  },
  call_phone() {
    var phone = this.data.phone[0];
    wx.makePhoneCall({
      phoneNumber: phone,
    })
  },
  //进入页面判断是否缴纳押金
  getState(e) {
    app.request('/user/status', '', 'GET').then(res => {
      console.log(res, '判断');
      this.setData({
        index: res.data.is_deposit,
        state: res.data
      })
    })
    app.request('/pages/user-deposit-status', '', 'GET').then(res => {
      console.log(res)
      this.setData({
        deposit_status: res.data.type
      })
    })
  },
  //缴纳押金
  pay: util.throttle(function (e) {
    this.setData({
      pay: 1
    })
  }, 100),
  //显示弹窗
  showPop: util.throttle(function (e) {
    this.setData({
      popShow: false,
    })
  }, 1000),
  //取消退回押金
  cancle: util.throttle(function (e) {
    this.setData({
      popShow: true,
    })
  }, 1000),
  //确定退回押金
  confirm: util.throttle(function (e) {
    var that = this;
    this.setData({
      popShow: true,
      index: 1,
    })
    if (this.data.setting.deposit_delay_setting.status == true) {
      wx.showModal({
        title: '退还押金',
        content: '您这笔退款预计一到三天内到账是否确认退款',
        showCancel: true, //是否显示取消按钮
        cancelText: "取消", //默认是“取消”
        cancelColor: '#000000', //取消文字的颜色
        confirmText: "确认", //默认是“确定”
        confirmColor: '#FF0000', //确定文字的颜色
        success: function (res) {
          if (res.cancel) {
            //点击取消,默认隐藏弹框
          } else {
            //点击确定
            app.request('/deposit/refund-job', '', 'POST').then(res => {
              console.log(res);
              if (res.statusCode == 200) {
                wx.reLaunch({
                  url: '/pages/refund_success/refund_success?number=' + that.data.setting.deposit_delay_setting.day,
                })
              }
            })
          }
        }
      })
    } else {
      app.request('/deposit/refund', '', 'POST').then(res => {
        console.log(res);
        if (res.statusCode == 200) {
          wx.showToast({
            title: '申请成功，1个工作日内到账！',
            icon: 'none'
          })
          this.getState()
        }
      })
    }
  }, 1000),
  //立即支付
  depoSuce: util.throttle(function (e) {
    console.log('支付')
    var that = this;
    let url = '';
    var data = '';
    if (app.globalData.req) {
      console.log(that.data.depShow)
      if (that.data.depShow) { //缴纳押金
        console.log(11111)
        url = "/deposit/pay";
        data = {
          area_id: wx.getStorageSync('home').id
        }
      } else { //购买免押金卡
        console.log(2222)
        url = "/deposit_card/pay";
        data = {
          area_id: wx.getStorageSync('home').id,
          id: that.data.curShow
        }
      }
      if (wx.getStorageSync('setting') == '') {
        wx.showModal({
          title: '提示',
          content: '您附近暂无运营区域~',
          showCancel: false,
          success: function (res) {
            if (res.confirm) {
              wx.navigateBack()
            }
          }
        })
      } else {
        app.request(url, data, 'POST', app.globalData.req).then(res => {
          console.log(res)
          if (res.statusCode == 200) {
            console.log(res)
            wx.requestPayment({
              timeStamp: res.data.timeStamp.toString(),
              nonceStr: res.data.nonceStr,
              package: res.data.package,
              signType: res.data.signType,
              paySign: res.data.paySign,
              success(res) {
                console.log(res)
                let init = '';
                if (that.data.depShow) {
                  init = "购买成功"
                } else {
                  init = "支付成功"
                }
                wx.showToast({
                  title: init,
                  icon: 'none',
                  duration: 1000,
                  success: function () {
                    wx.reLaunch({
                      url: '/pages/personal/depoSuce/depoSuce',
                    })
                  }
                })
                that.setData({
                  depoSuce: true
                })
              },
              fail(err) {
                console.log(err)
                wx.showToast({
                  title: '支付失败',
                  icon: 'none'
                })
                that.setData({
                  depoSuce: true
                })
              }
            })
          } else {
            console.log(res)
            that.setData({
              depoSuce: true
            })
          }
        })
      }
    } else {
      wx.showToast({
        title: '您的操作过于频繁，请稍后再试~',
        icon: 'none'
      })
    }
  }, 1000),
  onLoad: function (options) {
    // 是否缴纳押金
    console.log(this.data.depShow)
    var that = this;
    wx.showLoading({
      title: '加载中...',
    })
    this.getState();
    this.setData({
      money: wx.getStorageSync('setting').deposit
    })
    if (wx.getStorageSync('setting') == '') {
      this.setData({
        money: '59.00'
      })
    } else {
      this.setData({
        money: wx.getStorageSync('setting').deposit
      })
    }
    if (options.home) {
      this.setData({
        pay: 1
      })
    }
    console.log(this.data.userState, '哈哈哈')
    if (wx.createRewardedVideoAd) {
      videoAd = wx.createRewardedVideoAd({
        adUnitId: 'adunit-edc5c4664389a87a'
      })
      videoAd.onLoad(() => {})
      videoAd.onError((err) => {})
      videoAd.onClose((res) => {
        if (res && res.isEnded || res === undefined) {
          app.request('/deposit/refund', '', 'POST').then(res => {
            console.log(res);
            if (res.statusCode == 200) {
              wx.showToast({
                title: '加速成功，稍后到账',
                icon: 'none'
              })
              that.getState()
            }
          })
        } else {
          wx.showToast({
            title: '加速失败！',
            icon: 'none'
          })
        }
      })
    }
    this.getCard();
  },
  adShow() {
    if (videoAd) {
      videoAd.show().catch(() => {
        // 失败重试
        videoAd.load()
          .then(() => videoAd.show())
          .catch(err => {
            console.log('激励视频 广告显示失败')
          })
      })
    }
  },
  agreement: util.throttle(function (e) {
    //充值条约
    wx.navigateTo({
      url: '/pages/agreement/agreement',
    })
  }, 1000),
  onReady: function () {

  },
  onShow: function () {
    var phones = wx.getStorageSync('home').customer_service_phone
    this.setData({
      phone: this.data.phone.concat(phones),
      is_deposit_ad: wx.getStorageSync('setting').is_return_deposit_ad_video,
      userState: wx.getStorageSync('userState'),
      img: app.globalData.imgUrl,
      setting: wx.getStorageSync('setting')
    })
  },
  onHide: function () {

  },
  onUnload: function () {

  },
  onPullDownRefresh: function () {

  },
  onReachBottom: function () {

  },
  onShareAppMessage: function () {

  }
})