<?php $basePath = defined('BASE_PATH') ? BASE_PATH : '/gmind'; ?>
<!-- Affiliations & Partners Management -->
<div class="admin-section">
    <!-- Filter Tabs -->
    <div style="margin-bottom: 20px; display:flex; gap:8px;">
        <a href="<?= $basePath ?>/admin/affiliations?type=all" class="admin-btn <?= ($currentType ?? 'all') === 'all' ? 'admin-btn-primary' : 'admin-btn-secondary' ?> admin-btn-sm">All</a>
        <a href="<?= $basePath ?>/admin/affiliations?type=affiliation" class="admin-btn <?= ($currentType ?? 'all') === 'affiliation' ? 'admin-btn-primary' : 'admin-btn-secondary' ?> admin-btn-sm">Affiliations</a>
        <a href="<?= $basePath ?>/admin/affiliations?type=partner" class="admin-btn <?= ($currentType ?? 'all') === 'partner' ? 'admin-btn-primary' : 'admin-btn-secondary' ?> admin-btn-sm">Partners</a>
    </div>

    <!-- Add New Form -->
    <div class="admin-card" style="margin-bottom: 24px;">
        <div class="admin-card-header">
            <h2 class="admin-card-title">Add Affiliation / Partner</h2>
        </div>
        <div class="admin-card-body">
            <form method="POST" action="<?= $basePath ?>/admin/affiliations" class="admin-form" enctype="multipart/form-data" novalidate>
                <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($csrfToken ?? '', ENT_QUOTES, 'UTF-8') ?>">
                <div style="display:grid; grid-template-columns: 1fr 1fr; gap: 16px;">
                    <div class="admin-form-group">
                        <label class="admin-form-label">Organization Name *</label>
                        <input type="text" name="name" class="admin-form-input" required placeholder="e.g. Microsoft, NIELIT">
                    </div>
                    <div class="admin-form-group">
                        <label class="admin-form-label">Type *</label>
                        <select name="type" class="admin-form-select" required>
                            <option value="affiliation">Affiliation</option>
                            <option value="partner">Partner</option>
                        </select>
                    </div>
                </div>
                <div style="display:grid; grid-template-columns: 1fr 1fr 1fr; gap: 16px;">
                    <div class="admin-form-group">
                        <label class="admin-form-label">Logo</label>
                        <input type="file" name="logo" class="admin-form-file" accept="image/*">
                    </div>
                    <div class="admin-form-group">
                        <label class="admin-form-label">Website URL</label>
                        <input type="url" name="website" class="admin-form-input" placeholder="https://">
                    </div>
                    <div class="admin-form-group">
                        <label class="admin-form-label">Sort Order</label>
                        <input type="number" name="sort_order" class="admin-form-input" value="0" min="0">
                    </div>
                </div>
                <div class="admin-form-group">
                    <label style="display:flex; align-items:center; gap:8px; cursor:pointer;">
                        <input type="checkbox" name="active" value="1" checked>
                        <span>Active (visible on website)</span>
                    </label>
                </div>
                <div class="admin-form-actions">
                    <button type="submit" class="admin-btn admin-btn-primary">Add Organization</button>
                </div>
            </form>
        </div>
    </div>

    <!-- Items List -->
    <div class="admin-card">
        <div class="admin-card-header">
            <h2 class="admin-card-title">All Organizations (<?= count($items ?? []) ?>)</h2>
        </div>
        <div class="admin-card-body">
            <div class="admin-table-responsive">
                <table class="admin-table">
                    <thead>
                        <tr>
                            <th>Logo</th>
                            <th>Name</th>
                            <th>Website</th>
                            <th>Type</th>
                            <th>Sort</th>
                            <th>Status</th>
                            <th>Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($items ?? [] as $item): ?>
                        <tr>
                            <td>
                                <?php if (!empty($item['logo'])): ?>
                                <img src="<?= htmlspecialchars(upload_url($item['logo']), ENT_QUOTES, 'UTF-8') ?>" alt="" style="height:30px;width:auto;">
                                <?php else: ?>
                                <span style="color:#999;font-size:0.8rem;">No logo</span>
                                <?php endif; ?>
                            </td>
                            <td><?= htmlspecialchars($item['name'] ?? '', ENT_QUOTES, 'UTF-8') ?></td>
                            <td>
                                <?php if (!empty($item['website'])): ?>
                                <a href="<?= htmlspecialchars($item['website'], ENT_QUOTES, 'UTF-8') ?>" target="_blank" style="color:#1a4fa0;font-size:0.85rem;"><?= htmlspecialchars($item['website'], ENT_QUOTES, 'UTF-8') ?></a>
                                <?php else: ?>
                                <span style="color:#999;">-</span>
                                <?php endif; ?>
                            </td>
                            <td>
                                <span style="padding:2px 10px;border-radius:20px;font-size:0.75rem;font-weight:600;<?= ($item['type'] ?? '') === 'affiliation' ? 'background:rgba(26,79,160,0.1);color:#1a4fa0;' : 'background:rgba(243,156,18,0.1);color:#e67e22;' ?>">
                                    <?= htmlspecialchars(ucfirst($item['type'] ?? 'affiliation'), ENT_QUOTES, 'UTF-8') ?>
                                </span>
                            </td>
                            <td><?= (int) ($item['sort_order'] ?? 0) ?></td>
                            <td><?= ($item['active'] ?? 0) ? '<span style="color:#27ae60;font-weight:600;">Active</span>' : '<span style="color:#999;">Inactive</span>' ?></td>
                            <td>
                                <form method="POST" action="<?= $basePath ?>/admin/affiliations/<?= (int) ($item['id'] ?? 0) ?>/delete" style="display:inline" class="delete-form">
                                    <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($csrfToken ?? '', ENT_QUOTES, 'UTF-8') ?>">
                                    <button type="submit" class="admin-btn admin-btn-danger admin-btn-sm" onclick="return confirm('Delete this organization?')">Delete</button>
                                </form>
                            </td>
                        </tr>
                        <?php endforeach; ?>
                        <?php if (empty($items)): ?>
                        <tr><td colspan="7" style="text-align:center;padding:24px;color:#999;">No affiliations or partners yet.</td></tr>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
