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

$submission = $submission ?? [];
$rule = $rule ?? [];
$versions = $versions ?? [];
$reviews = $reviews ?? [];
$rebuttals = $rebuttals ?? [];
$activity = $activity ?? [];

$title = ($submission['title'] ?? '') ?: (($submission['title_en'] ?? '') ?: (($submission['title_ar'] ?? '') ?: ('Submission #' . ($submission['id'] ?? ''))));
?>

<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">Author Workflow Center</h3>
            <div class="opacity-75">
                <?= aw_h($submission['submission_no'] ?? '') ?> — <?= aw_h($title) ?>
            </div>
            <div class="mt-3">
                <span class="badge bg-light text-dark"><?= aw_h($submission['status'] ?? '') ?></span>
                <span class="badge bg-warning text-dark"><?= aw_h($submission['stage'] ?? '') ?></span>
                <span class="badge bg-info text-dark"><?= aw_h($submission['production_status'] ?? '') ?></span>
            </div>
        </div>
    </div>

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

                    <div class="d-flex flex-column gap-2 mt-3">
                        <span class="badge <?= !empty($rule['allow_author_update']) ? 'bg-success' : 'bg-secondary' ?> p-2">
                            Metadata update: <?= !empty($rule['allow_author_update']) ? 'Allowed' : 'Locked' ?>
                        </span>
                        <span class="badge <?= !empty($rule['allow_author_file_upload']) ? 'bg-success' : 'bg-secondary' ?> p-2">
                            Revision upload: <?= !empty($rule['allow_author_file_upload']) ? 'Allowed' : 'Locked' ?>
                        </span>
                        <span class="badge <?= !empty($rule['allow_rebuttal']) ? 'bg-success' : 'bg-secondary' ?> p-2">
                            Rebuttal: <?= !empty($rule['allow_rebuttal']) ? 'Allowed' : 'Locked' ?>
                        </span>
                        <span class="badge <?= !empty($rule['allow_camera_ready']) ? 'bg-success' : 'bg-secondary' ?> p-2">
                            Camera-ready: <?= !empty($rule['allow_camera_ready']) ? 'Allowed' : 'Locked' ?>
                        </span>
                        <span class="badge <?= !empty($rule['visible_reviews_to_author']) ? 'bg-success' : 'bg-secondary' ?> p-2">
                            Reviews visible: <?= !empty($rule['visible_reviews_to_author']) ? 'Yes' : 'No' ?>
                        </span>
                    </div>
                </div>
            </div>
        </div>

        <div class="col-lg-8">
            <div class="card border-0 shadow-sm h-100" style="border-radius:22px;">
                <div class="card-body p-4">
                    <h5 class="fw-bold">Next Step</h5>

                    <?php if (!empty($rule['allow_author_file_upload'])): ?>
                        <div class="alert alert-warning">A revision is required. Upload your revised manuscript below.</div>
                    <?php elseif (!empty($rule['allow_camera_ready'])): ?>
                        <div class="alert alert-success">Your paper is accepted. Please upload camera-ready files.</div>
                    <?php elseif (!empty($rule['allow_rebuttal'])): ?>
                        <div class="alert alert-info">You may submit a rebuttal response to reviewer comments.</div>
                    <?php else: ?>
                        <div class="alert alert-secondary">No author action is currently required. Please wait for conference updates.</div>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    </div>

    <?php if (!empty($rule['allow_author_file_upload'])): ?>
        <div class="card border-0 shadow-sm mb-4" style="border-radius:22px;">
            <div class="card-body p-4">
                <h5 class="fw-bold mb-3">Upload Revision</h5>
                <form method="post" action="/easychair/author/workflow/revision" enctype="multipart/form-data" class="row g-2">
                    <input type="hidden" name="submission_id" value="<?= (int)$submission['id'] ?>">
                    <div class="col-md-5">
                        <input type="file" name="file" class="form-control" required>
                    </div>
                    <div class="col-md-5">
                        <input name="notes" class="form-control" placeholder="Notes to chairs / editor">
                    </div>
                    <div class="col-md-2">
                        <button class="btn btn-success w-100">Upload</button>
                    </div>
                </form>
            </div>
        </div>
    <?php endif; ?>

    <?php if (!empty($rule['allow_camera_ready'])): ?>
        <div class="card border-0 shadow-sm mb-4" style="border-radius:22px;">
            <div class="card-body p-4">
                <h5 class="fw-bold mb-3">Upload Camera-ready File</h5>
                <form method="post" action="/easychair/author/workflow/camera-ready" enctype="multipart/form-data" class="row g-2">
                    <input type="hidden" name="submission_id" value="<?= (int)$submission['id'] ?>">
                    <div class="col-md-5">
                        <input type="file" name="file" class="form-control" required>
                    </div>
                    <div class="col-md-5">
                        <input name="notes" class="form-control" placeholder="Final file notes">
                    </div>
                    <div class="col-md-2">
                        <button class="btn btn-success w-100">Upload</button>
                    </div>
                </form>
            </div>
        </div>
    <?php endif; ?>

    <?php if (!empty($rule['allow_rebuttal'])): ?>
        <div class="card border-0 shadow-sm mb-4" style="border-radius:22px;">
            <div class="card-body p-4">
                <h5 class="fw-bold mb-3">Submit Rebuttal</h5>
                <form method="post" action="/easychair/author/workflow/rebuttal">
                    <input type="hidden" name="submission_id" value="<?= (int)$submission['id'] ?>">
                    <textarea name="rebuttal_text" class="form-control mb-2" rows="6" required placeholder="Write your rebuttal response..."></textarea>
                    <button class="btn btn-primary">Submit Rebuttal</button>
                </form>
            </div>
        </div>
    <?php endif; ?>

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

                <?php foreach ($reviews as $rv): ?>
                    <div class="border rounded-4 p-3 mb-2">
                        <div class="fw-bold">
                            Score:
                            <?= aw_h($rv['overall_score'] ?? $rv['score'] ?? '-') ?>
                        </div>
                        <div class="text-muted small">Review #<?= (int)$rv['id'] ?></div>
                        <div class="mt-2">
                            <?= nl2br(aw_h($rv['comments_to_author'] ?? $rv['comments'] ?? $rv['review_text'] ?? '')) ?>
                        </div>
                    </div>
                <?php endforeach; ?>
            </div>
        </div>
    <?php endif; ?>

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

                    <div class="table-responsive">
                        <table class="table table-bordered align-middle">
                            <thead class="table-dark">
                                <tr>
                                    <th>Type</th>
                                    <th>Version</th>
                                    <th>File</th>
                                    <th>Date</th>
                                </tr>
                            </thead>
                            <tbody>
                            <?php foreach ($versions as $v): ?>
                                <tr>
                                    <td><span class="badge bg-primary"><?= aw_h($v['upload_type'] ?? 'submission') ?></span></td>
                                    <td><?= (int)$v['version_no'] ?></td>
                                    <td>
                                        <?php if (!empty($v['file_path'])): ?>
                                            <a target="_blank" href="/easychair/storage/<?= aw_h($v['file_path']) ?>">
                                                <?= aw_h($v['original_filename'] ?: $v['file_path']) ?>
                                            </a>
                                        <?php endif; ?>
                                    </td>
                                    <td><?= aw_h($v['created_at']) ?></td>
                                </tr>
                            <?php endforeach; ?>

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

                </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 p-4">
                    <h5 class="fw-bold mb-3">Activity Log</h5>

                    <?php foreach ($activity as $a): ?>
                        <div class="border-start border-4 border-primary ps-3 mb-3">
                            <div class="fw-bold"><?= aw_h($a['event_title']) ?></div>
                            <div class="text-muted small"><?= aw_h($a['event_key']) ?> | <?= aw_h($a['created_at']) ?></div>
                            <?php if (!empty($a['event_body'])): ?>
                                <div><?= aw_h($a['event_body']) ?></div>
                            <?php endif; ?>
                        </div>
                    <?php endforeach; ?>

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

</div>
