<?php
// Pack511: enforce automatic conference scope for non-superadmin layouts.
if (defined('BASE_PATH') && file_exists(BASE_PATH . '/app/helpers/conference_scope.php')) {
    require_once BASE_PATH . '/app/helpers/conference_scope.php';
}
if (function_exists('ec_scope_role') && ec_scope_role() !== 'super_admin' && function_exists('ec_scope_auto_select_conference')) {
    ec_scope_auto_select_conference(false);
}
?>

<?php
if (session_status() !== PHP_SESSION_ACTIVE) {
    session_start();
}

$content = $content ?? '';
$userName = $_SESSION['user_name'] ?? ($_SESSION['email'] ?? 'Author');
$userEmail = $_SESSION['email'] ?? '';
$conferenceId = $_SESSION['conference_id'] ?? $_SESSION['current_conference_id'] ?? null;
$roleCode = $_SESSION['conference_role'] ?? $_SESSION['role'] ?? 'author';

if (defined('BASE_PATH') && file_exists(BASE_PATH . '/app/helpers/i18n.php')) {
    require_once BASE_PATH . '/app/helpers/i18n.php';
}
if (defined('BASE_PATH') && file_exists(BASE_PATH . '/app/helpers/easychair_translate.php')) {
    require_once BASE_PATH . '/app/helpers/easychair_translate.php';
}
if (defined('BASE_PATH') && file_exists(BASE_PATH . '/app/views/partials/sidebar_translate.php')) {
    require_once BASE_PATH . '/app/views/partials/sidebar_translate.php';
}

if (!function_exists('author_layout_h')) {
    function author_layout_h($v) {
        return htmlspecialchars((string)$v, ENT_QUOTES, 'UTF-8');
    }
}

if (!function_exists('author_layout_t')) {
    function author_layout_t(string $key, string $fallback): string {
        if (function_exists('ec_t')) {
            return ec_t($key, $fallback);
        }
        if (function_exists('t')) {
            return t($key, $fallback);
        }
        return $fallback;
    }
}

$lang = function_exists('ec_lang') ? ec_lang() : 'ar';
$dir = function_exists('ec_dir') ? ec_dir() : ($lang === 'en' ? 'ltr' : 'rtl');
$isRtl = $dir === 'rtl';

$conferenceShort = $_SESSION['conference_short_name'] ?? ('Conference #' . ($conferenceId ?: ''));
$conferenceTitle = '';
try {
    if (defined('BASE_PATH') && class_exists('\App\Core\Database') && $conferenceId) {
        $db = \App\Core\Database::getInstance();
        $stmt = $db->prepare("SELECT short_name, title_en, title_ar FROM conferences WHERE id=? LIMIT 1");
        $stmt->execute([(int)$conferenceId]);
        $c = $stmt->fetch(PDO::FETCH_ASSOC);
        if ($c) {
            $conferenceShort = $c['short_name'] ?: $conferenceShort;
            $conferenceTitle = $lang === 'en'
                ? ($c['title_en'] ?: $c['title_ar'] ?: '')
                : ($c['title_ar'] ?: $c['title_en'] ?: '');
        }
    }
} catch (Throwable $e) {
    // Keep layout alive.
}

$links = [

    [
        'url' => '/easychair/author/dashboard',
        'icon' => 'fa-gauge-high',
        'label' => author_layout_t(
            'menu.author_dashboard',
            'Author Dashboard'
        ),
    ],

    [
        'url' => '/easychair/author/dashboard',
        'icon' => 'fa-file-lines',
        'label' => author_layout_t(
            'menu.author_submissions',
            'My Submissions'
        ),
    ],

    [
        'url' => '/easychair/author/submit',
        'icon' => 'fa-file-circle-plus',
        'label' => author_layout_t(
            'menu.author_submit',
            'Submit Paper'
        ),
    ],

    [
        'url' => '/easychair/profile',
        'icon' => 'fa-user-graduate',
        'label' => author_layout_t(
            'layout.academic_profile',
            'Academic Profile'
        ),
    ],

    [
        'url' => '/easychair/notifications',
        'icon' => 'fa-bell',
        'label' => author_layout_t(
            'layout.notifications',
            'Notifications'
        ),
    ],

    [
        'url' => '/easychair/logout',
        'icon' => 'fa-right-from-bracket',
        'label' => author_layout_t(
            'menu.logout',
            'Logout'
        ),
    ],

];

/*
Conditional conference switch
*/

$conferenceList =
    $_SESSION['conference_list'] ?? [];

if (
    is_array($conferenceList)
    && count($conferenceList) > 1
) {

    $links[] = [
        'url' => '/easychair/select-conference',
        'icon' => 'fa-building-columns',
        'label' => author_layout_t(
            'menu.change_conference',
            'Change Conference'
        ),
    ];

}


$currentPath = parse_url($_SERVER['REQUEST_URI'] ?? '', PHP_URL_PATH) ?: '';
?>
<!DOCTYPE html>
<html lang="<?php echo author_layout_h($lang); ?>" dir="<?php echo author_layout_h($dir); ?>">
<head>
    <meta charset="UTF-8">
    <title><?php echo author_layout_h(author_layout_t('menu.author_dashboard', 'Author Dashboard')); ?> - EasyChair</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <link href="https://fonts.googleapis.com/css2?family=Cairo:wght@400;600;700;800;900&display=swap" rel="stylesheet">
    <?php if ($isRtl): ?>
        <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.rtl.min.css" rel="stylesheet">
    <?php else: ?>
        <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <?php endif; ?>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.2/css/all.min.css" rel="stylesheet">

    <style>
        :root {
            --ec-sidebar-w: 270px;
            --ec-blue: #1d4ed8;
            --ec-navy: #0f172a;
            --ec-bg: #f4f7fb;
        }

        * {
            box-sizing: border-box;
        }

        html,
        body {
            min-height: 100%;
            max-width: 100%;
            overflow-x: hidden;
        }

        body {
            margin: 0;
            font-family: 'Cairo', sans-serif;
            background: var(--ec-bg);
            color: #111827;
            direction: <?php echo $isRtl ? 'rtl' : 'ltr'; ?>;
            text-align: <?php echo $isRtl ? 'right' : 'left'; ?>;
        }

        .author-sidebar {
            position: fixed;
            top: 0;
            bottom: 0;
            <?php echo $isRtl ? 'right' : 'left'; ?>: 0;
            width: var(--ec-sidebar-w);
            z-index: 1040;
            overflow-y: auto;
            padding: 20px 14px;
            color: #fff;
            background: linear-gradient(180deg, #0f172a, #1e3a8a);
            box-shadow: 0 20px 45px rgba(15,23,42,.22);
        }

        .author-brand {
            display: flex;
            align-items: center;
            gap: 10px;
            font-weight: 900;
            font-size: 20px;
            margin-bottom: 18px;
            padding: 8px 8px 14px;
        }

        .author-brand i {
            font-size: 23px;
        }

        .author-lang-switch {
            display: flex;
            gap: 8px;
            margin: 0 6px 16px;
        }

        .author-lang-switch a {
            flex: 1;
            text-align: center;
            text-decoration: none;
            border-radius: 999px;
            padding: 7px 10px;
            font-weight: 900;
            color: #1d4ed8;
            background: #fff;
        }

        .author-lang-switch a.active {
            color: #fff;
            background: #2563eb;
            box-shadow: inset 0 0 0 1px rgba(255,255,255,.35);
        }

        .author-conf {
            background: rgba(255,255,255,.08);
            border: 1px solid rgba(255,255,255,.12);
            border-radius: 18px;
            padding: 12px;
            margin: 0 6px 16px;
        }

        .author-conf .short {
            font-weight: 900;
            font-size: 13px;
        }

        .author-conf .title {
            opacity: .82;
            font-size: 12px;
            line-height: 1.6;
            margin-top: 4px;
        }

        .author-user {
            background: rgba(255,255,255,.08);
            border-radius: 18px;
            padding: 12px;
            margin: 0 6px 16px;
        }

        .author-user .name {
            font-weight: 900;
        }

        .author-user .meta {
            opacity: .75;
            font-size: 12px;
            overflow-wrap: anywhere;
        }

        .author-link {
            display: flex;
            align-items: center;
            gap: 10px;
            padding: 11px 12px;
            margin-bottom: 7px;
            border-radius: 14px;
            color: rgba(255,255,255,.92);
            text-decoration: none;
            font-weight: 800;
            transition: .16s ease;
        }

        .author-link i {
            width: 22px;
            text-align: center;
        }

        .author-link:hover,
        .author-link.active {
            color: #fff;
            background: rgba(255,255,255,.15);
            transform: translateY(-1px);
        }

        .author-main {
            min-height: 100vh;
            width: calc(100% - var(--ec-sidebar-w));
            <?php echo $isRtl ? 'margin-right' : 'margin-left'; ?>: var(--ec-sidebar-w);
            <?php echo $isRtl ? 'margin-left' : 'margin-right'; ?>: 0;
        }

        .author-topbar {
            min-height: 74px;
            background: #fff;
            border-bottom: 1px solid #e5e7eb;
            padding: 14px 24px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            gap: 14px;
            box-shadow: 0 8px 24px rgba(15,23,42,.04);
        }

        .author-topbar-title {
            font-weight: 900;
            font-size: 16px;
        }

        .author-topbar-subtitle {
            color: #64748b;
            font-size: 13px;
        }

        .author-badge {
            background: #e0f2fe;
            color: #075985;
            border-radius: 999px;
            padding: 7px 12px;
            font-weight: 900;
            font-size: 12px;
            white-space: nowrap;
        }

        .author-content {
            padding: 24px;
            max-width: 100%;
        }

        .author-content > * {
            max-width: 100%;
        }

        .container,
        .container-fluid {
            max-width: 100% !important;
        }

        .card,
        [class*="hero"],
        [class*="dashboard"] {
            max-width: 100%;
        }

        .table-responsive {
            width: 100%;
            overflow-x: auto;
            -webkit-overflow-scrolling: touch;
        }

        table {
            max-width: 100%;
        }

        th,
        td {
            vertical-align: middle;
        }

        td {
            overflow-wrap: anywhere;
        }

        .mobile-menu-btn {
            display: none;
            border: 0;
            border-radius: 999px;
            background: #1d4ed8;
            color: #fff;
            font-weight: 900;
            padding: 9px 13px;
            box-shadow: 0 12px 30px rgba(15,23,42,.20);
        }

        .author-overlay {
            display: none;
            position: fixed;
            inset: 0;
            z-index: 1030;
            background: rgba(15,23,42,.48);
        }

        body.author-sidebar-open .author-overlay {
            display: block;
        }

        @media (max-width: 1100px) {
            :root {
                --ec-sidebar-w: 250px;
            }

            .author-content {
                padding: 18px;
            }
        }

        @media (max-width: 900px) {
            .mobile-menu-btn {
                display: inline-flex;
                align-items: center;
                gap: 8px;
            }

            .author-sidebar {
                width: 285px;
                max-width: 86vw;
                transition: transform .22s ease;
                <?php if ($isRtl): ?>
                    transform: translateX(105%);
                <?php else: ?>
                    transform: translateX(-105%);
                <?php endif; ?>
            }

            body.author-sidebar-open .author-sidebar {
                transform: translateX(0);
            }

            .author-main {
                width: 100%;
                margin-left: 0;
                margin-right: 0;
            }

            .author-topbar {
                padding: 12px 14px;
                position: sticky;
                top: 0;
                z-index: 1020;
            }

            .author-content {
                padding: 14px;
            }
        }

        @media (max-width: 576px) {
            .author-topbar {
                align-items: flex-start;
                flex-direction: column;
            }

            .author-badge {
                white-space: normal;
            }

            h1, .h1 { font-size: 1.55rem !important; }
            h2, .h2 { font-size: 1.35rem !important; }
            h3, .h3 { font-size: 1.18rem !important; }

            table {
                font-size: 13px;
            }
        }
    </style>
</head>

<body class="<?php echo $isRtl ? 'rtl' : 'ltr'; ?>">
    <aside class="author-sidebar">
        <div class="author-brand">
            <i class="fa-solid fa-feather"></i>
            <span>EasyChair Author</span>
        </div>

        <div class="author-lang-switch">
            <a class="<?php echo $lang === 'ar' ? 'active' : ''; ?>" href="/easychair/lang?set=ar">عربي</a>
            <a class="<?php echo $lang === 'en' ? 'active' : ''; ?>" href="/easychair/lang?set=en">EN</a>
        </div>

        <div class="author-conf">
            <div class="short"><?php echo author_layout_h($conferenceShort); ?></div>
            <?php if ($conferenceTitle): ?>
                <div class="title"><?php echo author_layout_h($conferenceTitle); ?></div>
            <?php endif; ?>
        </div>

        <div class="author-user">
            <div class="name"><?php echo author_layout_h($userName); ?></div>
            <div class="meta"><?php echo author_layout_h($roleCode); ?><?php echo $userEmail ? ' | ' . author_layout_h($userEmail) : ''; ?></div>
        </div>

        <nav>
            <?php foreach ($links as $link): ?>
                <?php
                    $url = $link['url'];
                    $active = rtrim($currentPath, '/') === rtrim($url, '/')
                        || ($url === '/easychair/author/dashboard' && str_contains($currentPath, '/easychair/author/dashboard'));
                ?>
                <a class="author-link <?php echo $active ? 'active' : ''; ?>" href="<?php echo author_layout_h($url); ?>">
                    <i class="fa-solid <?php echo author_layout_h($link['icon']); ?>"></i>
                    <span><?php echo author_layout_h($link['label']); ?></span>
                </a>
            <?php endforeach; ?>
        </nav>
    </aside>

    <div class="author-overlay" onclick="document.body.classList.remove('author-sidebar-open')"></div>

    <main class="author-main">
        <div class="author-topbar">
            <button type="button" class="mobile-menu-btn" onclick="document.body.classList.toggle('author-sidebar-open')">
                <i class="fa-solid fa-bars"></i>
                <span><?php echo author_layout_h(author_layout_t('action.menu', 'Menu')); ?></span>
            </button>

            <div>
                <div class="author-topbar-title"><?php echo author_layout_h($userName); ?></div>
                <div class="author-topbar-subtitle"><?php echo author_layout_h(author_layout_t('author.dashboard.welcome', 'Author Workspace')); ?></div>
            </div>

            <div class="author-badge">
                <?php echo author_layout_h($conferenceShort); ?>
            </div>
        </div>

        <div class="author-content">
            <?php echo $content; ?>
        </div>
    </main>

    <script>
        document.addEventListener('DOMContentLoaded', function () {
            document.querySelectorAll('table').forEach(function (table) {
                if (!table.parentElement.classList.contains('table-responsive')) {
                    const wrapper = document.createElement('div');
                    wrapper.className = 'table-responsive';
                    table.parentNode.insertBefore(wrapper, table);
                    wrapper.appendChild(table);
                }
            });

            document.querySelectorAll('.author-sidebar a').forEach(function (a) {
                a.addEventListener('click', function () {
                    document.body.classList.remove('author-sidebar-open');
                });
            });
        });
    </script>
<?php
if (defined('BASE_PATH') && file_exists(BASE_PATH . '/app/views/partials/toast.php')) {
    require BASE_PATH . '/app/views/partials/toast.php';
}
?>
</body>
</html>
