<?php
session_start();

require_once __DIR__ . '/../includes/helpers.php';
require_once __DIR__ . '/../includes/layout.php';

if (!isset($_SESSION['authenticated'])) {
    redirect(page_url('signin'));
}

$userContext = current_user_context();
$workspace = trim((string) ($userContext['view_mini'] ?? 'GT'));
if ($workspace === '') {
    $workspace = 'GT';
}

$organizationSettings = [
    [
        'title' => 'Organization',
        'icon' => 'fa-regular fa-building',
        'tone' => 'is-green',
        'items' => [
            ['label' => 'Organization Profile', 'href' => page_url('organization')],
            ['label' => 'Branding', 'href' => page_url('organization')],
            ['label' => 'Departments', 'href' => page_url('organization')],
            ['label' => 'AI Preferences', 'href' => page_url('organization')],
            ['label' => 'Subscription', 'href' => 'https://b2lmobitech.in/', 'external' => true],
        ],
    ],
    [
        'title' => 'Users & Roles',
        'icon' => 'fa-regular fa-circle-user',
        'tone' => 'is-blue',
        'items' => [
            ['label' => 'Users', 'href' => page_url('organization')],
            ['label' => 'Roles & Permissions', 'href' => page_url('organization')],
        ],
        'subgroup' => [
            'title' => 'Taxes & Compliance',
            'icon' => 'fa-regular fa-file-lines',
            'tone' => 'is-red',
            'items' => [
                ['label' => 'Taxes', 'href' => page_url('organization')],
            ],
        ],
    ],
    [
        'title' => 'Setup & Configuration',
        'icon' => 'fa-solid fa-sliders',
        'tone' => 'is-amber',
        'items' => [
            ['label' => 'Currencies', 'href' => page_url('organization')],
            ['label' => 'Policies', 'href' => page_url('organization')],
        ],
    ],
    [
        'title' => 'Customization',
        'icon' => 'fa-regular fa-id-card',
        'tone' => 'is-green',
        'items' => [
            ['label' => 'Tags', 'href' => page_url('my_settings')],
            ['label' => 'PDF Templates', 'href' => page_url('my_settings')],
            ['label' => 'Email Templates', 'href' => page_url('my_settings')],
            ['label' => 'SMS Notifications', 'href' => page_url('my_settings')],
            ['label' => 'Web Tabs', 'href' => page_url('my_settings')],
        ],
    ],
    [
        'title' => 'Automation',
        'icon' => 'fa-regular fa-calendar-check',
        'tone' => 'is-blue',
        'items' => [
            ['label' => 'Report Automation', 'href' => page_url('reports')],
            ['label' => 'Workflow Rules', 'href' => page_url('organization')],
            ['label' => 'Workflow Actions', 'href' => page_url('organization')],
            ['label' => 'Workflow Logs', 'href' => page_url('organization')],
            ['label' => 'Schedules', 'href' => page_url('organization')],
        ],
    ],
];

$moduleSettings = [
    [
        'title' => 'General',
        'icon' => 'fa-regular fa-rectangle-list',
        'tone' => 'is-amber',
        'items' => [
            ['label' => 'Categories', 'href' => page_url('my_settings', 'navType=preferences&dialog=expense-category')],
            ['label' => 'Merchants', 'href' => page_url('my_settings')],
            ['label' => 'Customers', 'href' => page_url('my_settings')],
            ['label' => 'Projects', 'href' => page_url('my_settings')],
        ],
        'subgroup' => [
            'title' => 'Payments',
            'icon' => 'fa-regular fa-money-bill-1',
            'tone' => 'is-green',
            'items' => [
                ['label' => 'Online Reimbursements', 'href' => page_url('my_settings')],
                ['label' => 'Paid Through Accounts', 'href' => page_url('my_settings')],
            ],
        ],
    ],
    [
        'title' => 'Travel & Expense',
        'icon' => 'fa-regular fa-bookmark',
        'tone' => 'is-red',
        'items' => [
            ['label' => 'Trips', 'href' => page_url('trips')],
            ['label' => 'Expenses', 'href' => page_url('expenses')],
            ['label' => 'Reports', 'href' => page_url('reports')],
            ['label' => 'Advances', 'href' => page_url('advances')],
            ['label' => 'Petty Cash Accounts', 'href' => page_url('organization')],
        ],
    ],
    [
        'title' => 'Custom Modules',
        'icon' => 'fa-solid fa-sliders',
        'tone' => 'is-amber',
        'items' => [
            ['label' => 'Overview', 'href' => page_url('organization')],
        ],
    ],
];

render_header('Admin Settings | Expense', 'admin-settings-page');
?>
<div class="admin-settings-shell">
    <header class="admin-settings-topbar">
        <a class="admin-settings-back" href="<?= e(page_url('admin')) ?>" aria-label="Back to admin">
            <i class="fa-solid fa-arrow-left" aria-hidden="true"></i>
        </a>

        <div class="admin-settings-brand">
            <div class="admin-settings-brand-icon">
                <i class="fa-regular fa-flag" aria-hidden="true"></i>
            </div>
            <div>
                <h1>All Settings</h1>
                <p><?= e(strtolower($workspace)) ?></p>
            </div>
        </div>

        <label class="admin-settings-search" data-admin-settings-search>
            <i class="fa-solid fa-magnifying-glass" aria-hidden="true"></i>
            <input type="search" placeholder="Search Settings" aria-label="Search Settings" data-admin-settings-search-input autocomplete="off">
            <button type="button" class="admin-settings-search-clear" data-admin-settings-search-clear aria-label="Clear search" hidden>
                <i class="fa-solid fa-xmark" aria-hidden="true"></i>
            </button>
            <div class="admin-settings-search-results" data-admin-settings-search-results hidden></div>
        </label>

        <a class="admin-settings-close" href="<?= e(page_url('admin')) ?>">
            <span>Close Settings</span>
            <i class="fa-solid fa-xmark" aria-hidden="true"></i>
        </a>
    </header>

    <main class="admin-settings-content">
        <section class="admin-settings-section">
            <h2>Organization Settings</h2>
            <div class="admin-settings-grid admin-settings-grid--five">
                <?php foreach ($organizationSettings as $card): ?>
                    <article class="admin-settings-card" data-admin-settings-card data-admin-settings-group="<?= e($card['title']) ?>">
                        <div class="admin-settings-card-head">
                            <span class="admin-settings-chip <?= e($card['tone']) ?>">
                                <i class="<?= e($card['icon']) ?>" aria-hidden="true"></i>
                            </span>
                            <strong><?= e($card['title']) ?></strong>
                        </div>

                        <ul class="admin-settings-links">
                            <?php foreach ($card['items'] as $item): ?>
                                <li>
                                    <a href="<?= e($item['href']) ?>"<?= !empty($item['external']) ? ' target="_blank" rel="noopener noreferrer"' : '' ?>>
                                        <span data-admin-settings-item><?= e($item['label']) ?></span>
                                        <?php if (!empty($item['external'])): ?><i class="fa-solid fa-arrow-up-right-from-square" aria-hidden="true"></i><?php endif; ?>
                                    </a>
                                </li>
                            <?php endforeach; ?>
                        </ul>

                        <?php if (!empty($card['subgroup'])): ?>
                            <div class="admin-settings-subgroup" data-admin-settings-group="<?= e((string) ($card['subgroup']['title'] ?? '')) ?>">
                                <div class="admin-settings-card-head admin-settings-card-head--sub">
                                    <span class="admin-settings-chip <?= e((string) ($card['subgroup']['tone'] ?? '')) ?>">
                                        <i class="<?= e((string) ($card['subgroup']['icon'] ?? 'fa-regular fa-square')) ?>" aria-hidden="true"></i>
                                    </span>
                                    <strong><?= e((string) ($card['subgroup']['title'] ?? '')) ?></strong>
                                </div>
                                <ul class="admin-settings-links">
                                    <?php foreach ((array) ($card['subgroup']['items'] ?? []) as $item): ?>
                                        <li><a href="<?= e((string) ($item['href'] ?? '#')) ?>"><span data-admin-settings-item><?= e((string) ($item['label'] ?? '')) ?></span></a></li>
                                    <?php endforeach; ?>
                                </ul>
                            </div>
                        <?php endif; ?>
                    </article>
                <?php endforeach; ?>
            </div>
        </section>

        <section class="admin-settings-section">
            <h2>Module Settings</h2>
            <div class="admin-settings-grid admin-settings-grid--three">
                <?php foreach ($moduleSettings as $card): ?>
                    <article class="admin-settings-card" data-admin-settings-card data-admin-settings-group="<?= e($card['title']) ?>">
                        <div class="admin-settings-card-head">
                            <span class="admin-settings-chip <?= e($card['tone']) ?>">
                                <i class="<?= e($card['icon']) ?>" aria-hidden="true"></i>
                            </span>
                            <strong><?= e($card['title']) ?></strong>
                        </div>

                        <ul class="admin-settings-links">
                            <?php foreach ($card['items'] as $item): ?>
                                <li><a href="<?= e($item['href']) ?>"><span data-admin-settings-item><?= e($item['label']) ?></span></a></li>
                            <?php endforeach; ?>
                        </ul>

                        <?php if (!empty($card['subgroup'])): ?>
                            <div class="admin-settings-subgroup" data-admin-settings-group="<?= e((string) ($card['subgroup']['title'] ?? '')) ?>">
                                <div class="admin-settings-card-head admin-settings-card-head--sub">
                                    <span class="admin-settings-chip <?= e((string) ($card['subgroup']['tone'] ?? '')) ?>">
                                        <i class="<?= e((string) ($card['subgroup']['icon'] ?? 'fa-regular fa-square')) ?>" aria-hidden="true"></i>
                                    </span>
                                    <strong><?= e((string) ($card['subgroup']['title'] ?? '')) ?></strong>
                                </div>
                                <ul class="admin-settings-links">
                                    <?php foreach ((array) ($card['subgroup']['items'] ?? []) as $item): ?>
                                        <li><a href="<?= e((string) ($item['href'] ?? '#')) ?>"><span data-admin-settings-item><?= e((string) ($item['label'] ?? '')) ?></span></a></li>
                                    <?php endforeach; ?>
                                </ul>
                            </div>
                        <?php endif; ?>
                    </article>
                <?php endforeach; ?>
            </div>
        </section>
    </main>
</div>
<?php render_footer(); ?>
