<?php
function rd_h($v) {
    return htmlspecialchars((string)$v, ENT_QUOTES, 'UTF-8');
}
function rd_t($key, $fallback) {
    if (function_exists('ec_t')) return ec_t($key, $fallback);
    if (function_exists('t')) return t($key, $fallback);
    return $fallback;
}
function rd_status_label($v) {
    if (function_exists('ec_submission_status_t')) return ec_submission_status_t($v);
    return (string)$v;
}

$role = $role ?? ($_SESSION['conference_role'] ?? $_SESSION['role'] ?? '');
$conference = $conference ?? [];
$stats = $stats ?? [];
$actions = $actions ?? [];
$recentSubmissions = $recentSubmissions ?? [];
$workflowRows = $workflowRows ?? [];
$lang = function_exists('ec_lang') ? ec_lang() : ($_SESSION['lang'] ?? 'ar');

$roleLabel = function_exists('ec_role_layout_role_label')
    ? ec_role_layout_role_label((string)$role)
    : ucwords(str_replace('_',' ', (string)$role));

$confTitle = $lang === 'en'
    ? (($conference['title_en'] ?? '') ?: ($conference['title_ar'] ?? ''))
    : (($conference['title_ar'] ?? '') ?: ($conference['title_en'] ?? ''));
?>

<div class="container-fluid py-3">
    <div class="card border-0 shadow-sm mb-4" style="border-radius:28px;background:linear-gradient(135deg,#0f172a,#2563eb);color:#fff;overflow:hidden;">
        <div class="card-body p-4 p-lg-5">
            <div class="d-flex justify-content-between align-items-start flex-wrap gap-3">
                <div>
                    <div class="badge bg-light text-primary rounded-pill px-3 py-2 mb-3">
                        <?= rd_h($roleLabel) ?>
                    </div>

                    <h2 class="fw-black fw-bold mb-2">
                        <i class="fa fa-gauge-high"></i>
                        <?= rd_h(rd_t('dashboard.role.title','Role Dashboard')) ?>
                    </h2>

                    <div class="opacity-75">
                        <?= rd_h(rd_t('dashboard.role.subtitle','Role-based overview and shortcuts')) ?>
                    </div>

                    <div class="mt-3 small opacity-75">
                        <strong><?= rd_h($conference['short_name'] ?? '') ?></strong>
                        <?php if ($confTitle): ?>
                            — <?= rd_h($confTitle) ?>
                        <?php endif; ?>
                    </div>
                </div>

                <div class="text-end">
                    <div class="rounded-4 p-3" style="background:rgba(255,255,255,.12);border:1px solid rgba(255,255,255,.18);">
                        <div class="small opacity-75"><?= rd_h(rd_t('dashboard.section.role_scope','Current Scope')) ?></div>
                        <div class="fw-bold"><?= rd_h($conference['org_name'] ?? ('Org '.($conference['org_id'] ?? ''))) ?></div>
                        <div class="small opacity-75">Conference #<?= (int)($conference['id'] ?? 0) ?></div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class="row g-3 mb-4">
        <?php foreach ($stats as $s): ?>
            <div class="col-xl-3 col-md-6">
                <div class="card border-0 shadow-sm h-100" style="border-radius:22px;">
                    <div class="card-body p-4 d-flex justify-content-between align-items-center">
                        <div>
                            <div class="text-muted small fw-bold"><?= rd_h(rd_t($s['key'] ?? '', $s['label'] ?? '')) ?></div>
                            <div class="fs-2 fw-bold"><?= (int)($s['value'] ?? 0) ?></div>
                        </div>
                        <div class="rounded-circle d-flex align-items-center justify-content-center" style="width:56px;height:56px;background:#eef2ff;color:#1d4ed8;">
                            <i class="fa <?= rd_h($s['icon'] ?? 'fa-circle') ?>"></i>
                        </div>
                    </div>
                </div>
            </div>
        <?php endforeach; ?>
    </div>

    <div class="row g-4">
        <div class="col-xl-4">
            <div class="card border-0 shadow-sm h-100" style="border-radius:22px;">
                <div class="card-body p-4">
                    <h5 class="fw-bold mb-3">
                        <i class="fa fa-bolt text-primary"></i>
                        <?= rd_h(rd_t('dashboard.section.quick_actions','Quick Actions')) ?>
                    </h5>

                    <div class="d-grid gap-2">
                        <?php foreach ($actions as $a): ?>
                            <a href="<?= rd_h($a[0]) ?>" class="btn btn-light border d-flex align-items-center justify-content-between rounded-4 p-3 text-start">
                                <span>
                                    <i class="fa <?= rd_h($a[1]) ?> text-primary"></i>
                                    <strong><?= rd_h(rd_t($a[2], $a[3])) ?></strong>
                                </span>
                                <i class="fa fa-chevron-left opacity-50"></i>
                            </a>
                        <?php endforeach; ?>

                        <?php if (!$actions): ?>
                            <div class="text-muted py-4 text-center">
                                <?= rd_h(rd_t('dashboard.section.no_data','No data available.')) ?>
                            </div>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
        </div>

        <div class="col-xl-5">
            <div class="card border-0 shadow-sm h-100" style="border-radius:22px;">
                <div class="card-body p-4">
                    <h5 class="fw-bold mb-3">
                        <i class="fa fa-inbox text-primary"></i>
                        <?= rd_h(rd_t('dashboard.section.recent_submissions','Recent Submissions')) ?>
                    </h5>

                    <div class="table-responsive">
                        <table class="table table-bordered align-middle">
                            <thead class="table-dark">
                            <tr>
                                <th>#</th>
                                <th><?= rd_h(rd_t('submission.title','Title')) ?></th>
                                <th><?= rd_h(rd_t('submission.status','Status')) ?></th>
                            </tr>
                            </thead>
                            <tbody>
                            <?php foreach ($recentSubmissions as $r): ?>
                                <tr>
                                    <td>
                                        <a href="/easychair/admin/submission-workflow?submission_id=<?= (int)$r['id'] ?>" class="fw-bold">
                                            <?= rd_h($r['submission_no'] ?? ('#'.$r['id'])) ?>
                                        </a>
                                    </td>
                                    <td>
                                        <strong><?= rd_h(($r['title'] ?? '') ?: ($r['title_ar'] ?? '')) ?></strong>
                                        <div class="text-muted small"><?= rd_h($r['updated_at'] ?? '') ?></div>
                                    </td>
                                    <td>
                                        <span class="badge bg-info text-dark"><?= rd_h(rd_status_label($r['stage'] ?? $r['status'] ?? '')) ?></span>
                                    </td>
                                </tr>
                            <?php endforeach; ?>

                            <?php if (!$recentSubmissions): ?>
                                <tr>
                                    <td colspan="3" class="text-center text-muted py-4">
                                        <?= rd_h(rd_t('dashboard.section.no_data','No data available.')) ?>
                                    </td>
                                </tr>
                            <?php endif; ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>

        <div class="col-xl-3">
            <div class="card border-0 shadow-sm h-100" style="border-radius:22px;">
                <div class="card-body p-4">
                    <h5 class="fw-bold mb-3">
                        <i class="fa fa-diagram-project text-primary"></i>
                        <?= rd_h(rd_t('dashboard.section.workflow_status','Workflow Status')) ?>
                    </h5>

                    <?php foreach ($workflowRows as $w): ?>
                        <?php
                            $total = max(1, array_sum(array_map(fn($x) => (int)($x['total'] ?? 0), $workflowRows)));
                            $pct = min(100, round(((int)$w['total'] / $total) * 100));
                        ?>
                        <div class="mb-3">
                            <div class="d-flex justify-content-between small fw-bold mb-1">
                                <span><?= rd_h(rd_status_label($w['stage_key'] ?? '')) ?></span>
                                <span><?= (int)($w['total'] ?? 0) ?></span>
                            </div>
                            <div class="progress" style="height:10px;border-radius:999px;">
                                <div class="progress-bar" style="width:<?= (int)$pct ?>%;"></div>
                            </div>
                        </div>
                    <?php endforeach; ?>

                    <?php if (!$workflowRows): ?>
                        <div class="text-muted py-4 text-center">
                            <?= rd_h(rd_t('dashboard.section.no_data','No data available.')) ?>
                        </div>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    </div>
</div>
