<?php
$lang =
    $_SESSION['lang']
    ?? 'en';

$isRtl =
    $lang === 'ar';
$title = "Submission Workspace";

$submission = $submission ?? [];
$authors = $authors ?? [];
$files = $files ?? [];
$reviews = $reviews ?? [];
$workflowTimeline = $workflowTimeline ?? [];

function esc($v)
{
    return ec_h($v ?? '');
}

$status =
    strtolower(
        $submission['status']
        ?? 'submitted'
    );

$badge =
    match ($status) {

        'accepted' => 'success',
        'rejected' => 'danger',
        'revision_required' => 'warning',
        'under_review' => 'primary',
        'revised' => 'info',

        default => 'secondary'
    };

$currentStage =
    $submission['stage']
    ?? 'submission';

?>

<style>

.author-hero{
    background:
        linear-gradient(
            135deg,
            #0f172a,
            #1e293b
        );
    color:#fff;
    border-radius:24px;
    padding:40px;
    margin-bottom:30px;
    position:relative;
    overflow:hidden;
}

.author-hero:before{
    content:'';
    position:absolute;
    top:-100px;
    right:-100px;
    width:260px;
    height:260px;
    border-radius:50%;
    background:rgba(255,255,255,.05);
}

.workspace-card{
    border:none;
    border-radius:22px;
    box-shadow:0 10px 35px rgba(0,0,0,.06);
    overflow:hidden;
}

.workflow-step{
    display:flex;
    gap:14px;
    margin-bottom:20px;
    position:relative;
}

.workflow-dot{
    width:18px;
    height:18px;
    border-radius:50%;
    background:#cbd5e1;
    margin-top:4px;
}

.workflow-dot.active{
    background:#22c55e;
    box-shadow:0 0 0 5px rgba(34,197,94,.15);
}

.review-score{
    font-size:22px;
    font-weight:800;
}

.criteria-box{
    border:1px solid #e2e8f0;
    border-radius:16px;
    padding:14px;
    margin-bottom:12px;
    background:#fff;
}

.section-title{
    font-size:20px;
    font-weight:800;
    margin-bottom:20px;
}

.meta-badge{
    border-radius:999px;
    padding:10px 16px;
    font-size:13px;
}

.version-row:hover{
    background:#f8fafc;
}

.author-card{
    border-radius:18px;
    border:1px solid #e2e8f0;
    padding:18px;
    height:100%;
}

.review-card{
    border:none;
    border-radius:20px;
    box-shadow:0 8px 30px rgba(0,0,0,.05);
}

</style>

<div class="container-fluid px-4 py-4">

<div class="author-hero">

<div class="row align-items-center">

<div class="col-lg-8">

<div class="d-flex flex-wrap gap-2 mb-3">

<span class="badge bg-light text-dark meta-badge">
Submission #<?= (int)$submission['id'] ?>
</span>

<span class="badge bg-<?= $badge ?> meta-badge">
<?= esc($status) ?>
</span>

<span class="badge bg-info meta-badge">
Round <?= (int)($submission['current_round_no'] ?? 1) ?>
</span>

</div>

<h1 class="fw-bold mb-3">

<?= esc(
    $submission['title_en']
    ?? $submission['title_ar']
    ?? $submission['title']
) ?>

</h1>

<div class="opacity-75">

Track:
<strong>

<?= esc(
    $submission['track_name_en']
    ?? $submission['track_name_ar']
    ?? '-'
) ?>

</strong>

</div>

</div>

<div class="col-lg-4 text-lg-end mt-4 mt-lg-0">

<a
href="/easychair/author/submission/edit?id=<?= (int)$submission['id'] ?>"
class="btn btn-warning btn-lg rounded-pill px-4">

Edit Submission

</a>

<?php if (($submission['status'] ?? '') === 'accepted'): ?>

<a
href="/easychair/author/submission/camera-ready?id=<?= (int)$submission['id'] ?>"
class="btn btn-success btn-lg rounded-pill px-4 mt-2">

Camera Ready

</a>

<?php endif; ?>

</div>

</div>

</div>

<div class="row g-4">

<div class="col-lg-3">

<div class="card workspace-card mb-4">

<div class="card-body">

<div class="section-title">
<?= $isRtl ? 'مسار العمل' : 'Workflow' ?>
</div>



<?php foreach ($workflowTimeline as $step): ?>

<?php

$active =
    $step['key'] === $status
    ||
    $step['key'] === $currentStage;

?>

<div class="workflow-step">

<div class="workflow-dot <?= $active ? 'active' : '' ?>">
</div>

<div>

<div class="fw-bold">
<?= esc($step['label']) ?>
</div>

<div class="small text-muted">
<?= $active ? 'Current Stage' : 'Pending' ?>
</div>

</div>

</div>

<?php endforeach; ?>

</div>

</div>

<div class="card workspace-card">

<div class="card-body">

<div class="section-title">
<?= $isRtl ? 'الملفات' : 'Files' ?>
</div>

<?php foreach ($files as $f): ?>

<div class="d-flex justify-content-between align-items-center mb-3 version-row p-2 rounded">

<div>

<div class="fw-bold">

<?= esc($f['upload_type']) ?>

</div>
<?= $isRtl ? 'التحديثات' : 'Versions' ?>
<div class="small text-muted">


<?= (int)($f['version_no'] ?? 1) ?>

</div>

</div>

<a
href="/easychair<?= esc($f['file_path']) ?>"
target="_blank"
class="btn btn-sm btn-outline-primary rounded-pill">

<?= $isRtl ? 'التحميل' : 'Download' ?>

</a>

</div>

<?php endforeach; ?>

</div>

</div>

</div>

<div class="col-lg-6">

<div class="card workspace-card mb-4">

<div class="card-body">


<!-- =======================================================
FILE CENTER
======================================================= -->

<div class="card border-0 shadow-sm rounded-4 mb-4">

<div class="card-header bg-dark text-white rounded-top-4">

<h5 class="mb-0">

<i class="fa-solid fa-folder-tree"></i>

<?= $isRtl
    ? 'مركز الملفات والإصدارات'
    : 'Submission File Center' ?>

</h5>

</div>

<div class="card-body">

<?php if (!empty($versions)): ?>

<div class="table-responsive">

<table class="table align-middle">

<thead>

<tr>

<th>
<?= $isRtl ? 'الجولة' : 'Round' ?>
</th>

<th>
<?= $isRtl ? 'الإصدار' : 'Version' ?>
</th>

<th>
<?= $isRtl ? 'النوع' : 'Type' ?>
</th>

<th>
<?= $isRtl ? 'الملف' : 'File' ?>
</th>

<th>
<?= $isRtl ? 'الرافع' : 'Uploaded By' ?>
</th>

<th>
<?= $isRtl ? 'التاريخ' : 'Date' ?>
</th>

</tr>

</thead>

<tbody>

<?php foreach ($versions as $v): ?>

<tr>

<td>

<span class="badge bg-primary rounded-pill">

#<?= (int)$v['round_no'] ?>

</span>

</td>

<td>

<?= (int)$v['version_no'] ?>

</td>

<td>

<?php
$typeColor = 'secondary';

if ($v['upload_type'] === 'revision') {
    $typeColor = 'warning';
}

if ($v['upload_type'] === 'camera_ready') {
    $typeColor = 'success';
}
?>

<span class="badge bg-<?= $typeColor ?> rounded-pill">

<?= esc($v['upload_type']) ?>

</span>

</td>

<td>

<a
href="/easychair<?= esc($v['file_path']) ?>"
target="_blank"
class="btn btn-outline-primary btn-sm rounded-pill"
>

<i class="fa-solid fa-download"></i>

<?= $isRtl
    ? 'تحميل'
    : 'Download' ?>

</a>

</td>

<td>

<?= esc($v['uploader_name']) ?>

</td>

<td>

<?= esc($v['created_at']) ?>

</td>

</tr>

<?php endforeach; ?>

</tbody>

</table>

</div>

<?php else: ?>

<div class="alert alert-light border rounded-4 mb-0">

<?= $isRtl
    ? 'لا توجد ملفات مرفوعة بعد'
    : 'No uploaded versions yet.' ?>

</div>

<?php endif; ?>

</div>

</div>


<div class="section-title">
<?= $isRtl ? 'نتائج التحكيم' : 'Review Results' ?>
</div>

<?php if (empty($reviews)): ?>

<div class="alert alert-info">

<?= $isRtl ? 'لا يوجد مراجعات' : 'Review Not Released Yet!!' ?>

</div>

<?php else: ?>

<?php

$currentRound = null;

foreach ($reviews as $review):

$reviewRound =
    (int)(
        $review['round_no']
        ?? 1
    );

if ($currentRound !== $reviewRound):

$currentRound = $reviewRound;

?>

<div class="review-round-divider mb-3 mt-4">

    <span class="badge bg-primary px-4 py-2 rounded-pill">

        <?= $isRtl
            ? 'جولة التحكيم رقم'
            : 'Review Round' ?>

        #<?= $reviewRound ?>

    </span>

</div>

<?php endif; ?>

<div class="alert alert-primary rounded-4 mt-4">

<h5 class="mb-0">

<?= $isRtl
    ? 'جولة التحكيم'
    : 'Review Round' ?>

#<?= $reviewRound ?>

</h5>

</div>


<div class="card review-card mb-4">

<div class="card-body">

<div class="d-flex justify-content-between align-items-center mb-3">

<div>

<div class="fw-bold">

<?= $isRtl ? 'جولات التحكيم' : 'Review Rounds' ?>
<?= (int)($review['round_no'] ?? 1) ?>

</div>

<div class="small text-muted">

<?= $isRtl ? 'نتائج التحكيم' : 'Review Feedbacks' ?>

</div>

</div>

<div class="review-score text-success">

<?= number_format(
    (float)($review['overall_score'] ?? 0),
    1
) ?>

</div>

</div>

<?php foreach (($review['criteria_scores'] ?? []) as $score): ?>

<div class="criteria-box">

<div class="d-flex justify-content-between align-items-center">

<div class="fw-semibold">

<?php

$lang =
    $_SESSION['lang']
    ?? 'en';

?>

<?= esc(
    $lang === 'ar'
    ? (
        $score['title_ar']
        ?? $score['title_en']
    )
    : (
        $score['title_en']
        ?? $score['title_ar']
    )
) ?>
</div>

<div class="badge bg-primary rounded-pill px-3 py-2">

<?= esc($score['score']) ?>
/
<?= esc($score['max_score']) ?>

</div>

</div>

</div>

<?php endforeach; ?>

<?php if (
    !empty($review['comments_to_author'])
    ||
    !empty($review['comments'])
): ?>

<div class="mt-3">

<div class="fw-bold mb-2">

<?= $isRtl
    ? 'ملاحظات المحكم'
    : 'Reviewer Comments' ?>

</div>

<div class="p-3 bg-light rounded-4">

<?= nl2br(
    esc(
        $review['comments_to_author']
        ?? $review['comments']
        ?? ''
    )
) ?>

</div>

</div>

<?php endif; ?>

<?php if (
    !empty($review['recommendation'])
): ?>

<div class="mt-3">

<span class="badge bg-warning text-dark rounded-pill px-3 py-2">

<?= $isRtl
    ? 'توصية المحكم: '
    : 'Reviewer Recommendation: ' ?>

<?= esc($review['recommendation']) ?>

</span>

</div>

<?php endif; ?>

<?php if (
    !empty($review['review_file_path'])
    ||
    !empty($review['file_path'])
): ?>

<div class="mt-3">

<a
href="/easychair<?= esc(
    $review['review_file_path']
    ?? $review['file_path']
) ?>"
target="_blank"
class="btn btn-outline-primary rounded-pill btn-sm"
>

<i class="fa-solid fa-file-arrow-down"></i>

<?= $isRtl
    ? 'تحميل ملف التحكيم'
    : 'Download Review File' ?>

</a>

</div>

<?php endif; ?>
</div>

</div>

<?php endforeach; ?>

<?php endif; ?>

</div>

</div>

<div class="card workspace-card">

<div class="card-body">

<div class="section-title">
<?= $isRtl ? 'رفع نسخة منقحة' : 'Upload New Version' ?>
</div>

<form
method="POST"
action="/easychair/author/submission/upload-revision"
enctype="multipart/form-data"
>

<input
type="hidden"
name="submission_id"
value="<?= (int)$submission['id'] ?>"
>

<div class="mb-3">

<label class="form-label">
<?= $isRtl ? 'اختر التصنيف' : 'Upload Type' ?>
</label>

<select
name="upload_type"
class="form-select"
required
>

<option value="revision">
<?= $isRtl ? 'نسخة منقحة' : 'Revised Version' ?>
</option>

<option value="response">
<?= $isRtl ? 'ملاحظات هامة' : 'Response Letter' ?>
</option>

<option value="camera_ready">
<?= $isRtl ? 'نسخة النشر المنقحة' : 'Camera Ready' ?>
</option>

<option value="supplement">
<?= $isRtl ? 'ملفات ووثائق اضافية' : 'Other' ?>
</option>

</select>
</div>

<div class="mb-3">

<label class="form-label">
<?= $isRtl ? 'اختر ملفا' : 'Select File' ?>
</label>

<input
type="file"
name="file"
class="form-control"
required
>

</div>

<div class="mb-3">

<label class="form-label">
<?= $isRtl ? 'ملاحظات اضافية' : 'Notes' ?>
</label>

<textarea
name="notes"
class="form-control"
rows="3"
></textarea>

</div>

<button class="btn btn-primary btn-lg rounded-pill px-4">

<?= $isRtl ? 'رفع نسخة منقحة' : 'Upload Version' ?>

</button>

</form>

</div>

</div>

</div>

<div class="col-lg-3">

<div class="card workspace-card mb-4">

<div class="card-body">

<div class="section-title">
<?= $isRtl ? 'باحثون مشاركون' : 'Co-Authors' ?>
</div>

<div class="row g-3">

<?php foreach ($authors as $a): ?>

<div class="col-12">

<div class="author-card">

<div class="fw-bold">

<?= esc($a['full_name']) ?>

</div>

<div class="small text-muted mb-2">

<?= esc($a['email']) ?>

</div>

<div class="small">

<?= esc($a['affiliation']) ?>

</div>

<?php if ($a['is_corresponding']): ?>

<div class="mt-2">

<span class="badge bg-success rounded-pill">

<?= $isRtl ? 'الباحث الرئيسي' : 'Corresponding Author' ?>

</span>

</div>

<?php endif; ?>

</div>

</div>

<?php endforeach; ?>

</div>

</div>

</div>

<div class="card workspace-card">

<div class="card-body">

<div class="section-title">
<?= $isRtl ? 'روابط سريعة' : 'Quick Actions' ?>
</div>

<div class="d-grid gap-3">

<a
href="/easychair/author/workflow?submission_id=<?= (int)$submission['id'] ?>"
class="btn btn-outline-primary rounded-pill">
<?= $isRtl ? 'مراحل النشر' : 'Workflow' ?>

</a>


<a
href="/easychair/author/dashboard"
class="btn btn-outline-secondary rounded-pill">

<?= $isRtl ? 'الرئيسية' : 'Dashboard' ?>

</a>

</div>

</div>

</div>

</div>

</div>

</div>
