<template>
  <div>
    <img :src="imgurl" alt="" style="margin: 10px auto;width: 64px;height: 64px;">
    <div style="padding: 10px 30px 50px">
      <p>尊敬的车主您好：{{tipText}}</p>
    </div>
    <div style="padding: 0 20px">
      <mt-button size="large" type="primary" @click="toPay" style="margin-bottom: 20px;" v-show="payFlag">去支付</mt-button>
      <mt-button size="large" type="danger" @click="toBackPage">返回</mt-button>
    </div>

  </div>
</template>

<script>


import { scanCouponQR, queryParkingRecordPageByCarNumbers, appApplyParkOut, parkingQuery } from '@/api/couponPay/couponPay.js'

export default {
  name: "coupon",
  data() {
    return {
      imgurl:'',
      successiconUrl: require('../../assets/images/successIcon.png'),
      loseiconUrl: require('../../assets/images/loseIcon.png'),
      carNum: '',
      carNumberColor: '',
      parkingData:[],
      carType:'',
      codeType:'',
      tipText:'',
      payFlag:false,
    }
  },
  created() {
    this.carNum = this.$route.query.carNumber
    this.carType = this.$route.query.carType
    this.codeType = this.$route.query.codeType

    // this.carNum = this.$route.query.carNumber  // 获取车牌号
    // this.carNumberColor = this.$route.query.carNumberColor  // 获取颜色   0:蓝牌;1:黄牌;2:白牌;3:黑牌;4:绿色
    // this.currentTabActive = this.$route.query.parkFlag ? this.$route.query.parkFlag : 0
    // console.log(this.currentTabActive)
    // console.log(this.carNumber)
    this.scanCouponQR()
  },
  methods: {
    scanCouponQR(){
      var salt = this.$utils.myCommonSalt(32);
      var jsondata = {
        app_id: this.$utils.myVarAppid,
        deviceInfo: this.$utils.myDeviceInfo,
        salt: salt,
        sign_type: "md5",
        carType:this.carType,
        codeType:this.codeType,
        cardRelParkNo:'123qwe',
        key:'1',
        carNum: this.carNum,
        // carNumberColor: this.carNumberColor,
        orgId: this.$utils.myOrgId,
        terminalSource:'7',
        // token:'e2c1e43722dd43a5b7e268001b39e6f0'
      }
      jsondata.sign = this.$utils.signObject(jsondata)
      scanCouponQR(jsondata).then(response => {
        console.log(response)
        this.tipText = response.data.message
        if(response.data.code==0){
          this.payFlag = true
          this.imgurl = this.successiconUrl
        }else{
          this.payFlag = false
          this.imgurl = this.loseiconUrl
        }
      })
    },
    toPay() {
      this.parkRecordList()
      // this.$router.push({
      //   path: 'couponPay'
      // })
    },
    toBackPage() {
      this.$router.back()
    },
    parkRecordList() {  // 获取停车记录数据
      var salt = this.$utils.myCommonSalt(32);
      var jsondata = {
        app_id: this.$utils.myVarAppid,
        deviceInfo: this.$utils.myDeviceInfo,
        salt: salt,
        sign_type: "md5",
        pageNum: '1',
        pageSize: '1000',
        parkState: '10',
        terminalSource: '3',
        // parkState:'10',
        carNumber: this.carNum,
        // carNumberColor: '1',
        orgId: this.$utils.myOrgId,

      }
      jsondata.sign = this.$utils.signObject(jsondata)

      // jsondata.sign = md5sign
      console.log('停车记录传参  ' + JSON.stringify(jsondata));
      queryParkingRecordPageByCarNumbers(jsondata).then(response => {
        console.log(response)
        // this.parkList = response.data.dataList

        this.parkingData = response.data.dataList.filter(item => {
          return item.parkState == '10'
        })
        console.log(this.parkingData)
        this.toPayCurrent(this.parkingData)

      })
    },
    toPayCurrent(i) { // 缴纳本次在停费用
      var salt = this.$utils.myCommonSalt(32);
      var params = {
        app_id: this.$utils.myVarAppid,
        deviceInfo: this.$utils.myDeviceInfo,
        salt: salt,
        sign_type: "md5",
        orderId: i[0].orderId,
        payOrderType: '101',
        terminalSource: '3',
      }
      params.sign = this.$utils.signObject(params)
      appApplyParkOut(params).then(response => {
        console.log(response)

        if (response.code == '5005') {
          var salt = this.$utils.myCommonSalt(32);
          var jsondata = {
            app_id: this.$utils.myVarAppid,
            deviceInfo: this.$utils.myDeviceInfo,
            salt: salt,
            sign_type: "md5",
            payType: this.$utils.clientBrowsePayType(),
            appId: this.$utils.myVxAppId,
            orderId: i[0].orderId,
            terminalSource: '7',
            parkCode: i[0].parkCode,
            carNumber: this.carNum,
          }
          jsondata.sign = this.$utils.signObject(jsondata)
          parkingQuery(jsondata).then(result => {
            console.log(result)
            let res = result.data
            let _dis = res.discountFee
            let discountFee = _dis.slice(1, _dis.length - 1)
            sessionStorage.setItem('couponPark',JSON.stringify(result.data))
              this.$router.push(
                {
                  path: 'couponPay',
                  query: {
                    carColor: this.carColor,// 车牌颜色
                    arrearageTotalFee: res.orderTotalFee,// 应收
                    arrearageDiscFee: discountFee * 100,// 优惠
                    arrearageActFee: res.orderFee,// 实收
                    carNumber: this.carNumber, // 车牌
                    paySrcType: 101, //支付的类型   101 是本次   103是历史欠费
                    ordeID: i.orderId, //支付的订单号
                    appOrderTimeout: res.appOrderTimeout, // 超时描述
                  }
                }
              )
          })

          } else {
            //$('.dialog-out').show()
            this.$msgbox('提示', '停车15分钟内无需缴费，申请成功。')
          }

      })

    },
  }
};
</script>

<style scoped>

</style>
