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

$submission = $submission ?? [];
$versions = $versions ?? [];
$activity = $activity ?? [];
$authorReviews = $authorReviews ?? [];

function sh_badge($status) {
    return match ((string)$status) {
        'accepted','published' => 'bg-success',
        'rejected' => 'bg-danger',
        'revision_required','revised' => 'bg-warning text-dark',
        'under_review' => 'bg-primary',
        default => 'bg-secondary',
    };
}
?>

<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">
            <div class="opacity-75"><?= sh_h($submission['short_name'] ?? '') ?></div>
            <h3 class="fw-bold mb-1"><?= sh_h($submission['title'] ?: $submission['title_en'] ?: $submission['title_ar']) ?></h3>
            <div class="opacity-75"><?= sh_h($submission['submission_no']) ?></div>
        </div>
    </div>

    <div class="row g-3 mb-4">
        <div class="col-lg-8">
            <div class="card border-0 shadow-sm h-100" style="border-radius:22px;">
                <div class="card-body">
                    <h5 class="fw-bold">Submission Details</h5>

                    <table class="table table-bordered">
                        <tr><th>Status</th><td><span class="badge <?= sh_badge($submission['status']) ?>"><?= sh_h($submission['status']) ?></span></td></tr>
                        <tr><th>Stage</th><td><?= sh_h($submission['stage']) ?></td></tr>
                        <tr><th>Decision</th><td><?= sh_h($submission['decision'] ?: '-') ?></td></tr>
                        <tr><th>Recommended Decision</th><td><?= sh_h($submission['recommended_decision'] ?: '-') ?></td></tr>
                        <tr><th>Average Score</th><td><?= sh_h($submission['avg_score'] ?: '-') ?></td></tr>
                        <tr><th>Round</th><td><?= (int)$submission['current_round_no'] ?></td></tr>
                        <tr><th>Version</th><td><?= (int)$submission['current_version'] ?></td></tr>
                    </table>

                    <h6 class="fw-bold mt-4">Abstract</h6>
                    <p><?= nl2br(sh_h($submission['abstract_ar'] ?: $submission['abstract'] ?: $submission['abstract_en'])) ?></p>

                    <?php if (!empty($submission['file_path'])): ?>
                        <a class="btn btn-outline-primary" target="_blank" href="/easychair/storage/<?= sh_h($submission['file_path']) ?>">
                            Original File
                        </a>
                    <?php endif; ?>

                    <?php if (!empty($submission['revision_file'])): ?>
                        <a class="btn btn-outline-warning" target="_blank" href="/easychair/storage/<?= sh_h($submission['revision_file']) ?>">
                            Latest Revision
                        </a>
                    <?php endif; ?>
                </div>
            </div>
        </div>

        <div class="col-lg-4">
            <div class="card border-0 shadow-sm h-100" style="border-radius:22px;">
                <div class="card-body">
                    <h5 class="fw-bold">Upload Revision</h5>
                    <div class="text-muted small mb-3">
                        Upload a revised version when requested by the chair/editor.
                    </div>

                    <form method="post" action="/easychair/author/submission/revision" enctype="multipart/form-data">
                        <input type="hidden" name="submission_id" value="<?= (int)$submission['id'] ?>">
                        <textarea name="revision_note" class="form-control mb-2" placeholder="Revision note / response to reviewers"></textarea>
                        <input type="file" name="revision_file" class="form-control mb-2" accept=".pdf,.doc,.docx" required>
                        <button class="btn btn-success w-100">Upload Revision</button>
                    </form>
                </div>
            </div>
        </div>
    </div>

    <?php if (!empty($authorReviews)): ?>
    <div class="card border-0 shadow-sm mb-4" style="border-radius:22px;">
        <div class="card-body">
            <h5 class="fw-bold">Reviews visible to author</h5>

            <?php foreach ($authorReviews as $r): ?>
                <div class="border rounded-4 p-3 mb-2">
                    <div class="d-flex justify-content-between">
                        <strong><?= sh_h($r['recommendation'] ?: '-') ?></strong>
                        <span class="badge bg-primary"><?= sh_h($r['overall_score'] ?: $r['score'] ?: '-') ?></span>
                    </div>
                    <div class="text-muted small"><?= sh_h($r['submitted_at'] ?: $r['completed_at']) ?></div>
                    <div class="mt-2"><?= nl2br(sh_h($r['comments_to_author'] ?: 'No comments.')) ?></div>
                </div>
            <?php endforeach; ?>
        </div>
    </div>
    <?php endif; ?>

    
    <?php if (!empty($coauthors)): ?>
    <div class="card border-0 shadow-sm mb-4" style="border-radius:22px;">
        <div class="card-body">
            <h5 class="fw-bold">Co-authors</h5>
            <table class="table table-bordered">
                <thead class="table-light"><tr><th>Name</th><th>Email</th><th>Affiliation</th><th>Country</th></tr></thead>
                <tbody>
                <?php foreach ($coauthors as $ca): ?>
                    <tr>
                        <td><?= sh_h($ca['full_name']) ?></td>
                        <td><?= sh_h($ca['email']) ?></td>
                        <td><?= sh_h($ca['affiliation']) ?></td>
                        <td><?= sh_h($ca['country']) ?></td>
                    </tr>
                <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </div>
    <?php endif; ?>

    <?php if (!empty($files)): ?>
    <div class="card border-0 shadow-sm mb-4" style="border-radius:22px;">
        <div class="card-body">
            <h5 class="fw-bold">Submission Files</h5>
            <table class="table table-bordered">
                <thead class="table-light"><tr><th>Category</th><th>File</th><th>Size</th><th>Date</th></tr></thead>
                <tbody>
                <?php foreach ($files as $f): ?>
                    <tr>
                        <td><span class="badge bg-primary"><?= sh_h($f['category']) ?></span></td>
                        <td><a target="_blank" href="/easychair/storage/<?= sh_h($f['file_path']) ?>"><?= sh_h($f['original_filename']) ?></a></td>
                        <td><?= (int)($f['file_size'] ?? 0) ?></td>
                        <td><?= sh_h($f['created_at']) ?></td>
                    </tr>
                <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </div>
    <?php endif; ?>

    <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">Versions</h5>
                    <table class="table table-bordered">
                        <thead class="table-light">
                        <tr><th>Version</th><th>Type</th><th>File</th><th>Date</th></tr>
                        </thead>
                        <tbody>
                        <?php foreach ($versions as $v): ?>
                            <tr>
                                <td><?= (int)$v['version_no'] ?></td>
                                <td><?= sh_h($v['upload_type']) ?></td>
                                <td>
                                    <?php if (!empty($v['file_path'])): ?>
                                        <a target="_blank" href="/easychair/storage/<?= sh_h($v['file_path']) ?>"><?= sh_h($v['original_filename'] ?: 'file') ?></a>
                                    <?php endif; ?>
                                </td>
                                <td><?= sh_h($v['created_at']) ?></td>
                            </tr>
                        <?php endforeach; ?>
                        <?php if (!$versions): ?>
                            <tr><td colspan="4" class="text-center text-muted">No versions.</td></tr>
                        <?php endif; ?>
                        </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">Activity Timeline</h5>

                    <?php foreach ($activity as $a): ?>
                        <div class="border-start border-3 ps-3 mb-3">
                            <strong><?= sh_h($a['event_title']) ?></strong>
                            <div class="text-muted small"><?= sh_h($a['created_at']) ?> | <?= sh_h($a['event_key']) ?></div>
                            <div class="small"><?= sh_h($a['event_body']) ?></div>
                        </div>
                    <?php endforeach; ?>

                    <?php if (!$activity): ?>
                        <div class="text-muted">No activity yet.</div>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    </div>

</div>
