// pages/confirmProjectOrder/confirmProjectOrder.js
import {
  get,
  post
} from '../../utils/http'
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentCoupon: -1,
    visible: false,
    CouPonIndex: [],
    needSelect: false,
    coupSelectType: '',
    productDiscount: '',
    isSelectCoup: [], // 选择的优惠券
    // 下面是支付的数据
    pay_methon: 1,
    payNow: false,
    switchPay: 'product',
    showMoney: 0, // 价值总额
    showRuleText: 10086,
    dotSelectCard: false,
    marks: {
      // 优惠券类型，1直接抵扣券，2满减券，3全额减免券，4卡券
      1: '抵扣券',
      2: '满减券',
      3: '全额减免券',
      4: '项目卡券'
    },
    orderDiscount: 0,
    priceSet: 0,
    cardList: [], // 我的次卡对应项目列表
    selectCodeList: [], //选择的次卡的列表
    visibleCard: false,
    card_data: [], //选择次卡的项目id
    selectCardProjectList: [], //选择次卡的项目列表
    selectCoupPrice: 0, //选择优惠券之后的价格
    discountList: [], //折扣列表
    level: '', //用户充值折扣等级 (0是未拥有等级)
    levelPrice: '', //充值等级折扣优惠价格
  },
  // 获取用户充值折扣等级
  getLevel() {
    get('/v2/api/user/discount_level', {}, (res) => {
      if (res.code == 200) {
        res.data = Number(res.data)
        if (res.data == 0) {
          this.setData({
            level: 0
          })
          return
        }
        this.setData({
          level: Number(res.data * 0.1)
        })
        this.setData({
          tolevel: (this.data.level * 10).toFixed(1)
        })
      }
    })
  },
  clickRuleText(e) {
    let that = this
    let index = e.currentTarget.dataset.index
    if (this.data.showRuleText == index) {
      that.setData({
        showRuleText: 10086
      })
    } else {
      that.setData({
        showRuleText: index
      })
    }
  },

  /**
   * 点击使用按钮、勾选按钮
   */
  checkSelect: function (e) {
    // let index = e.currentTarget.dataset.index
    let CouPonIndex = this.data.productDiscount
    let selectIndex = e.currentTarget.dataset.index
    let project_list = this.data.orderDetails.project_list //这里是项目列表
    let project_ids = this.data.productDiscount[selectIndex].project_ids
    let _ids = 'productDiscount[' + selectIndex + '].project_ids'
    let isReturn = false
    //去掉已选择次卡的项目id
    this.data.card_data.forEach(res => {
      if (project_ids.indexOf(res.id) > -1) {
        project_ids.forEach((e, i) => {
          if (res.id == e) {
            project_ids.splice(i, 1)
          }
        })
      }
    })
    this.setData({
      _ids: project_ids
    })
    if (this.data.productDiscount[selectIndex].type != 4 && this.data.card_data.length != 0) {
      wx.showToast({
        title: '该项目已选择次卡',
        icon: 'none'
      })
      return
    }
    this.data.selectCardProjectList.forEach(res => {
      if (this.data.productDiscount[selectIndex].project_ids.indexOf(res.id) > -1) {
        wx.showToast({
          title: '该项目已选择次卡',
          icon: 'none'
        })
        isReturn = true
      }
    })
    if (isReturn) {
      return
    }
    let curreValue = !CouPonIndex[selectIndex].isSelect
    // 判断是否是不相同的项目券
    if (this.data.productDiscount[selectIndex].type != 4) {
      this.setData({
        priceSet: 0,
        showMoney: this.data.orderDetails.price
      })

      for (let i = 0; i < CouPonIndex.length; i++) {
        CouPonIndex[i].isSelect = false
      }
      for (let i = 0; i < project_list.length; i++) {
        project_list[i].cardId = ''
      }

    } else if (this.data.coupSelectType != 4) {
      for (let i = 0; i < CouPonIndex.length; i++) {
        CouPonIndex[i].isSelect = false
      }
      for (let i = 0; i < project_list.length; i++) {
        project_list[i].cardId = ''
      }
    } else {
      let numberSelect = 0
      for (let i = 0; i < CouPonIndex.length; i++) {
        if (CouPonIndex[i].isSelect) {
          numberSelect = numberSelect + 1
        }
      }
      console.log('numberSelect')
      console.log(numberSelect)
      if (numberSelect == this.data.orderDetails.project_list.length && curreValue) {
        wx.showToast({
          title: '最多选择' + this.data.orderDetails.project_list.length + '项目券!',
          icon: 'none'
        })
        return
      }
    }

    CouPonIndex[selectIndex].isSelect = curreValue
    if (!curreValue) {
      for (let j = 0; j < project_list.length; j++) {
        if (project_list[j].cardId && project_list[j].cardId == this.data.productDiscount[selectIndex].id) { // 如果是取消选择，那么也要取消绑定项目的id
          project_list[j].cardId = ''
        }
      }
      CouPonIndex[selectIndex].projectids = ''
    }
    let orderDetails1 = this.data.orderDetails
    orderDetails1.project_list = project_list

    this.setData({
      orderDetails: orderDetails1,
      productDiscount: CouPonIndex,
      coupSelectType: this.data.productDiscount[selectIndex].type
    })
    console.log(this.data.CouPonIndex)
    this.selectCoup()
  },

  // 计算总额
  calculateMoney() {
    let that = this
    let initPrice = 0
    if (this.data.selectCardProjectList.length > 0) {
      this.data.selectCardProjectList.forEach(res => {
        initPrice = Number(initPrice) + Number(res.price)
      })
    }
    console.log(initPrice, 'initPrice');
    let money = this.data.selectCardProjectList.length > 0 ? Number(this.data.orderDetails.price - initPrice) : this.data.orderDetails.price
    console.log(money, 'moneymoney');
    let isSelectCoup = this.data.isSelectCoup
    let project_list = this.data.orderDetails.project_list //这里是项目列表

    // if (isSelectCoup.length == 0) {
    //   that.setData({
    //     showMoney: money.toFixed(2)
    //   })
    //   return
    // }
    // 如果是项目
    if (isSelectCoup.length > 0) {

      if (isSelectCoup[0].type == 4) {
        let numberHaveCardId = 0
        for (let i = 0; i < project_list.length; i++) {
          if (project_list[i].cardId) {
            money = money - project_list[i].price
            numberHaveCardId = numberHaveCardId + 1
          }
        }
      } else if (isSelectCoup[0].type == 3) {
        for (let i = 0; i < project_list.length; i++) {
          money = money - project_list[i].price
        }

      } else {
        money = money - isSelectCoup[0].price
      }
    }

    /**
     * type:1项目，2设备分类，3耗材
     * discount_type:折扣条件，1数量/类型，2金额
     */
    let notSelectId = []
    this.data.isSelectCoup.forEach(item => {
      notSelectId.push(item.projectids)
    })
    //去除选择优惠券或者次卡的项目
    let _project_list = project_list.filter(res => {
      return notSelectId.toString().indexOf(res.id) && res.isCard == false
    })

    let _money = money
    console.log(money, 'money===', _money);
    let devicePrice = 0 //满足折扣设备的项目总金额
    let productPrice = 0 //耗材总金额
    //满足折扣设备的项目
    let deviceList = _project_list.filter(item => {
      return item.device_type_id == item.device_id
    })
    if (deviceList.length > 0) {
      deviceList.forEach(item => {
        devicePrice = devicePrice + Number(item.price)
      })
    }
    //拥有耗材的项目
    let productList = _project_list.filter(item => {
      return item.product_name != ""
    })
    console.log(productList, 'productList');
    if (productList.length > 0) {
      productList.forEach(item => {
        productPrice = productPrice + Number(item.product_price)
      })
    }
    let _priceSet = this.data.priceSet
    if (this.data.discountList.length > 0) {
      let discountList = this.data.discountList
      //计算所有的折扣金额
      discountList.forEach(item => {
        let priceSet = 0,
          initmoney = money,
          _initmoney = money
        if (item.type == 1 && item.discount_type == 1 && _project_list.length >= item.num) {
          initmoney = initmoney * item.discount_rate / 10
          priceSet = _initmoney - initmoney
          item["priceSet"] = priceSet
        } else if (item.type == 1 && item.discount_type == 2 && money > Number(item.amount)) {
          initmoney = initmoney * item.discount_rate / 10
          priceSet = _initmoney - initmoney
          item["priceSet"] = priceSet
        } else if (item.type == 2 && item.discount_type == 2 && devicePrice > Number(item.amount)) {
          //满足条件的项目折扣
          initmoney = (initmoney - devicePrice) + devicePrice * item.discount_rate / 10
          priceSet = _initmoney - initmoney
          item["priceSet"] = priceSet

        } else if (item.type == 3 && item.discount_type == 1 && productList.length >= item.num) {
          //满足条件的项目折扣
          initmoney = (initmoney - productPrice) + productPrice * item.discount_rate / 10
          priceSet = _initmoney - initmoney
          item["priceSet"] = priceSet

        } else if (item.type == 3 && item.discount_type == 2 && productPrice >= item.amount) {
          //满足条件的项目折扣
          initmoney = (initmoney - productPrice) + productPrice * item.discount_rate / 10
          priceSet = _initmoney - initmoney
          item["priceSet"] = priceSet
        }
      })
      console.log(discountList, 'discountList');
      //排序，最优折扣放第一位
      discountList = discountList.sort(this.compare("priceSet"))

      let discountInfo = discountList[0]
      if (discountInfo.type == 1 && discountInfo.discount_type == 1 && _project_list.length >= discountInfo.num) {
        money = money * discountInfo.discount_rate / 10
        _priceSet = _money - money
      } else if (discountInfo.type == 1 && discountInfo.discount_type == 2 && money > Number(discountInfo.amount)) {
        money = money * discountInfo.discount_rate / 10
        _priceSet = _money - money
      } else if (discountInfo.type == 2 && discountInfo.discount_type == 2 && devicePrice > Number(discountInfo.amount)) {
        //满足条件的项目折扣
        money = (money - devicePrice) + devicePrice * discountInfo.discount_rate / 10
        _priceSet = _money - money

      } else if (discountInfo.type == 3 && discountInfo.discount_type == 1 && productList.length >= discountInfo.num) {
        //满足条件的项目折扣
        money = (money - productPrice) + productPrice * discountInfo.discount_rate / 10
        _priceSet = _money - money
      } else if (discountInfo.type == 3 && discountInfo.discount_type == 2 && productPrice >= discountInfo.amount) {
        //满足条件的项目折扣
        money = (money - productPrice) + productPrice * discountInfo.discount_rate / 10
        _priceSet = _money - money
      }
    }
    console.log(_priceSet, '_priceSet');
    that.setData({
      showMoney: Number(money).toFixed(2) < 0 ? 0.00 : Number(money).toFixed(2),
      priceSet: Number(_priceSet).toFixed(2),
      orderDiscount: this.data.discountList.length!=0?this.data.discountList.sort(this.compare("priceSet"))[0].id:0
    })
  },
  //排序
  compare(property) {
    return function (obj1, obj2) {
      var value1 = obj1[property];
      var value2 = obj2[property];
      return value2 - value1; // 降序
    }
  },

  // 支付的操作
  onClosePay() {
    this.setData({
      payNow: false
    })
  },


  /**
   * 关闭弹框
   */
  onClose() {
    this.setData({
      visible: false
    })
  },

  // 关闭次卡弹窗
  onCloseCard() {
    this.setData({
      visibleCard: false
    })
  },

  // 点击x关闭次卡弹窗
  onCancelCard() {
    this.setData({
      visibleCard: false
    })
  },

  showCoup() {
    if (this.data.dotSelectCard) {
      wx.showToast({
        title: '本订单已使用优惠券!',
        icon: 'none'
      })
      return
    }
    this.setData({
      visible: true
    })
  },

  // 打开次卡窗口
  showCard() {

    this.setData({
      visibleCard: true,
      selectCoupPrice: this.data.showMoney
    })
  },

  // 选中次卡触发
  cardSel(e) {
    console.log(this.data.selectCardProjectList, e.currentTarget.dataset, 'e.currentTarget.dataset')
    let index = e.currentTarget.dataset.index
    let item = e.currentTarget.dataset.item
    let selList = this.data.cardList
    let isselectCard = false
    let isRepeat = false

    this.data.cardList.forEach(res => {
      if (JSON.stringify(item.project_ids) == JSON.stringify(res.project_ids) && res.isSelectCard && res.id != item.id) {
        isRepeat = true
      }
    })
    if (isRepeat) {
      wx.showToast({
        title: '不可重复选择相同次卡',
        icon: 'none'
      })
      return
    }
    this.data.orderDetails.project_list.forEach((res, i) => {
      if (res.cardId && this.data.cardList[index].project_ids.indexOf(res.id) > -1) {
        isselectCard = true
      }
    })
    if (isselectCard) {
      wx.showToast({
        title: '该项目已选择优惠券',
        icon: 'none'
      })
      return
    }
    let isProjectList = []
    //判断是否重复选择次卡
    let isTrueCode = false
    this.data.cardList.forEach(res => {
      if (res.isSelectCard) {
        isProjectList = isProjectList.concat(res.project_ids)
      }
    })
    if (e.currentTarget.dataset.item.isSelectCard) {
      e.currentTarget.dataset.item.project_ids.forEach((_item, index) => {
        isProjectList.forEach((item, i) => {
          if (_item == item) {
            isProjectList.splice(i, 1)
          }
        })
      })
    }
    if (this.includes(isProjectList, e.currentTarget.dataset.item.project_ids)) {
      isTrueCode = true
    }
    if (isTrueCode) {
      wx.showToast({
        title: '请勿重复选择次卡',
        icon: 'none'
      })
      return
    }

    console.log(isProjectList, 'isProjectList');
    let selValue = !selList[index].isSelectCard
    selList[index].isSelectCard = selValue
    this.setData({
      cardList: selList
    })
  },

  includes(arr1, arr2) {
    return arr2.every(val => arr1.includes(val));
  },
  onConfirmCard() {
    let price = this.data.orderDetails.price //订单价格
    let card_data = []
    let card_data_info = {}
    let selectCodeList = []
    let selectCardProjectList = []

    //选择次卡&价格计算
    let projectId = []
    this.data.orderDetails.project_list.forEach((res, i) => {
      if (res.cardId) {
        price = price - res.price
      }
      this.data.cardList.forEach(item => {
        //如果已选择优惠券
        console.log(res.cardId, 'cardId.length', item.isSelectCard);

        if (item.project_ids.indexOf(res.id) > -1 && projectId.indexOf(res.id) == -1 && item.isSelectCard) {
          projectId.push(res.id)
          price = price - res.price
          price = price < 0 ? 0 : price
          selectCardProjectList.push(res)
          selectCardProjectList = Array.from(new Set(selectCardProjectList))
          selectCodeList.push(item)
          card_data_info = {
            id: res.id,
            card_id: item.id
          }
          card_data.push(card_data_info)
          let _item = 'orderDetails.project_list[' + i + '].isCard'
          this.setData({
            [_item]: true
          })
        }
      })
    })
    let priceSet = 0
    if (selectCodeList.length == 0 && this.data.discountList.length > 0) {
      priceSet = this.data.discountList[0].priceSet.toFixed(2)
      price = this.data.orderDetails.price - priceSet
    }
    this.setData({
      card_data: card_data,
      showMoney: price,
      visibleCard: false,
      selectCodeList: selectCodeList,
      selectCardProjectList: selectCardProjectList,
      priceSet: priceSet
    })
    this.calculateMoney() //计算总额
  },

  onCancelCoupon() {
    this.setData({
      visible: false
    })
  },
  onConfirmCoupon() {
    // let currentChangeCoupon = this.data.couponList[this.data.currentCoupon]
    this.setData({
        visible: false,
        // currentChangeCoupon
      },
      () => {
        // this.calculateOrderPayMoney()
      }
    )
  },


  selectCoup() {
    let that = this
    let isSelectCoups = []
    for (let i = 0; i < this.data.productDiscount.length; i++) {
      if (this.data.productDiscount[i].isSelect) {
        let selectCoup = that.data.productDiscount[i]
        isSelectCoups.push(selectCoup)
      }
    }
    this.setData({
      isSelectCoup: isSelectCoups
    })
    console.log(isSelectCoups)

    that.theMoneyBest()
  },

  /**
   * 获取订单详情
   */
  getOrderData() {
    let that = this
    get('v2/api/order/info', {
      id: this.data.orderDetailsId
    }, (res) => {
      res.data.project_list.forEach(item => {
        item.isCard = false
      })
      if (res.data.discount != 0) {
        let newsPrice = res.data.price
        res.data.price = newsPrice * 1 + res.data.discount_price * 1

        this.setData({
          dotSelectCard: true,
          orderDetails: res.data,
          showMoney: newsPrice,
          priceSet: res.data.discount_price
        })
        return
      }

      if (res.data.coupon_list.length > 0) {
        res.data.coupon_list[0].type = 0
      }

      this.setData({
        orderDetails: res.data,
        showMoney: res.data.price,
        isSelectCoup: res.data.coupon_list,
        dotSelectCard: res.data.coupon_list.length !== 0 ? true : false,
      }, )
      that.productDiscount()
      if (res.data.type == "one") {
        return
      }
      that.orderDiscount()

    })
  },

  // 这里是立即支付的接口
  // v2/api/order/pay
  payNow: function () {
    // 计算折扣
    this.setData({
      levelPrice: (this.data.showMoney * this.data.level).toFixed(2)
    })
    this.setData({
      toLevelPrice: (this.data.showMoney - this.data.levelPrice).toFixed(2)
    })
    this.setData({
      payNow: true,
    })
  },
  // 活动折扣类型，非必填，0无折扣（默认），50新人五折活动，100仪器免费使用权益
  paybtm() {
    let isSelectCoup = this.data.isSelectCoup
    let coupType = isSelectCoup.length > 0 ? isSelectCoup[0].type : 5
    let coupData = []
    // 卡券，项目券数据包,id：订单项目ID,coupon_id：优惠券ID,如：[{"id":1220,coupon_id:199}]
    // 支付方式，amount余额支付，weixin微信支付
    if (coupType == 4) {
      for (let i = 0; i < isSelectCoup.length; i++) {
        let notData = {
          id: isSelectCoup[i].projectids,
          coupon_id: isSelectCoup[i].id,
        }
        coupData.push(notData)
      }
    }

    let dataTo = {
      id: this.data.orderDetails.id,
      pay_way: this.data.pay_methon == 1 ? 'weixin' : 'amount',
    }

    if (coupType == 4) {
      dataTo.coupon_data = JSON.stringify(coupData)
    } else if (coupType != 5) {
      dataTo.coupon_id = isSelectCoup[0].id
    }
    if (this.data.card_data.length != 0) {
      dataTo.card_data = JSON.stringify(this.data.card_data)
    }

    if (this.data.priceSet != 0 && !this.data.dotSelectCard && this.data.card_data.length == 0) {
      dataTo.discount = this.data.orderDiscount
    }
    let that = this
    console.log(dataTo)
    post('v2/api/order/pay', dataTo, (res) => {
      that.setData({
        payNow: false
      })

      if (res.data.pay_status == 1) {
        // wx.showToast({
        //   title: '支付成功!',
        //   icon: "none"
        // })
        that.payOkey()
        return
      }

      if (that.data.pay_methon == 2) {
        // wx.showToast({
        //   title: '支付成功!',
        //   icon: "none"
        // })
        that.payOkey()
        return
      }

      wx.requestPayment({
        timeStamp: res.data.pay_data.timeStamp,
        nonceStr: res.data.pay_data.nonceStr,
        package: res.data.pay_data.package,
        signType: res.data.pay_data.signType,
        paySign: res.data.pay_data.paySign,
        success(res) {
          if (res.errMsg == 'requestPayment:ok') {
            // wx.showToast({
            //   title: '支付成功!',
            //   icon: "none"
            // })
            that.payOkey()
          }
        },
        fail(res) {
          that.orderDiscount()
          that.getOrderData()
          wx.showToast({
            title: '支付失败!',
            icon: "none"
          })
        }
      })
    })
  },

  payOkey() {
    wx.showToast({
      title: '支付成功!',
      icon: "none"
    })
    let that = this
    setTimeout(function () {
      that.toDetails()
    }, 1000); //延迟时间 这里是1秒
  },

  toDetails() {
    wx.redirectTo({
      url: '/pages/orderDetailsProject/orderDetailsProject?id=' + this.data.orderDetails.id,
    })
  },

  pay_methonw() {
    this.setData({
      pay_methon: 1
    })
  },

  pay_methonc() {
    this.setData({
      pay_methon: 2
    })
  },


  //
  // 卡券，项目券数据包,id：订单项目ID,coupon_id：优惠券ID,如：[{"id":1220,coupon_id:199}]
  theMoneyBest() {

    let that = this
    let project_list = this.data.orderDetails.project_list //这里是项目列表
    let isSelectCoup = this.data.isSelectCoup //这里选择了的卡券列表
    let selectProjectList = [] // 适合的卡券列表
    // let CouPonIndex = this.data.CouPonIndex // 卡券选择的Id
    let productDiscount = this.data.productDiscount // 卡券列表
    console.log(productDiscount)


    if (isSelectCoup.length == 0) {
      this.setData({
        priceSet: 0,
        showMoney: this.data.orderDetails.price
      })
      that.onShowPrice()
      return
    }


    // debugger
    if (isSelectCoup[0].type == 4) {
      for (let i = 0; i < isSelectCoup.length; i++) { // 首先遍历卡券
        if (!isSelectCoup[i].projectids) { // 如果卡券没绑定项目
          console.log('没绑定过!')

          let selectIndexMoney = 0 // 初始值
          let selectIndex = 111 // 初始金额
          for (let j = 0; j < project_list.length; j++) { // 遍历项目
            if (isSelectCoup[i].project_ids.indexOf(project_list[j].id) != -1 && !project_list[j].cardId) { // 如果卡券能够使用这个项目并且这个项目没有被绑定过
              console.log('project_list[j].price')
              console.log(project_list[j].price)
              console.log(selectIndexMoney)
              console.log(project_list[j].price > selectIndexMoney)
              if (project_list[j].price * 1 > selectIndexMoney * 1) {
                selectIndex = j
                selectIndexMoney = project_list[j].price
                console.log('--' + selectIndexMoney)
              }
              // selectProjectList.push(project_list[i])
            }
          }
          if (selectIndex != 111) {
            isSelectCoup[i].projectids = project_list[selectIndex].id
            isSelectCoup[i].projectName = project_list[selectIndex].name
            project_list[selectIndex].cardId = isSelectCoup[i].id

          } else {
            for (let k = 0; k < productDiscount.length; k++) {
              if (productDiscount[k].id == isSelectCoup[i].id) {
                // isSelectCoup.
                productDiscount[k].isSelect = false
                // isSelectCoup.splice(isSelectCoup.length-1,1);
                wx.showToast({
                  title: '没有适合的项目!',
                  icon: "none"
                })
              }
            }
          }

          if (selectIndex == 111) {
            isSelectCoup.splice(isSelectCoup.length - 1, 1);
          }

        }
      }
      let orderDetails1 = this.data.orderDetails
      orderDetails1.project_list = project_list
      this.setData({
        // CouPonIndex: CouPonIndex,
        productDiscount: productDiscount,
        isSelectCoup: isSelectCoup,
        orderDetails: orderDetails1,
      })

    }

    this.calculateMoney() //计算总额

  },

  /**
   * 获取订单详情
   */
  productDiscount() {
    let that = this
    let ids = ''
    // for (let i = 0; i < this.data.orderDetails.project_list.length; i++) {
    //   ids = ids + ',' + this.data.orderDetails.project_list[i].id
    // }
    let storeData = getApp().globalData.storeData
    let storeId = storeData.id || wx.getStorageSync('store_id')

    get('v2/api/order/coupon', {
      id: this.data.orderDetails.id,
      store_id: storeId,
      page: 1,
      limit: 200,
    }, (res) => {
      let showLine = res.data.list
      let CouPonIndex = []
      for (let i = 0; i < showLine.length; i++) {

        if (showLine[i].full_price * 1 < that.data.orderDetails.price * 1 || showLine[i].full_price * 1 == that.data.orderDetails.price * 1) {
          showLine[i].isSelect = false
          CouPonIndex.push(showLine[i])
        }
      }

      this.setData({
        productDiscount: CouPonIndex,
        // CouPonIndex: CouPonIndex
      }, )
    })
  },

  /**
   * 获取当前用户折扣
   */
  orderDiscount() {
    get('/v3/api/order/discount', {
      id: this.data.orderDetailsId
    }, (res) => {
      this.setData({
        discountList: res.data.list
      }, )
      this.onShowPrice()
      // 判断是否有别的折扣
      if(res.data.total==0){
        // 获取用户折扣等级
        this.getLevel()
      }
    })
  },

  // 计算折扣价格
  onShowPrice() {

    if (this.data.discountList.length == 0) {
      return
    }
    this.calculateMoney()
    // let money = Number(this.data.orderDetails.price) * Number(this.data.discountList[0].discount_rate) / 10

    // this.setData({
    //   priceSet: (Number(this.data.orderDetails.price) - money).toFixed(2),
    //   showMoney: money.toFixed(2)
    // })
    // orderDiscount
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.q) {
      let conUrlAll = decodeURIComponent(options.q)
      this.setData({
        orderDetailsId: conUrlAll.replace('https://test-img.ijolijoli.com/wxmini/pay?id=', '')
      })
    } else {
      this.setData({
        orderDetailsId: options.id
      })
    }
    this.getOrderData()
    // 获取项目可用的次卡列表
    this.getAvaCardList()
    let lat1 = app.address.latitude
    let lng1 = app.address.longitude
    this.setData({
      lat1: lat1,
      lng1: lng1,
    })
  },

  onCopy: function (e) {
    let content = e.currentTarget.dataset.no
    var that = this;
    wx.setClipboardData({
      data: content,
      success: function (res) {
        wx.showToast({
          title: '复制成功!',
          icon: "none"
        })
      }
    });
  },

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

  },

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

  /**
   * 获取我的储值
   * api/user
   */
  getAmount() {
    get('api/user/amount', {}, (res) => {
      if (res.data) {
        this.setData({
          inserllAmount: res.data.amount
        })
      }
    })
  },

  // 获取项目可用的次卡列表
  getAvaCardList() {
    console.log(this, 'shit')
    get('v2/api/order/card', {
      page: 1,
      limit: 200,
      id: this.data.orderDetailsId
    }, (res) => {
      console.log(res, 'getAvaCardList')
      res.data.list.forEach((item) => {
        item.isSelectCard = false
      })
      this.setData({
        cardList: res.data.list,
      })
    }, (error) => {
      console.log("请求失败", error)
    })
  },

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

  },

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

  },

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

  },

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

  },

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

  }
})