<template>
  <view class="sp-new-header-wrap">
    <view class="heder-box" :class="{ active: isShowBlur }">
      <view class="heder-left" @click="gotoPage('../my/my', 1)">
        <view class="avatar-item">
          <image
            mode="widthFix"
            :src="securityInfo.avatar || '/static/img/avatar.png'"
          />
        </view>
        <view class="welcome-css"
          >Hi ,
          {{
            isOkAuth || securityInfo.realName
              ? securityInfo.nickName
              : $t("betPageTMy.notAuthText")
          }}</view
        >
        <view class="auth-icon">
          <view
            class="iconfont icon-renzheng"
            :class="{ isOkAuth: isOkAuth }"
          ></view>
        </view>
      </view>
      <view class="heder-right">
        <view class="right-item" @click="toChat">
          <view class="iconfont icon-support"></view>
        </view>
        <view class="right-item" @click="scanCode">
          <view class="iconfont icon-code"></view>
        </view>
        <view class="right-item" @click="gotoPage('../homeNotice/index')">
          <view class="iconfont icon-tongzhi"></view>
          <view v-if="unreadNoticeIds" class="sp-header__alt-new">{{
            unreadNoticeIds
          }}</view>
        </view>
      </view>
    </view>
    <view v-if="isShowBlur" class="header-bar-css"> </view>
  </view>
</template>

<script>
export default {
  name: "BetoneNewHeader",
  props: {
    isShowBlur: {
      type: Boolean,
      default: false,
    },
    userInfo: {
      type: Object,
      default: () => {
        return {
          realName: "",
          avatar: "",
          realName: "",
          nickName: "",
        };
      },
    },
  },
  data() {
    return {
      securityInfo: {},
      unreadNoticeIds: 0,
    };
  },
  computed: {
    isOkAuth() {
      return uni.getStorageSync("isOkAuth");
    },
    tCommonToast() {
      return this.$t("tCommonToast");
    },
  },
  mounted() {},
  methods: {
    // 由外部调用
    refresh(res, res2 = null, refreshSecurity = false) {
      try {
        const D = uni.getStorageSync("securityInfo");
        this.securityInfo = JSON.parse(D);
      } catch (e) {}
      if (!refreshSecurity) {
        this.getMemberSecurityInfo(res);
      }
      this.findUnreadNoticeId(res2);
    },
    findUnreadNoticeId(res2 = null) {
      if (!res2) {
        this.$u
          .get("/message/findUnreadCount")
          .then(this.findUnreadNoticeIdCallBack);
      } else {
        this.findUnreadNoticeIdCallBack(res2);
      }
    },
    findUnreadNoticeIdCallBack(res) {
      var that = this;
      // console.log("TTTT:::::");
      that.unreadNoticeIds = res.data;
    },
    getMemberSecurityInfo(res) {
      // console.log("res::::::KKK", res);
      if (res) {
        this.rs(res);
      } else {
        this.$u.get("/member/getMemberSecurityInfo").then(this.rs);
      }
    },
    rs(res) {
      this.securityInfo = res.data;
      this.$emit("securityInfo", this.securityInfo);
      try {
        uni.setStorage({
          key: "securityInfo",
          data: JSON.stringify(this.securityInfo),
        });
      } catch (e) {}
    },
    toChat() {
      // uni.navigateTo({
      // 	url: "../csChat/csChat?vip=1",
      // });
      uni.navigateTo({
        url: "/pages/cusservice/cusservice",
        fail: (err) => {
          uni.showToast({
            title: this.$t("tCommonToast").toast55,
            icon: "none",
            duration: 5000,
            mask: true,
          });
        },
      });
    },
    gotoPage(path, ack = 0) {
      if (!ack) {
        uni.navigateTo({
          url: path,
        });
      } else {
        uni.switchTab({
          url: path,
        });
      }
    },
    lockPreReceiptOrder() {
      var that = this;
      if (that.orderNo === null || that.orderNo === "") {
        // 请输入订单号
        uni.showToast({
          title: this.tCommonToast.toast10,
          icon: "none",
        });
        return;
      }
      this.$u
        .post("/transfer/lockPreReceiptOrder", {
          orderNo: that.orderNo,
        })
        .then((res) => {
          that.showC2BModal = false;
          uni.navigateTo({
            url: "../transferC2B/transferC2B?orderNo=" + that.orderNo,
          });
        });
    },
    scanCode() {
      var that = this;
      // #ifdef APP-PLUS
      uni.scanCode({
        scanType: ["qrCode"],
        autoZoom: true,
        success: function (res) {
          if (res.result && 1) {
            var reg = /^-?[0-9]\d*$/;
            if (reg.test(res.result)) {
              that.orderNo = res.result;
              that.lockPreReceiptOrder();
            } else {
              that.walletAddr = res.result;
              uni.navigateTo({
                url: "../transferC2C/transferC2C?walletAddr=" + that.walletAddr,
              });
            }
          } else {
            // uni.showToast({
            // 	title: "二维码错误",
            // 	icon: "none",
            // });
          }
        },
        fail: function (res) {
          // 扫码失败
          uni.showToast({
            title: that.tCommonToast.toast11,
            icon: "none",
          });
        },
      });
      // #endif

      // #ifdef H5
      uni.navigateTo({
        url: "../QRCodeReader/QRCodeReader",
      });
      // #endif
    },
  },
};
</script>

<style>
</style>