<template>
  <div class="paying">
    <div>
      <span class="pay_titile">报名信息</span>
      <div class="pay_top">
        <div>营地名称：{{ camp_name }}</div>
        <div>真实姓名：{{ personalInfo.name }}</div>
        <div>身份证号：{{ personalInfo.id_card }}</div>
        <!-- <div>缴费时间：{{ personalInfo.create_time }}</div> -->
        <div>报名费用： {{ has_amount }}元</div>
      </div>
      <div class="line"></div>
      <div class="pay-money flex">
        <div>
          报名费用 ：<span>￥{{ has_amount }} </span>
        </div>
        <div class="btn">
          <el-button @click="paying()">立即支付 </el-button>
        </div>
      </div>

      <div class="pay_bottom">
        <div>注意事项：</div>
        <div>
          1、学生支付完成后请不要立即关闭浏览器，等待支付完成返回本页面，以确认支付完成。
        </div>
        <div>
          2、支付确认后，将不能再修改姓名和身份证号，请在支付前核对信息。
        </div>
        <div>
          3、如果在完成支付后，状态许久未更新为“报名成功”，请仔细核查自己支付账号是否已扣费。
        </div>
        <div>
          4、对于多次缴费的考生，除生效一笔之外其他重复支付的得用会在缴费结束阶段统一退款。
        </div>
        <!-- <div>5、网上缴费如有问题，请致电项目组工作人员：XXX-XXXXXXXXX。</div> -->
      </div>
    </div>
    <!-- 支付扫码框 -->
    <!-- <el-dialog title="微信支付" :visible.sync="dialogVisible" width="20%">
      <div class="pay_code">
        <div class="pay_time">支付剩余时间<span>15分30秒</span></div>
        <div>￥150</div>
        <div class="pay_img">
         <div class="qrcode" ref="qrCodeUrl"></div>
        </div>
        <div>请使用<span>微信</span>扫一扫，扫描二维码支付</div>
        <div>手机完成支付后，请等待系统处理，在此之前请勿关闭窗口</div>
      </div>
      <span slot="footer" class="dialog-footer">
        <div class="btn">
          <el-button @click="dialogVisible = false">刷新二维码</el-button>
        </div>
      </span>
    </el-dialog> -->
    <!-- 微信支付宝2选1 -->
    <el-dialog
      title="支付方式"
      :visible.sync="dialogChoose"
      width="600px"
      :before-close="handleClose"
    >
      <div class="choose">
        <div class="flex img_choose">
          <img
            src="../../../assets/img/paying/weichat.png"
            alt=""
            @click="toWeichat"
          />
          <img
            src="../../../assets/img/paying/paybao.png"
            alt=""
            @click="toPaybao"
          />
        </div>
        <div class="text_choose">请选择支付方式</div>
      </div>
    </el-dialog>
    <!-- 微信支付 -->
    <confirm-sign-up
      :confirmSignUpDialogFu="confirmSignUpDialogFu"
      @closeCFSUDialog="closeCFSUDialog"
    ></confirm-sign-up>
  </div>
</template>

<script>
/* eslint-disable */
import ConfirmSignUp from "c/index/SignUp/ConfirmSignUp";
import { getAccount } from "r/index/signUp";
import { signUpInit } from "r/index/signUp";
import { getWxConfig } from "r/index/pay";

export default {
  name: "Paying",
  components: {
    ConfirmSignUp,
  },
  data() {
    return {
      dialogChoose: false, //付款方式选择
      confirmSignUpDialogFu: false, // 控制确认报名弹框的显示与隐藏
      personalInfo: {},
      camp_name: "", //营地名称
      has_amount: "", //支付金额
      status: "", //状态
      status_wx: "", //状态
      type: "", //状态
    };
  },
  created() {
    const code = this.$store.state.indexIdentity;
    this.camp_name = window.localStorage.getItem("camp_name" + code);
    this.has_amount = window.localStorage.getItem("has_amount" + code);
    this.getAccount();
  },
  methods: {
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then((_) => {
          done();
        })
        .catch((_) => {});
    },
    // 子组件触发，关闭确认报名弹框
    closeCFSUDialog(val, type) {
      console.log(val, "val");
      this.confirmSignUpDialogFu = val;
    },
    toWeichat() {
      // this.dialogChoose = false;
      this.confirmSignUpDialogFu = true;
    },
    toPaybao() {
      getWxConfig({
        pay_type: 2,
      }).then((res) => {
        console.log(res, "getWxConfig");
        if (res.data.code != 200) {
          return this.$message.error(res.data.message);
        }
        /* 此处form就是后台返回接收到的数据 */
        let form = res.data.data;
        console.log(form);
        // return
        let div = document.createElement("div");
        div.innerHTML = form;
        document.body.appendChild(div);
        document.forms[0].acceptCharset = "GBK"; //保持与支付宝默认编码格式一致，如果不一致将会出现：调试错误，请回到请求来源地.
        // 重新发起请求，错误代码 invalid-signature 错误原因: 验签出错，建议检查签名字符串或签名私钥与应用公钥是否匹配
        document.forms[0].submit(); //
        let alipay_submit = document.getElementById("alipay_submit");
        div.removeChild(alipay_submit);
        //初始化 websocket 链接
        this.initWebSocket();
      });
    },
    // 初始化 webSocket
    initWebSocket() {
      if (typeof WebSocket == "undefined") {
        this.$message({
          showClose: true,
          message: "您的浏览器不支持WebSocket",
          type: "error",
        });
      } else if (!this.dialogChoose) {
        return;
      } else {
        let code = this.$store.state.indexIdentity;
        if (!this.token || this.token == "") {
          this.token = window.localStorage.getItem("index-token" + code);
        }
        let token = this.token;
        var wssURL = SERVER_WS_URL + "/order?token=" + token + "&channel=pc";
        this.websocket = new WebSocket(wssURL);
        console.log(this.websocket);
        // 连接发生错误的回调方法
        this.websocket.onerror = this.websocketOnerror;
        // 连接成功建立的回调方法
        this.websocket.onopen = this.websocketOnopen;
        // 接收到消息的回调方法
        this.websocket.onmessage = this.websocketOnmessage;
        // 连接关闭的回调方法
        this.websocket.onclose = this.websocketOnclose;
        // 监听窗口关闭事件，当窗口关闭时，主动去关闭websocket连接，防止连接还没断开就关闭窗口，server端会抛异常。
        this.websocket.onbeforeunload = this.websocketOnbeforeunload;
        // this.websocketSend();
        // setTimeout(() => {
        //   this.websocketSend();
        // }, 1000);
      }
    },
    reconnect() {
      //重新连接
      let that = this;
      if (!that.dialogChoose) {
        return;
      }
      if (that.lockReconnect) {
        return;
      }
      that.lockReconnect = true;
      //没连接上会一直重连，设置延迟避免请求过多
      that.timeoutnum && clearTimeout(that.timeoutnum);
      that.timeoutnum = setTimeout(function () {
        //新连接
        that.initWebSocket();
        that.lockReconnect = false;
      }, 5000);
    },
    reset() {
      //重置心跳
      let that = this;
      //清除时间
      clearTimeout(that.timeoutObj);
      clearTimeout(that.serverTimeoutObj);
      //重启心跳
      that.start();
    },
    start() {
      //开启心跳
      let self = this;
      self.timeoutObj && clearTimeout(self.timeoutObj);
      self.serverTimeoutObj && clearTimeout(self.serverTimeoutObj);
      self.timeoutObj = setTimeout(function () {
        //这里发送一个心跳，后端收到后，返回一个心跳消息，
        if (self.websocket.readyState == 1) {
          //如果连接正常
          self.websocket.send("heartCheck");
        } else {
          //否则重连
          self.reconnect();
        }
        self.serverTimeoutObj = setTimeout(function () {
          //超时关闭
          self.websocket.close();
        }, self.timeout);
      }, self.timeout);
    },
    // 连接发生错误的回调方法
    websocketOnerror() {
      this.$message.error("WebSocket连接发生错误");
      //重连
      this.reconnect();
    },
    // 连接成功建立的回调方法
    websocketOnopen() {
      this.websocketSend();
      // console.log("连接成功建立的回调方法");
      //开启心跳
      this.start();
    },
    websocketSend() {
      //数据发送
      let sendData = {
        order_no: window.localStorage.getItem(
          "order_no" + this.$store.state.indexIdentity
        ),
        cam_id: window.localStorage.getItem(
          "campsite_id" + this.$store.state.indexIdentity
        ),
        token: this.token,
      };
      this.websocket.send(JSON.stringify(sendData));
    },
    // 接收到消息的回调方法
    websocketOnmessage(event) {
      console.log(event, "event");
      if (event.data !== "Opened") {
        //const data = JSON.parse(event.data);
        // console.log("接收到消息的回调方法", event.data);
        if (event.data == 1) {
          this.lockReconnect = false;
          this.closeWebSocket();
          // this.dialogVisible = false;
          // this.$emit("closeCFSUDialog", this.dialogVisible, true);
          // status=6，跳转到报名成功页面
          // this.$emit("getstatus", res.data.status.status);
        } else {
          //收到服务器信息，心跳重置
          this.reset();
        }
      }
    },
    // 连接关闭的回调方法
    websocketOnclose() {
      // console.log("连接关闭的回调方法");
      //重连
      this.reconnect();
    },
    // 监听窗口关闭事件，当窗口关闭时，主动去关闭websocket连接，防止连接还没断开就关闭窗口，server端会抛异常
    websocketOnbeforeunload() {
      this.closeWebSocket();
      // console.log('监听窗口关闭事件，当窗口关闭时，主动去关闭websocket连接，防止连接还没断开就关闭窗口，server端会抛异常');
    },
    // 关闭WebSocket连接
    closeWebSocket() {
      this.websocket && this.websocket.close();
    },

    paying() {
      this.dialogChoose = true;
    },
    signUpInit(status_wx) {
      signUpInit({}).then((res) => {
        console.log(res, "signUpInit");
        if (res.data.code != 200) {
          return this.$message.error(res.data.message);
        }
        // this.status = res.data.status;
        // 点击下一步传值给status,更新
        if (status_wx) {
          this.status = status_wx;
          console.log(this.status, "status_wx");
        } else {
          this.status = res.data.status;
          // console.log(this.status, "11111");
        }
        // return;
        // 点击下一步传值给status,更新
        if (this.status == 4) {
          this.type = "check";
          this.status = 3;
          if (this.isNexts == 1) {
            if (res.data.examine_status == 1) {
              console.log(333);
              this.type = "pay";
              this.status = 4;
            } else if (res.data.examine_status == 2) {
              this.type = "referInfo";
              this.status = 2;
            }
          }
          this.confirmSignUpDialogFu = true;
        } else if (this.status == 5) {
          this.type = "pay";
          this.confirmSignUpDialogFu = true;
        } else if (this.status == 6) {
          this.type = "success";
        }
      });
    },
    getAccount() {
      getAccount({}).then((res) => {
        // console.log(res, "getAccount");
        if (res.data.code != 200) {
          return this.$message.error(res.data.message);
        }
        this.personalInfo = res.data.info;
        // this.personalInfo.create_time = parseTime(res.data.info.create_time);
      });
    },
  },
};
</script>

<style  lang="scss" scoped>
@import "a/scss/btn";
.btn {
  padding-bottom: 0 !important;
}
.flex {
  display: flex;
}
.paying {
  padding: 0 56px 65px 56px;
  .pay_titile {
    font-size: 18px;
    font-weight: 500;
    color: #12141c;
    line-height: 18px;
  }
  .pay_top {
    margin: 24px 0;
    div {
      font-size: 14px;
      font-weight: 500;
      color: #12141c;
      line-height: 32px;
    }
  }

  .line {
    height: 1px;
    border-bottom: 1px solid #d1d1d1;
  }
  .pay-money {
    font-size: 18px;
    font-weight: 500;
    color: #12141c;
    line-height: 24px;
    justify-content: space-between;
    margin: 43px 0 0 0;
    span {
      color: #60194a;
    }
  }
  .pay_bottom {
    font-size: 12px;
    font-weight: 500;
    color: #999999;
    line-height: 21px;
  }
}
.choose {
  text-align: center;
  .img_choose {
    justify-content: space-around;
    img {
      width: 38%;
      cursor: pointer;
    }
  }

  .text_choose {
    font-size: 15px;
    margin: 20px 0 50px 0;
  }
  .btn .el-button {
    width: 120px !important;
  }
}
// .pay_code {
//   text-align: center;
//   :nth-child(1) {
//     font-size: 14px;
//     font-weight: 500;
//     color: #12141c;
//     line-height: 14px;
//     span {
//       font-size: 13px !important;
//       color: var(--color);
//     }
//   }
//   :nth-child(2) {
//     font-size: 14px;
//     font-weight: 500;
//     color: var(--color);
//     line-height: 24px;
//   }
//   :nth-child(4) {
//     font-size: 14px;
//     font-weight: 500;
//     color: #333333;
//     line-height: 16px;
//     span {
//       font-size: 17px !important;
//       color: var(--color);
//     }
//   }
//   :nth-child(5) {
//     font-size: 12px;
//     font-weight: 500;
//     color: #666666;
//     line-height: 12px;
//   }
// }
</style>
