<?php
function ri_h($v) {
    return htmlspecialchars((string)$v, ENT_QUOTES, 'UTF-8');
}
$assignment = $assignment ?? [];
$preference = $preference ?? [];
$discussions = $discussions ?? [];
?>

<div class="container py-4">

    <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">Reviewer Interaction</h3>
            <div class="opacity-75">
                <?= ri_h($assignment['submission_no'] ?? '') ?> —
                <?= ri_h($assignment['submission_title'] ?? '') ?>
            </div>
        </div>
    </div>

    <div class="card border-0 shadow-sm mb-4" style="border-radius:22px;">
        <div class="card-body p-4">
            <h5 class="fw-bold">Preference / Conflict</h5>

            <form method="post" action="/easychair/reviewer/interaction/preference">
                <input type="hidden" name="assignment_id" value="<?= (int)$assignment['id'] ?>">

                <div class="row g-2">
                    <div class="col-md-4">
                        <select name="preference" class="form-select">
                            <?php foreach (['want_review','can_review','neutral','not_interested','conflict'] as $p): ?>
                                <option value="<?= ri_h($p) ?>" <?= (($preference['preference'] ?? 'neutral') === $p) ? 'selected' : '' ?>>
                                    <?= ri_h($p) ?>
                                </option>
                            <?php endforeach; ?>
                        </select>
                    </div>

                    <div class="col-md-2">
                        <input type="number" name="expertise_score" min="0" max="5" class="form-control" placeholder="Expertise 0-5" value="<?= ri_h($preference['expertise_score'] ?? '') ?>">
                    </div>

                    <div class="col-md-4">
                        <input name="notes" class="form-control" placeholder="Notes / conflict reason" value="<?= ri_h($preference['notes'] ?? '') ?>">
                    </div>

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

    <div class="card border-0 shadow-sm" style="border-radius:22px;">
        <div class="card-body p-4">
            <h5 class="fw-bold">Reviewer Discussion</h5>

            <form method="post" action="/easychair/reviewer/interaction/discuss" class="mb-4">
                <input type="hidden" name="assignment_id" value="<?= (int)$assignment['id'] ?>">
                <select name="visibility" class="form-select mb-2" style="max-width:260px;">
                    <option value="reviewers_and_chairs">Reviewers and chairs</option>
                    <option value="chairs_only">Chairs only</option>
                </select>
                <textarea name="message" class="form-control mb-2" rows="4" placeholder="Write a discussion note..."></textarea>
                <button class="btn btn-primary">Post Discussion</button>
            </form>

            <?php foreach ($discussions as $d): ?>
                <div class="border rounded-4 p-3 mb-2">
                    <div class="fw-bold"><?= ri_h(trim($d['actor_name']) ?: $d['email']) ?></div>
                    <div class="text-muted small">
                        <?= ri_h($d['actor_role']) ?> |
                        <?= ri_h($d['visibility']) ?> |
                        <?= ri_h($d['created_at']) ?>
                    </div>
                    <div class="mt-2"><?= nl2br(ri_h($d['message'])) ?></div>
                </div>
            <?php endforeach; ?>

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