<?php
$financeModuleEnabled = false;
$contractorModuleEnabled = false;
$spellcheckEnabled = true;

if (isset($admin_con) && isset($dbTenant)) {
    $stmt = $admin_con->prepare("SELECT finance_module, contractor_module FROM client_subscriptions WHERE subdomain_prefix = ?");
    $stmt->bind_param("s", $dbTenant);

    if ($stmt->execute()) {
        $result = $stmt->get_result();
        if ($row = $result->fetch_assoc()) {
            $financeModuleEnabled = ((int)$row['finance_module'] === 1);
            $contractorModuleEnabled = ((int)$row['contractor_module'] === 1);
        }
    }

    $stmt->close();
}

if (isset($GLOBALS['con'])) {
    $stmt = $GLOBALS['con']->prepare("SELECT * FROM `system` WHERE `id`='1'") or die(mysqli_error($GLOBALS['con']));
    if ($stmt->execute()) {
        $result = $stmt->get_result();
        if ($row = $result->fetch_assoc()) {
            $spellcheckEnabled = !array_key_exists('spellcheck_enabled', $row) || (int)$row['spellcheck_enabled'] === 1;
        }
    }
    $stmt->close();
}

/*
|--------------------------------------------------------------------------
| Tenant custom left-nav tools include
|--------------------------------------------------------------------------
| This looks for: /TENANT_URL/framework/left.php
| The included file should output <li class="sidenav-item">...</li> items.
*/
$tenantToolsNavPath = null;

if (defined('TENANT_URL') && trim((string)TENANT_URL) !== '') {
    $tenantFolder = trim((string)TENANT_URL, '/\\');

    $pathCandidates = [
        rtrim((string)($_SERVER['DOCUMENT_ROOT'] ?? ''), '/\\') . DIRECTORY_SEPARATOR . $tenantFolder . DIRECTORY_SEPARATOR . 'framework' . DIRECTORY_SEPARATOR . 'left.php',
        dirname(__DIR__) . DIRECTORY_SEPARATOR . $tenantFolder . DIRECTORY_SEPARATOR . 'framework' . DIRECTORY_SEPARATOR . 'left.php',
        dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . $tenantFolder . DIRECTORY_SEPARATOR . 'framework' . DIRECTORY_SEPARATOR . 'left.php',
        dirname(__DIR__, 3) . DIRECTORY_SEPARATOR . $tenantFolder . DIRECTORY_SEPARATOR . 'framework' . DIRECTORY_SEPARATOR . 'left.php'
    ];

    foreach ($pathCandidates as $candidate) {
        if ($candidate && file_exists($candidate)) {
            $tenantToolsNavPath = $candidate;
            break;
        }
    }
}
?>
<style>
#sidenav-1 {
    position: fixed;
    top: 0;
    bottom: 0;
    left: 0;
    height: 100vh;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

#sidenav-1 .sidenav-menu {
    flex: 1 1 auto;
    min-height: 0;
    overflow-y: auto;
    overflow-x: hidden;
    padding-bottom: 78px;
}

.sidenav-bottom-bar {
    position: absolute;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 20;
    background: rgba(33, 37, 41, 0.98);
    border-top: 1px solid rgba(255, 255, 255, 0.1);
}

.sidenav-bottom-bar .container-fluid {
    display: flex;
    align-items: center;
    justify-content: flex-start;
    gap: 1rem;
    min-height: 56px;
    padding-top: 0.5rem;
    padding-bottom: 0.5rem;
}

.sidenav-bottom-bar a {
    color: #fff;
    text-decoration: none;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font-size: 1.05rem;
}

.sidenav-bottom-bar a:hover,
.sidenav-bottom-bar a:focus {
    color: #fff;
    opacity: 0.85;
}

.sidenav-bg {
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    z-index: -1;
    background-attachment: fixed;
    background-image: url(<?= ROOT_URL ?>/images/sidenav1.jpg);
    background-repeat: no-repeat;
    background-position: center;
    background-size: cover;
    box-shadow: 0 2px 5px 0 rgba(0, 0, 0, 0.16), 0 2px 10px 0 rgba(0, 0, 0, 0.12);
    opacity: 0.1;
}
</style>

<nav data-mdb-sidenav-init id="sidenav-1" class="sidenav bg-dark text-light" data-mdb-mode="over" data-mdb-hidden="true"
    data-mdb-accordion="true" data-mdb-color="primary">
    <ul class="sidenav-menu">
        <li>
            <div class="logo-wrapper text-center border-bottom">
                <a href="index.php" class="site_title">
                    <span><img src="<?= ROOT_URL ?>/images/logo.png" class="img-fluid pt-2 pb-2"></span>
                </a>
            </div>
        </li>

        <li class="sidenav-item">
            <a class="sidenav-link" href="index.php?t=includes/master&p=client.php&view=list">
                <i class="me-3 fad fa-user-tie-hair"></i> Clients
            </a>
        </li>

        <?php if ($_SESSION['access'] == 1 || $_SESSION['access'] == 999) { ?>
        <?php if ($financeModuleEnabled) { ?>
        <li class="sidenav-item">
            <a class="sidenav-link">
                <i class="me-3 fad fa-chart-line"></i> Finance
                <i class="fad fa-angle-down rotate-icon"></i>
            </a>
            <ul class="sidenav-collapse">
                <li class="sidenav-item"><a class="sidenav-link"
                        href="index.php?t=includes/finance&p=finance_invoices.php">Invoices</a></li>
                <li class="sidenav-item"><a class="sidenav-link"
                        href="index.php?t=includes/finance&p=finance_outgoings.php">Register Outgoings</a></li>
                <li class="sidenav-item"><a class="sidenav-link"
                        href="index.php?t=includes/finance&p=finance_receipts.php">Register Receipts</a></li>
                <li class="sidenav-item"><a class="sidenav-link"
                        href="index.php?t=includes/finance&p=finance_expenses.php&payment_type=Expenses">Register
                        Expenses</a></li>
                <li class="sidenav-item"><a class="sidenav-link"
                        href="index.php?t=includes/finance&p=finance_vat.php">VAT Reporting</a></li>
                <li class="sidenav-item"><a class="sidenav-link"
                        href="index.php?t=includes/finance&p=finance_renewals.php">Check Renewals</a></li>
                <li class="sidenav-item"><a class="sidenav-link"
                        href="index.php?t=includes/finance&p=finance_commissions.php">Commission Reports</a></li>
                <li class="sidenav-item"><a class="sidenav-link"
                        href="index.php?t=includes/finance&p=finance_reports.php">Finance Reports</a></li>
                <li class="sidenav-item"><a class="sidenav-link"
                        href="index.php?t=includes/finance&p=statements.php">Statements</a></li>
                <li class="sidenav-item"><a class="sidenav-link"
                        href="index.php?t=includes/finance&p=finance_quotes.php">Quotations</a></li>
                <li class="sidenav-item"><a class="sidenav-link" href="index.php?t=includes/finance&p=jump.php">Jump
                        list Manager</a></li>
            </ul>
        </li>
        <?php } ?>

        <li class="sidenav-item">
            <a class="sidenav-link">
                <i class="me-3 fad fa-users"></i> Surveyor
                <i class="fad fa-angle-down rotate-icon"></i>
            </a>
            <ul class="sidenav-collapse">
                <li class="sidenav-item"><a class="sidenav-link"
                        href="index.php?t=includes/master&p=surveyor.php&view=new">New Surveyor</a></li>
                <li class="sidenav-item"><a class="sidenav-link"
                        href="index.php?t=includes/master&p=surveyor.php&view=list">List Surveyors</a></li>
                <li class="sidenav-item"><a class="sidenav-link"
                        href="index.php?t=includes/master&p=surveyor.php&view=assign">Quick Assign Surveyors</a></li>
            </ul>
        </li>

        <li class="sidenav-item">
            <a class="sidenav-link">
                <i class="me-3 fad fa-handshake"></i> Agents
                <i class="fad fa-angle-down rotate-icon"></i>
            </a>
            <ul class="sidenav-collapse">
                <li class="sidenav-item"><a class="sidenav-link"
                        href="index.php?t=includes/master&p=agent.php&view=new">New Agent</a></li>
                <li class="sidenav-item"><a class="sidenav-link"
                        href="index.php?t=includes/master&p=agent.php&view=list">List Agents</a></li>
            </ul>
        </li>

        <li class="sidenav-item">
            <a class="sidenav-link">
                <i class="me-3 fad fa-handshake"></i> Brokers
                <i class="fad fa-angle-down rotate-icon"></i>
            </a>
            <ul class="sidenav-collapse">
                <li class="sidenav-item"><a class="sidenav-link"
                        href="index.php?t=includes/master&p=broker.php&view=new">New Broker</a></li>
                <li class="sidenav-item"><a class="sidenav-link"
                        href="index.php?t=includes/master&p=broker.php&view=list">List Brokers</a></li>
            </ul>
        </li>

        <?php if ($contractorModuleEnabled) { ?>
        <li class="sidenav-item">
            <a class="sidenav-link">
                <i class="me-3 fad fa-handshake"></i> Contractors
                <i class="fad fa-angle-down rotate-icon"></i>
            </a>
            <ul class="sidenav-collapse">
                <li class="sidenav-item"><a class="sidenav-link"
                        href="index.php?t=includes/master&p=contractor.php&view=new">New Contractor</a></li>
                <li class="sidenav-item"><a class="sidenav-link"
                        href="index.php?t=includes/master&p=contractor.php&view=list">List Contractors</a></li>
                <li class="sidenav-item"><a class="sidenav-link"
                        href="index.php?t=includes/master&p=contractor_assign.php">Assign Contractors</a></li>
            </ul>
        </li>
        <?php } ?>

        <?php if (!empty($_SESSION['contractorNumber']) && $_SESSION['contractor_surveyor'] != 'yes') { ?>
        <li class="sidenav-item">
            <a class="sidenav-link">
                <i class="me-3 fad fa-users"></i> Surveyor
                <i class="fad fa-angle-down rotate-icon"></i>
            </a>
            <ul class="sidenav-collapse">
                <li class="sidenav-item"><a class="sidenav-link"
                        href="index.php?t=includes/master&p=surveyor.php&view=new">New Surveyor</a></li>
                <li class="sidenav-item"><a class="sidenav-link"
                        href="index.php?t=includes/master&p=surveyor.php&view=list">Manage Surveyors</a></li>
            </ul>
        </li>
        <?php } ?>
        <?php } ?>

        <?php if ($_SESSION['access'] == 1 || $_SESSION['access'] == 999) { ?>
        <li class="sidenav-item">
            <a class="sidenav-link">
                <i class="me-3 fad fa-wrench"></i> Tools
                <i class="fad fa-angle-down rotate-icon"></i>
            </a>
            <ul class="sidenav-collapse">
                <li class="sidenav-item"><a class="sidenav-link"
                        href="index.php?t=includes/master&p=address.php&view=new">Address Book</a></li>

                <?php if ($_SESSION['access'] == 999) { ?>
                <li class="sidenav-item"><a class="sidenav-link"
                        href="index.php?t=includes/master&p=admin.php&view=list">Admin Manager</a></li>
                <?php } ?>

                <li class="sidenav-item"><a class="sidenav-link"
                        href="index.php?t=includes/asset&p=asset_search.php">Asset Search</a></li>

                <?php if ($_SESSION['access'] == 1 || $_SESSION['access'] == 999) { ?>
                <li class="sidenav-item"><a class="sidenav-link"
                        href="index.php?t=assetmanager&p=settings.php">Asset Manager Settings</a></li>
                <?php } ?>

                <?php if ($_SESSION['access'] == 999) { ?>
                <li class="sidenav-item"><a class="sidenav-link"
                        href="index.php?t=includes/master&p=company.php">Company Manager</a></li>
                <?php } ?>

                <?php if ($_SESSION['access'] == 999) { ?>
                <li class="sidenav-item"><a class="sidenav-link" href="index.php?t=includes/email&p=tracking.php">Email
                        Tracking</a></li>
                <?php } ?>

                <li class="sidenav-item"><a class="sidenav-link" href="index.php?t=includes/master&p=text.php">Default
                        Text</a></li>
                <?php if ($spellcheckEnabled) { ?>
                <li class="sidenav-item"><a class="sidenav-link"
                        href="index.php?t=includes/master&p=dictionary.php">Dictionary</a></li>
                <?php } ?>

                <?php if ($_SESSION['access'] == 999) { ?>
                <li class="sidenav-item"><a class="sidenav-link"
                        href="index.php?t=includes/master&p=form_select.php">Form Select</a></li>
                        <li class="sidenav-item"><a class="sidenav-link"
                        href="index.php?t=includes/master&p=reptypes.php">Report Type Titles</a></li>
                <li class="sidenav-item"><a class="sidenav-link"
                        href="index.php?t=includes/formgen&p=form_nature.php">Nature list</a></li>
                <?php } ?>

                <li class="sidenav-item"><a class="sidenav-link"
                        href="index.php?t=includes/master&p=system_totals.php">1000ft View</a></li>
                <li class="sidenav-item"><a class="sidenav-link" href="index.php?t=includes/master&p=hse.php">HSE
                        Directory</a></li>
                <li class="sidenav-item"><a class="sidenav-link"
                        href="index.php?t=includes/master&p=inspection_reports.php">Inspection Reports</a></li>
                <li class="sidenav-item"><a class="sidenav-link" href="index.php?t=includes/master&p=jump.php">Jump List
                        Manager</a></li>
                <li class="sidenav-item"><a class="sidenav-link"
                        href="index.php?t=includes/master&p=location_report.php">Location Reports</a></li>
                <li class="sidenav-item"><a class="sidenav-link"
                        href="index.php?t=includes/master&p=repArchive.php">Report Archive</a></li>

                <?php if ($_SESSION['access'] == 999) { ?>
                <li class="sidenav-item"><a class="sidenav-link" href="index.php?t=includes/master&p=system.php">System
                        Manager</a></li>
                <?php } ?>

                <!--<li class="sidenav-item"><a class="sidenav-link" href="index.php?t=includes/master&p=users.php">User Manager</a></li>-->
                <li class="sidenav-item"><a class="sidenav-link"
                        href="index.php?t=includes/master&p=vehicles.php">Vehicle Manager</a></li>

                <?php if ($_SESSION['access'] == 999) { ?>
                <li class="sidenav-item"><a class="sidenav-link" href="index.php?t=includes/master&p=log.php">Log
                        Manager</a></li>
                <li class="sidenav-item"><a class="sidenav-link" href="index.php?t=includes/master&p=backup.php">System
                        Backup</a></li>
                <?php } ?>

                <?php
                    if ($tenantToolsNavPath !== null) {
                        include $tenantToolsNavPath;
                    }
                    ?>
            </ul>
        </li>
        <?php } ?>

        <?php if (empty($_SESSION['contractorNumber'])) { ?>
        <!--<li class="sidenav-item"><a class="sidenav-link" href="index.php?t=includes/master&p=diary.php&month=<?= date('n') ?>&longdate=<?= date('F') ?>&year=<?= date('Y') ?>&view=days"><i class="me-3 fad fa-calendar-alt"></i> Diary</a></li>-->
        <li class="sidenav-item">
            <a class="sidenav-link" href="index.php?f=filebrowse&p=assetmain.php">
                <i class="me-3 fad fa-file-alt"></i> Documents
            </a>
        </li>
        <?php } ?>

        <?php if ($_SESSION['usertype'] == 'admins') { ?>
        <li class="spacer"></li>
        <li class="sidenav-item">
            <a class="sidenav-link" href="index.php?t=includes/master&p=request_admin.php">
                <i class="me-3 fad fa-ticket"></i> Client Requests
            </a>
        </li>
        <li class="sidenav-item">
            <a class="sidenav-link" href="index.php?t=includes/support&p=support_tickets.php">
                <i class="me-3 fad fa-ticket"></i> Support Ticket
            </a>
        </li>
        <?php } ?>
    </ul>

    <div class="sidenav-bottom-bar bg-dark text-light">
        <div class="container-fluid">
            <?php if ($_SESSION['access'] == 1) { ?>
            <a href="index.php?t=includes/master&p=settings.php" data-mdb-tooltip-init data-mdb-placement="top"
                data-mdb-trigger="hover" title="Settings">
                <i class="fad fa-cog" aria-hidden="true"></i>
            </a>
            <?php } ?>

            <a onclick="loadcalc()" data-mdb-tooltip-init data-mdb-placement="top" data-mdb-trigger="hover"
                title="Calculator">
                <i id="fsButton" class="fad fa-calculator" aria-hidden="true"></i>
            </a>

            <a href="index.php?op=logout" data-mdb-tooltip-init data-mdb-placement="top" data-mdb-trigger="hover"
                title="Logout">
                <i class="fad fa-sign-out" aria-hidden="true"></i>
            </a>
        </div>
    </div>

    <div class="sidenav-bg mask-strong"></div>
</nav>
