// pages/kabaw/detail.js
// 引入SDK核心类
var QQMapWX = require('../../utils/qqmap-wx-jssdk.min.js'), WxParse = require("../../wxParse/wxParse.js");

// 实例化API核心类
var qqmapsdk = new QQMapWX({
  key: 'YGZBZ-QSKCU-7LGVI-2WWXU-E5YEJ-2WFBA' // 必填
});
Page({

  /**
   * 页面的初始数据
   */
  data: {
    is_sell:1,
    qrurl:'',
    detail:'',
    way:'',
    useqr:false,
    can_use:'',
  },

  //在Page({})中使用下列代码
  //触发表单提交事件，调用接口
  jingwei(e) {
    var _this = this;
    //调用地址解析接口
    qqmapsdk.geocoder({
      //获取表单传入地址
      address: e, //地址参数，例：固定地址，address: '北京市海淀区彩和坊路海淀西大街74号'
      success: function (res) {//成功后的回调
        console.log(res);
        var res = res.result;
        var latitude = res.location.lat;
        var longitude = res.location.lng;
        wx.getLocation({
          type: 'gcj02', //返回可以用于wx.openLocation的经纬度
          success(res) {
            const latitude = latitude
            const longitude = longitude
            wx.openLocation({
              latitude,
              longitude,
              scale: 18
            })
          }
        })
      },
      fail: function (error) {
        console.error(error);
      },
      complete: function (res) {
        console.log(res);
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.jingwei('成都市傲城')
    console.log(options)
    // var item = JSON.parse(options.id)
    
    // console.log(item)
    this.setData({
      // detail:item,
      way:options.way,
      send_id:options.send_id
    })
    var id = options.id
    if (options.way == 'use') {
      this.getdetail(id)
    } else {
      this.getbuydetail(id)
    }
  },
  getdetail: function (id) {
    var that = this
    console.log(getApp().api.everyday_coupon.user_info)
    getApp().request({
      url: getApp().api.everyday_coupon.coupondetail,
      data: {
        coupon_id: id
      },
      success: function (t) {
        console.log(t)
        var list = t.data.list
        WxParse.wxParse("detail", "html", list.desc, that)
        that.setData({
          detail: list
        })
      }
    })
  },
  getbuydetail: function (id) {
    var that = this
    console.log(getApp().api.everyday_coupon.user_info)
    getApp().request({
      url: getApp().api.everyday_coupon.buydetail,
      data: {
        coupon_id: id
      },
      success: function (t) {
        console.log(t)
        var list = t.data.list
        WxParse.wxParse("detail", "html", list.desc, that)
        that.setData({
          detail: list
        })
      }
    })
  },
  touse:function(){
    var that = this
    console.log('使用优惠券')
  },
  useit: function () {
    var that = this
    console.log('购买优惠券')

    var that = this
    console.log(getApp().api.everyday_coupon.user_info)
    getApp().request({
      url: getApp().api.everyday_coupon.usecoupon,
      data: {
        user_coupon_id: that.data.detail.user_coupon_id,
      },
      success: function (t) {
        console.log(t)
        if (t.code != 0) {
          return wx.showModal({
            title: '提示',
            content: t.msg,
            showCancel: false
          })
        }
        var url = ''
        if (t.data) {
          url = t.data.url ? t.data.url : '';
        }else{
          
        }
        that.setData({
          useqr:true,
          qrurl: url
        })
      }
    })
  },
  shuaxin:function(){
    var that = this
    wx.showModal({
      title: '提示',
      content: '确定刷新吗？',
      success:function(res){
        if(res.confirm){
          console.log('1')
          that.shua()
        } else {
          console.log('0')
        }
      }
    })
  },
  shuas: function () {
    var that = this
    console.log(getApp().api.everyday_coupon.user_info)
    getApp().request({
      url: getApp().api.everyday_coupon.shuaxin,
      data: {
        id: that.data.detail.user_coupon_id,
      },
      success: function (t) {
        console.log(t)
        if (t.code != 0) {
          return wx.showModal({
            title: '提示',
            content: t.msg,
            showCancel: false,
            success: function (res) {
              if (res.confirm) {
                var detail = that.data.detail
                that.getdetail(detail.id)
              }
            }
          })
        } else if (t.code == 0) {
          var detail = that.data.detail
          detail.user_coupon_id = t.data ? t.data.id : 0
          console.log(detail)
          that.setData({
            detail: detail
          })
          that.getdetail(detail.id)
        }
      }
    })
  },
  shua:function(){
    var that = this
    console.log(getApp().api.everyday_coupon.user_info)
    getApp().request({
      url: getApp().api.everyday_coupon.shuaxin,
      data: {
        id: that.data.detail.user_coupon_id,
      },
      success: function (t) {
        console.log(t)
        if (t.code != 0) {
          return wx.showModal({
            title: '提示',
            content: t.msg,
            showCancel: false,
            success:function(res){
              if(res.confirm){
                var detail = that.data.detail
                that.getdetail(detail.id)
              }
            }
          })
        }else if(t.code==0){
          var detail = that.data.detail
          detail.user_coupon_id = t.data ? t.data.id:0
          console.log(detail)
          that.setData({
            detail: detail
          })
          that.getdetail(detail.id)
          that.useit()
        }
      }
    })
  },
  gothere:function(){
    var that = this
    wx.getLocation({//获取当前经纬度
      type: 'wgs84', //返回可以用于wx.openLocation的经纬度，官方提示bug: iOS 6.3.30 type 参数不生效，只会返回 wgs84 类型的坐标信息  
      success: function (res) {
        wx.openLocation({//​使用微信内置地图查看位置。
          latitude: that.data.detail.lat,
          longitude: that.data.detail.lon,
          name: that.data.detail.shop_name,
          address: that.data.detail.address,
        })
      }
    })
  },
  close:function(){
    var num = this.data.detail.can_use
    var that = this
    if(num==1){
      wx.showModal({
        title: '提示',
        content: '是否已使用该张二维码？',
        confirmText:'已使用',
        cancelText:'未使用',
        success:function(res){
          if(res.confirm){

            that.shuas()
            that.setData({
              useqr: false
            })
          } else {
            var detail = that.data.detail
            that.getdetail(detail.id)
              that.setData({
                useqr: false
              })
          }
        }
      })
    } else {
      that.setData({
        useqr: false
      })
    }
  },

  nopay: function (id) {
    var that = this

    var that = this
    console.log(getApp().api.everyday_coupon.user_info)
    getApp().request({
      url: getApp().api.everyday_coupon.cancelpaycoupon,
      data: {
        order_no: id,
        // send_id: that.data.send_id
      },
      success: function (t) {
        console.log(t)

      }
    })
  },
  buy: function () {
    var that = this
    console.log('购买优惠券')

    var that = this
    console.log(getApp().api.everyday_coupon.user_info)
    getApp().request({
      url: getApp().api.everyday_coupon.buycoupon,
      data: {
        id: that.data.detail.id,
        send_id:that.data.send_id
      },
      success: function (t) {
        console.log(t)
        if(t.code!=0){
          return wx.showModal({
            title: '提示',
            content: t.msg,
            showCancel: false,
            success:function(res){
              if(res.confirm){

                that.nopay(t.order_no)
              }
            }

          })
        }
        var dt = t.data
        var num = t.num ? t.num:''
        if(that.data.detail.is_pay==1){
          wx.requestPayment(
            {
              'timeStamp': dt.timeStamp,
              'nonceStr': dt.nonceStr,
              'package': dt.package,
              'signType': 'MD5',
              'paySign': dt.paySign,
              'success': function (res) {
                console.log(res)
                // wx.redirectTo({
                //   url: '/pages/kabaw/kabaw',
                // })
                wx.showModal({
                  title: '提示',
                  content: '购买成功,共购买' + num + '张',
                  showCancel: false,
                  success: function (res) {
                    if (res.confirm) {
                      // wx.redirectTo({
                      //   url: '/pages/kabaw/kabaw',
                      // })

                      wx.navigateBack({

                      })
                    }
                  }
                })
              },
              'fail': function (res) {
                console.log(res)

                that.nopay(t.order_no)
              },
              'complete': function (res) { }
            })
        }else{
          wx.showModal({
            title: '提示',
            content: '领取成功,共领取' + num+'张',
            showCancel:false,
            success:function(res){
              if(res.confirm){
                // wx.redirectTo({
                //   url: '/pages/kabaw/kabaw',
                // })

                wx.navigateBack({

                })
              }
            }
          })
        }
        
      }
    })
  },
  del: function (id) {
    var that = this
    console.log('购买优惠券')

    var that = this
    console.log(getApp().api.everyday_coupon.user_info)
    getApp().request({
      url: getApp().api.everyday_coupon.delcard,
      data: {
        user_everyday_coupon_id: that.data.detail.id,
        // send_id: that.data.send_id
      },
      success: function (t) {
        console.log(t)
        
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

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

  },

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

  },

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

  },

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

  },

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

  }
})