<?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;
}

/* 🔥 SAFE DEFAULTS */
$assignments = $assignments ?? [];
$stats = $stats ?? [
    'total' => 0,
    'assigned' => 0,
    'completed' => 0,
    'late_reviews' => 0
];

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

function rev_status_class($s)
{
$s=strtolower((string)$s);

return match($s){

'completed'
=>
'bg-success',

'accepted'
=>
'bg-primary',

'declined'
=>
'bg-danger',

'overdue'
=>
'bg-danger',

default
=>
'bg-warning text-dark'

};
}

?>

<style>

.assignment-row:hover{
background:#fafcff;
}

.progress-review{
height:7px;
border-radius:999px;
}

.paper-meta{
font-size:12px;
color:#64748b;
}

</style>

<div class="container-fluid">


<?php

$rvTotal=
count(
$assignments
??
[]
);

$rvReady=0;

$rvOverdue=0;

$rvActive=0;

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

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

if(
in_array(
$st,
[
'completed',
'submitted'
],
true
)
){
$rvReady++;
}

if(
in_array(
$st,
[
'assigned',
'under_review'
],
true
)
){
$rvActive++;
}

if(
!empty(
$a[
'due_date'
]
)
&&
$a[
'due_date'
]
<
date(
'Y-m-d'
)
&&
$st!=='completed'
){
$rvOverdue++;
}

}

$rvHealth=
$rvTotal
?
round(
(
$rvReady
/
$rvTotal
)
*
100
)
:
0;

?>

<!-- PACK 684B -->

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

<div class="card-body">

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

<div>

<h4>

<?= rev_h(

rev_t(
'reviewer.assignments.intelligence',
'Assignment Intelligence'

)

) ?>

</h4>

<div class="text-muted">

Review Pipeline + Decision Readiness

</div>

</div>

<div
class="
display-5
fw-bold
text-primary
"
>

<?= $rvHealth ?>%

</div>

</div>

<div class="row g-3">

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

<div class="border rounded-4 p-4 text-center">

<div class="display-6">

<?= $rvTotal ?>

</div>

Total

</div>

</div>

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

<div class="border rounded-4 p-4 text-center">

<div class="display-6">

<?= $rvReady ?>

</div>

Ready

</div>

</div>

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

<div class="border rounded-4 p-4 text-center">

<div class="display-6">

<?= $rvActive ?>

</div>

Active

</div>

</div>

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

<div class="border rounded-4 p-4 text-center">

<div class="display-6 text-danger">

<?= $rvOverdue ?>

</div>

Risk

</div>

</div>

</div>

</div>

</div>

<!-- END PACK 684B -->


<?php

$rv=
$assignments
??
[];

$total=
count(
$rv
);

$round1=0;

$round2=0;

$completed=0;

$dueSoon=0;

foreach(
$rv
as
$a
){

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

if(
in_array(
$status,
[
'assigned',
'under_review'
],
true
)
){
$round1++;
}

if(
in_array(
$status,
[
'submitted',
'completed'
],
true
)
){
$round2++;
}

if(
$status==='completed'
){
$completed++;
}

if(
!empty(
$a[
'due_date'
]
)
){

$d=
strtotime(
$a[
'due_date'
]
);

if(
$d
>
time()
&&
$d
<
time()+(
86400*3
)
){
$dueSoon++;
}

}

}

$total=
max(
1,
$total
);

$workspace=
round(
(
$completed
/
$total
)
*
100
);

$recommend=
round(
(
(
$round2
+
$completed
)
/
$total
)
*
100
);

?>

<!-- PACK 685 -->

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

<div class="card-body">

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

<div>

<h4>

<?= rev_h(
rev_t(
'reviewer.workspace',
'Assignment Workspace'
)
) ?>

</h4>

<div class="text-muted">

Multi-Round Queue + Recommendation Assistant

</div>

</div>

<div
class="
display-5
fw-bold
text-success
"
>

<?= $workspace ?>%

</div>

</div>

<div class="row g-3">

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

<div class="border rounded-4 p-4 text-center">

<div class="display-6">

<?= $round1 ?>

</div>

Round 1

</div>

</div>

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

<div class="border rounded-4 p-4 text-center">

<div class="display-6">

<?= $round2 ?>

</div>

Round 2

</div>

</div>

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

<div class="border rounded-4 p-4 text-center">

<div class="display-6">

<?= $recommend ?>

</div>

Recommend

</div>

</div>

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

<div class="border rounded-4 p-4 text-center">

<div class="display-6 text-warning">

<?= $dueSoon ?>

</div>

Due Soon

</div>

</div>

</div>

</div>

</div>

<!-- END PACK 685 -->


<?php

$rv=
$assignments
??
[];

$total=
count(
$rv
);

$active=0;

$completed=0;

$revision=0;

$late=0;

foreach(
$rv
as
$a
){

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

if(
in_array(
$status,
[
'assigned',
'under_review'
],
true
)
){
$active++;
}

if(
in_array(
$status,
[
'completed',
'submitted'
],
true
)
){
$completed++;
}

if(
$status==='revision_required'
){
$revision++;
}

if(
!empty(
$a[
'due_date'
]
)
){

$due=
strtotime(
$a[
'due_date'
]
);

if(
$due
<
time()
&&
$status!=='completed'
){
$late++;
}

}

}

$total=
max(
1,
$total
);

$capacity=
round(
(
$completed
/
$total
)
*
100
);

$simulation=
max(
0,

100-
(
$revision
*
7
)

);

$tower=
round(
(
$capacity
+
$simulation
+
(
$active
*
5
)
)
/
3
);

$tower=
min(
100,
$tower
);

?>

<!-- PACK 686 -->

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

<div class="card-body">

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

<div>

<h4>

<?= rev_h(
rev_t(
'reviewer.capacity',
'Assignment Control Tower'
)
) ?>

</h4>

<div class="text-muted">

Decision Simulation + Capacity Planner

</div>

</div>

<div
class="
display-5
fw-bold
text-primary
"
>

<?= $tower ?>%

</div>

</div>

<div class="row g-3">

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

<div class="border rounded-4 p-4 text-center">

<div class="display-6">

<?= $capacity ?>

</div>

Capacity

</div>

</div>

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

<div class="border rounded-4 p-4 text-center">

<div class="display-6">

<?= $simulation ?>

</div>

Simulation

</div>

</div>

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

<div class="border rounded-4 p-4 text-center">

<div class="display-6">

<?= $active ?>

</div>

Active

</div>

</div>

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

<div class="border rounded-4 p-4 text-center">

<div class="display-6 text-danger">

<?= $late ?>

</div>

Late

</div>

</div>

</div>

</div>

</div>

<!-- END PACK 686 -->







    <!-- HEADER -->
    <div class="card border-0 shadow-sm mb-4" style="border-radius:24px;background:linear-gradient(135deg,#0f172a,#2563eb);color:#fff;">
        <div class="card-body p-4">
            <h3 class="fw-bold mb-1">
                <i class="fa fa-clipboard-check"></i>
                <?= rev_h(rev_t('reviewer.dashboard.title','Reviewer Dashboard')) ?>
            </h3>
            <div class="opacity-75">
                <?= rev_h(rev_t('reviewer.dashboard.welcome','Welcome to your reviewer dashboard')) ?>
            </div>
        </div>
    </div>

    <!-- STATS -->
    <div class="row g-3 mb-4">
        <?php
        $cards = [
            ['reviewer.dashboard.total_assignments','Total Assignments',$stats['total'] ?? 0,'fa-list-check'],
            ['reviewer.dashboard.pending_assignments','Pending Assignments',$stats['assigned'] ?? 0,'fa-hourglass-half'],
            ['reviewer.dashboard.completed_reviews','Completed Reviews',$stats['completed'] ?? 0,'fa-circle-check'],
            ['reviewer.dashboard.late_reviews','Late Reviews',$stats['late_reviews'] ?? 0,'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-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>

    <!-- ASSIGNMENTS TABLE -->
    <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 mb-3">
                <h5 class="fw-bold mb-0">
                    <?= rev_h(rev_t('reviewer.assignments.title','Review Assignments')) ?>
                </h5>
            </div>

            <div class="table-responsive">
                <table class="table table-bordered align-middle">
                    <thead class="table-dark">
                        <tr>
                            <th>#</th>
                            <th><?= rev_h(rev_t('reviewer.assignment.paper_title','Paper Title')) ?></th>
                            <th><?= rev_h(rev_t('reviewer.assignment.review_status','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 ($assignments as $a): ?>
                        <?php

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

$isOverdue=
!empty(
$a['due_date']
)
&&
$status!=='completed'
&&
$a['due_date']<$today;

$progress=
match($status){

'assigned'=>20,

'accepted'=>55,

'completed'=>100,

'overdue'=>10,

default=>15

};

?>

<tr class="<?= $isOverdue ? 'table-danger assignment-row':'assignment-row' ?>">

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

                            <!-- Title -->
                            <td>

<div class="fw-bold">

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

</div>

<div class="paper-meta">

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

—

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

</div>

</td>
                            <!-- Status -->
     <td>

<span
class="
badge
<?= rev_status_class(
$a['status']
?? ''
) ?>
"
>

<?= rev_h(

rev_status(

$a['status']
?? 'assigned'

)

) ?>

</span>

<div
class="
progress
progress-review
mt-2
"
>

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

</div>

<div
class="
small
text-muted
"
>

<?= $progress ?>%

</div>

</td>
                            <!-- Deadline -->
     <td>

<?= rev_h(
$a['due_date']
?? '-'
) ?>

<?php if(
$isOverdue
): ?>

<div
class="
small
text-danger
fw-bold
"
>

Overdue

</div>

<?php endif; ?>

<?php if(
!empty(
$a[
'current_round_no'
]
)
): ?>

<div
class="
small
text-muted
"
>

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

</div>

<?php endif; ?>

</td>
                            <!-- Actions -->
                            <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>

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

                                <?php endif; ?>

                            </td>

                        </tr>
                    <?php endforeach; ?>

                    <?php if (empty($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>
