<?php if (!empty($_SESSION['success'])): ?>
<div class="alert alert-success">
<?= e($_SESSION['success']) ?>
</div>
<?php unset($_SESSION['success']); ?>
<?php endif; ?>

<?php if (!empty($_SESSION['error'])): ?>
<div class="alert alert-danger">
<?= e($_SESSION['error']) ?>
</div>
<?php unset($_SESSION['error']); ?>
<?php endif; ?>
<?php 
function ad_h($v) {
    return htmlspecialchars((string)$v, ENT_QUOTES, 'UTF-8');
}

$submissions = $submissions ?? [];
$stats = $stats ?? [];
$conference = $conference ?? [];
$workflow = $workflow ?? [];

$confTitle = ($conference['title_ar'] ?? '') ?: (($conference['title_en'] ?? '') ?: 'Conference');

/*
|--------------------------------------------------------------------------
| LANGUAGE MODE
|--------------------------------------------------------------------------
*/

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

$isArabic =
    $lang === 'ar';

$dir =
    $isArabic
        ? 'rtl'
        : 'ltr';

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

<style>
.author-kpi-card{
    border:0;
    border-radius:20px;
    box-shadow:0 14px 35px rgba(15,23,42,.08);
}
.author-kpi-label{
    color:#64748b;
    font-size:13px;
    font-weight:700;
}
.author-kpi-value{
    font-size:30px;
    font-weight:900;
    color:#0f172a;
}
.workflow-pill{
    display:inline-flex;
    align-items:center;
    border-radius:999px;
    padding:7px 12px;
    margin:3px;
    font-weight:800;
    background:#eff6ff;
    color:#1e3a8a;
    border:1px solid #dbeafe;
    font-size:13px;
}
.workflow-pill.off{
    background:#f1f5f9;
    color:#64748b;
    border-color:#e2e8f0;
}
.paper-card{
    border:0;
    border-radius:22px;
    box-shadow:0 14px 40px rgba(15,23,42,.08);
}

.meta-chip{
padding:10px 18px;
border-radius:999px;
font-weight:700;
display:inline-flex;
align-items:center;
gap:8px;
}

</style>

<div class="container-fluid ec-author-dashboard-responsive py-3">

<div
class="card border-0 shadow-sm mb-4 overflow-hidden"
style="
border-radius:28px;
background:
linear-gradient(
135deg,
#0f172a,
#1d4ed8
);
color:#fff;
"
>

<div class="card-body p-5 position-relative">

<div
style="
position:absolute;
top:-60px;
right:-60px;
width:220px;
height:220px;
background:rgba(255,255,255,.08);
border-radius:50%;
"
></div>

<div class="position-relative z-2">

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

<div>

<div class="opacity-75 mb-2">

<?= $isArabic
? 'مساحة المؤلف'
: 'Author Workspace' ?>

</div>

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

<?= ad_h($confTitle) ?>

</h2>

<div class="opacity-75 fs-5">

<?= $isArabic
? 'إدارة الأبحاث والتحكيم والنسخ النهائية'
: 'Research Submission & Review Management' ?>

</div>

<div class="mt-4 d-flex flex-wrap gap-3">

<div class="meta-chip text-dark bg-white">

<i class="fa fa-file-lines"></i>

<?= (int)($stats['total'] ?? 0) ?>

<?= $isArabic
? 'بحث'
: 'Submissions' ?>

</div>

<div class="meta-chip text-dark bg-white">

<i class="fa fa-check-circle"></i>

<?= (int)($stats['accepted'] ?? 0) ?>

<?= $isArabic
? 'مقبول'
: 'Accepted' ?>

</div>

<div class="meta-chip text-dark bg-white">

<i class="fa fa-rotate"></i>

<?= (int)($stats['revision_required'] ?? 0) ?>

<?= $isArabic
? 'تعديل'
: 'Revisions' ?>

</div>

</div>

</div>

<div class="d-flex flex-column gap-2">

<a
class="btn btn-light btn-lg rounded-pill fw-bold"
href="/easychair/author/submit"
>

<i class="fa fa-file-circle-plus"></i>

<?= $isArabic
? 'إرسال بحث جديد'
: 'Submit New Paper' ?>

</a>

<a
class="btn btn-outline-light rounded-pill fw-bold"
href="/easychair/conference/<?= urlencode(
$_SESSION['conference_slug']
?? (
$conference['slug']
?? ('conference-' . (int)($_SESSION['conference_id'] ?? 0))
)
) ?>"
>

<?= $isArabic
? 'صفحة المؤتمر'
: 'Conference Homepage' ?>

</a>

</div>

</div>

</div>

</div>

</div>    

<?php

$deadlineCards = [];

if (!empty($conference['submission_deadline'])) {

    $deadlineCards[] = [
        'icon'  => 'fa-file-circle-plus',
        'title' => $isArabic
            ? 'آخر موعد للتقديم'
            : 'Submission Deadline',
        'date'  => $conference['submission_deadline'],
        'class' => 'primary'
    ];
}

if (!empty($conference['review_deadline'])) {

    $deadlineCards[] = [
        'icon'  => 'fa-user-check',
        'title' => $isArabic
            ? 'آخر موعد للتحكيم'
            : 'Review Deadline',
        'date'  => $conference['review_deadline'],
        'class' => 'warning'
    ];
}

if (!empty($conference['camera_ready_deadline'])) {

    $deadlineCards[] = [
        'icon'  => 'fa-upload',
        'title' => $isArabic
            ? 'آخر موعد للنسخة النهائية'
            : 'Camera Ready Deadline',
        'date'  => $conference['camera_ready_deadline'],
        'class' => 'success'
    ];
}

?>

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

<div class="row g-3 mb-4">

<?php foreach ($deadlineCards as $d): ?>

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

<div
class="card border-0 shadow-sm h-100"
style="border-radius:22px;"
>

<div class="card-body">

<div class="d-flex align-items-center gap-3">

<div
class="rounded-circle bg-<?= ad_h($d['class']) ?> text-white d-flex align-items-center justify-content-center"
style="
width:58px;
height:58px;
font-size:22px;
"
>

<i class="fa <?= ad_h($d['icon']) ?>"></i>

</div>

<div>

<div class="fw-bold">

<?= ad_h($d['title']) ?>

</div>

<div class="text-muted">

<?= ad_h($d['date']) ?>

</div>

</div>

</div>

</div>

</div>

</div>

<?php endforeach; ?>

</div>

<?php endif; ?>    

<div class="card border-0 shadow-sm mb-4" style="border-radius:22px;">

<div class="card-body">

<h5 class="fw-bold mb-4">

<?= $isArabic
? 'مراحل سير العمل'
: 'Workflow Progress' ?>

</h5>

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

<?php

$workflowLabels = [

'submission' => [
    'ar' => 'التقديم',
    'en' => 'Submission'
],

'screening' => [
    'ar' => 'الفحص',
    'en' => 'Screening'
],

'review' => [
    'ar' => 'التحكيم',
    'en' => 'Review'
],

'revision' => [
    'ar' => 'التعديل',
    'en' => 'Revision'
],

'decision' => [
    'ar' => 'القرار',
    'en' => 'Decision'
],

'production' => [
    'ar' => 'الإنتاج',
    'en' => 'Production'
],

'published' => [
    'ar' => 'النشر',
    'en' => 'Published'
],

];

?>

<?php foreach ($workflowLabels as $k => $label): ?>

<span class="workflow-pill <?= empty($workflow[$k]) ? 'off' : '' ?>">

<?= !empty($workflow[$k]) ? '●' : '○' ?>

<?= $isArabic
? $label['ar']
: $label['en'] ?>

</span>

<?php endforeach; ?>

</div>

</div>

</div>

    <div class="row g-3 mb-4">
        <?php
        $cards = [
            ['label'=>'كل التقديمات', 'value'=>$stats['total'] ?? 0],
            ['label'=>'مسودة', 'value'=>$stats['draft'] ?? 0],
            ['label'=>'تم التقديم', 'value'=>$stats['submitted'] ?? 0],
            ['label'=>'قيد التحكيم', 'value'=>$stats['under_review'] ?? 0],
            ['label'=>'تعديل مطلوب', 'value'=>$stats['revision_required'] ?? 0],
            ['label'=>'مقبول', 'value'=>$stats['accepted'] ?? 0],
        ];
        ?>
        <?php foreach ($cards as $c): ?>
            <div class="col-xl-2 col-md-4 col-6">
                <div class="card author-kpi-card h-100">
                    <div class="card-body">
                        <div class="author-kpi-label"><?= ad_h($c['label']) ?></div>
                        <div class="author-kpi-value"><?= (int)$c['value'] ?></div>
                    </div>
                </div>
            </div>
        <?php endforeach; ?>
    </div>

<?php

$pendingRevision =
    (int)($stats['revision_required'] ?? 0);

$acceptedCount =
    (int)($stats['accepted'] ?? 0);

?>

<?php if ($pendingRevision > 0): ?>

<div class="alert alert-warning border-0 shadow-sm mb-4" style="border-radius:18px;">

<i class="fa fa-rotate"></i>

<?= $isArabic
? "لديك {$pendingRevision} بحث يحتاج إلى تعديل."
: "You have {$pendingRevision} submissions requiring revision." ?>

</div>

<?php endif; ?>

<?php if ($acceptedCount > 0): ?>

<div class="alert alert-success border-0 shadow-sm mb-4" style="border-radius:18px;">

<i class="fa fa-check-circle"></i>

<?= $isArabic
? "تهانينا! لديك {$acceptedCount} بحث مقبول."
: "Congratulations! You have {$acceptedCount} accepted papers." ?>

</div>

<?php endif; ?>
    
<div class="card paper-card">
        <div class="card-body">
            <div class="d-flex justify-content-between align-items-center flex-wrap gap-2 mb-3">
                <h5 class="fw-bold mb-0">أبحاثي</h5>
                <a class="btn btn-sm btn-primary" href="/easychair/author/submit">
                    <i class="fa fa-plus"></i>
                    بحث جديد
                </a>
            </div>

            <div class="table-responsive">
                <table class="table table-bordered align-middle">
                    <thead class="table-dark">
                    <tr>
                        <th>#</th>
                        <th>رقم التقديم</th>
                        <th><?php echo ec_h(ec_t('submission.title','العنوان')); ?></th>
                        <th><?php echo ec_h(ec_t('submission.status','الحالة')); ?></th>
                        <th><?php echo ec_h(ec_t('submission.stage','المرحلة')); ?></th>
                        <th>القرار</th>
                        <th>الجولة</th>
                        <th>المراجعات</th>
                        <th>المعدل</th>
                        <th>الملفات</th>
                        <th>التاريخ</th>
                        <th>تفاصيل</th>
                    <th><?php echo ec_h(ec_t('author.dashboard.workflow','Workflow')); ?></th></tr>
                    </thead>
                    <tbody>

                    <?php foreach ($submissions as $s): ?>
                        <tr>
                            <td><?= (int)$s['id'] ?></td>
                            <td>
                                <span class="badge bg-light text-dark">
                                    <?= ad_h($s['submission_no'] ?? '-') ?>
                                </span>
                            </td>
                            <td style="min-width:260px;">
                                <strong>
<?= ec_h(
$s['display_title']
?? $s['title']
?? $s['title_en']
?? 'Submission #' . $s['id']
) ?>
</strong>
                                <?php if (!empty($s['keywords']) || !empty($s['keywords_ar'])): ?>
                                    <div class="text-muted small">
                                        <?= ad_h(($s['keywords_ar'] ?? '') ?: ($s['keywords'] ?? '')) ?>
                                    </div>
                                <?php endif; ?>
                            </td>
                            <td>
                                <span class="badge <?= status_badge_class($s['status'] ?? '') ?>">
                                    <?= ad_h($s['status'] ?? '-') ?>
                                </span>
                            </td>
                            <td style="min-width:180px;">

<?php

$status =
    strtolower(
        (string)(
            $s['status']
            ?? ''
        )
    );

$progress = match ($status) {

    'draft' => 10,

    'submitted' => 25,

    'screening' => 40,

    'under_review' => 60,

    'revision_required' => 75,

    'accepted' => 90,

    'published' => 100,

    default => 15
};

?>

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

<?= ad_h($s['stage'] ?? '-') ?>

</div>

<div class="progress" style="height:8px;border-radius:999px;">

<div
class="progress-bar"
style="width:<?= $progress ?>%"
></div>

</div>

<div class="small text-muted mt-1">

<?= $progress ?>%

</div>

</td>
                            <td><?= ad_h($s['decision'] ?? '-') ?></td>
                            <td><?= (int)($s['current_round_no'] ?? 1) ?></td>
                            <td>
                                <span class="badge bg-primary"><?= (int)($s['reviews_count'] ?? 0) ?></span>
                            </td>
                            <td>
                                <?= ad_h(($s['avg_score'] ?? '') ?: (($s['reviews_avg_score'] ?? '') ?: '-')) ?>
                            </td>
                            <td>
                                <div class="d-flex flex-column gap-1">
                                    <?php if (!empty($s['file_path'])): ?>
                                        <a class="btn btn-sm btn-outline-primary" target="_blank"
                                           href="/easychair/<?= ad_h($s['file_path']) ?>">
                                            الملف الأصلي
                                        </a>
                                    <?php endif; ?>

                                    <?php if (!empty($s['revision_file'])): ?>
                                        <a class="btn btn-sm btn-outline-warning" target="_blank"
                                           href="/easychair/<?= ad_h($s['revision_file']) ?>">
                                            ملف التعديل
                                        </a>
                                    <?php endif; ?>

                                    <?php if (!empty($s['response_letter_path'])): ?>
                                        <a class="btn btn-sm btn-outline-success" target="_blank"
                                           href="/easychair/<?= ad_h($s['response_letter_path']) ?>">
                                            خطاب الرد
                                        </a>
                                    <?php endif; ?>

                                    <?php if (empty($s['file_path']) && empty($s['revision_file']) && empty($s['response_letter_path'])): ?>
                                        <span class="text-muted small">لا يوجد</span>
                                    <?php endif; ?>
                                </div>
                            </td>
                            <td>
                                <div class="small">
                                    <?= ad_h($s['submitted_at'] ?: $s['created_at']) ?>
                                </div>
                            </td>
                            <td>
                                <a class="btn btn-sm btn-primary" href="/easychair/author/submission/show?id=<?= (int)$s['id'] ?>">عرض التفاصيل</a>
                            </td>
                        
<td>
<?php
$authorSubmissionRow = $s ?? $row ?? $submission ?? [];
$asid = (int)($authorSubmissionRow['id'] ?? $authorSubmissionRow['submission_id'] ?? 0);
if ($asid > 0):
?>
    <a class="btn btn-sm btn-primary rounded-pill" href="/easychair/author/workflow?submission_id=<?= $asid ?>"><?php echo ec_h(ec_t('author.dashboard.workflow','Workflow')); ?></a>
<?php endif; ?>
</td>
</tr>
                    <?php endforeach; ?>

                    <?php if (empty($submissions)): ?>
                        <tr>
                            <td colspan="12" class="text-center text-muted py-5">
                                لا توجد تقديمات لهذا المؤلف في المؤتمر الحالي.
                                <div class="mt-3">
                                    <a class="btn btn-primary" href="/easychair/author/submit">
                                        تقديم أول بحث
                                    </a>
                                </div>
                            </td>
                        </tr>
                    <?php endif; ?>

                    </tbody>
                </table>
            </div>
</div>
