<!DOCTYPE html>
<html lang="<?= service('request')->getLocale() ?>" dir="<?= service('request')->getLocale() === 'ar' ? 'rtl' : 'ltr' ?>">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title><?= $title ?? lang('App.dashboard') ?></title>
    <?php $isRtl = service('request')->getLocale() === 'ar'; ?>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/<?= $isRtl ? 'bootstrap.rtl.min.css' : 'bootstrap.min.css' ?>" rel="stylesheet">
    <?php if ($isRtl): ?>
        <link href="https://fonts.googleapis.com/css2?family=Tajawal:wght@400;500;700&display=swap" rel="stylesheet">
        <style>body{font-family:'Tajawal',sans-serif;}</style>
    <?php endif; ?>
    <style>
        body { background:#f5f6fa; }
        .sidebar { min-height:100vh; background:#1e1e2d; }
        .sidebar a { color:#c5c7d0; display:block; padding:10px 16px; text-decoration:none; border-radius:6px; }
        .sidebar a:hover, .sidebar a.active { background:#2b2b40; color:#fff; }
        .card { border:none; box-shadow:0 1px 4px rgba(0,0,0,.06); }
    </style>
</head>
<body>
<div class="d-flex">
    <div class="sidebar p-3" style="width:230px;">
        <h5 class="text-white mb-3">🏢 <?= esc(session()->get('admin_name')) ?></h5>

        <?php if (session()->get('is_super_admin')): ?>
            <?php $companies = (new \App\Models\CompanyModel())->findAll(); ?>
            <div class="mb-3">
                <label class="text-white-50 small d-block mb-1">الشركة النشطة</label>
                <select class="form-select form-select-sm" onchange="location.href='/admin/switch-company/' + this.value">
                    <?php foreach ($companies as $co): ?>
                        <option value="<?= $co['id'] ?>" <?= session()->get('active_company_id') == $co['id'] ? 'selected' : '' ?>>
                            <?= esc($co['name']) ?>
                        </option>
                    <?php endforeach; ?>
                </select>
            </div>
        <?php endif; ?>

        <a href="/admin/dashboard"><?= lang('App.dashboard') ?></a>
        <a href="/admin/clients"><?= lang('App.clients') ?></a>
        <a href="/admin/services"><?= lang('App.services') ?></a>
        <a href="/admin/invoices"><?= lang('App.invoices') ?></a>
        <a href="/admin/quotes"><?= lang('App.quotes') ?></a>
        <a href="/admin/expenses"><?= lang('App.expenses') ?></a>
        <a href="/admin/products"><?= lang('App.products') ?></a>
        <a href="/admin/reports/profit"><?= lang('App.reports') ?></a>
        <a href="/admin/logs"><?= lang('App.logs') ?></a>
        <hr class="text-white-50 my-2">
        <a href="/admin/site-settings">🌐 إعدادات الموقع</a>
        <a href="/admin/site-slides">سلايدر الموقع</a>
        <a href="/admin/site-testimonials">آراء العملاء</a>
        <a href="/admin/contact-messages">الرسائل الواردة</a>
        <a href="/admin/marketers"><?= lang('App.marketers') ?></a>
        <a href="/admin/exchange-rates"><?= lang('App.exchange_rates') ?></a>
        <?php if (session()->get('is_super_admin')): ?>
            <a href="/admin/companies"><?= lang('App.companies') ?></a>
        <?php endif; ?>
        <a href="/admin/users"><?= lang('App.users') ?></a>
        <a href="/admin/settings"><?= lang('App.settings') ?></a>
        <a href="/admin/currencies"><?= lang('App.currencies') ?></a>
        <hr class="text-white-50">
        <a href="?lang=<?= $isRtl ? 'en' : 'ar' ?>"><?= $isRtl ? 'English' : 'العربية' ?></a>
        <form method="post" action="/admin/logout" class="mt-2">
            <?= csrf_field() ?>
            <button class="btn btn-sm btn-outline-light w-100"><?= lang('App.logout') ?></button>
        </form>
    </div>
    <div class="flex-grow-1 p-4">
        <?php if (session()->getFlashdata('message')): ?>
            <div class="alert alert-success"><?= session()->getFlashdata('message') ?></div>
        <?php endif; ?>
        <?php if (session()->getFlashdata('error')): ?>
            <div class="alert alert-danger"><?= session()->getFlashdata('error') ?></div>
        <?php endif; ?>
        <?= $this->renderSection('content') ?>
    </div>
</div>
</body>
</html>
