<template>
  <view class="phone-binding-header">
    <theme-image
      :style="{ width: '46rpx', height: '46rpx' }"
      src="@/static/icon-back-white.png"
      @click="back"
    ></theme-image>
    <view class="title" :style="{ color: themeStore.color }">{{
      props.title
    }}</view>
    <theme-image
      src="@/static/account/supprot.png"
      class="supprot-image"
      @click="supprotClick"
    ></theme-image>
  </view>
</template>

<script setup>
import { requestServiceList } from "@/api/home.js";
import Toast from "@/module/toast/toast";
import { openUrl } from "@/module/utils/openUrl";
import { useUserStore } from "@/store/useUserStore";
import { useThemeStore } from "@/store/useThemeStore";
const themeStore = useThemeStore().theme.account.header;
// const themeStore = useThemeStore()
const props = defineProps({
  title: String,
});
const userStore = useUserStore();
/**
 * 返回上一级
 */
const back = () => {
  uni.navigateBack({
    delta: 1,
  });
};
/**
 * 客服功能调用
 */
const supprotClick = async () => {
  Toast.showLoading("Loading");
  const { code, data } = await requestServiceList();
  Toast.hideLoading();
  if (code !== 200) {
    return;
  }
  if (data.mobile && data.mobile.length) {
    const getObjWithName = (list, name) => {
      for (let i = 0; i < list.length; i++) {
        if (name === list[i].mobileName) {
          return list[i];
        }
      }
      return null;
    };

    const vipLevel = userStore.vipInfo.currentVipConfig.level;

    const vipCS = getObjWithName(data.mobile, "VipCS");
    const vipWebCS = getObjWithName(data.mobile, "VipWebCS");
    const webCS = getObjWithName(data.mobile, "WebCS");

    let url = "";
    if (vipLevel > vipCS.minVipLevel) {
      if (vipCS && vipCS.mobileNumber && vipCS.mobileNumber.length) {
        url = vipCS.mobileNumber[0];
      } else if (
        vipWebCS &&
        vipWebCS.mobileNumber &&
        vipWebCS.mobileNumber.length
      ) {
        url = vipWebCS.mobileNumber[0];
      }
    } else {
      if (webCS && webCS.mobileNumber && webCS.mobileNumber.length) {
        url = webCS.mobileNumber[0];
      }
    }

    if (!url) {
      return;
    }
    openUrl(url);
  }
};
</script>

<style lang="scss" scoped>
.phone-binding-header {
  width: 100%;
  height: 88rpx;
  box-sizing: border-box;
  padding: 0 31rpx;
  display: flex;
  justify-content: space-between;
  align-items: center;
  .title {
    width: calc(100% - 40%);
    text-align: center;
    font-size: 36rpx;
    // color: #fff;
  }
  .back-image {
    width: 17rpx;
    height: 29rpx;
  }
  .supprot-image {
    width: 62rpx;
    height: 61rpx;
  }
}
</style>
