<template>
  <div class="home">
    <nav-bar/>
    <div class="bg-pattern"></div>
    
    <div class="nav">
      <router-link to="Hospital_dh1">院外导航</router-link>
      <router-link to="Hospital_dh" class="on">院内导航</router-link>
    </div>

    <!-- 主内容区卡片容器 -->
    <div class="main-card">
      <van-tabs class="custom-tabs" title-active-color="#3A9FA5">
        <van-tab v-for="item in hospitalData" :key="item.id">
          <template #title>
            <span class="tab-name">{{ item.name }}</span>
          </template>

          <!-- 楼层内容区 -->
          <div class="floors-wrapper">
            <div class="floor-section"
                 v-for="item1 in item.floors"
                 :key="item1.id">
              
              <!-- 楼层标题栏 -->
              <div class="floor-header">
                <div class="floor-badge">{{ item1.name.charAt(0) }}</div>
                <h2 class="floor-title">{{ item1.name }}</h2>
              </div>
              
              <!-- 分隔线 -->
              <div class="section-divider"></div>
              
              <!-- 科室区域 -->
              <div class="departments-container">
                <div 
                  v-for="(group, direction) in item1.directionGroups" 
                  :key="direction"
                  class="direction-section"
                >
                  <!-- 方向标题 -->
                  <h3 v-if="direction" class="direction-heading">
                    <span class="heading-icon">◆</span>
                    {{ direction }}
                  </h3>
                  
                  <!-- 科室网格 -->
                  <div class="dept-grid">
                    <div 
                      v-for="item2 in group" 
                      :key="item2.id"
                      class="dept-card"
                      @click="showDeptImage(item2)"
                    >
                      <span class="dept-name">{{ item2.name }}</span>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </van-tab>
      </van-tabs>
    </div>
  </div>
</template>

<script>
import {apiGetHospitalGuide} from "@/request/api.js";
import {ImagePreview} from "vant";

export default {
  components: {},
  mounted() {
    this.title = this.$route.meta.title;
    this.getData();
  },
  data() {
    return {
      title: "",
      hospitalData: [],
      currentDept: null, // 当前点击的科室信息
    };
  },
  methods: {
    // 获取医院导航数据
    getData() {
      apiGetHospitalGuide().then((res) => {
        console.log(res)
        this.hospitalData = res.data.map(hospital => ({
          ...hospital,
          floors: hospital.floors.map(floor => ({
            ...floor,
            directionGroups: this.groupByDirection(floor.labels)
          }))
        }));
        console.log(this.hospitalData)
      });
    },
    // 按方向分组处理
    groupByDirection(labels) {
      const groups = {};
      const noDirectionLabels = [];
      
      labels.forEach(label => {
        const dir = label.direction ? label.direction.trim() : null;
        
        if (dir && dir !== '') {
          groups[dir] = groups[dir] || [];
          groups[dir].push(label);
        } else {
          noDirectionLabels.push(label);
        }
      });
      
      if (noDirectionLabels.length > 0) {
        groups[''] = noDirectionLabels;
      }
      
      return groups;
    },
    
    // 显示科室图片
    showDeptImage(dept) {
      this.currentDept = dept;
      if (dept.imgurl) {
        ImagePreview({
          images: [dept.imgurl],
          startPosition: 0,
          showIndex: true,
          closeable: true,
          title: dept.name
        });
      } else {
        console.log('该科室没有图片链接');
      }
    }
  },
};
</script>

<style scoped lang="scss">
// 基础样式
.home {
  background: #f0f7f5;
  min-height: 100vh;
  padding: 0 15px 30px;
  font-family: 'Helvetica Neue', Arial, sans-serif;
}

// 背景图案
.bg-pattern {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-image: 
    linear-gradient(rgba(58, 159, 165, 0.03) 1px, transparent 1px),
    linear-gradient(90deg, rgba(58, 159, 165, 0.03) 1px, transparent 1px);
  background-size: 30px 30px;
  z-index: -1;
}

// 导航样式保持不变但增强对比
.nav {
  background: #fff;
  display: flex;
  justify-content: center;
  margin: 15px 0;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.05);

  a {
    font-size: 30px;
    height: 70px;
    line-height: 70px;
    padding: 0 30px;
    margin: 0 50px;
    position: relative;
    color: #666;
    text-decoration: none;
    font-weight: 500;
  }

  a.on {
    color: #3A9FA5;
    font-weight: 700;
  }

  a.on::after {
    content: "";
    width: 100%;
    height: 5px;
    background: #3A9FA5;
    display: block;
    position: absolute;
    left: 0;
    bottom: 0;
    border-radius: 2px 2px 0 0;
  }
}

// 主内容卡片
.main-card {
  background: #fff;
  border-radius: 12px;
  box-shadow: 0 5px 20px rgba(0,0,0,0.08);
  overflow: hidden;
  margin-top: 10px;
}

// 自定义标签页
.custom-tabs {
  .van-tabs__wrap {
    background: #f9fcfb;
    border-bottom: 2px solid #eaf4f2;
  }
  
  .van-tab {
    padding: 0 35px;
    height: 80px;
    line-height: 80px;
    font-size: 26px;
    color: #666;
    border-right: 2px solid #eaf4f2;
    font-weight: 500;
  }
  
  .van-tab--active {
    color: #3A9FA5;
    background: #eaf4f2;
    font-weight: 700;
  }
  
  .van-tab:first-child {
    border-left: none;
  }
  
  .van-tabs__content {
    padding: 20px;
  }
}

// 楼层容器
.floors-wrapper {
  padding: 10px 0;
}

// 楼层区块
.floor-section {
  background: #f9fcfb;
  border-radius: 10px;
  padding: 25px;
  margin-bottom: 30px;
  border: 1px solid #e6f2ef;
}

// 楼层标题
.floor-header {
  display: flex;
  align-items: center;
  margin-bottom: 25px;
}

.floor-badge {
  width: 50px;
  height: 50px;
  background: #3A9FA5;
  color: white;
  font-size: 30px;
  font-weight: bold;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 18px;
  border-radius: 6px;
}

.floor-title {
  font-size: 32px;
  color: #2d3748;
  margin: 0;
  font-weight: 700;
}

// 分隔线
.section-divider {
  height: 3px;
  background: linear-gradient(90deg, #3A9FA5 0%, rgba(58, 159, 165, 0.2) 100%);
  margin: 0 0 30px 0;
  border-radius: 2px;
}

// 方向区块
.direction-section {
  margin-bottom: 40px;
}

// 方向标题
.direction-heading {
  font-size: 28px;
  color: #3A9FA5;
  margin: 0 0 25px 0;
  font-weight: 600;
  display: flex;
  align-items: center;
}

.heading-icon {
  color: #3A9FA5;
  margin-right: 15px;
  font-size: 20px;
}

// 科室网格布局
.dept-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 20px;
}

// 科室卡片
.dept-card {
  background: white;
  border: 2px solid #e6f2ef;
  border-radius: 8px;
  padding: 25px 15px;
  text-align: center;
  transition: all 0.3s ease;
  box-shadow: 0 3px 6px rgba(0,0,0,0.04);
}

.dept-name {
  font-size: 24px;
  color: #2d3748;
  font-weight: 500;
}

// 响应式调整
@media (max-width: 768px) {
  .dept-grid {
    grid-template-columns: repeat(2, 1fr);
  }
  
  .nav a {
    margin: 0 20px;
    font-size: 26px;
  }
  
  .van-tab {
    padding: 0 20px;
    font-size: 22px;
  }
}

@media (max-width: 480px) {
  .dept-grid {
    grid-template-columns: 1fr;
  }
}
</style>
