<?php
function cm_h($v) {
    return htmlspecialchars((string)$v, ENT_QUOTES, 'UTF-8');
}

$members = $members ?? [];
$conference = $conference ?? [];

$types = [
    'chair' => 'Chair',
    'co_chair' => 'Co-chair',
    'editor' => 'Editor',
    'section_editor' => 'Section Editor',
    'patron' => 'Patron',
    'keynote' => 'Keynote',
    'tpc' => 'TPC',
    'reviewer' => 'Reviewer',
    'panel' => 'Panelist',
    'organizing' => 'Organizing',
    'scientific' => 'Scientific',
    'steering' => 'Steering',
    'sponsor' => 'Sponsor',
    'other' => 'Other',
];
?>

<div class="container-fluid py-3">

    <div class="card border-0 shadow-sm mb-4" style="border-radius:24px;background:linear-gradient(135deg,#0f172a,#1d4ed8);color:#fff;">
        <div class="card-body p-4 d-flex justify-content-between align-items-center flex-wrap gap-3">
            <div>
                <h3 class="fw-bold mb-1"><i class="fa fa-users"></i> Committee Photo Manager</h3>
                <div class="opacity-75">Reviewers, patrons, chairs, editors, TPC, and other committees</div>
            </div>
            <div class="d-flex gap-2">
                <a class="btn btn-light rounded-pill fw-bold" href="/easychair/admin/conference-business">Business Dashboard</a>
                <a class="btn btn-outline-light rounded-pill fw-bold" onclick="return confirm('Delete empty committee rows?')" href="/easychair/admin/committee-media/cleanup-empty">Clean Empty</a>
            </div>
        </div>
    </div>

    <div class="card border-0 shadow-sm mb-4" style="border-radius:22px;">
        <div class="card-body">
            <h5 class="fw-bold mb-3">Add New Person</h5>

            <form method="post" action="/easychair/admin/committee-media/store" enctype="multipart/form-data" class="row g-2">
                <div class="col-md-2">
                    <select name="committee_type" class="form-select">
                        <?php foreach ($types as $k=>$label): ?>
                            <option value="<?= cm_h($k) ?>"><?= cm_h($label) ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>

                <div class="col-md-2"><input name="full_name_ar" class="form-control" placeholder="الاسم بالعربية"></div>
                <div class="col-md-2"><input name="full_name_en" class="form-control" placeholder="English name"></div>
                <div class="col-md-2"><input name="email" class="form-control" placeholder="Email"></div>
                <div class="col-md-2"><input type="file" name="image" class="form-control" accept=".jpg,.jpeg,.png,.webp"></div>
                <div class="col-md-2"><button class="btn btn-success w-100">Add</button></div>

                <div class="col-md-3"><input name="title_ar" class="form-control" placeholder="الصفة"></div>
                <div class="col-md-3"><input name="title_en" class="form-control" placeholder="Title"></div>
                <div class="col-md-3"><input name="affiliation_ar" class="form-control" placeholder="المؤسسة"></div>
                <div class="col-md-3"><input name="affiliation_en" class="form-control" placeholder="Affiliation"></div>
                <div class="col-md-6"><textarea name="bio_ar" class="form-control" rows="2" placeholder="نبذة عربية"></textarea></div>
                <div class="col-md-6"><textarea name="bio_en" class="form-control" rows="2" placeholder="English bio"></textarea></div>
            </form>
        </div>
    </div>

    <div class="row g-3">
        <?php foreach ($members as $m): ?>
            <div class="col-xl-4 col-lg-6">
                <div class="card border-0 shadow-sm h-100" style="border-radius:22px;">
                    <div class="card-body">
                        <form method="post" action="/easychair/admin/committee-media/update" enctype="multipart/form-data">
                            <input type="hidden" name="id" value="<?= (int)$m['id'] ?>">

                            <div class="d-flex gap-3 align-items-center mb-3">
                                <?php if (!empty($m['image_path'])): ?>
                                    <img src="/easychair/storage/<?= cm_h($m['image_path']) ?>" style="width:82px;height:82px;border-radius:50%;object-fit:cover;">
                                <?php else: ?>
                                    <div style="width:82px;height:82px;border-radius:50%;background:#e2e8f0;display:flex;align-items:center;justify-content:center;font-weight:900;color:#64748b;">
                                        No
                                    </div>
                                <?php endif; ?>

                                <div>
                                    <div class="fw-bold"><?= cm_h(($m['full_name_ar'] ?? '') ?: ($m['full_name_en'] ?? 'Unnamed')) ?></div>
                                    <span class="badge bg-primary"><?= cm_h($m['committee_type']) ?></span>
                                    <?php if (!empty($m['is_active'])): ?>
                                        <span class="badge bg-success">active</span>
                                    <?php else: ?>
                                        <span class="badge bg-secondary">hidden</span>
                                    <?php endif; ?>
                                </div>
                            </div>

                            <div class="row g-2">
                                <div class="col-md-6">
                                    <select name="committee_type" class="form-select form-select-sm">
                                        <?php foreach ($types as $k=>$label): ?>
                                            <option value="<?= cm_h($k) ?>" <?= $m['committee_type']===$k ? 'selected' : '' ?>>
                                                <?= cm_h($label) ?>
                                            </option>
                                        <?php endforeach; ?>
                                    </select>
                                </div>

                                <div class="col-md-6">
                                    <input
    type="number"
    name="sort_order"
    min="1"
    step="1"
    class="form-control form-control-sm text-center fw-bold"
    value="<?= (int)($m['sort_order'] ?: 1) ?>"
>
                                </div>

                                <div class="col-md-6"><input name="full_name_ar" class="form-control form-control-sm" value="<?= cm_h($m['full_name_ar']) ?>" placeholder="Arabic name"></div>
                                <div class="col-md-6"><input name="full_name_en" class="form-control form-control-sm" value="<?= cm_h($m['full_name_en']) ?>" placeholder="English name"></div>
                                <div class="col-md-6"><input name="title_ar" class="form-control form-control-sm" value="<?= cm_h($m['title_ar']) ?>" placeholder="Arabic title"></div>
                                <div class="col-md-6"><input name="title_en" class="form-control form-control-sm" value="<?= cm_h($m['title_en']) ?>" placeholder="English title"></div>
                                <div class="col-md-12"><input name="email" class="form-control form-control-sm" value="<?= cm_h($m['email']) ?>" placeholder="Email"></div>
                                <div class="col-md-6"><input name="affiliation_ar" class="form-control form-control-sm" value="<?= cm_h($m['affiliation_ar']) ?>" placeholder="Arabic affiliation"></div>
                                <div class="col-md-6"><input name="affiliation_en" class="form-control form-control-sm" value="<?= cm_h($m['affiliation_en']) ?>" placeholder="English affiliation"></div>

                                <div class="col-md-12">
                                    <input type="file" name="image" class="form-control form-control-sm" accept=".jpg,.jpeg,.png,.webp">
                                </div>

                                <div class="col-md-6"><textarea name="bio_ar" class="form-control form-control-sm" rows="2"><?= cm_h($m['bio_ar']) ?></textarea></div>
                                <div class="col-md-6"><textarea name="bio_en" class="form-control form-control-sm" rows="2"><?= cm_h($m['bio_en']) ?></textarea></div>

                                <div class="col-md-4">
                                    <button class="btn btn-sm btn-primary w-100">Save</button>
                                </div>
                                <div class="col-md-4">
                                    <a class="btn btn-sm btn-warning w-100" href="/easychair/admin/committee-media/toggle?id=<?= (int)$m['id'] ?>">Toggle</a>
                                </div>
                                <div class="col-md-4">
                                    <a class="btn btn-sm btn-danger w-100" onclick="return confirm('Delete this member?')" href="/easychair/admin/committee-media/delete?id=<?= (int)$m['id'] ?>">Delete</a>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        <?php endforeach; ?>

        <?php if (!$members): ?>
            <div class="col-12">
                <div class="alert alert-info">No committee members yet.</div>
            </div>
        <?php endif; ?>
    </div>
</div>
