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

$conference = $conference ?? [];
$submissions = $submissions ?? [];
$reviewers = $reviewers ?? [];
$assignments = $assignments ?? [];
?>

<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">
            <h3 class="fw-bold mb-1">توزيع المحكمين</h3>
            <div class="opacity-75"><?= aa_h(($conference['title_ar'] ?? '') ?: ($conference['title_en'] ?? '')) ?></div>
        </div>
    </div>

    <?php if(isset($_GET['msg'])): ?>
        <div class="alert alert-success">تم تنفيذ العملية: <?= aa_h($_GET['msg']) ?></div>
    <?php endif; ?>

    <div class="card border-0 shadow-sm mb-4" style="border-radius:22px;">
        <div class="card-body">
            <h5 class="fw-bold mb-3">تعيين محكم لبحث</h5>

            <form method="post" action="/easychair/admin/assignments/assign" class="row g-2">
                <div class="col-md-4">
                    <select name="submission_id" class="form-select" required>
                        <option value="">اختر البحث</option>
                        <?php foreach($submissions as $s): ?>
                            <option value="<?= (int)$s['id'] ?>">
                                <?= aa_h(($s['submission_no'] ?? '#'.$s['id']) . ' - ' . $s['title']) ?>
                            </option>
                        <?php endforeach; ?>
                    </select>
                </div>

                <div class="col-md-4">
                    <select name="reviewer_id" class="form-select" required>
                        <option value="">اختر المحكم</option>
                        <?php foreach($reviewers as $r): ?>
                            <option value="<?= (int)$r['id'] ?>">
                                <?= aa_h(trim(($r['first_name'] ?? '').' '.($r['last_name'] ?? '')).' - '.$r['email'].' | Load: '.$r['load_count']) ?>
                            </option>
                        <?php endforeach; ?>
                    </select>
                </div>

                <div class="col-md-2">
                    <input type="date" name="due_date" class="form-control">
                </div>

                <div class="col-md-1">
                    <input type="number" name="round_no" class="form-control" value="1" min="1">
                </div>

                <div class="col-md-1">
                    <button class="btn btn-success w-100">Assign</button>
                </div>
            </form>
        </div>
    </div>

    <div class="row g-3">
        <div class="col-lg-6">
            <div class="card border-0 shadow-sm h-100" style="border-radius:22px;">
                <div class="card-body">
                    <h5 class="fw-bold mb-3">الأبحاث</h5>

                    <table class="table table-bordered align-middle">
                        <thead class="table-dark">
                        <tr>
                            <th>ID</th>
                            <th>Title</th>
                            <th>Status</th>
                            <th>Assignments</th>
                            <th>Completed</th>
                        </tr>
                        </thead>
                        <tbody>
                        <?php foreach($submissions as $s): ?>
                            <tr>
                                <td><?= (int)$s['id'] ?></td>
                                <td><?= aa_h($s['title']) ?></td>
                                <td><span class="badge bg-secondary"><?= aa_h($s['status']) ?></span></td>
                                <td><?= (int)$s['assignments_count'] ?></td>
                                <td><?= (int)$s['completed_count'] ?></td>
                            </tr>
                        <?php endforeach; ?>
                        </tbody>
                    </table>

                </div>
            </div>
        </div>

        <div class="col-lg-6">
            <div class="card border-0 shadow-sm h-100" style="border-radius:22px;">
                <div class="card-body">
                    <h5 class="fw-bold mb-3">آخر التوزيعات</h5>

                    <table class="table table-bordered align-middle">
                        <thead class="table-dark">
                        <tr>
                            <th>ID</th>
                            <th>Submission</th>
                            <th>Reviewer</th>
                            <th>Status</th>
                            <th>Due</th>
                        </tr>
                        </thead>
                        <tbody>
                        <?php foreach($assignments as $a): ?>
                            <tr>
                                <td><?= (int)$a['id'] ?></td>
                                <td><?= aa_h($a['submission_title']) ?></td>
                                <td>
                                    <?= aa_h($a['reviewer_name']) ?>
                                    <div class="text-muted small"><?= aa_h($a['reviewer_email']) ?></div>
                                </td>
                                <td><span class="badge bg-primary"><?= aa_h($a['status']) ?></span></td>
                                <td><?= aa_h($a['due_date'] ?? '-') ?></td>
                            </tr>
                        <?php endforeach; ?>

                        <?php if(!$assignments): ?>
                            <tr><td colspan="5" class="text-center text-muted py-4">No assignments yet.</td></tr>
                        <?php endif; ?>
                        </tbody>
                    </table>

                </div>
            </div>
        </div>
    </div>

</div>
