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

<div class="d-flex justify-content-between align-items-center mb-4">
    <div>
        <h1 class="fw-bold mb-0"><?= e(trans('make_decision')) ?></h1>
        <div class="text-muted"><?= e((string) $submission['submission_no']) ?> — <?= e((string) $submission['title']) ?></div>
    </div>
    <a href="<?= e(base_path('submissions/show?id=' . (int) $submission['id'])) ?>" class="btn btn-outline-secondary rounded-3"><?= e(trans('back')) ?></a>
</div>

<div class="row g-4">
    <div class="col-lg-8">
        <div class="card border-0 shadow-sm rounded-4 mb-4">
            <div class="card-body">
                <div class="mb-2"><strong><?= e(trans('conference')) ?>:</strong> <?= e(current_lang() === 'ar' ? (string) $submission['title_ar'] : (string) $submission['title_en']) ?></div>
                <div class="mb-2"><strong><?= e(trans('tracks')) ?>:</strong> <?= e(current_lang() === 'ar' ? (string) $submission['name_ar'] : (string) $submission['name_en']) ?></div>
                <div class="mb-0"><strong><?= e(trans('submission_title')) ?>:</strong> <?= e((string) $submission['title']) ?></div>
            </div>
        </div>

        <div class="card border-0 shadow-sm rounded-4 mb-4">
            <div class="card-body">
                <h3 class="h5 fw-bold mb-3"><?= e(trans('review_summary')) ?></h3>
                <div class="table-responsive">
                    <table class="table align-middle mb-0">
                        <thead class="table-light">
                            <tr>
                                <th><?= e(trans('reviewer_user')) ?></th>
                                <th><?= e(trans('assignment_status')) ?></th>
                                <th><?= e(trans('overall_score')) ?></th>
                                <th><?= e(trans('recommendation')) ?></th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php if (!empty($assignments)): ?>
                                <?php foreach ($assignments as $row): ?>
                                    <tr>
                                        <td><?= e(trim((string) $row['first_name'] . ' ' . (string) $row['last_name'])) ?></td>
                                        <td><span class="badge bg-primary"><?= e((string) $row['status']) ?></span></td>
                                        <td><?= e((string) ($row['overall_score'] ?? '')) ?></td>
                                        <td><?= e((string) ($row['recommendation'] ?? '')) ?></td>
                                    </tr>
                                <?php endforeach; ?>
                            <?php else: ?>
                                <tr><td colspan="4" class="text-center py-4"><?= e(trans('no_data')) ?></td></tr>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>

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

                    <?php $curr = (string) ($currentDecision['decision'] ?? ''); ?>

                    <div class="mb-3">
                        <label class="form-label ec-required"><?= e(trans('final_decision')) ?></label>
                        <select name="decision" class="form-select rounded-3" required>
                            <option value="">--</option>
                            <option value="accept" <?= $curr === 'accept' ? 'selected' : '' ?>><?= e(trans('accept')) ?></option>
                            <option value="minor_revision" <?= $curr === 'minor_revision' ? 'selected' : '' ?>><?= e(trans('minor_revision')) ?></option>
                            <option value="major_revision" <?= $curr === 'major_revision' ? 'selected' : '' ?>><?= e(trans('major_revision')) ?></option>
                            <option value="reject" <?= $curr === 'reject' ? 'selected' : '' ?>><?= e(trans('reject')) ?></option>
                        </select>
                    </div>

                    <div class="mb-4">
                        <label class="form-label"><?= e(trans('chair_comments')) ?></label>
                        <textarea name="chair_comments" class="form-control rounded-3" rows="7"><?= e((string) ($currentDecision['chair_comments'] ?? '')) ?></textarea>
                    </div>

                    <button type="submit" class="btn btn-success rounded-3"><?= e(trans('save')) ?></button>
                </form>
            </div>
        </div>
    </div>

    <div class="col-lg-4">
        <div class="card border-0 shadow-sm rounded-4 mb-4">
            <div class="card-body">
                <div class="mb-2"><strong><?= e(trans('reviews_count')) ?>:</strong> <?= e((string) ($reviewStats['reviews_count'] ?? 0)) ?></div>
                <div class="mb-0"><strong><?= e(trans('average_score')) ?>:</strong> <?= e((string) ($reviewStats['average_score'] ?? '')) ?></div>
            </div>
        </div>

        <div class="card border-0 shadow-sm rounded-4">
            <div class="card-body">
                <h3 class="h5 fw-bold mb-3"><?= e(trans('decision_history')) ?></h3>
                <?php if (!empty($decisionHistory)): ?>
                    <div class="small">
                        <?php foreach ($decisionHistory as $h): ?>
                            <div class="border rounded-3 p-2 mb-2">
                                <div><strong><?= e((string) $h['decision']) ?></strong></div>
                                <div><?= e((string) $h['created_at']) ?></div>
                                <div><?= e(trim((string) $h['first_name'] . ' ' . (string) $h['last_name'])) ?></div>
                            </div>
                        <?php endforeach; ?>
                    </div>
                <?php else: ?>
                    <div class="alert alert-info rounded-4 mb-0"><?= e(trans('no_data')) ?></div>
                <?php endif; ?>
            </div>
        </div>
    </div>
</div>

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