<template>
  <!-- 支付二维码弹出层 -->
  <el-dialog @close="closeCode" width="1000px" class="pay-dialog" :visible.sync="innerVisible" destroy-on-close append-to-body>
    <img @click="closeCode" class="close" src="../../static/image/icon_close.png" alt="">
    <div class="pay-modal">
      <div class="pay-top">{{ paymentType == 1 ? '支付宝' : '微信' }}支付</div>
    </div>
    <div class="code-content">
      <div class="code-top">
        <div class="code-left">
          <div class="l-title">支付金额</div>
        </div>
        <div class="code-right">￥{{ payAmount }}</div>
      </div>
      <div class="code-bottom">
        <div id="qrcode" ref="qrcode" class="code-img"></div>
        <div class="des">请使用手机打开{{ paymentType == 1 ? '支付宝' : '微信' }}扫描二维码完成支付</div>
        <div class="guo-qi">
          <span @click.stop="refreshCode()">点击刷新</span>
          重新获取二维码
        </div>
      </div>
    </div>
  </el-dialog>
</template>

<script>
import QRCode from "qrcodejs2";
import { getPayResult } from "../api/amount.js";
export default {
  data() {
    return {
      innerVisible: true,
      timer: null,
    };
  },
  props: {
    paymentType: Number,
    link: String,
    payAmount: Number,
    orderSn: String,
  },
  watch: {
    link() {
      this.crateQrcode();
    },
  },
  mounted() {
    this.$nextTick(() => {
      this.crateQrcode();
    });
  },
  methods: {
    //  生成二维码
    crateQrcode() {
      console.log("link", this.link);
      this.$refs.qrcode.innerHTML = "";
      let _this = this;
      let qrcode = new QRCode("qrcode", {
        width: 260,
        height: 260, // 高度
        text: _this.link, // 二维码内容
      });
      this.timer = setInterval(() => {
        this.getPayResult();
      }, 4000);
    },
    showCode() {
      this.payModal = true;
    },
    // 关闭弹框,清除已经生成的二维码
    closeCode() {
      clearInterval(this.timer);
      this.timer = null;
      this.$emit("closeCode");
    },
    refreshCode() {
      clearInterval(this.timer);
      this.timer = null;
      this.$emit("refreshCode");
    },
    getPayResult() {
      let params = {
        orderSn: this.orderSn,
        payType: this.paymentType,
      };
      getPayResult(params).then((res) => {
        if (res.code === 200) {
          let data = res.data;
          if (data.status === 0) {
            this.closeCode();
            this.$message({
              type: data.status ? "sucess" : "error",
              message: data.msg,
            });
          }
          if (data.status === 1) {
            clearInterval(this.timer);
            this.timer = null;
            this.$emit("paySuccess");
          }
        } else {
          this.$message({
            type: "error",
            message: res.msg,
          });
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.pay-dialog {
  /deep/ .el-dialog__header {
    padding: 0;
  }

  /deep/ .el-dialog__body {
    padding: 0px !important;
  }
}
.close {
  position: absolute;
  top: 10px;
  right: 10px;
  width: 40px;
  height: 40px;
  cursor: pointer;
}
.pay-modal {
  // border-radius: 16px;
  overflow: hidden;

  .pay-top {
    height: 80px;
    line-height: 80px;
    text-align: center;
    font-size: 22px;
    font-family: PingFang SC;
    font-weight: bold;
    color: #ffffff;
    background: url(../../static/image/pay-bg.png) no-repeat;
    background-size: 100% 100%;
  }
}

.code-content {
  width: 640px;
  margin: 40px auto 0;
  padding-bottom: 100px;
  box-sizing: border-box;

  .code-top {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 14px 20px;

    .code-left {
      .l-title {
        font-size: 14px;
        font-family: PingFang SC;
        font-weight: bold;
        color: #0d1e40;
      }

      .l-price {
        font-size: 14px;
        font-family: PingFang SC;
        font-weight: 400;
        color: #9ea7b7;
      }
    }

    .code-right {
      font-size: 24px;
      font-family: PingFang SC;
      font-weight: 600;
      color: #f74141;
    }
  }

  .code-bottom {
    text-align: center;
    padding-top: 30px;

    .code-img {
      width: 260px;
      height: 260px;
      margin: 0 auto;
    }

    .des {
      margin: 10px 0;
      font-size: 13px;
      color: #fc7200;
      font-weight: bold;
      text-align: center;
    }

    .guo-qi {
      font-size: 14px;
      font-family: PingFang SC;
      font-weight: 400;
      color: #9ea7b7;

      span {
        color: #175199;
        cursor: pointer;
      }
    }
  }
}
</style>