<?php
function dash_h($v) {
    return htmlspecialchars((string)$v, ENT_QUOTES, 'UTF-8');
}

$role = $role ?? ($_SESSION['role'] ?? 'author');
$dashboardType = $dashboardType ?? 'author';
$data = $data ?? [];
$conference = $conference ?? null;

$roleTitle = [
    'super_admin' => 'Super Admin',
    'org_admin' => 'Organization Admin',
    'admin' => 'Admin',
    'organizer' => 'Organizer',
    'conference_chair' => 'Conference Chair',
    'track_chair' => 'Track Chair',
    'reviewer' => 'Reviewer',
    'author' => 'Author',
];

$displayRole = $roleTitle[$role] ?? ucfirst(str_replace('_', ' ', $role));

$conferenceTitle = '';
if ($conference) {
    $conferenceTitle = ($conference['title_ar'] ?? '') ?: (($conference['title_en'] ?? '') ?: ('Conference #' . $conference['id']));
}
?>

<style>
.dash-hero{
    border-radius:24px;
    background:linear-gradient(135deg,#0f172a,#1d4ed8);
    color:#fff;
    padding:28px;
    box-shadow:0 20px 55px rgba(15,23,42,.18);
}
.kpi-card{
    border:0;
    border-radius:20px;
    box-shadow:0 14px 40px rgba(15,23,42,.08);
}
.kpi-label{
    color:#64748b;
    font-size:13px;
    font-weight:700;
}
.kpi-value{
    font-size:32px;
    font-weight:900;
    color:#0f172a;
}
.workflow-chip{
    display:inline-flex;
    align-items:center;
    gap:7px;
    border-radius:999px;
    padding:8px 13px;
    margin:3px;
    font-weight:800;
    background:#eff6ff;
    color:#1e3a8a;
    border:1px solid #dbeafe;
}
.workflow-chip.off{
    background:#f1f5f9;
    color:#64748b;
    border-color:#e2e8f0;
}
.quick-link{
    text-decoration:none;
    display:block;
    border-radius:18px;
    padding:18px;
    background:#fff;
    border:1px solid #e5e7eb;
    box-shadow:0 10px 28px rgba(15,23,42,.06);
    height:100%;
    color:#111827;
}
.quick-link:hover{
    border-color:#2563eb;
    transform:translateY(-2px);
}
</style>

<div class="container-fluid py-3">

    <div class="dash-hero mb-4">
        <div class="d-flex justify-content-between align-items-center flex-wrap gap-3">
            <div>
                <div class="opacity-75 mb-1">EasyChair / OCS Portal</div>
                <h2 class="fw-bold mb-1"><?= dash_h($displayRole) ?> Dashboard</h2>

                <?php if ($conferenceTitle): ?>
                    <div class="opacity-75">
                        <?= dash_h($conferenceTitle) ?>
                        <?php if (!empty($conference['short_name'])): ?>
                            — <?= dash_h($conference['short_name']) ?>
                        <?php endif; ?>
                    </div>
                <?php else: ?>
                    <div class="opacity-75">Organization / multi-conference workspace</div>
                <?php endif; ?>
            </div>

            <div class="d-flex gap-2 flex-wrap">
                <a href="/easychair/select-conference" class="btn btn-light rounded-pill fw-bold">
                    تغيير المؤتمر
                </a>

                <?php if ($dashboardType === 'admin'): ?>
                    <a href="/easychair/admin/workflow" class="btn btn-outline-light rounded-pill fw-bold">
                        Workflow
                    </a>
                <?php elseif ($dashboardType === 'reviewer'): ?>
                    <a href="/easychair/reviewer/dashboard" class="btn btn-outline-light rounded-pill fw-bold">
                        مراجعاتي
                    </a>
                <?php else: ?>
                    <a href="/easychair/author/submission/create" class="btn btn-outline-light rounded-pill fw-bold">
                        تقديم بحث
                    </a>
                <?php endif; ?>
            </div>
        </div>
    </div>

    <?php if ($dashboardType === 'admin'): ?>

        <div class="row g-3 mb-4">
            <?php
            $cards = [
                ['label'=>'Conferences', 'value'=>$data['conferences_count'] ?? 0],
                ['label'=>'Submissions', 'value'=>$data['submissions_count'] ?? 0],
                ['label'=>'Under Review', 'value'=>$data['under_review_count'] ?? 0],
                ['label'=>'Accepted', 'value'=>$data['accepted_count'] ?? 0],
                ['label'=>'Reviewers', 'value'=>$data['reviewers_count'] ?? 0],
                ['label'=>'Reviews', 'value'=>$data['reviews_count'] ?? 0],
            ];
            ?>
            <?php foreach ($cards as $c): ?>
                <div class="col-xl-2 col-md-4 col-6">
                    <div class="card kpi-card h-100">
                        <div class="card-body">
                            <div class="kpi-label"><?= dash_h($c['label']) ?></div>
                            <div class="kpi-value"><?= (int)$c['value'] ?></div>
                        </div>
                    </div>
                </div>
            <?php endforeach; ?>
        </div>

        <?php if (!empty($data['workflow']) || !empty($data['workflow_settings'])): ?>
            <div class="card kpi-card mb-4">
                <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">Workflow Overview</h5>

                        <?php if (!empty($data['workflow_settings'])): ?>
                            <div class="text-muted small">
                                Rounds: <?= (int)($data['workflow_settings']['review_rounds'] ?? 1) ?> |
                                Revision: <?= !empty($data['workflow_settings']['allow_revision']) ? 'ON' : 'OFF' ?> |
                                Auto Decision: <?= !empty($data['workflow_settings']['auto_decision']) ? 'ON' : 'OFF' ?>
                            </div>
                        <?php endif; ?>
                    </div>

                    <?php foreach (($data['workflow'] ?? []) as $w): ?>
                        <span class="workflow-chip <?= empty($w['is_enabled']) ? 'off' : '' ?>">
                            <?= !empty($w['is_enabled']) ? '●' : '○' ?>
                            <?= dash_h($w['stage'] ?? '') ?>
                        </span>
                    <?php endforeach; ?>
                </div>
            </div>
        <?php endif; ?>

        <div class="row g-3 mb-4">
            <div class="col-md-3">
                <a class="quick-link" href="/easychair/admin/submissions">
                    <h6 class="fw-bold">Submissions</h6>
                    <div class="text-muted small">Manage screening, status, and workflow.</div>
                </a>
            </div>
            <div class="col-md-3">
                <a class="quick-link" href="/easychair/admin/reviewers">
                    <h6 class="fw-bold">Reviewers</h6>
                    <div class="text-muted small">Reviewer pool and assignments.</div>
                </a>
            </div>
            <div class="col-md-3">
                <a class="quick-link" href="/easychair/admin/decisions">
                    <h6 class="fw-bold">Decisions</h6>
                    <div class="text-muted small">Chair decisions and recommendations.</div>
                </a>
            </div>
            <div class="col-md-3">
                <a class="quick-link" href="/easychair/admin/workflow">
                    <h6 class="fw-bold">Workflow</h6>
                    <div class="text-muted small">Configure conference stages.</div>
                </a>
            </div>
        </div>

        <div class="card kpi-card">
            <div class="card-body">
                <h5 class="fw-bold mb-3">Recent Submissions</h5>

                <div class="table-responsive">
                    <table class="table table-bordered align-middle">
                        <thead class="table-light">
                            <tr>
                                <th>ID</th>
                                <th>Title</th>
                                <th>Conference</th>
                                <th>Status</th>
                                <th>Stage</th>
                                <th>Decision</th>
                                <th>Avg</th>
                            </tr>
                        </thead>
                        <tbody>
                        <?php foreach (($data['recent_submissions'] ?? []) as $s): ?>
                            <tr>
                                <td><?= (int)$s['id'] ?></td>
                                <td class="fw-bold"><?= dash_h($s['title'] ?? '') ?></td>
                                <td><?= dash_h($s['short_name'] ?? '') ?></td>
                                <td><span class="badge bg-secondary"><?= dash_h($s['status'] ?? '') ?></span></td>
                                <td><?= dash_h($s['stage'] ?? '-') ?></td>
                                <td><?= dash_h($s['decision'] ?? '-') ?></td>
                                <td><?= dash_h($s['avg_score'] ?? '-') ?></td>
                            </tr>
                        <?php endforeach; ?>

                        <?php if (empty($data['recent_submissions'])): ?>
                            <tr>
                                <td colspan="7" class="text-center text-muted py-4">No submissions found.</td>
                            </tr>
                        <?php endif; ?>
                        </tbody>
                    </table>
                </div>

            </div>
        </div>

    <?php elseif ($dashboardType === 'reviewer'): ?>

        <div class="row g-3 mb-4">
            <?php
            $cards = [
                ['label'=>'Assigned', 'value'=>$data['assigned_count'] ?? 0],
                ['label'=>'Accepted', 'value'=>$data['accepted_count'] ?? 0],
                ['label'=>'Completed', 'value'=>$data['completed_count'] ?? 0],
                ['label'=>'Declined', 'value'=>$data['declined_count'] ?? 0],
            ];
            ?>
            <?php foreach ($cards as $c): ?>
                <div class="col-md-3 col-6">
                    <div class="card kpi-card h-100">
                        <div class="card-body">
                            <div class="kpi-label"><?= dash_h($c['label']) ?></div>
                            <div class="kpi-value"><?= (int)$c['value'] ?></div>
                        </div>
                    </div>
                </div>
            <?php endforeach; ?>
        </div>

        <div class="card kpi-card">
            <div class="card-body">
                <h5 class="fw-bold mb-3">My Review Assignments</h5>

                <div class="table-responsive">
                    <table class="table table-bordered align-middle">
                        <thead class="table-light">
                            <tr>
                                <th>Assignment</th>
                                <th>Submission</th>
                                <th>Title</th>
                                <th>Status</th>
                                <th>Round</th>
                                <th>Due Date</th>
                                <th>Action</th>
                            </tr>
                        </thead>
                        <tbody>
                        <?php foreach (($data['my_assignments'] ?? []) as $a): ?>
                            <tr>
                                <td><?= (int)$a['assignment_id'] ?></td>
                                <td><?= (int)$a['submission_id'] ?></td>
                                <td class="fw-bold"><?= dash_h($a['title'] ?? '') ?></td>
                                <td><span class="badge bg-secondary"><?= dash_h($a['status'] ?? '') ?></span></td>
                                <td><?= dash_h($a['round_no'] ?? 1) ?></td>
                                <td><?= dash_h($a['due_date'] ?? '-') ?></td>
                                <td>
                                    <a class="btn btn-sm btn-primary" href="/easychair/reviewer/dashboard">
                                        Open
                                    </a>
                                </td>
                            </tr>
                        <?php endforeach; ?>

                        <?php if (empty($data['my_assignments'])): ?>
                            <tr>
                                <td colspan="7" class="text-center text-muted py-4">No assignments found.</td>
                            </tr>
                        <?php endif; ?>
                        </tbody>
                    </table>
                </div>

            </div>
        </div>

    <?php else: ?>

        <div class="row g-3 mb-4">
            <?php
            $cards = [
                ['label'=>'My Submissions', 'value'=>$data['my_submissions_count'] ?? 0],
                ['label'=>'Drafts', 'value'=>$data['draft_count'] ?? 0],
                ['label'=>'Under Review', 'value'=>$data['under_review_count'] ?? 0],
                ['label'=>'Accepted', 'value'=>$data['accepted_count'] ?? 0],
                ['label'=>'Revision', 'value'=>$data['revision_count'] ?? 0],
            ];
            ?>
            <?php foreach ($cards as $c): ?>
                <div class="col-md col-6">
                    <div class="card kpi-card h-100">
                        <div class="card-body">
                            <div class="kpi-label"><?= dash_h($c['label']) ?></div>
                            <div class="kpi-value"><?= (int)$c['value'] ?></div>
                        </div>
                    </div>
                </div>
            <?php endforeach; ?>
        </div>

        <div class="row g-3 mb-4">
            <div class="col-md-4">
                <a class="quick-link" href="/easychair/author/submission/create">
                    <h6 class="fw-bold">Submit New Paper</h6>
                    <div class="text-muted small">Create a new submission for the selected conference.</div>
                </a>
            </div>
            <div class="col-md-4">
                <a class="quick-link" href="/easychair/author/submissions">
                    <h6 class="fw-bold">My Submissions</h6>
                    <div class="text-muted small">Track status, revision, and final decision.</div>
                </a>
            </div>
            <div class="col-md-4">
                <a class="quick-link" href="/easychair/conference/home?id=<?= (int)($_SESSION['conference_id'] ?? 0) ?>">
                    <h6 class="fw-bold">Conference Home</h6>
                    <div class="text-muted small">View dates, workflow, and conference information.</div>
                </a>
            </div>
        </div>

        <div class="card kpi-card">
            <div class="card-body">
                <h5 class="fw-bold mb-3">My Recent Submissions</h5>

                <div class="table-responsive">
                    <table class="table table-bordered align-middle">
                        <thead class="table-light">
                            <tr>
                                <th>ID</th>
                                <th>Title</th>
                                <th>Conference</th>
                                <th>Status</th>
                                <th>Stage</th>
                                <th>Decision</th>
                                <th>Avg</th>
                            </tr>
                        </thead>
                        <tbody>
                        <?php foreach (($data['my_submissions'] ?? []) as $s): ?>
                            <tr>
                                <td><?= (int)$s['id'] ?></td>
                                <td class="fw-bold"><?= dash_h($s['title'] ?? '') ?></td>
                                <td><?= dash_h($s['short_name'] ?? '') ?></td>
                                <td><span class="badge bg-secondary"><?= dash_h($s['status'] ?? '') ?></span></td>
                                <td><?= dash_h($s['stage'] ?? '-') ?></td>
                                <td><?= dash_h($s['decision'] ?? '-') ?></td>
                                <td><?= dash_h($s['avg_score'] ?? '-') ?></td>
                            </tr>
                        <?php endforeach; ?>

                        <?php if (empty($data['my_submissions'])): ?>
                            <tr>
                                <td colspan="7" class="text-center text-muted py-4">No submissions found.</td>
                            </tr>
                        <?php endif; ?>
                        </tbody>
                    </table>
                </div>

            </div>
        </div>

    <?php endif; ?>

</div>
