<template>
  <div v-show="loadFlag">
    <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"
                 :disabled="disabledFlag">
        {{btnText}}
      </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: require("../../assets/images/successIcon.png"),
      successiconUrl: require("../../assets/images/successIcon.png"),
      loseiconUrl: require("../../assets/images/loseIcon.png"),
      carNum: "",
      carNumberColor: "",
      parkingData: [],
      carType: "",
      codeType: "",
      tipText: "",
      payFlag: true,
      couponsType: 1,// 1:按次优惠,2:按时优惠,3:折扣优惠(打折),4：满减优惠'
      cardRuleName: "",
      loadFlag: false,  //
      disabledFlag: false,
      btnText: "去查询停车费用"
    };
  },
  created() {
    this.carNum = this.$route.query.carNumber;
    this.carType = this.$route.query.carType;
    this.codeType = this.$route.query.codeType;
    this.cardRelParkNo = this.$route.query.cardRelParkNo;
    // 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() {
      let url = process.env.VUE_APP_API;
      console.log(url);
      var salt = this.$utils.myCommonSalt(32);
      var jsondata = {
        app_id: this.$utils.myVarAppid,
        deviceInfo: this.$utils.myDeviceInfo,
        salt: salt,
        sign_type: "md5",
        carType: "2",
        codeType: this.codeType,
        cardRelParkNo: this.cardRelParkNo,
        key: this.$route.query.key,
        encryptTime: this.$route.query.encryptTime,
        carNum: this.carNum,
        // carNumberColor: this.carNumberColor,
        orgId: this.$utils.myOrgId,
        terminalSource: "7"
        // token:'e2c1e43722dd43a5b7e268001b39e6f0'
      };
      jsondata.sign = this.$utils.signObject(jsondata);
      console.log(jsondata);
      scanCouponQR(url, jsondata).then(response => {
        console.log(response);
        if (response.code == 0) {
          this.payFlag = true;
          this.imgurl = this.successiconUrl;
          this.tipText = "领取优惠券成功";
          this.cardRuleName = response.data.cardRuleName;
          this.couponsType = response.data.cardType;
          console.log(this.couponsType);
        } else {
          this.payFlag = false;
          this.imgurl = this.loseiconUrl;
          this.tipText = response.message;
        }
        setTimeout(() => {
          this.loadFlag = true;
        }, 300);
        // this.tipText =
      });
    },
    toPay() {
      this.parkRecordList();
      // this.$router.push({
      //   path: 'couponPay'
      // })
    },
    toBackPage() {
      this.$router.back();
    },
    parkRecordList() {  // 获取停车记录数据
      this.disabledFlag = true
      this.btnText = "查询中,请等待"
      let url = process.env.VUE_APP_PAYAPI;
      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: "7",
        // 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(url, jsondata).then(response => {
        console.log(response);
        this.disabledFlag = false
        this.btnText = "去查询停车费用"
        // 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) { // 缴纳本次在停费用
      let url = process.env.VUE_APP_PAYAPI;
      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: "7"
      };
      params.sign = this.$utils.signObject(params);
      appApplyParkOut(url, 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(url, 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, // 超时描述
                  couponsType: this.couponsType
                }
              }
            );
          });
        } else {
          //$('.dialog-out').show()
          this.$msgbox("提示", "成功领取" + this.cardRuleName + "，离场时自动抵扣");
        }
      });
    }
  }
};
</script>

<style scoped>

</style>
