<?php
function rev_h($v) { return htmlspecialchars((string)$v, ENT_QUOTES, 'UTF-8'); }
function rev_t($key, $fallback) {
    if (function_exists('ec_t')) return ec_t($key, $fallback);
    if (function_exists('t')) return t($key, $fallback);
    return $fallback;
}
function rev_status($v) {
    return function_exists('ec_review_status_t') ? ec_review_status_t($v) : (string)$v;
}

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

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

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

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

$assignments = $assignments ?? [];
$stats = $stats ?? [];


?>

<style>

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

</style>

<div class="container-fluid">
    
<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
? 'مساحة المحكّم'
: 'Reviewer Workspace' ?>

</div>

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

<?= rev_h(
$_SESSION['conference_short_name']
?? 'Conference'
) ?>

</h2>

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

<?= $isArabic
? 'إدارة عمليات التحكيم العلمي'
: 'Scientific Review Management Portal' ?>

</div>

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

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

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

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

<?= $isArabic
? 'تكليف'
: 'Assignments' ?>

</div>

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

<i class="fa fa-hourglass-half"></i>

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

<?= $isArabic
? 'قيد الانتظار'
: 'Pending' ?>

</div>

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

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

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

<?= $isArabic
? 'مكتمل'
: 'Completed' ?>

</div>

</div>

</div>

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

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

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

<?= $isArabic
? 'إدارة التكليفات'
: 'Manage Assignments' ?>

</a>

<a
class="btn btn-outline-light rounded-pill fw-bold"
href="/easychair/reviewer/history"
>

<?= $isArabic
? 'سجل التحكيم'
: 'Review History' ?>

</a>

</div>

</div>

</div>

</div>

</div>

<?php

$today =
    date('Y-m-d');

$overdueCount = 0;

foreach (($assignments ?? []) as $a) {

    if (
        !empty($a['due_date'])
        &&
        ($a['status'] ?? '') !== 'completed'
        &&
        $a['due_date'] < $today
    ) {
        $overdueCount++;
    }
}

?>    

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

$cards = [

[
'reviewer.dashboard.total_assignments',
'Total Assignments',
(int)($stats['total'] ?? 0),
'fa-list-check'
],

[
'reviewer.dashboard.pending_assignments',
'Pending Assignments',
(int)($stats['assigned'] ?? 0),
'fa-hourglass-half'
],

[
'reviewer.dashboard.completed_reviews',
'Completed Reviews',
(int)($stats['completed'] ?? 0),
'fa-circle-check'
],

[
'reviewer.dashboard.late_reviews',
'Late Reviews',
$overdueCount,
'fa-triangle-exclamation'
],

];        

        foreach ($cards as $c):
        ?>
            <div class="col-lg-3 col-md-6">
                <div class="card border-0 shadow-sm h-100" style="border-radius:20px;">
                    <div class="card-body p-4 d-flex justify-content-between align-items-center">
                        <div>
                            <div class="text-muted small fw-bold"><?= rev_h(rev_t($c[0],$c[1])) ?></div>
                            <div class="fs-2 fw-black fw-bold"><?= (int)$c[2] ?></div>
                        </div>
                        <div class="rounded-circle d-flex align-items-center justify-content-center" style="width:52px;height:52px;background:#eef2ff;color:#1d4ed8;">
                            <i class="fa <?= rev_h($c[3]) ?>"></i>
                        </div>
                    </div>
                </div>
            </div>
        <?php endforeach; ?>


    </div>

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

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

<i class="fa fa-triangle-exclamation"></i>

<?= $isArabic
? "لديك {$overdueCount} مراجعة متأخرة."
: "You have {$overdueCount} overdue reviews." ?>

</div>

<?php endif; ?>

<?php if ((int)($stats['assigned'] ?? 0) > 0): ?>

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

<i class="fa fa-hourglass-half"></i>

<?= $isArabic
? 'هناك مراجعات بانتظار الإكمال.'
: 'Pending reviews require completion.' ?>

</div>

<?php endif; ?>
    
<div class="card border-0 shadow-sm" style="border-radius:22px;">
        <div class="card-body p-4">
            <div class="d-flex justify-content-between align-items-center flex-wrap gap-2 mb-3">
                <h5 class="fw-bold mb-0"><?= rev_h(rev_t('reviewer.assignments.title','Review Assignments')) ?></h5>
                <a class="btn btn-primary rounded-pill" href="/easychair/reviewer/assignments">
                    <?= rev_h(rev_t('action.view','View')) ?>
                </a>
            </div>

            <div class="table-responsive">
                <table class="table table-bordered align-middle">
                    <thead class="table-dark">
                    <tr>
                        <th><?= rev_h(rev_t('reviewer.assignment.submission_no','Submission No.')) ?></th>
                        <th><?= rev_h(rev_t('reviewer.assignment.paper_title','Paper Title')) ?></th>
                        <th><?= rev_h(rev_t('reviewer.assignment.review_status','Review Status')) ?></th>
                        <th><?= rev_h(rev_t('reviewer.assignment.deadline','Deadline')) ?></th>
                        <th><?= rev_h(rev_t('submission.actions','Actions')) ?></th>
                    </tr>
                    </thead>
                    <tbody>
                    <?php foreach (array_slice($assignments, 0, 10) as $a): ?>
                        <tr>

                            <td>
    #<?= (int)($a['submission_id'] ?? 0) ?>
</td>

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

<?= rev_h(
($a['title'] ?? '')
?: ($a['title_ar'] ?? '')
) ?>

</div>

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

<span class="badge bg-primary">

<?= $isArabic
? 'جولة'
: 'Round' ?>

#<?= (int)(
$a['current_round_no']
?? 1
) ?>

</span>

<?php if (($a['status'] ?? '') === 'completed'): ?>

<span class="badge bg-success">

<?= $isArabic
? 'مكتمل'
: 'Completed' ?>

</span>

<?php endif; ?>

</div>
                            </td>
                            <td style="min-width:180px;">

<?php

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

$progress = match ($status) {

    'assigned' => 25,

    'accepted' => 45,

    'completed' => 100,

    'overdue' => 15,

    default => 20
};

?>

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

<?= rev_h(
rev_status(
$a['status']
?? 'assigned'
)
) ?>

</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><?= rev_h($a['due_date'] ?? '') ?></td>
                            <td>
			<?php if (($a['status'] ?? '') !== 'completed'): ?>

<a
href="/easychair/reviewer/review?assignment_id=<?= (int)$a['id'] ?>"
class="btn btn-primary btn-sm"
>
Review
</a>

<?php else: ?>

<span
class="badge bg-success"
>
Completed
</span>

<?php endif; ?>

                            </td>
                        </tr>
                    <?php endforeach; ?>

                    <?php if (!$assignments): ?>
                        <tr>
                            <td colspan="5" class="text-center text-muted py-4">
                                <?= rev_h(rev_t('reviewer.assignments.no_assignments','No review assignments found.')) ?>
                            </td>
                        </tr>
                    <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
