<!--
 * @Author: Fan
 * @Description: logo component
-->
<template>
  <div class="anticon" :class="getAppLogoClass" @click="goHome">
    <img :src="showLogo" />
    <div class="ml-2 truncate md:opacity-100" :class="getTitleClass" v-show="showTitle">
      {{ title }}
    </div>
  </div>
</template>
<script lang="ts" setup>
  import { computed, onMounted, ref, unref, watch } from 'vue';
  import { useGlobSetting } from '/@/hooks/setting';
  import { useGo } from '/@/hooks/web/usePage';
  import { useMenuSetting } from '/@/hooks/setting/useMenuSetting';
  import { useDesign } from '/@/hooks/web/useDesign';
  import { PageEnum } from '/@/enums/pageEnum';
  import { useUserStore } from '/@/store/modules/user';
  import { useAppStore } from '/@/store/modules/app';

  // import logoSvg from '/@/assets/svg/logo.svg';
  // import logoWhiteSvg from '/@/assets/svg/logo-white.svg';

  const props = defineProps({
    /**
     * The theme of the current parent component
     */
    theme: { type: String, validator: (v: string) => ['light', 'dark'].includes(v) },
    /**
     * Whether to show title
     */
    showTitle: { type: Boolean, default: true },
    /**
     * The title is also displayed when the menu is collapsed
     */
    alwaysShowTitle: { type: Boolean },
  });

  const { prefixCls } = useDesign('app-logo');
  const { getCollapsedShowTitle } = useMenuSetting();
  const userStore = useUserStore();
  const { title } = useGlobSetting();
  const go = useGo();
  import { sysSettingDetail } from '/@/api/sys/sysSettingApi';
  const appStore = useAppStore();

  // const showLogo = ref(logoSvg);
  const showLogo = ref('');
  onMounted(async () => {
    const bgColor = appStore.getMenuSetting.bgColor;
    setBgColor(bgColor);
  });

  watch(
    () => appStore.getMenuSetting.bgColor,
    newVal => {
      setBgColor(newVal);
    },
  );

  const getAppLogoClass = computed(() => [
    prefixCls,
    props.theme,
    { 'collapsed-show-title': unref(getCollapsedShowTitle) },
  ]);

  const getTitleClass = computed(() => [
    `${prefixCls}__title`,
    {
      'xs:opacity-0': !props.alwaysShowTitle,
    },
  ]);

  async function setBgColor(val) {
    if (val == '#ffffff') {
      // showLogo.value = logoSvg;
      const res = await sysSettingDetail();
      showLogo.value = res.logo;
    } else {
      // showLogo.value = logoWhiteSvg;
      const res = await sysSettingDetail();
      showLogo.value = res.logo;
    }
  }

  function goHome() {
    go(userStore.getUserInfo.homePath || PageEnum.BASE_HOME);
  }
</script>
<style lang="less" scoped>
  @prefix-cls: ~'@{namespace}-app-logo';

  .@{prefix-cls} {
    display: flex;
    align-items: center;
    padding-left: 7px;
    cursor: pointer;
    transition: all 0.2s ease;

    &.light {
      // border-bottom: 1px solid @border-color-base;
    }

    &.collapsed-show-title {
      // padding-left: 20px;
    }

    &.light &__title {
      color: @primary-color;
    }

    &.dark &__title {
      color: @white;
    }

    &__title {
      font-size: 16px;
      font-weight: 700;
      transition: all 0.5s;
      line-height: normal;
    }
  }
</style>
