<?php

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

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

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

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

$submissions =
    $submissions ?? [];

$reviewers =
    $reviewers ?? [];

?>

<?php

$totalSubmissions =
    count($submissions ?? []);

$underReview =
    0;

$revisionRequired =
    0;

$accepted =
    0;

$published =
    0;

foreach (($submissions ?? []) as $sx) {

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

    if ($st === 'under_review') {
        $underReview++;
    }

    elseif ($st === 'revision_required') {
        $revisionRequired++;
    }

    elseif ($st === 'accepted') {
        $accepted++;
    }

    elseif ($st === 'published') {
        $published++;
    }
}

?>


<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
? 'مركز إدارة الأبحاث'
: 'Submission Management Center' ?>

</div>

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

<?= $isArabic
? 'إدارة التقديمات والتحكيم'
: 'Submission & Review Workflow' ?>

</h2>

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

<?= $isArabic
? 'إدارة دورة حياة الأوراق العلمية'
: 'Scientific Paper Lifecycle 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>

<?= $totalSubmissions ?>

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

</div>

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

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

<?= $underReview ?>

<?= $isArabic
? 'قيد التحكيم'
: 'Under Review' ?>

</div>

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

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

<?= $revisionRequired ?>

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

</div>

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

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

<?= $accepted ?>

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

</div>

</div>

</div>

<div>

<span
class="badge bg-light text-dark fs-5 px-4 py-3 rounded-pill"
>

<?= $totalSubmissions ?>

<?= $isArabic
? 'ورقة علمية'
: 'Scientific Papers' ?>

</span>

</div>

</div>

</div>

</div>

</div>

        <span class="badge bg-dark fs-6 px-3 py-2">

            <?= count($submissions) ?>

            ورقة

        </span>

    </div>

</div>

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

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

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

<?= $isArabic
? "يوجد {$underReview} بحث قيد التحكيم حالياً."
: "There are {$underReview} papers currently under review." ?>

</div>

<?php endif; ?>

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

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

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

<?= $isArabic
? "يوجد {$revisionRequired} بحث يحتاج إلى تعديل."
: "There are {$revisionRequired} papers requiring revision." ?>

</div>

<?php endif; ?>

<?php if(empty($_SESSION['conference_id'])): ?>

    <div class="alert alert-warning shadow-sm border-0 rounded-4">

        يرجى اختيار مؤتمر أولاً

    </div>

<?php elseif(empty($submissions)): ?>

    <div class="alert alert-info shadow-sm border-0 rounded-4">

        لا يوجد تقديمات لهذا المؤتمر

    </div>

<?php else: ?>

<style>

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

.ec-submissions-shell{
border-radius:24px;
overflow:hidden;
background:#fff;
box-shadow:0 18px 45px rgba(15,23,42,.10);
}

.ec-submissions-table{
min-width:1850px;
font-size:14px;
}

.ec-submissions-table thead th{
background:#0f172a!important;
color:#fff!important;
font-weight:800;
font-size:12px;
letter-spacing:.02em;
text-transform:uppercase;
white-space:nowrap;
padding:16px 14px;
vertical-align:middle;
}

.ec-submissions-table tbody td{
padding:16px 14px;
vertical-align:middle;
border-bottom:1px solid #e5e7eb;
}

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

.ec-title-cell{
min-width:460px;
max-width:560px;
width:34vw;
}

.ec-title-main{
font-weight:800;
font-size:15px;
line-height:1.45;
color:#0f172a;
display:-webkit-box;
-webkit-line-clamp:2;
-webkit-box-orient:vertical;
overflow:hidden;
}

.ec-title-main:hover{
-webkit-line-clamp:unset;
overflow:visible;
}

.ec-title-meta{
margin-top:8px;
display:flex;
flex-wrap:wrap;
gap:8px;
align-items:center;
font-size:12px;
color:#64748b;
}

.ec-author-cell{
min-width:240px;
max-width:280px;
}

.ec-author-email{
max-width:240px;
overflow:hidden;
text-overflow:ellipsis;
white-space:nowrap;
}

.ec-nowrap{
white-space:nowrap;
}

.workflow-progress{
min-width:170px;
}

.ec-actions-cell{
min-width:240px;
}

.ec-reviewer-cell{
min-width:340px;
}

.ec-submission-no{
font-family:ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,"Liberation Mono","Courier New",monospace;
}

</style>

<div class="card border-0 ec-submissions-shell">

<div class="card-body p-0">

<div class="table-responsive">

<table class="table align-middle table-hover mb-0 ec-submissions-table">

<thead class="table-dark">

<tr class="submission-row">

<th width="60">#</th>

<th width="120">رقم الورقة</th>

<th style="width:520px;">العنوان</th>

<th width="260">المؤلف</th>

<th width="120">المرحلة</th>

<th width="120">القرار</th>

<th width="140">الحالة</th>

<th width="90">Round</th>

<th width="180">Review Progress</th>

<th width="120">Avg Score</th>

<th width="160">Recommendation</th>

<th width="170">التاريخ</th>

<th width="360">تعيين المراجعين</th>

<th width="260">Workflow</th>

</tr>

</thead>

<tbody>

<?php foreach($submissions as $s): ?>

<?php

/*
|--------------------------------------------------------------------------
| DECISION
|--------------------------------------------------------------------------
*/

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

$decisionColor =
    'secondary';

if ($decision === 'accepted') {
    $decisionColor = 'success';
}

elseif ($decision === 'rejected') {
    $decisionColor = 'danger';
}

elseif (
    $decision === 'revision'
    ||
    $decision === 'minor_revision'
    ||
    $decision === 'major_revision'
) {
    $decisionColor = 'warning';
}

/*
|--------------------------------------------------------------------------
| STATUS
|--------------------------------------------------------------------------
*/

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

$statusColor = 'secondary';

switch ($status) {

    case 'submitted':
        $statusColor = 'secondary';
    break;

    case 'under_review':
        $statusColor = 'primary';
    break;

    case 'revision_required':
        $statusColor = 'warning';
    break;

    case 'accepted':
        $statusColor = 'success';
    break;

    case 'rejected':
        $statusColor = 'danger';
    break;

    case 'production':
        $statusColor = 'info';
    break;

    case 'published':
        $statusColor = 'dark';
    break;
}

/*
|--------------------------------------------------------------------------
| SCORE
|--------------------------------------------------------------------------
*/

$avgScore =
    (float)(
        $s['avg_score']
        ?? 0
    );

$scoreColor = 'secondary';

if ($avgScore >= 85) {

    $scoreColor = 'success';

} elseif ($avgScore >= 70) {

    $scoreColor = 'warning';

} elseif ($avgScore > 0) {

    $scoreColor = 'danger';
}

/*
|--------------------------------------------------------------------------
| RECOMMENDATION
|--------------------------------------------------------------------------
*/

$rec =
    strtolower(
        trim(
            (string)(
                $s['recommended_decision']
                ?? '-'
            )
        )
    );

$recColor = 'secondary';

if ($rec === 'accept') {

    $recColor = 'success';

} elseif ($rec === 'minor_revision') {

    $recColor = 'warning';

} elseif ($rec === 'major_revision') {

    $recColor = 'danger';

} elseif ($rec === 'reject') {

    $recColor = 'dark';
}

/*
|--------------------------------------------------------------------------
| REVIEW PROGRESS
|--------------------------------------------------------------------------
*/

$totalReviews =
    (int)(
        $s['assignments_count']
        ?? 0
    );

$completedReviews =
    (int)(
        $s['completed_reviews']
        ?? 0
    );

$progress =
    $totalReviews > 0
    ? round(
        ($completedReviews / $totalReviews) * 100
    )
    : 0;

?>

<tr class="submission-row">

<td class="fw-bold ec-nowrap">

    #<?= (int)$s['id'] ?>

</td>

<td>

    <span class="badge bg-secondary rounded-pill px-3 py-2 ec-submission-no">

        <?= htmlspecialchars(
            $s['submission_no']
            ?? '—'
        ) ?>

    </span>

</td>

<td class="ec-title-cell">

    <div class="ec-title-main" title="<?= htmlspecialchars($s['display_title'] ?? $s['title'] ?? '—') ?>">

        <?= htmlspecialchars(
            $s['display_title']
            ?? $s['title']
            ?? '—'
        ) ?>

    </div>

    <?php if(!empty($s['track_name'])): ?>

        <div class="ec-title-meta">

            <span><i class="fa fa-layer-group"></i>

            <?= htmlspecialchars(
                $s['track_name']
            ) ?></span>

        </div>

    <?php endif; ?>

</td>

<td class="ec-author-cell">

    <div class="fw-semibold">

        <?= htmlspecialchars(
            $s['author_name']
            ?? '—'
        ) ?>

    </div>

    <?php if(!empty($s['author_email'])): ?>

        <div class="small text-muted ec-author-email" title="<?= htmlspecialchars($s['author_email']) ?>">

            <?= htmlspecialchars(
                $s['author_email']
            ) ?>

        </div>

    <?php endif; ?>

</td>

<td>

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

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

    </span>

</td>

<td>

    <span class="badge bg-<?= $decisionColor ?> rounded-pill px-3 py-2">

        
<?= htmlspecialchars(

$decision
? ucfirst(
    str_replace(
        '_',
        ' ',
        $decision
    )
)
: '-'

) ?>

    </span>

</td>

<td>

    <span class="badge bg-<?= $statusColor ?> rounded-pill px-3 py-2">

<?= htmlspecialchars(

$status
? ucfirst(
    str_replace(
        '_',
        ' ',
        $status
    )
)
: '-'

) ?>

    </span>

</td>

<td>

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

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

    </span>

</td>

<td>

<div
class="workflow-progress"
>

<div
class="progress"
style="
height:22px;
border-radius:999px;
background:#e2e8f0;
"
>

<div
class="progress-bar bg-success fw-bold"
style="
width:<?= $progress ?>%;
"
>

<?= $completedReviews ?>/<?= $totalReviews ?>

</div>

</div>

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

<?= $progress ?>%

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

</div>

</div>

</td>

<td>

    <span class="badge bg-<?= $scoreColor ?> rounded-pill px-3 py-2">

        <?= $avgScore > 0
? number_format($avgScore, 1)
: '-' ?>

    </span>

</td>

<td>

    <span class="badge bg-<?= $recColor ?> rounded-pill px-3 py-2">

        <?= htmlspecialchars(
            $rec ?: '-'
        ) ?>

    </span>

</td>

<td>

    <div class="small">

        <?= htmlspecialchars(
            $s['created_at']
            ?? '-'
        ) ?>

    </div>

</td>

<td class="ec-reviewer-cell">

<form
method="post"
action="/easychair/admin/review-assign"
class="d-flex gap-2 align-items-center flex-wrap"
>

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

<select
name="reviewer_id"
class="form-select form-select-sm rounded-pill shadow-sm reviewer-search"
style="min-width:190px;"
>

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

    <option value="">

        لا يوجد مراجعين

    </option>

<?php else: ?>

    <?php foreach ($reviewers as $r): ?>

        <option value="<?= (int)$r['id'] ?>">

            <?= htmlspecialchars(
                trim(
                    ($r['first_name'] ?? '')
                    . ' '
                    . ($r['last_name'] ?? '')
                )
            ) ?>

            -

            <?= htmlspecialchars(
                $r['email']
            ) ?>

        </option>

    <?php endforeach; ?>

<?php endif; ?>

</select>

<button
class="btn btn-success btn-sm rounded-pill px-4 shadow-sm"
>

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

Assign

</button>

</form>

</td>

<td class="ec-actions-cell">

<?php

$submissionRow =
    $s ?? [];

require BASE_PATH
    . '/app/views/admin/partials/submission_actions.php';

?>

</td>

</tr>

<?php endforeach; ?>

</tbody>

</table>

</div>

</div>

</div>

<?php endif; ?>

<script>

document.addEventListener(
'DOMContentLoaded',
function(){

let reviewer=
document.querySelector(
'select[name="reviewer_id"]'
);

if(
!reviewer
)
return;

if(
reviewer.dataset.searchReady
)
return;

reviewer.dataset.searchReady=1;

try{

reviewer.setAttribute(
'data-live-search',
'true'
);

reviewer.setAttribute(
'data-search',
'true'
);

if(
window.jQuery
&&


<style>

.select2-search__field{
padding:10px!important;
}

.select2-selection{
min-height:42px!important;
border-radius:12px!important;
}

.select2-results__option{
padding:10px!important;
}

</style>



<script>

document.addEventListener(
'DOMContentLoaded',
function(){

if(
typeof jQuery==='undefined'
||
!jQuery.fn
||
!jQuery.fn.select2
){
console.log('select2 missing');
return;
}

jQuery('.reviewer-search').each(function(){

if(
jQuery(this)
.hasClass(
'select2-hidden-accessible'
)
){
return;
}

jQuery(this).select2({

width:'100%',

placeholder:'Search reviewer…',

allowClear:true,

dropdownAutoWidth:true,

minimumResultsForSearch:0

});

});

}
);

</script>

<style>



.select2-selection{
min-height:40px!important;
border-radius:999px!important;
}

.select2-search__field{
min-height:34px!important;
}

</style>




<link rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css">

<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>

<script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js"></script>

<script>

$(function(){

$('select.reviewer-search').each(function(){

if($(this).data('select2')){
return;
}

$(this).select2({

width:'style',

dropdownParent:
$(this).parent(),

dropdownAutoWidth:false,

placeholder:'Search reviewer',

allowClear:false,

dropdownAutoWidth:true,

matcher:function(params,data){

if($.trim(params.term)===''){
return data;
}

if(typeof data.text==='undefined'){
return null;
}

return data.text
.toLowerCase()
.indexOf(
params.term.toLowerCase()
)>=0
?data
:null;

}

});

});

});

</script>

<style>



.select2-selection{
height:38px!important;
border-radius:999px!important;
padding-top:4px!important;
}

.select2-search__field{
min-height:34px!important;
}

</style>



<style>

select.reviewer-search{
width:190px!important;
max-width:190px!important;
}

.select2-container{

width:190px!important;

max-width:190px!important;

min-width:190px!important;

}

.select2-dropdown{

width:190px!important;

min-width:190px!important;

max-width:190px!important;

border-radius:16px!important;

overflow:hidden!important;

}

.select2-results{

max-height:260px!important;

overflow-y:auto!important;

}

.select2-selection{

height:38px!important;

padding-top:3px!important;

border-radius:999px!important;

}

.select2-search--dropdown{

padding:8px!important;

}

.select2-search__field{

width:100%!important;

border-radius:10px!important;

padding:8px!important;

}

</style>

