<template>
  <div class="company-page" v-if="group">

    <!-- ① 总院 Hero -->
    <section class="hero-section">
      <div class="hero-inner">
        <div class="hero-logo">
          <img v-if="group.logo" :src="group.logo" alt="logo" />
          <div v-else class="logo-placeholder">{{ group.name?.charAt(0) }}</div>
        </div>
        <div class="hero-content">
          <h1 class="hero-name hero-name-link" @click="$router.push(`/companies/${group.id}`)">{{ group.name }}</h1>
          <div class="hero-meta">
            <span v-if="group.email">
              <el-icon><Message /></el-icon> {{ group.email }}
            </span>
            <span>
              <el-icon><Briefcase /></el-icon> 在招职位 {{ group.job_count }} 个
            </span>
            <span>
              <el-icon><OfficeBuilding /></el-icon> 成员单位 {{ group.children?.length || 0 }} 家
            </span>
          </div>
          <div class="hero-desc">{{ group.description }}</div>
          <el-button
            type="primary"
            class="hero-btn"
            @click="$router.push({ name: 'JobList', query: { org: group.id } })"
          >
            查看全部职位
          </el-button>
        </div>
      </div>
    </section>

    <!-- ② 组织架构图 -->
    <section class="org-section">
      <div class="section-inner">
        <div class="section-header">
          <span class="section-bar"></span>
          <span class="section-title">组织架构</span>
        </div>
        <!-- org-chart 只负责外层滚动 -->
        <div class="org-chart">
          <!-- 内层固定宽度，所有元素在此对齐 -->
          <div class="org-inner">
            <!-- 根节点 + 竖线 -->
            <div class="org-root">
              <div class="org-node root-node">
                <div class="node-name">{{ group.name }}</div>
                <div class="node-sub">集团总院</div>
              </div>
              <div class="root-v-line"></div>
            </div>
            <!-- 横线：宽度与 org-inner 相同，保证对齐 -->
            <div class="org-h-line"></div>
            <!-- 子节点行：宽度与 org-inner 相同 -->
            <div class="org-children">
              <div
                v-for="child in group.children"
                :key="child.id"
                class="org-child-wrap"
              >
                <div class="child-v-line"></div>
                <div
                  class="org-node child-node"
                  @click="$router.push(`/companies/${child.id}`)"
                >
                  <div class="node-name">{{ child.name }}</div>
                  <div class="node-jobs" v-if="child.job_count">在招 {{ child.job_count }} 岗</div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </section>

    <!-- ③ 成员单位 -->
    <section class="members-section">
      <div class="section-inner">
        <div class="section-header">
          <span class="section-bar"></span>
          <span class="section-title">成员单位</span>
          <span class="section-count">共 {{ group.children?.length || 0 }} 家</span>
        </div>
        <div class="members-grid">
          <div
            v-for="child in group.children"
            :key="child.id"
            class="member-card"
            @click="$router.push({ path: '/home', query: { org: child.id } })"
          >
            <div class="member-header">
              <div class="member-logo">
                <img v-if="child.logo" :src="child.logo" alt="logo" />
                <div v-else class="member-logo-placeholder">{{ child.name?.charAt(0) }}</div>
              </div>
              <div class="member-info">
                <div class="member-name">{{ child.name }}</div>
                <div class="member-jobs" v-if="child.job_count">
                  <el-tag size="small" type="success">在招 {{ child.job_count }} 个职位</el-tag>
                </div>
              </div>
            </div>
            <div class="member-desc">{{ child.description?.slice(0, 120) }}{{ child.description?.length > 120 ? '...' : '' }}</div>
            <div class="member-footer">
              <span class="member-email" v-if="child.email">
                <el-icon><Message /></el-icon> {{ child.email }}
              </span>
              <el-button type="primary" link size="small">查看职位 →</el-button>
            </div>
          </div>
        </div>
      </div>
    </section>
  </div>

  <div v-else-if="loading" v-loading="true" style="height: 400px"></div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { getOrganizations } from '@/api/organizations'
import { Message, OfficeBuilding, Briefcase } from '@element-plus/icons-vue'

const router = useRouter()
const group = ref(null)
const loading = ref(true)

onMounted(async () => {
  try {
    const { data } = await getOrganizations()
    // 取第一个顶级组织作为集团总院
    group.value = data.results?.[0] ?? data[0] ?? null
  } finally {
    loading.value = false
  }
})
</script>

<style scoped>
.company-page {
  background: #f5f6fa;
  min-height: calc(100vh - 120px);
}

/* ========== Hero ========== */
.hero-section {
  background: linear-gradient(135deg, #1a1a2e 0%, #16213e 60%, #0f3460 100%);
  padding: 56px 0 48px;
  color: #fff;
}
.hero-inner {
  max-width: 1100px;
  margin: 0 auto;
  padding: 0 24px;
  display: flex;
  gap: 36px;
  align-items: flex-start;
}
.hero-logo {
  flex-shrink: 0;
  width: 96px;
  height: 96px;
  border-radius: 12px;
  overflow: hidden;
  border: 2px solid rgba(255,255,255,0.2);
  background: rgba(255,255,255,0.05);
}
.hero-logo img { width: 100%; height: 100%; object-fit: cover; }
.logo-placeholder {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 36px;
  font-weight: 700;
  color: #c9a84c;
}
.hero-name {
  font-size: 26px;
  font-weight: 700;
  margin: 0 0 12px;
  color: #fff;
}
.hero-name-link {
  cursor: pointer;
  transition: color 0.2s;
}
.hero-name-link:hover {
  color: #c9a84c;
}
.hero-meta {
  display: flex;
  gap: 20px;
  font-size: 13px;
  color: rgba(255,255,255,0.65);
  margin-bottom: 16px;
  flex-wrap: wrap;
}
.hero-meta span {
  display: flex;
  align-items: center;
  gap: 4px;
}
.hero-desc {
  font-size: 13px;
  line-height: 1.9;
  color: rgba(255,255,255,0.8);
  max-height: 120px;
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 4;
  -webkit-box-orient: vertical;
  margin-bottom: 20px;
}
.hero-btn {
  background: #c9a84c !important;
  border-color: #c9a84c !important;
  color: #1a1a2e !important;
  font-weight: 600;
}
.hero-btn:hover {
  background: #e0bc5a !important;
  border-color: #e0bc5a !important;
}

/* ========== 通用 Section ========== */
.org-section,
.members-section {
  padding: 40px 0;
}
.members-section {
  padding-bottom: 56px;
}
.section-inner {
  max-width: 1100px;
  margin: 0 auto;
  padding: 0 24px;
}
.section-header {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 28px;
}
.section-bar {
  display: inline-block;
  width: 4px;
  height: 20px;
  background: #c9a84c;
  border-radius: 2px;
}
.section-title {
  font-size: 20px;
  font-weight: 700;
  color: #1a1a2e;
}
.section-count {
  font-size: 13px;
  color: #999;
  margin-left: 4px;
}

/* ========== 组织架构图 ========== */
.org-chart {
  background: #fff;
  border-radius: 12px;
  padding: 32px 24px 28px;
  box-shadow: 0 1px 6px rgba(0,0,0,.06);
  overflow-x: auto;
}
/* 内层：所有元素同宽、列方向排列 */
.org-inner {
  display: flex;
  flex-direction: column;
  align-items: stretch;   /* 子元素全部拉伸到同一宽度 */
  min-width: 880px;
}
/* 根节点区：居中 */
.org-root {
  display: flex;
  flex-direction: column;
  align-items: center;
}
.root-v-line {
  width: 2px;
  height: 20px;
  background: #c8c8c8;
}
/* 横线：和 org-inner 等宽，天然对齐 */
.org-h-line {
  height: 2px;
  background: #c8c8c8;
  width: 100%;
}
.org-node {
  border-radius: 8px;
  padding: 10px 16px;
  text-align: center;
  min-width: 100px;
}
.root-node {
  background: linear-gradient(135deg, #1a1a2e, #0f3460);
  color: #fff;
  padding: 14px 28px;
  border-radius: 10px;
  min-width: 220px;
}
.root-node .node-name {
  font-size: 15px;
  font-weight: 700;
  color: #fff;
  line-height: 1.4;
}
.root-node .node-sub {
  font-size: 11px;
  color: #c9a84c;
  margin-top: 4px;
}
/* 子节点行：和 org-inner 等宽 */
.org-children {
  display: flex;
  width: 100%;
}
.org-child-wrap {
  display: flex;
  flex-direction: column;
  align-items: center;
  flex: 1;
}
.child-v-line {
  width: 2px;
  height: 20px;
  background: #c8c8c8;
}
.child-node {
  background: #f0f4ff;
  border: 1px solid #d0d9f0;
  cursor: pointer;
  transition: all 0.2s;
  width: 100%;
  box-sizing: border-box;
}
.child-node:hover {
  background: #1a1a2e;
  border-color: #1a1a2e;
}
.child-node:hover .node-name {
  color: #fff;
}
.child-node:hover .node-jobs {
  color: #c9a84c;
}
.child-node .node-name {
  font-size: 11px;
  font-weight: 600;
  color: #1a1a2e;
  line-height: 1.4;
}
.child-node .node-jobs {
  font-size: 10px;
  color: #888;
  margin-top: 3px;
}

/* ========== 成员单位网格 ========== */
.members-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 20px;
}
@media (max-width: 900px) {
  .members-grid { grid-template-columns: repeat(2, 1fr); }
}
@media (max-width: 600px) {
  .members-grid { grid-template-columns: 1fr; }
}
.member-card {
  background: #fff;
  border-radius: 10px;
  padding: 20px;
  box-shadow: 0 1px 4px rgba(0,0,0,.06);
  cursor: pointer;
  transition: box-shadow 0.2s, transform 0.2s;
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.member-card:hover {
  box-shadow: 0 4px 16px rgba(0,0,0,.12);
  transform: translateY(-2px);
}
.member-header {
  display: flex;
  gap: 12px;
  align-items: flex-start;
}
.member-logo {
  width: 44px;
  height: 44px;
  border-radius: 8px;
  overflow: hidden;
  border: 1px solid #eee;
  flex-shrink: 0;
}
.member-logo img { width: 100%; height: 100%; object-fit: cover; }
.member-logo-placeholder {
  width: 100%;
  height: 100%;
  background: linear-gradient(135deg, #1a1a2e, #0f3460);
  color: #c9a84c;
  font-size: 18px;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
}
.member-name {
  font-size: 14px;
  font-weight: 600;
  color: #1a1a2e;
  line-height: 1.4;
  margin-bottom: 4px;
}
.member-desc {
  font-size: 12px;
  color: #666;
  line-height: 1.8;
  flex: 1;
}
.member-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-top: 1px solid #f0f0f0;
  padding-top: 10px;
  font-size: 12px;
  color: #999;
}
.member-email {
  display: flex;
  align-items: center;
  gap: 4px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 60%;
}
</style>
