<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>

          </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({ path: '/home', query: { org: child.id } })"
                >
                  <div class="node-name">{{ child.name }}</div>
                  <div class="node-jobs" v-if="child.job_count">在招 {{ child.job_count }} 岗</div>
                </div>
                <!-- 学历招聘人数（竖排） -->
                <div class="child-edu-list" v-if="child.edu_summary && child.edu_summary.length">
                  <div
                    v-for="item in child.edu_summary"
                    :key="item.education"
                    class="child-edu-col"
                  >
                    <span v-for="ch in item.education" :key="ch" class="child-edu-char">{{ ch }}</span>
                    <span class="child-edu-count">{{ item.count }}</span>
                    <span class="child-edu-char">人</span>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </section>

    <!-- ③ 在招职位汇总（折叠式） -->
    <section class="jobs-edu-section" v-if="group.jobs_by_education && group.jobs_by_education.length">
      <div class="section-inner">
        <div class="section-header">
          <span class="section-bar"></span>
          <span class="section-title">在招职位</span>
        </div>
        <div class="edu-table">
          <div v-for="group_edu in group.jobs_by_education" :key="group_edu.education" class="edu-row">
            <div class="edu-summary" @click="toggleEdu(group_edu.education)">
              <div class="edu-tag">{{ group_edu.education }}</div>
              <div class="edu-total-badge">
                共计 <strong>{{ group_edu.jobs.reduce((s, j) => s + (j.headcount || 0), 0) }}</strong> 人
              </div>
              <el-icon class="edu-arrow" :class="{ 'is-open': expandedEdus.has(group_edu.education) }">
                <ArrowDown />
              </el-icon>
            </div>
            <transition name="slide">
              <div v-if="expandedEdus.has(group_edu.education)" class="edu-chips">
                <div v-for="job in group_edu.jobs" :key="job.title" class="edu-chip">
                  <span class="chip-title">{{ job.title }}</span>
                  <span class="chip-count">{{ job.headcount }}人</span>
                </div>
              </div>
            </transition>
          </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, Briefcase, ArrowDown } from '@element-plus/icons-vue'

const router = useRouter()
const group = ref(null)
const loading = ref(true)
const expandedEdus = ref(new Set())

function toggleEdu(edu) {
  const s = new Set(expandedEdus.value)
  if (s.has(edu)) s.delete(edu)
  else s.add(edu)
  expandedEdus.value = s
}

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

<style scoped>
.company-page {
  --serif: 'Noto Serif SC', 'Source Han Serif SC', 'Songti SC', 'STSong', 'SimSun', serif;
  background: var(--bg-page);
  min-height: calc(100vh - 120px);
  font-family: 'PingFang SC', 'Microsoft YaHei', 'Noto Sans SC', system-ui, sans-serif;
}

/* ========== Hero ========== */
.hero-section {
  background: #fff;
  border-bottom: 1px solid var(--border);
  padding: 48px 0 44px;
  color: var(--text-main);
}
.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: 4px;
  overflow: hidden;
  border: 1px solid var(--border);
  background: var(--bg-page);
}
.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-family: var(--serif);
  font-size: 36px;
  font-weight: 700;
  color: var(--brand-red);
}
.hero-name {
  font-family: var(--serif);
  font-size: 28px;
  font-weight: 700;
  margin: 0 0 12px;
  color: var(--text-main);
  letter-spacing: 0.04em;
}
.hero-name-link {
  cursor: pointer;
  transition: color 0.2s;
}
.hero-name-link:hover {
  color: var(--brand-red);
}
.hero-meta {
  display: flex;
  gap: 20px;
  font-size: 13px;
  color: var(--text-muted);
  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: var(--text-muted);
  max-height: 120px;
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 4;
  -webkit-box-orient: vertical;
  margin-bottom: 20px;
}
.hero-btn {
  font-weight: 600;
  letter-spacing: 0.05em;
}

/* ========== 通用 Section ========== */
.org-section {
  padding: 40px 0 0;
}
.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: var(--brand-red);
  border-radius: 2px;
}
.section-title {
  font-family: var(--serif);
  font-size: 20px;
  font-weight: 700;
  color: var(--text-main);
  letter-spacing: 0.06em;
}
.section-count {
  font-size: 13px;
  color: var(--text-muted);
  margin-left: 4px;
}

/* ========== 在招职位（折叠式） ========== */
.jobs-edu-section {
  padding: 40px 0 56px;
}
.edu-table {
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 4px;
  box-shadow: 0 1px 6px rgba(0,0,0,.04);
  padding: 8px 24px;
}
.edu-row {
  padding: 0;
  border-bottom: 1px solid var(--border);
}
.edu-row:last-child {
  border-bottom: none;
}
.edu-summary {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 16px 8px;
  cursor: pointer;
  border-radius: 4px;
  transition: background 0.2s;
  user-select: none;
}
.edu-summary:hover {
  background: var(--brand-red-bg);
}
.edu-tag {
  font-family: var(--serif);
  font-size: 16px;
  font-weight: 700;
  color: #fff;
  background: var(--brand-red);
  border-radius: 3px;
  padding: 6px 18px;
  text-align: center;
  white-space: nowrap;
}
.edu-total-badge {
  font-size: 15px;
  color: var(--text-main);
  white-space: nowrap;
}
.edu-total-badge strong {
  color: var(--brand-red);
  font-size: 20px;
  margin: 0 2px;
}
.edu-arrow {
  margin-left: auto;
  font-size: 16px;
  color: var(--text-muted);
  transition: transform 0.3s;
}
.edu-arrow.is-open {
  transform: rotate(180deg);
  color: var(--brand-red);
}
.edu-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  padding: 0 8px 16px 52px;
}
.edu-chip {
  display: flex;
  align-items: center;
  gap: 6px;
  background: var(--brand-red-bg);
  border: 1px solid #FBC4C1;
  border-radius: 3px;
  padding: 5px 14px;
  font-size: 14px;
}
.chip-title {
  color: var(--text-main);
}
.chip-count {
  color: var(--brand-red);
  font-weight: 600;
  white-space: nowrap;
}
/* 展开/折叠动画 */
.slide-enter-active,
.slide-leave-active {
  transition: all 0.3s ease;
  overflow: hidden;
}
.slide-enter-from,
.slide-leave-to {
  opacity: 0;
  max-height: 0;
  padding-top: 0;
  padding-bottom: 0;
}
.slide-enter-to,
.slide-leave-from {
  opacity: 1;
  max-height: 500px;
}

/* ========== 组织架构图（放大） ========== */
.org-chart {
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 48px 32px 40px;
  box-shadow: 0 1px 6px rgba(0,0,0,.04);
  overflow-x: auto;
}
/* 内层：所有元素同宽、列方向排列 */
.org-inner {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  min-width: 960px;
}
/* 根节点区：居中 */
.org-root {
  display: flex;
  flex-direction: column;
  align-items: center;
}
.root-v-line {
  width: 2px;
  height: 20px;
  background: var(--border);
}
/* 横线：和 org-inner 等宽，天然对齐 */
.org-h-line {
  height: 2px;
  background: var(--border);
  width: 100%;
}
.org-node {
  border-radius: 4px;
  padding: 10px 16px;
  text-align: center;
  min-width: 100px;
}
.root-node {
  background: var(--brand-red);
  color: #fff;
  padding: 18px 36px;
  border-radius: 4px;
  min-width: 260px;
}
.root-node .node-name {
  font-family: var(--serif);
  font-size: 18px;
  font-weight: 700;
  color: #fff;
  line-height: 1.4;
}
.root-node .node-sub {
  font-size: 12px;
  color: rgba(255,255,255,0.8);
  margin-top: 4px;
  letter-spacing: 0.1em;
}
/* 子节点行：和 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: var(--border);
}
.child-node {
  background: #fff;
  border: 1px solid var(--border);
  cursor: pointer;
  transition: all 0.2s;
  width: 100%;
  box-sizing: border-box;
}
.child-node:hover {
  background: var(--brand-red);
  border-color: var(--brand-red);
}
.child-node:hover .node-name {
  color: #fff;
}
.child-node:hover .node-jobs {
  color: rgba(255,255,255,0.85);
}
.child-node .node-name {
  font-size: 13px;
  font-weight: 600;
  color: var(--text-main);
  line-height: 1.4;
}
.child-node .node-jobs {
  font-size: 11px;
  color: var(--text-muted);
  margin-top: 4px;
}
/* 子公司学历招聘人数（竖排） */
.child-edu-list {
  margin-top: 8px;
  display: flex;
  justify-content: center;
  gap: 6px;
}
.child-edu-col {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  background: var(--bg-page);
  border: 1px solid var(--border);
  border-radius: 3px;
  padding: 6px 4px;
}
.child-edu-char {
  font-size: 11px;
  color: var(--text-muted);
  line-height: 1.2;
}
.child-edu-count {
  color: var(--brand-red);
  font-weight: 700;
  font-size: 14px;
  line-height: 1.2;
  margin: 2px 0;
}
</style>
