<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Organogram — {{ $organization->name }}</title>
    <style>
        * { box-sizing: border-box; margin: 0; padding: 0; }
        body { font-family: Arial, Helvetica, sans-serif; font-size: 12px; color: #1f2937; background: #fff; }
        .header { background: #16a34a; color: #fff; padding: 20px 30px; margin-bottom: 20px; }
        .header h1 { font-size: 20px; font-weight: bold; }
        .header p { font-size: 12px; opacity: 0.85; margin-top: 4px; }
        .stats { display: flex; gap: 16px; padding: 0 30px 20px; flex-wrap: wrap; }
        .stat-card { background: #f9fafb; border: 1px solid #e5e7eb; border-radius: 8px; padding: 12px 16px; min-width: 120px; }
        .stat-card .label { font-size: 10px; color: #6b7280; text-transform: uppercase; letter-spacing: 0.05em; }
        .stat-card .value { font-size: 22px; font-weight: bold; color: #111827; margin-top: 2px; }
        .section { padding: 0 30px 20px; }
        .section-title { font-size: 14px; font-weight: bold; color: #374151; border-bottom: 2px solid #16a34a; padding-bottom: 6px; margin-bottom: 14px; }
        .org-tree { overflow-x: auto; }
        .node { display: inline-block; vertical-align: top; text-align: center; padding: 0 8px; }
        .node-box { display: inline-block; background: #fff; border: 2px solid #16a34a; border-radius: 8px; padding: 10px 14px; min-width: 140px; margin-bottom: 8px; }
        .node-title { font-weight: bold; font-size: 11px; color: #111827; }
        .node-assignee { font-size: 10px; color: #16a34a; margin-top: 3px; }
        .node-vacant { font-size: 10px; color: #9ca3af; margin-top: 3px; }
        .node-level { font-size: 9px; color: #9ca3af; margin-top: 2px; }
        .children { display: flex; justify-content: center; border-top: 2px solid #86efac; padding-top: 8px; margin-top: 0; }
        .footer { padding: 20px 30px; border-top: 1px solid #e5e7eb; font-size: 10px; color: #9ca3af; display: flex; justify-content: space-between; }
        table.flat-list { width: 100%; border-collapse: collapse; font-size: 11px; }
        table.flat-list th { background: #f3f4f6; text-align: left; padding: 8px 10px; font-weight: 600; color: #374151; border-bottom: 2px solid #e5e7eb; }
        table.flat-list td { padding: 7px 10px; border-bottom: 1px solid #f3f4f6; color: #374151; }
        table.flat-list tr:nth-child(even) td { background: #f9fafb; }
        .badge { display: inline-block; padding: 2px 8px; border-radius: 9999px; font-size: 9px; font-weight: 600; }
        .badge-filled { background: #dcfce7; color: #166534; }
        .badge-vacant { background: #f3f4f6; color: #6b7280; }
    </style>
</head>
<body>

    {{-- Header --}}
    <div class="header">
        <h1>{{ $organization->name }} — Organogram</h1>
        <p>Generated on {{ now()->format('F d, Y \a\t H:i') }} &nbsp;|&nbsp; {{ $organization->short_name }}</p>
    </div>

    {{-- Statistics --}}
    @if(!empty($statistics))
    <div class="stats">
        <div class="stat-card">
            <div class="label">Total Positions</div>
            <div class="value">{{ $statistics['total_positions'] }}</div>
        </div>
        <div class="stat-card">
            <div class="label">Filled</div>
            <div class="value" style="color:#16a34a;">{{ $statistics['filled_positions'] }}</div>
        </div>
        <div class="stat-card">
            <div class="label">Vacant</div>
            <div class="value" style="color:#d97706;">{{ $statistics['empty_positions'] }}</div>
        </div>
        <div class="stat-card">
            <div class="label">Fill Rate</div>
            <div class="value" style="color:#7c3aed;">{{ $statistics['fill_rate'] }}%</div>
        </div>
    </div>
    @endif

    {{-- Hierarchy Tree --}}
    @if(!empty($hierarchy))
    <div class="section">
        <div class="section-title">Organizational Hierarchy</div>
        <div class="org-tree">
            @foreach($hierarchy as $node)
                @include('organization.organogram.partials.export-node', ['node' => $node])
            @endforeach
        </div>
    </div>
    @endif

    {{-- Flat Position List --}}
    @if(!empty($hierarchy))
    <div class="section" style="margin-top: 20px;">
        <div class="section-title">Position Directory</div>
        <table class="flat-list">
            <thead>
                <tr>
                    <th>Position Title</th>
                    <th>Level</th>
                    <th>Current Assignee</th>
                    <th>Status</th>
                </tr>
            </thead>
            <tbody>
                @php
                    $flattenHierarchy = function(array $nodes) use (&$flattenHierarchy): array {
                        $flat = [];
                        foreach ($nodes as $node) {
                            $flat[] = $node;
                            if (!empty($node['children'])) {
                                $flat = array_merge($flat, $flattenHierarchy($node['children']));
                            }
                        }
                        return $flat;
                    };
                    $flatNodes = $flattenHierarchy($hierarchy);
                @endphp
                @foreach($flatNodes as $node)
                <tr>
                    <td>{{ $node['title'] }}</td>
                    <td>{{ $node['hierarchy_level_label'] }}</td>
                    <td>{{ $node['assignee'] ?? '—' }}</td>
                    <td>
                        @if($node['assignee'])
                            <span class="badge badge-filled">Filled</span>
                        @else
                            <span class="badge badge-vacant">Vacant</span>
                        @endif
                    </td>
                </tr>
                @endforeach
            </tbody>
        </table>
    </div>
    @endif

    {{-- Footer --}}
    <div class="footer">
        <span>{{ $organization->name }} &mdash; Confidential</span>
        <span>{{ $organization->email }}</span>
    </div>

</body>
</html>
