<template>
  <div class="charge-index">
    <div class="background">
      <div class="headline">
        <span class="text1">电子卡充值享优惠</span>
        <span class="text2">会员日充值更优惠</span>
      </div>
    </div>
    <div class="content">
      <div class="title">我的卡片 ({{ cardList.length }})</div>
      <div
        v-for="item in cardList"
        :key="item.id"
        :class="[
          item.cardOilsType == 1
            ? 'gasoline'
            : item.cardOilsType == 2
            ? 'diesel'
            : '',
        ]"
      >
        <div class="title">
          {{
            item.cardOilsType == 1
              ? "汽油卡"
              : item.cardOilsType == 2
              ? "柴油卡"
              : ""
          }}
        </div>
        <div class="text">
          余额 <span>{{ item.amt.toFixed(2) }}</span
          >元
        </div>
        <button @click="goCharge(item)">充值</button>
      </div>
    </div>

    <van-popup v-model="mask" closeable position="bottom" class="mask" round>
      <div class="charge">
        <div class="title">充值</div>
        <div class="tip">会员日充值更优惠，详情请咨询站长</div>
        <div class="amount">
          <span>{{
            chargeItem.cardOilsType == 1
              ? "汽油充值"
              : chargeItem.cardOilsType == 2
              ? "柴油充值"
              : ""
          }}</span>
          <input
            placeholder="输入充值金额"
            v-model="chargeAmount"
            type="number"
            step="0.01"
            min="0"
            onkeyup="this.value= this.value.match(/\d+(\.\d{0,2})?/) ? this.value.match(/\d+(\.\d{0,2})?/)[0] : ''"
          />
          <span>￥</span>
        </div>
        <div class="container">
          <div
            class="btn"
            v-for="item in discountList"
            :key="item.cardSettingDetailId"
            @click="selectDiscount(item.discountAmtEnd)"
          >
            <span>￥</span>
            <span>{{ item.discountAmtEnd }}</span>
            <span>满{{ item.discountAmtEnd }}送{{ item.presentAmt }}元</span>
          </div>
        </div>
      </div>
      <van-button
        :class="[chargeAmount !== '' ? 'selected' : '']"
        :loading="payLoading"
        loading-text="拉取支付中..."
        @click="charge"
        :disabled="chargeAmount === ''"
      >
        确认充值
      </van-button>
    </van-popup>
  </div>
</template>

<script>
import ProductList from "../../components/ProductList";
import { mapState, mapGetters, mapActions } from "vuex";
import wx from "weixin-js-sdk";
import Vue from "vue";
import { Toast, Button, Popup, NumberKeyboard } from "vant";

Vue.use(Popup);
Vue.use(Toast);
Vue.use(Button);
Vue.use(NumberKeyboard);

export default {
  head() {
    return {
      title: "会员充值",
    };
  },
  data() {
    return {
      mask: false,
      cardList: [],
      chargeItem: {},
      chargeAmount: "",
      stationInfo: {},
      payLoading: false,
      show: false,
      discountList: [],
    };
  },
  components: {
    ProductList,
  },
  computed: {
    ...mapState({
      pointInfo: (state) => state.point.pointInfo,
    }),
    ...mapGetters({
      userInfo: "authen/userInfo",
    }),
  },
  created() {
    this.init();
  },
  methods: {
    onInput(value) {
      Toast(value);
    },
    onDelete() {
      Toast("删除");
    },
    ...mapActions({
      getSdkConfig: "authen/getSdkConfig",
    }),
    switchMask() {
      this.mask = !this.mask;
    },
    getDiscount() {
      this.discountList = [];
      // 拉取折扣
      this.$axios
        .$get("/getCardRechargeSettingList", {
          params: {
            stationId: this.stationId,
            cardOilsType: this.chargeItem.cardOilsType,
          },
        })
        .then((res) => {
          if (res.retCode === 0) {
            this.discountList = res.data[0].customerCardSettingDetailList;
          }
        });
    },
    goCharge(ele) {
      this.chargeItem = ele;
      this.getDiscount();
      this.switchMask();
    },
    charge() {
      this.chargeAmount = this.chargeAmount.toString().replace(/\.$/g, "");
      this.pay();
    },
    init() {
      // 获取卡片
      this.$axios
        .$get("/getElectronicCardList", {
          params: {
            unionId: this.unionId,
            stationId: this.stationId,
          },
        })
        .then((res) => {
          if (res.retCode === 0) {
            this.cardList = res.data;
          }
        });
      // 这一块纯为了后端方便而已，获取station的信息
      this.$axios
        .$get("/getStationInfo", {
          params: {
            stationId: this.stationId,
          },
        })
        .then((res) => {
          this.stationInfo = res.data;
        });
    },
    async pay() {
      try {
        this.payLoading = true;
        const orderData = await this.$axios.$post("/rechargeBalance", {
          amt: this.chargeAmount,
          stationId: this.stationId,
          customerName: this.userInfo.nickname,
          cardOilsType: this.chargeItem.cardOilsType,
          payType: "1",
          unionId: this.unionId,
          stationName: this.stationInfo.stationName,
        });
        if (orderData.retCode !== 0) {
          throw new Error("生成订单失败");
        }
        const orderNum = orderData.data;
        const subject =
          this.stationInfo.stationName +
          "_" +
          (this.chargeItem.cardOilsType == 1
            ? "汽油电子卡充值"
            : this.chargeItem.cardOilsType == 2
            ? "柴油电子卡充值"
            : "");
        const payInfoData = await this.$axios.$post("/rechargeBalanceSXFPay", {
          amt: this.chargeAmount,
          openId: this.openId,
          stationId: this.stationId,
          userType: "1",
          subject,
          orderNo: orderNum,
        });
        if (payInfoData.retCode !== 0) {
          throw new Error("支付参数拉取失败");
        }
        if (payInfoData.data.code !== "0000") {
          throw new Error(payInfoData.data.msg);
        }
        if (payInfoData.data.code !== "0000") {
          throw new Error(payInfoData.data.msg);
        }
        if (payInfoData.data.respData.bizCode !== "0000") {
          throw new Error(payInfoData.data.respData.bizMsg);
        }
        const timestamp = payInfoData.data.respData.payTimeStamp;
        const nonceStr = payInfoData.data.respData.paynonceStr;
        const packageOrg = payInfoData.data.respData.payPackage;
        const signType = payInfoData.data.respData.paySignType;
        const paySign = payInfoData.data.respData.paySign;

        const that = this;
        // 拉取微信支付
        this.getSdkConfig(["chooseWXPay"]).then((res) => {
          wx.config(res);
          wx.ready(function (res) {
            wx.chooseWXPay({
              timestamp, // 支付签名时间戳，注意微信jssdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符
              nonceStr, // 支付签名随机串，不长于 32 位
              package: packageOrg, // 统一支付接口返回的prepay_id参数值，提交格式如：prepay_id=\*\*\*）
              signType, // 签名方式，默认为'SHA1'，使用新版支付需传入'MD5'
              paySign, // 支付签名
              success: function (res) {
                // that.$router.push("/personal/charge");
                that.payLoading = false;
                alert("支付完成");
              },
              cancel: function (err) {
                that.payLoading = false;
                alert("您取消了付款");
              },
              fail: function (err) {
                that.payLoading = false;
                alert(err);
              },
            });
          });
          wx.error(function (res) {
            alert("拉取微信Sdk配置出错");
          });
        });
      } catch (err) {
        this.payLoading = false;
        alert(err);
      }
    },
    selectDiscount(discount) {
      this.chargeAmount = discount;
    },
  },
};
</script>

<style>
.charge-index {
  position: relative;
  height: 100vh;
}
.charge-index .background {
  background-color: #f3b335;
  width: 7.5rem;
  height: 7.5rem;
  position: relative;
  background: url("~static/personal/23@2x.png") no-repeat 0px 0px;
  background-size: 100% 100%;
  z-index: -1;
  overflow: hidden;
}

.charge-index .background .headline {
  width: 6.9rem;
  height: 4.33rem;
  background: url("~static/personal/bj2@2x.png") no-repeat 0px 0px;
  background-size: 100% 100%;
  margin: 0.3rem auto;
  display: relative;
}
.charge-index .background .headline .text1 {
  position: absolute;
  left: 0.8rem;
  top: 1.6rem;
  color: #8e9aae;
  font-size: 0.3rem;
  font-weight: 600;
}
.charge-index .background .headline .text2 {
  position: absolute;
  left: 0.8rem;
  top: 2.2rem;
  color: #8e9aae;
  font-size: 0.3rem;
  font-weight: 600;
}
.charge-index .content {
  position: absolute;
  top: 3.78rem;
  bottom: 0rem;
  left: 0rem;
  right: 0rem;
  background-color: #ffffff;
  border-radius: 0.3rem 0.3rem 0 0;
  padding: 0.3rem;
  display: flex;
  flex-direction: column;
}
.charge-index .content > div:not(.title) {
  width: 6.9rem;
  height: 1.6rem;
  margin-top: 0.3rem;
  position: relative;
}
.charge-index .content .title {
  font-size: 0.35rem;
  color: #333333;
  font-weight: 600;
}
.charge-index .content .gasoline {
  background: url("~static/personal/bj3@2x.png") no-repeat 0px 0px;
  background-size: 100% 100%;
}
.charge-index .content .diesel {
  background: url("~static/personal/bj4@2x.png") no-repeat 0px 0px;
  background-size: 100% 100%;
}
.charge-index .content div .title {
  height: 0.45rem;
  font-size: 0.32rem;
  font-family: PingFangSC-Medium, PingFang SC;
  font-weight: 500;
  color: #ffffff;
  line-height: 0.45rem;
  position: absolute;
  top: 0.27rem;
  left: 1.67rem;
}
.charge-index .content .gasoline .text {
  height: 0.45rem;
  font-size: 0.32rem;
  font-family: PingFangSC-Medium, PingFang SC;
  font-weight: 500;
  color: #3db58f;
  line-height: 0.45rem;
  position: absolute;
  top: 0.9rem;
  left: 1.67rem;
}
.charge-index .content .diesel .text {
  height: 0.45rem;
  font-size: 0.32rem;
  font-family: PingFangSC-Medium, PingFang SC;
  font-weight: 500;
  color: #ea8c7d;
  line-height: 0.45rem;
  position: absolute;
  top: 0.9rem;
  left: 1.67rem;
}

.charge-index .content div .text span {
  color: #fff;
}
.charge-index .content div button {
  border: none;
  background-color: transparent;
  outline: none;
  display: block;
  width: 1.4rem;
  height: 0.6rem;
  background: #ffffff;
  border-radius: 0.3rem;
  position: absolute;
  top: 0.5rem;
  right: 0.3rem;
}
.charge-index .content .gasoline button {
  color: #24ac81;
}

.charge-index .content .diesel button {
  color: #e87d6d;
}

.charge-index .mask {
}

.charge-index .mask .charge {
  height: 7.5rem;
  background: #f2f2f2;
  border-radius: 0.42rem 0.42rem 0rem 0rem;
  box-sizing: border-box;
  padding: 0.3rem 0;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.charge-index .mask .charge > div:not(:nth-child(1)):not(.container) {
  margin-top: 0.2rem;
}
.charge-index .mask .charge .title {
  width: 6.5rem;
  height: 0.45rem;
  font-size: 0.32rem;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  color: #111111;
  line-height: 0.45rem;
  text-align: center;
}
.charge-index .mask .charge .title .close {
  background: url("~static/personal/6_d05_close@2x.png") no-repeat 0px 0px;
  background-size: 100% 100%;
  width: 0.4rem;
  height: 0.4rem;
  float: right;
}
.charge-index .mask .charge .tip {
  height: 0.33rem;
  font-size: 0.24rem;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  color: #aaaaaa;
  line-height: 0.33rem;
}
.charge-index .mask .charge .amount {
  width: 6.9rem;
  height: 1.2rem;
  background: url("~static/personal/biankuang@2x.png") no-repeat 0px 0px;
  background-size: 100% 100%;
  position: relative;
}
.charge-index .mask .charge .amount span:nth-child(1) {
  height: 0.4rem;
  font-size: 0.28rem;
  font-weight: 600;
  color: #884e16;
  line-height: 0.4rem;
  position: absolute;
  top: 0.4rem;
  left: 0.51rem;
}
.charge-index .mask .charge .amount input {
  background: none;
  outline: none;
  border: none;
  height: 0.4rem;
  font-size: 0.28rem;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  color: #111111;
  line-height: 0.4rem;
  position: absolute;
  top: 0.4rem;
  left: 2.54rem;
}
.charge-index .mask .charge .amount span:nth-last-child(1) {
  height: 0.4rem;
  font-size: 0.28rem;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  color: #aaaaaa;
  line-height: 0.4rem;
  position: absolute;
  top: 0.4rem;
  right: 0.4rem;
}

.charge-index .mask button {
  border: none;
  outline: none;
  bottom: 0.7rem;
  position: absolute;
  width: 6.9rem;
  height: 0.8rem;
  left: 0.3rem;
  background: #cccccc;
  border-radius: 0.45rem;
  font-size: 0.32rem;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  color: #ffffff;
  line-height: 0.45rem;
}
.charge-index .mask > button.selected {
  background-color: #0ea374;
}

.charge-index .mask .container {
  margin-top: 0.4rem;
  display: flex;
  flex-wrap: wrap;
  padding: 0.3rem 0.3rem;
  justify-content: space-between;
  overflow: scroll;
  height: 2rem;
}
.charge-index .mask .container .btn {
  display: inline-block;
  width: 3.3rem;
  height: 0.84rem;
  background: #ffffff;
  box-shadow: 0 0.04rem 0.08rem 0 #f2f2f2;
  border-radius: 0.42rem;
  margin-bottom: 0.35rem;
  position: relative;
  box-sizing: border-box;
  padding: 0 0.26rem;
  text-align: center;
}
.charge-index .mask .container .btn:nth-child(odd) {
  margin-right: 0.3rem;
}
.charge-index .mask .container .btn span {
  font-size: 0.35rem;
  font-family: PingFangSC-Regular, PingFang SC;
  color: #895017;
  line-height: 0.84rem;
}

.charge-index .mask .container .btn span:nth-child(1) {
  border-radius: 0.42rem;
  font-size: 0.2rem;
  float: left;
}
.charge-index .mask .container .btn span:nth-child(2) {
  float: left;
  font-weight: 600;
}
.charge-index .mask .container .btn span:nth-child(3) {
  font-size: 0.2rem;
  font-weight: 400;
  color: rgba(0, 0, 0, 0.29);
  float: right;
}
</style>