<?php require __DIR__ . '/../layouts/header.php'; ?>

<div class="d-flex justify-content-between align-items-center mb-4">
    <div>
        <h1 class="page-title"><?= e(trans('review_assignments')) ?></h1>
        <div class="text-muted"><?= e((string) ($submission['submission_no'] ?? '')) ?> — <?= e((string) ($submission['title'] ?? '')) ?></div>
    </div>
    <a href="<?= e(base_path('reviews/assignments')) ?>" class="btn btn-outline-secondary rounded-3"><?= e(trans('back')) ?></a>
</div>

<div class="card border-0 shadow-sm rounded-4 mb-4">
    <div class="card-body p-4">
        <div class="section-title"><span class="dot"></span><h2 class="h5 mb-0"><?= e(trans('review_instructions')) ?></h2></div>
        <div>
            <?= current_lang() === 'ar'
                ? (string) ($submission['review_instructions_ar'] ?? '')
                : (string) ($submission['review_instructions_en'] ?? '') ?>
        </div>
    </div>
</div>

<div class="card border-0 shadow-sm rounded-4 mb-4">
    <div class="card-body p-4">
        <form method="post" action="<?= e(base_path('reviews/bulk-assign')) ?>">
            <input type="hidden" name="_csrf" value="<?= e(csrf_token()) ?>">
            <input type="hidden" name="submission_id" value="<?= e((string) $submission['id']) ?>">

            <div class="row g-3">
                <div class="col-md-8">
                    <label class="form-label ec-required"><?= e(trans('reviewer')) ?></label>
                    <select name="reviewer_ids[]" class="form-select rounded-3" multiple size="8" required>
                        <?php foreach ($reviewers as $reviewer): ?>
                            <option value="<?= e((string) $reviewer['id']) ?>">
                                <?= e(trim((string) $reviewer['first_name'] . ' ' . (string) $reviewer['last_name'])) ?> — <?= e((string) $reviewer['email']) ?>
                            </option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div class="col-md-4">
                    <label class="form-label"><?= e(trans('due_date')) ?></label>
                    <input type="date" name="due_date" class="form-control rounded-3">
                </div>
            </div>

            <div class="mt-4">
                <button type="submit" class="btn btn-success rounded-3"><i class="fa-solid fa-user-check"></i><?= e(trans('assign_selected_reviewers')) ?></button>
            </div>
        </form>
    </div>
</div>

<div class="card border-0 shadow-sm rounded-4">
    <div class="table-responsive">
        <table class="table align-middle mb-0">
            <thead>
                <tr>
                    <th>#</th>
                    <th><?= e(trans('reviewer')) ?></th>
                    <th><?= e(trans('status')) ?></th>
                    <th><?= e(trans('due_date')) ?></th>
                    <th><?= e(trans('reviewer_rating')) ?></th>
                    <th><?= e(trans('actions')) ?></th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($assignments as $a): ?>
                    <tr>
                        <td><?= e((string) $a['id']) ?></td>
                        <td><?= e(trim((string) ($a['first_name'] ?? '') . ' ' . (string) ($a['last_name'] ?? ''))) ?></td>
                        <td><?= e((string) ($a['status'] ?? '')) ?></td>
                        <td><?= e((string) ($a['due_date'] ?? '')) ?></td>
                        <td>
                            <form method="post" action="<?= e(base_path('reviews/rate-reviewer')) ?>" class="d-flex flex-column gap-2">
                                <input type="hidden" name="_csrf" value="<?= e(csrf_token()) ?>">
                                <input type="hidden" name="review_assignment_id" value="<?= e((string) $a['id']) ?>">
                                <input type="hidden" name="reviewer_id" value="<?= e((string) $a['reviewer_id']) ?>">
                                <input type="hidden" name="submission_id" value="<?= e((string) $submission['id']) ?>">
                                <input type="number" min="0" max="5" step="0.1" name="rating_score" class="form-control form-control-sm rounded-3" value="<?= e((string) ($a['rating_score'] ?? '')) ?>">
                                <input type="text" name="rating_notes" class="form-control form-control-sm rounded-3" value="<?= e((string) ($a['rating_notes'] ?? '')) ?>" placeholder="<?= e(trans('rating_notes')) ?>">
                                <button type="submit" class="btn btn-sm btn-outline-success rounded-3"><?= e(trans('save')) ?></button>
                            </form>
                        </td>
                        <td>
                            <a href="<?= e(base_path('reviews/form?assignment_id=' . (int) $a['id'])) ?>" class="btn btn-sm btn-outline-primary rounded-3"><?= e(trans('edit')) ?></a>
                        </td>
                    </tr>
                <?php endforeach; ?>
                <?php if (empty($assignments)): ?>
                    <tr><td colspan="6" class="text-center"><?= e(trans('no_data')) ?></td></tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

<?php require __DIR__ . '/../layouts/footer.php'; ?>
