<template>
  <div
    class="sidebar-logo-container"
    :class="{ collapse: collapse }"
    :style="{
      backgroundColor:
        sideTheme === 'theme-dark' ? variables.menuBackground : variables.menuLightBackground,
    }"
  >
    <router-link class="sidebar-logo-link" to="/index">
      <el-image
        :src="logoImage"
        class="sidebar-logo"
        fit="contain"
      />
      <div v-if="!collapse" class="logo-text" :style="{ color: textColor }">
        <h1 class="sidebar-title">{{ title }}</h1>
        <p v-if="displayName" class="hospital-name">{{ displayName }}</p>
      </div>
    </router-link>
  </div>
</template>

<script setup>
import variables from '@/assets/styles/variables.module.scss';
import useSettingsStore from '@/store/modules/settings';
import useUserStore from '@/store/modules/user';
import {computed} from 'vue';

defineProps({
  collapse: {
    type: Boolean,
    required: true,
  },
});

const title = import.meta.env.VITE_APP_TITLE || '医院管理系统';
const settingsStore = useSettingsStore();
const userStore = useUserStore();
const sideTheme = computed(() => settingsStore.sideTheme);
const displayName = computed(() => userStore.tenantName || userStore.hospitalName || userStore.orgName || '');

const textColor = computed(() => {
  return sideTheme.value === 'theme-dark' ? '#fff' : '#303133';
});

const logoImage = computed(() => {
  return new URL('@/assets/logo/LOGO.jpg', import.meta.url).href;
});
</script>

<style lang="scss" scoped>
.sidebar-logo-container {
  position: relative;
  width: 100%;
  height: 50px;
  background: transparent;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;

  .sidebar-logo-link {
    height: 100%;
    width: 100%;
    display: flex;
    align-items: center;
    padding: 8px 12px;
    gap: 8px;
    text-decoration: none;
    cursor: pointer;

    &:hover {
      opacity: 0.8;
    }

    .sidebar-logo {
      width: 32px;
      height: 32px;
      flex-shrink: 0;
      object-fit: contain;
    }

    .logo-text {
      flex: 1;
      display: flex;
      flex-direction: column;
      align-items: flex-start;
      justify-content: center;
      gap: 2px;
      min-width: 0;
      pointer-events: none;
    }

    .sidebar-logo {
      pointer-events: none;
    }

    .sidebar-title {
      margin: 0;
      font-weight: 600;
      font-size: 15px;
      font-family: Avenir, Helvetica Neue, Arial, Helvetica, sans-serif;
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
      line-height: 1.2;
    }

    .hospital-name {
      margin: 0;
      font-size: 12px;
      font-weight: 400;
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
      line-height: 1.2;
      opacity: 0.85;
    }
  }

  &.collapse {
    .sidebar-logo-link {
      padding: 0;
      justify-content: center;
    }

    .logo-text {
      display: none;
    }
  }
}
</style>
