<template>
  <div class="body">
    <div class="content">
      <div class="head">
        <span class="back-button" @click="back">
          <i class="iconfont iconyidong"></i>返回
        </span>
        <span class="page-title">购买燃料套餐</span>
      </div>
      <ul class="package-list" v-if="showThePackageList">
        <li v-for="(item, index) in thePackageList" :key="index">
          <div
            class="package-name"
            :class="{
              'package-name--A': item.level === 1,
              'package-name--B': item.level === 2,
              'package-name--C': item.level === 3,
              'package-name--D': item.level === 4,
              'package-name--E': item.level === 5,
            }"
          >
            {{ item.name }}
          </div>
          <div class="VIP-level">
            <div class="vip-box">
              <div
                class="title-box"
                :class="{
                  'title-box--A': item.level === 1,
                  'title-box--B': item.level === 2,
                  'title-box--C': item.level === 3,
                  'title-box--D': item.level === 4,
                  'title-box--E': item.level === 5,
                }"
              >
                VIP{{ item.level }}
                <div
                  class="icon-box"
                  :class="{
                    'icon-box--A': item.level === 1,
                    'icon-box--B': item.level === 2,
                    'icon-box--C': item.level === 3,
                    'icon-box--D': item.level === 4,
                    'icon-box--E': item.level === 5,
                  }"
                >
                  <i class="iconfont iconshoucang"></i>
                </div>
              </div>
            </div>
          </div>
          <p class="number number--A">
            {{ item.chain_times }}
            <span>次</span>
          </p>
          <P class="price">
            价格：
            <span>{{ item.price }}</span
            >元人民币
          </P>
          <p class="average-price">上链均价：{{ item.chain_price }}</p>
          <div class="buy">
            <div class="buy-button" @click="buyNow(item)">立即购买</div>
          </div>
        </li>
      </ul>
      <div v-else class="order-payment">
        <p class="the-order-no">订单编号：{{ orderId }}</p>
        <p class="prompt-order">您的订单已提交成功，现在只差最后一步啦！</p>
        <p class="amount-payable">
          应付金额：
          <span>￥{{ price }}.00</span> 元
        </p>
        <div class="method-payment">
          <div class="WeChat" @click="WeChatPay">
            <div class="imgbox">
              <img v-if="payUrl" :src="payUrl" />
              <img v-else src="../../assets/images/userCenter/bg.jpg" />
            </div>
          </div>
          <div class="Alipay" @click="PaymentOrder"></div>
        </div>
        <p class="warm-prompt">
          温馨提示：所有套餐都是年费套餐，超过一年剩余的套餐将会被清零，详情请
          <span class="contact-customer-service">联系客服</span>
          <span class="customer-service-WeChat">
            <p class="title">客服微信</p>
            <span class="imgbox">
              <img src="../../assets/images/userCenter/qrcode.png" alt />
            </span>
            <p class="tip">扫一扫添加客服微信号</p>
          </span>
        </p>
      </div>
    </div>
    <common-dialog showMask v-if="PromptShow" @closePopup="PromptShow = false">
      <prompt
        tip="您的账户还很充足奥~请剩余少量余额时在进行充值"
        @close="PromptShow = false"
      />
    </common-dialog>
  </div>
</template>

<script>
import { packageList, pay, orderType } from "./Api.service";
import { payQR } from "@/utils/app/common";
import { GO_URLS } from "@/config/URLS";
import { getLoginCode } from "@/plugins/storage";
import CommonDialog from "@/components/CommonDialog.vue";
import prompt from "@/components/userCenter/prompt.vue";
export default {
  data() {
    return {
      thePackageList: [],
      showThePackageList: true,
      price: 0,
      orderId: 0,
      payUrl: "",
      time: 18,
      isclick: true,
      PromptShow: false,
    };
  },
  components: {
    CommonDialog,
    prompt,
  },
  created() {
    this.getlist();
    if (this.$route.query.charset) {
      this.back();
    }
  },
  methods: {
    async getOrder() {
      const res = await orderType(this.orderId);
      if (res) {
        if (res.data.status === 2) {
          clearInterval(this.intervalHandle);
          this.$message({
            message: "恭喜您支付成功",
            type: "success",
          });
          this.$router.push({
            path: "/userCenter",
          });
        }
      }
    },
    // 倒计时
    cycle() {
      this.intervalHandle = setInterval(() => {
        if (this.time > 0) {
          this.time--;
          this.getOrder();
        } else {
          clearInterval(this.intervalHandle);
          this.$router.push({
            path: "/userCenter",
          });
        }
      }, 10000);
    },
    // 微信支付
    async WeChatPay() {
      const res = await pay({
        device: 0,
        id: this.orderId,
        method: 1,
        return_url: window.location.href,
      });
      if (res) {
        this.cycle();
        this.payUrl = await payQR(res.data.pay_url);
      }
    },
    // 支付宝支付
    async PaymentOrder() {
      if (!this.isclick) return;
      this.isclick = false;
      const res = await pay({
        device: 0,
        id: this.orderId,
        method: 0,
        return_url: window.location.href,
      });
      if (res) {
        this.isclick = true;
        window.open(res.data.pay_url);
      }
    },
    async buyNow(item) {
      this.price = item.price;
      this.addOrder(item.id);
    },
    async addOrder(id) {
      var xhr = new XMLHttpRequest();
      var url = GO_URLS.addOrder;
      let data = {
        plan_id: id,
      };
      let token = "Bearer " + getLoginCode();
      xhr.open("post", url, true);
      xhr.setRequestHeader("Content-type", "application/json;charset=UTF-8");
      xhr.setRequestHeader("Authorization", token);
      xhr.onload = this.uploadComplete; //请求完成、
      xhr.onerror = this.uploadFailed; //请求失败
      xhr.send(JSON.stringify(data));
    },
    uploadComplete(xhr) {
      //返回的结果
      const res = JSON.parse(xhr.target.response);
      if (res.code === 10603) {
        this.PromptShow = true;
      } else if (res.code === 200) {
        this.orderId = res.data.id;
        this.showThePackageList = false;
      } else if (res.code == 403) {
        this.$message({
          message: "登录过期,请刷新页面",
          type: "warning",
        });
        sessionStorage.removeItem("user");
        router.push({ path: "/" });
      }
    },
    uploadFailed(xhr) {
      this.$message({
        message: "网络异常",
        type: "error",
      });
    },
    back() {
      this.$router.push({ path: "/userCenter" });
    },
    async getlist() {
      const res = await packageList();
      if (res) {
        this.thePackageList = res.data.results;
      }
    },
  },
};
</script>

<style lang="less" scoped>
.body {
  min-width: 1000px;
  padding: 16px 21px 56px 19px;
  background: rgba(248, 250, 251, 1);
}
.content {
  background: white;
  padding-left: 28px;
}
.head {
  padding-top: 20px;
  padding-bottom: 36px;
}
.back-button {
  font-size: 16px;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  cursor: pointer;
  color: rgba(63, 121, 254, 1);
  i {
    font-size: 14px;
  }
}
.page-title {
  font-size: 16px;
  font-family: PingFangSC-Medium, PingFang SC;
  font-weight: 500;
  color: rgba(53, 53, 53, 1);
  padding-left: 14px;
}
.package-list {
  overflow: hidden;
  margin: 0;
  padding: 0;
  li {
    list-style: none;
    width: 294px;
    height: 420px;
    float: left;
    margin-right: 20px;
    margin-bottom: 20px;
    background: rgba(255, 255, 255, 1);
    box-shadow: 0px 2px 10px 0px rgba(0, 0, 0, 0.2);
    border: 1px solid rgba(248, 250, 251, 1);
    border-radius: 3px;
    &:hover {
      border: 1px solid rgba(63, 121, 254, 1);
    }
  }
}
.package-name {
  font-size: 22px;
  font-family: PingFangSC-Medium, PingFang SC;
  font-weight: 500;
  color: rgba(255, 255, 255, 1);
  line-height: 64px;
  text-align: center;
  height: 64px;
}
.package-name--A {
  background: linear-gradient(
    270deg,
    rgba(52, 172, 255, 1) 0%,
    rgba(63, 128, 254, 1) 100%
  );
}
.package-name--B {
  background: linear-gradient(
    270deg,
    rgba(95, 190, 255, 1) 0%,
    rgba(0, 178, 254, 1) 100%
  );
}
.package-name--C {
  background: linear-gradient(
    270deg,
    rgba(52, 226, 255, 1) 0%,
    rgba(63, 190, 254, 1) 100%
  );
}
.package-name--D {
  background: linear-gradient(
    270deg,
    rgba(52, 226, 255, 1) 0%,
    rgba(63, 190, 254, 1) 100%
  );
}
.package-name--E {
  background: linear-gradient(
    270deg,
    rgba(52, 226, 255, 1) 0%,
    rgba(63, 190, 254, 1) 90%
  );
}
.VIP-level {
  margin-top: 54px;
  margin-bottom: 12px;
  .vip-box {
    margin: 0 auto;
    width: 40px;
  }
}
.title-box {
  width: 56px;
  height: 24px;
  border-radius: 12px;
  font-size: 12px;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  line-height: 24px;
  position: relative;
  text-indent: 22px;
}
.title-box--A {
  color: rgba(53, 53, 53, 1);
  background: rgba(193, 231, 245, 1);
}
.title-box--B {
  color: white;
  background: rgba(239, 200, 153, 1);
}
.title-box--C {
  color: white;
  background: rgba(255, 160, 101, 1);
}
.title-box--D {
  color: white;
  background: rgba(246, 210, 38, 1);
}
.title-box--E {
  color: white;
  background: rgba(246, 200, 38, 1);
}
.icon-box {
  width: 30px;
  position: absolute;
  height: 30px;
  border-radius: 15px;
  text-align: center;
  top: -3px;
  left: -11px;
  line-height: 30px;
  text-indent: initial;
  i {
    color: white;
  }
}
.icon-box--A {
  background: rgba(129, 188, 227, 1);
}
.icon-box--B {
  background: rgba(195, 141, 83, 1);
}
.icon-box--C {
  background: rgba(255, 127, 39, 1);
}
.icon-box--D {
  background: rgba(250, 203, 48, 1);
}
.icon-box--E {
  background: rgba(246, 200, 38, 1);
}
.number {
  text-align: center;
  margin: 0;
  height: 42px;
  font-size: 30px;
  font-family: PingFangSC-Medium, PingFang SC;
  font-weight: 500;

  line-height: 42px;
  span {
    color: #353535;
    font-size: 14px;
  }
}
.number--A {
  color: rgba(193, 210, 246, 1);
}
.number--B {
  color: #c37e3d;
}
.number--C {
  color: #ff7700;
}
.number--D {
  color: #ffd200;
}
.price {
  height: 22px;
  font-size: 16px;
  font-family: PingFangSC-Medium, PingFang SC;
  font-weight: 500;
  color: rgba(53, 53, 53, 1);
  text-align: center;
  line-height: 22px;
  margin: 0;
  margin-top: 32px;
  margin-bottom: 10px;
  span {
    color: #3f79fe;
    font-size: 16px;
  }
}
.average-price {
  text-align: center;
  margin: 0;
  height: 20px;
  font-size: 14px;
  font-family: PingFangSC-Medium, PingFang SC;
  font-weight: 500;
  color: rgba(129, 129, 129, 1);
  line-height: 20px;
}
.buy {
  margin-top: 46px;
  .buy-button {
    width: 120px;
    height: 40px;
    margin: 0 auto;
    background: rgba(63, 121, 254, 1);
    border-radius: 4px;
    text-align: center;
    line-height: 40px;
    color: white;
    cursor: pointer;
    font-size: 14px;
    font-family: PingFangSC-Regular, PingFang SC;
    font-weight: 400;
    color: rgba(255, 255, 255, 1);
    transition: 0.3s;
    &:hover {
      transform: scale(1.1);
    }
  }
}

.the-order-no {
  margin: 0;
  font-size: 14px;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  color: rgba(53, 53, 53, 1);
  line-height: 20px;
}
.prompt-order {
  margin: 0;
  margin-top: 22px;
  font-size: 22px;
  font-family: PingFangSC-Medium, PingFang SC;
  font-weight: 500;
  color: rgba(53, 53, 53, 1);
  line-height: 30px;
}
.amount-payable {
  margin: 0;
  margin-top: 82px;
  font-size: 14px;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  color: rgba(53, 53, 53, 1);
  line-height: 20px;
  span {
    color: #ff3838;
    font-size: 22px;
  }
}
.method-payment {
  overflow: hidden;
  margin-top: 44px;
}
.WeChat {
  float: left;
  width: 155px;
  height: 207px;
  margin-right: 122px;
  background: url("../../assets/images/userCenter/WeChat.png") center no-repeat;
  cursor: pointer;
  background-size: 100%;
  .imgbox {
    width: 123px;
    margin: 0 auto;
    height: 123px;
    margin-top: 17px;
    img {
      width: 100%;
    }
  }
}
.Alipay {
  float: left;
  height: 197px;
  width: 151px;
  background: url("../../assets/images/userCenter/Alipay.png") center no-repeat;
  background-size: 100%;
  cursor: pointer;
}
.warm-prompt {
  font-size: 14px;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  color: rgba(129, 129, 129, 1);
  margin: 0;
  margin-top: 79px;
  position: relative;
}
.contact-customer-service {
  color: #3f79fe;
  cursor: pointer;
  &:hover + .customer-service-WeChat {
    display: block;
  }
}
.customer-service-WeChat {
  position: absolute;
  width: 153px;
  height: 194px;
  display: none;
  background: rgba(255, 255, 255, 1);
  box-shadow: 0px 2px 20px 0px rgba(0, 0, 0, 0.13);
  top: -97px;
  left: 590px;
  .title {
    text-align: center;
    font-size: 16px;
    font-family: PingFangSC-Regular, PingFang SC;
    font-weight: 400;
    color: rgba(53, 53, 53, 1);
    margin: 0;
    margin-top: 17px;
    margin-bottom: 7px;
  }
  .imgbox {
    width: 103px;
    height: 103px;
    display: block;
    margin: 0 auto;
    img {
      width: 100%;
    }
  }
  .tip {
    margin: 0;
    font-size: 12px;
    font-family: PingFangSC-Regular, PingFang SC;
    font-weight: 400;
    color: rgba(129, 129, 129, 1);
    text-align: center;
    margin-top: 7px;
  }
}
</style>>

