<?php

require_once "includes/inc_all.php";

// Initialize the HTML Purifier to prevent XSS
require "../libs/htmlpurifier/HTMLPurifier.standalone.php";

$purifier_config = HTMLPurifier_Config::createDefault();
$purifier_config->set('URI.AllowedSchemes', ['data' => true, 'src' => true, 'http' => true, 'https' => true]);
$purifier = new HTMLPurifier($purifier_config);

if (isset($_GET['query'])) {

    $query = escapeSql($_GET['query']);

    $phone_query = preg_replace("/[^0-9]/", '', $query);
    if (empty($phone_query)) {
        $phone_query = $query;
    }

    $ticket_num_query = str_replace("$config_ticket_prefix", "", "$query");

    // Every dedicated page gates on its module, so search must too - otherwise this
    // page hands a role results it has no access to read anywhere else (see the
    // credentials panel, which renders plaintext usernames and passwords)
    $can_client     = lookupUserPermission('module_client')     >= 1;
    $can_support    = lookupUserPermission('module_support')    >= 1;
    $can_sales      = lookupUserPermission('module_sales')      >= 1;
    $can_credential = lookupUserPermission('module_credential') >= 1;

    $sql_clients = !$can_client ? false : mysqli_query($mysqli, "SELECT client_id, client_name, client_website, location_phone, location_phone_country_code
        FROM clients
        LEFT JOIN locations ON clients.client_id = locations.location_client_id AND location_primary = 1
        WHERE client_archived_at IS NULL
            AND (client_name LIKE '%$query%' OR client_abbreviation LIKE '%$query%')
            " . clientScopeSql('clients.client_id') . "
        ORDER BY client_id DESC LIMIT 5"
    );

    $sql_contacts = !$can_client ? false : mysqli_query($mysqli, "SELECT client_id, client_name, contact_department, contact_email, contact_extension, contact_id,
        contact_mobile, contact_mobile_country_code, contact_name, contact_phone,
        contact_phone_country_code, contact_title
        FROM contacts
        LEFT JOIN clients ON client_id = contact_client_id
        WHERE contact_archived_at IS NULL
            AND (contact_name LIKE '%$query%'
            OR contact_title LIKE '%$query%'
            OR contact_email LIKE '%$query%'
            OR contact_phone LIKE '%$phone_query%'
            OR contact_mobile LIKE '%$phone_query%')
            " . clientScopeSql('contact_client_id') . "
        ORDER BY contact_id DESC LIMIT 5"
    );

    $sql_vendors = !$can_client ? false : mysqli_query($mysqli, "SELECT client_id, client_name, vendor_description, vendor_name, vendor_phone,
        vendor_phone_country_code
        FROM vendors
        LEFT JOIN clients ON vendor_client_id = client_id
        WHERE vendor_archived_at IS NULL
            AND (vendor_name LIKE '%$query%' OR vendor_phone LIKE '%$phone_query%')
            " . clientScopeSql('vendor_client_id') . "
        ORDER BY vendor_id DESC LIMIT 5"
    );

    $sql_domains = !$can_support ? false : mysqli_query($mysqli, "SELECT client_id, client_name, domain_expire, domain_id, domain_name
        FROM domains
        LEFT JOIN clients ON domain_client_id = client_id
        WHERE domain_archived_at IS NULL
            AND domain_name LIKE '%$query%'
            " . clientScopeSql('domain_client_id') . "
        ORDER BY domain_id DESC LIMIT 5"
    );

    $sql_products = !$can_sales ? false : mysqli_query($mysqli, "SELECT product_description, product_name
        FROM products
        WHERE product_archived_at IS NULL
            AND product_name LIKE '%$query%'
        ORDER BY product_id DESC LIMIT 5"
    );

    $sql_documents = !$can_support ? false : mysqli_query($mysqli, "SELECT client_name, document_client_id, document_id, document_name
        FROM documents
        LEFT JOIN clients on document_client_id = clients.client_id
        WHERE document_archived_at IS NULL
            AND MATCH(document_content_raw) AGAINST ('$query')
            " . clientScopeSql('document_client_id') . "
        ORDER BY document_id DESC LIMIT 5"
    );

    $sql_files = !$can_support ? false : mysqli_query($mysqli, "SELECT client_name, file_client_id, file_description, file_id, file_name, folder_id, folder_name
        FROM files
        LEFT JOIN clients ON file_client_id = client_id
        LEFT JOIN folders ON folder_id = file_folder_id
        WHERE file_archived_at IS NULL
            AND (file_name LIKE '%$query%'
            OR file_description LIKE '%$query%')
            " . clientScopeSql('file_client_id') . "
        ORDER BY file_id DESC LIMIT 5"
    );

    $sql_tickets = !$can_support ? false : mysqli_query($mysqli, "SELECT client_name, ticket_client_id, ticket_id, ticket_number, ticket_prefix, ticket_status_name,
        ticket_subject
        FROM tickets
        LEFT JOIN clients on tickets.ticket_client_id = clients.client_id
        LEFT JOIN ticket_statuses ON ticket_status = ticket_status_id
        WHERE ticket_archived_at IS NULL
            AND (ticket_subject LIKE '%$query%'
            OR ticket_details LIKE '%$query%'
            OR CONCAT(ticket_prefix,ticket_number) LIKE '%$query%'
            OR ticket_number = '$ticket_num_query')
            " . clientScopeSql('tickets.ticket_client_id') . "
        ORDER BY ticket_id DESC LIMIT 5"
    );

    $sql_recurring_tickets = !$can_support ? false : mysqli_query($mysqli, "SELECT client_id, client_name, recurring_ticket_frequency, recurring_ticket_id,
        recurring_ticket_next_run, recurring_ticket_subject
        FROM recurring_tickets
        LEFT JOIN clients ON recurring_ticket_client_id = client_id
        WHERE (recurring_ticket_subject LIKE '%$query%'
            OR recurring_ticket_details LIKE '%$query%')
            " . clientScopeSql('recurring_ticket_client_id') . "
        ORDER BY recurring_ticket_id DESC LIMIT 5"
    );

    $sql_credentials = !$can_credential ? false : mysqli_query($mysqli, "SELECT client_id, client_name, credential_client_id, credential_description, credential_name,
        credential_password, credential_username
        FROM credentials
        LEFT JOIN contacts ON credential_contact_id = contact_id
        LEFT JOIN clients ON credential_client_id = client_id
        WHERE credential_archived_at IS NULL
            AND (credential_name LIKE '%$query%' OR credential_description LIKE '%$query%')
            " . clientScopeSql('credential_client_id') . "
        ORDER BY credential_id DESC LIMIT 5"
    );

    $sql_quotes = !$can_sales ? false : mysqli_query($mysqli, "SELECT client_id, client_name, quote_amount, quote_currency_code, quote_id, quote_number,
        quote_prefix, quote_status
        FROM quotes
        LEFT JOIN clients ON quote_client_id = client_id
        LEFT JOIN categories ON quote_category_id = category_id
        WHERE quote_archived_at IS NULL
            AND (CONCAT(quote_prefix,quote_number) LIKE '%$query%' OR quote_number LIKE '%$query%' OR quote_scope LIKE '%$query%')
            " . clientScopeSql('quote_client_id') . "
        ORDER BY quote_number DESC LIMIT 5"
    );

    $sql_invoices = !$can_sales ? false : mysqli_query($mysqli, "SELECT client_id, client_name, invoice_amount, invoice_currency_code, invoice_id, invoice_number,
        invoice_prefix, invoice_status
        FROM invoices
        LEFT JOIN clients ON invoice_client_id = client_id
        LEFT JOIN categories ON invoice_category_id = category_id
        WHERE invoice_archived_at IS NULL
            AND (CONCAT(invoice_prefix,invoice_number) LIKE '%$query%' OR invoice_number LIKE '%$query%' OR invoice_scope LIKE '%$query%')
            " . clientScopeSql('invoice_client_id') . "
        ORDER BY invoice_number DESC LIMIT 5"
    );

    $sql_assets = !$can_support ? false : mysqli_query($mysqli,"SELECT asset_client_id, asset_contact_id, asset_created_at, asset_description, asset_id,
        asset_location_id, asset_make, asset_model, asset_name, asset_serial, asset_status, asset_type,
        asset_uri, client_name, contact_archived_at, contact_id, contact_name
        FROM assets
        LEFT JOIN contacts ON asset_contact_id = contact_id
        LEFT JOIN locations ON asset_location_id = location_id
        LEFT JOIN clients ON asset_client_id = client_id
        LEFT JOIN asset_interfaces ON interface_asset_id = asset_id AND interface_primary = 1
        WHERE asset_archived_at IS NULL
            AND (asset_name LIKE '%$query%' OR asset_description LIKE '%$query%' OR asset_type LIKE '%$query%' OR asset_make LIKE '%$query%' OR asset_model LIKE '%$query%' OR asset_serial LIKE '%$query%' OR asset_os LIKE '%$query%' OR interface_ip LIKE '%$query%' OR interface_nat_ip LIKE '%$query%' OR interface_mac LIKE '%$query%' OR asset_status LIKE '%$query%')
            " . clientScopeSql('asset_client_id') . "
        ORDER BY asset_name DESC LIMIT 5"
    );

    $sql_ticket_replies = !$can_support ? false : mysqli_query($mysqli,"SELECT client_name, ticket_client_id, ticket_id, ticket_number, ticket_prefix, ticket_reply,
        ticket_subject
        FROM ticket_replies
        LEFT JOIN tickets ON ticket_reply_ticket_id = ticket_id
        LEFT JOIN clients ON ticket_client_id = client_id
        WHERE ticket_reply_archived_at IS NULL
            AND (ticket_reply LIKE '%$query%')
            " . clientScopeSql('ticket_client_id') . "
        ORDER BY ticket_id DESC, ticket_reply_id ASC LIMIT 20"
    );

    $q = escapeHtml($_GET['query']);

    ?>

<div class="card card-dark">
    <div class="card-header mb-3">
        <h4 class="card-title text-center"><i class="fas fa-fw fa-search me-2"></i>Global Search</h4>
    </div>

    <div class="card-body">

    <div class="row">
        <?php if ($sql_clients && mysqli_num_rows($sql_clients) > 0) { ?>

            <!-- Clients-->

            <div class="col-sm-6">
                <div class="card card-dark mb-3">
                    <div class="card-header">
                        <h6 class="card-title"><i class="fas fa-fw fa-users me-2"></i>Clients</h6>
                    </div>
                    <div class="card-body">
                        <table class="table table-striped table-borderless">
                            <thead>
                            <tr>
                                <th>Name</th>
                                <th>Phone</th>
                            </tr>
                            </thead>
                            <tbody>
                            <?php

                            while ($row = mysqli_fetch_assoc($sql_clients)) {
                                $client_id = intval($row['client_id']);
                                $client_name = escapeHtml($row['client_name']);
                                $location_phone_country_code = escapeHtml($row['location_phone_country_code']);
                                $location_phone = escapeHtml(formatPhoneNumber($row['location_phone'], $location_phone_country_code));
                                $client_website = escapeHtml($row['client_website']);

                                ?>
                                <tr>
                                    <td><a href="client_overview.php?client_id=<?= $client_id ?>"><?= $client_name ?></a></td>
                                    <td><?= $location_phone ?></td>
                                </tr>

                            <?php } ?>

                            </tbody>
                        </table>
                    </div>
                </div>
            </div>

        <?php } ?>

        <?php if ($sql_contacts && mysqli_num_rows($sql_contacts) > 0) { ?>

            <!-- Contacts-->

            <div class="col-sm-6">
                <div class="card card-dark mb-3">
                    <div class="card-header">
                        <h6 class="card-title"><i class="fas fa-fw fa-users me-2"></i>Contacts</h6>
                    </div>
                    <div class="card-body">
                        <table class="table table-striped table-borderless">
                            <thead>
                            <tr>
                                <th>Name</th>
                                <th>Email</th>
                                <th>Phone</th>
                                <th>Cell</th>
                                <th>Client</th>
                            </tr>
                            </thead>
                            <tbody>
                            <?php

                            while ($row = mysqli_fetch_assoc($sql_contacts)) {
                                $contact_id = intval($row['contact_id']);
                                $contact_name = escapeHtml($row['contact_name']);
                                $contact_title = escapeHtml($row['contact_title']);
                                $contact_phone_country_code = escapeHtml($row['contact_phone_country_code']);
                                $contact_phone = escapeHtml(formatPhoneNumber($row['contact_phone'], $contact_phone_country_code));
                                $contact_extension = escapeHtml($row['contact_extension']);
                                $contact_mobile_country_code = escapeHtml($row['contact_mobile_country_code']);
                                $contact_mobile = escapeHtml(formatPhoneNumber($row['contact_mobile'], $contact_mobile_country_code));
                                $contact_email = escapeHtml($row['contact_email']);
                                $client_id = intval($row['client_id']);
                                $client_name = escapeHtml($row['client_name']);
                                $contact_department = escapeHtml($row['contact_department']);

                                ?>
                                <tr>
                                    <td><a href="contact.php?client_id=<?= $client_id ?>&contact_id=<?= $contact_id ?>"><?= $contact_name ?></a>
                                        <br><small class="text-secondary"><?= $contact_title ?></small>
                                    </td>
                                    <td><?= $contact_email ?></td>
                                    <td><?= "$contact_phone $contact_extension" ?></td>
                                    <td><?= $contact_mobile ?></td>
                                    <td><a href="client_overview.php?client_id=<?= $client_id ?>"><?= $client_name ?></a></td>
                                </tr>

                            <?php } ?>


                            </tbody>
                        </table>
                    </div>
                </div>
            </div>

        <?php } ?>

        <?php if ($sql_vendors && mysqli_num_rows($sql_vendors) > 0) { ?>

            <!-- Vendors -->
            <div class="col-sm-6">
                <div class="card card-dark mb-3">
                    <div class="card-header">
                        <h6 class="card-title"><i class="fas fa-fw fa-building me-2"></i>Vendors</h6>
                    </div>
                    <div class="card-body">
                        <table class="table table-striped table-borderless">
                            <thead>
                            <tr>
                                <th>Name</th>
                                <th>Description</th>
                                <th>Phone</th>
                                <th>Client</th>
                            </tr>
                            </thead>
                            <tbody>
                            <?php

                            while ($row = mysqli_fetch_assoc($sql_vendors)) {
                                $vendor_name = escapeHtml($row['vendor_name']);
                                $vendor_description = escapeHtml($row['vendor_description']);
                                $vendor_phone_country_code = escapeHtml($row['vendor_phone_country_code']);
                                $vendor_phone = escapeHtml(formatPhoneNumber($row['vendor_phone'], $vendor_phone_country_code));
                                $client_id = intval($row['client_id']);
                                $client_name = escapeHtml($row['client_name']);

                                ?>
                                <tr>
                                    <td><a href="vendors.php?q=<?= $q ?>"><?= $vendor_name ?></a></td>
                                    <td><?= $vendor_description ?></td>
                                    <td><?= $vendor_phone ?></td>
                                    <td><a href="vendors.php?client_id=<?= $client_id ?>"><?= $client_name ?></a></td>
                                </tr>

                            <?php } ?>


                            </tbody>
                        </table>
                    </div>
                </div>
            </div>

        <?php } ?>

        <?php if ($sql_domains && mysqli_num_rows($sql_domains) > 0) { ?>

            <!-- Domains -->
            <div class="col-sm-6">
                <div class="card card-dark mb-3">
                    <div class="card-header">
                        <h6 class="card-title"><i class="fas fa-fw fa-globe me-2"></i>Domains</h6>
                    </div>
                    <div class="card-body">
                        <table class="table table-striped table-borderless">
                            <thead>
                            <tr>
                                <th>Name</th>
                                <th>Expiry</th>
                                <th>Client</th>
                            </tr>
                            </thead>
                            <tbody>
                            <?php

                            while ($row = mysqli_fetch_assoc($sql_domains)) {
                                $domain_name = escapeHtml($row['domain_name']);
                                $domain_expiry = escapeHtml($row['domain_expire']);
                                $domain_id = intval($row['domain_id']);
                                $client_id = intval($row['client_id']);
                                $client_name = escapeHtml($row['client_name']);

                                ?>
                                <tr>
                                    <td><a href="domains.php?client_id=<?= $client_id ?>&domain_id=<?= $domain_id ?>"><?= $domain_name ?></a>
                                    <td><?= $domain_expiry ?></td>
                                    <td><a href="client_overview.php?client_id=<?= $client_id ?>"><?= $client_name ?></a></td>
                                </tr>

                            <?php } ?>

                            </tbody>
                        </table>
                    </div>
                </div>
            </div>

        <?php } ?>

        <?php if ($sql_products && mysqli_num_rows($sql_products) > 0) { ?>

            <!-- Products -->
            <div class="col-sm-6">
                <div class="card card-dark mb-3">
                    <div class="card-header">
                        <h6 class="card-title"><i class="fas fa-fw fa-box me-2"></i>Products</h6>
                    </div>
                    <div class="card-body">
                        <table class="table table-striped table-borderless">
                            <thead>
                            <tr>
                                <th>Name</th>
                                <th>Description</th>
                            </tr>
                            </thead>
                            <tbody>
                            <?php

                            while ($row = mysqli_fetch_assoc($sql_products)) {
                                $product_name = escapeHtml($row['product_name']);
                                $product_description = escapeHtml($row['product_description']);
                                ?>
                                <tr>
                                    <td><a href="products.php?q=<?= $q ?>"><?= $product_name ?></a></td>
                                    <td><?= $product_description ?></td>
                                </tr>

                            <?php } ?>


                            </tbody>
                        </table>
                    </div>
                </div>
            </div>

        <?php } ?>

        <?php if ($sql_documents && mysqli_num_rows($sql_documents) > 0) { ?>

            <!-- Documents -->
            <div class="col-sm-6">
                <div class="card card-dark mb-3">
                    <div class="card-header">
                        <h6 class="card-title"><i class="fas fa-fw fa-file-alt me-2"></i>Documents</h6>
                    </div>
                    <div class="card-body">
                        <table class="table table-striped table-borderless">
                            <thead>
                            <tr>
                                <th>Document</th>
                                <th>Client</th>
                            </tr>
                            </thead>
                            <tbody>
                            <?php

                            while ($row = mysqli_fetch_assoc($sql_documents)) {
                                $document_id = intval($row['document_id']);
                                $document_name = escapeHtml($row['document_name']);
                                $client_id = intval($row['document_client_id']);
                                $client_name = escapeHtml($row['client_name']);

                                ?>
                                <tr>
                                    <td><a href="document.php?client_id=<?= $client_id ?>&document_id=<?= $document_id ?>"><?= $document_name ?></a></td>
                                    <td>
                                        <a href="documents.php?client_id=<?= $client_id ?>"><?= $client_name ?></a>
                                    </td>
                                </tr>

                            <?php } ?>


                            </tbody>
                        </table>
                    </div>
                </div>
            </div>

        <?php } ?>

        <?php if ($sql_files && mysqli_num_rows($sql_files) > 0) { ?>

            <!-- Files -->
            <div class="col-sm-6">
                <div class="card card-dark mb-3">
                    <div class="card-header">
                        <h6 class="card-title"><i class="fas fa-fw fa-paperclip me-2"></i>Files</h6>
                    </div>
                    <div class="card-body">
                        <table class="table table-striped table-borderless">
                            <thead>
                            <tr>
                                <th>File Name</th>
                                <th>Description</th>
                                <th>Client</th>
                            </tr>
                            </thead>
                            <tbody>
                            <?php

                            while ($row = mysqli_fetch_assoc($sql_files)) {
                                $file_id = intval($row['file_id']);
                                $file_name = escapeHtml($row['file_name']);
                                $file_description = escapeHtml($row['file_description']);
                                $folder_id = intval($row['folder_id']);
                                $folder_name = escapeHtml($row['folder_name']);
                                $client_id = intval($row['file_client_id']);
                                $client_name = escapeHtml($row['client_name']);

                                ?>
                                <tr>
                                    <td>
                                        <a href="file.php?file_id=<?= $file_id ?>&action=view" target="_blank">
                                            <?= "$folder_name/$file_name"; ?>
                                        </a>
                                    </td>
                                    <td><?= $file_description ?></td>
                                    <td>
                                        <a href="files.php?client_id=<?= $client_id ?>&folder_id=<?= $folder_id ?>"><?= $client_name ?></a>
                                    </td>
                                </tr>

                            <?php } ?>


                            </tbody>
                        </table>
                    </div>
                </div>
            </div>

        <?php } ?>

        <?php if ($sql_tickets && mysqli_num_rows($sql_tickets) > 0) { ?>

            <!-- Tickets -->
            <div class="col-sm-6">
                <div class="card card-dark mb-3">
                    <div class="card-header">
                        <h6 class="card-title"><i class="fas fa-fw fa-life-ring me-2"></i>Tickets</h6>
                    </div>
                    <div class="card-body">
                        <table class="table table-striped table-borderless">
                            <thead>
                            <tr>
                                <th>Ticket ID</th>
                                <th>Description</th>
                                <th>Status</th>
                                <th>Client</th>
                            </tr>
                            </thead>
                            <tbody>
                            <?php

                            while ($row = mysqli_fetch_assoc($sql_tickets)) {
                                $ticket_id = intval($row['ticket_id']);
                                $ticket_prefix = escapeHtml($row['ticket_prefix']);
                                $ticket_number = intval($row['ticket_number']);
                                $ticket_subject = escapeHtml($row['ticket_subject']);
                                $ticket_status_name = escapeHtml($row['ticket_status_name']);
                                $client_name = escapeHtml($row['client_name']);
                                $client_id = intval($row['ticket_client_id']);

                                ?>
                                <tr>
                                    <td><a href="ticket.php?client_id=<?= $client_id ?>&ticket_id=<?= $ticket_id ?>"><?= $ticket_prefix . $ticket_number ?></a></td>
                                    <td><?= $ticket_subject ?></td>
                                    <td><?= $ticket_status_name ?></td>
                                    <td><a href="tickets.php?client_id=<?= $client_id ?>"><?= $client_name ?></a></td>
                                </tr>

                            <?php } ?>


                            </tbody>
                        </table>
                    </div>
                </div>
            </div>

        <?php } ?>


        <?php if ($sql_recurring_tickets && mysqli_num_rows($sql_recurring_tickets) > 0) { ?>

            <!-- Recurring Tickets -->
            <div class="col-sm-6">
                <div class="card card-dark mb-3">
                    <div class="card-header">
                        <h6 class="card-title"><i class="fas fa-fw fa-undo-alt me-2"></i>Recurring Tickets</h6>
                    </div>
                    <div class="card-body">
                        <table class="table table-striped table-borderless">
                            <thead>
                            <tr>
                                <th>Subject</th>
                                <th>Frequency</th>
                                <th>Next</th>
                                <th>Client</th>
                            </tr>
                            </thead>
                            <tbody>
                            <?php

                            while ($row = mysqli_fetch_assoc($sql_recurring_tickets)) {
                                $recurring_ticket_id = intval($row['recurring_ticket_id']);
                                $recurring_ticket_subject = escapeHtml($row['recurring_ticket_subject']);
                                $recurring_ticket_frequency = escapeHtml($row['recurring_ticket_frequency']);
                                $recurring_ticket_next_run = escapeHtml($row['recurring_ticket_next_run']);
                                $client_name = escapeHtml($row['client_name']);
                                $client_id = intval($row['client_id']);

                                ?>
                                <tr>
                                    <td><a href="recurring_tickets.php"><?= $recurring_ticket_subject ?></a></td>
                                    <td><?= $recurring_ticket_frequency ?></td>
                                    <td><?= $recurring_ticket_next_run ?></td>
                                    <td><a href="recurring_tickets.php?client_id=<?= $client_id ?>"><?= $client_name ?></a></td>
                                </tr>

                            <?php } ?>


                            </tbody>
                        </table>
                    </div>
                </div>
            </div>

        <?php } ?>


        <?php if ($sql_credentials && mysqli_num_rows($sql_credentials) > 0) { ?>

            <!-- Credentials -->
            <div class="col-sm-6">
                <div class="card card-dark mb-3">
                    <div class="card-header">
                        <h3 class="card-title"><i class="fas fa-fw fa-key me-2"></i>Credentials</h3>
                    </div>
                    <div class="card-body">
                        <table class="table table-striped table-borderless">
                            <thead>
                            <tr>
                                <th>Name</th>
                                <th>Description</th>
                                <th>Username</th>
                                <th>Password</th>
                                <th>Client</th>
                            </tr>
                            </thead>
                            <tbody>
                            <?php

                            while ($row = mysqli_fetch_assoc($sql_credentials)) {
                                $credential_name = escapeHtml($row['credential_name']);
                                $credential_description = escapeHtml($row['credential_description']);
                                $credential_client_id = intval($row['credential_client_id']);
                                $credential_username = escapeHtml(decryptCredentialEntry($row['credential_username']));
                                $credential_password = escapeHtml(decryptCredentialEntry($row['credential_password']));
                                $client_id = intval($row['client_id']);
                                $client_name = escapeHtml($row['client_name']);

                                ?>
                                <tr>
                                    <td><a href="credentials.php?client_id=<?= $credential_client_id ?>&q=<?= $q ?>"><?= $credential_name ?></a></td>
                                    <td><?= $credential_description ?></td>
                                    <td><?= $credential_username ?></td>
                                    <td><a tabindex="0" class="btn btn-sm" data-bs-toggle="popover" data-bs-trigger="focus" data-bs-placement="left" data-bs-content="<?= $credential_password ?>"><i class="far fa-eye text-secondary"></i></a><button class="btn btn-sm clipboardjs" data-clipboard-text="<?= $credential_password ?>"><i class="far fa-copy text-secondary"></i></button>
                                    </td>
                                    <td><a href="credentials.php?client_id=<?= $client_id ?>"><?= $client_name ?></a></td>
                                </tr>

                            <?php } ?>


                            </tbody>
                        </table>
                    </div>
                </div>
            </div>

        <?php } ?>

        <?php if ($sql_quotes && mysqli_num_rows($sql_quotes) > 0) { ?>

            <!-- Contacts-->

            <div class="col-sm-6">
                <div class="card card-dark mb-3">
                    <div class="card-header">
                        <h6 class="card-title"><i class="fas fa-fw fa-file-invoice me-2"></i>Quotes</h6>
                    </div>
                    <div class="card-body">
                        <table class="table table-striped table-borderless">
                            <thead>
                            <tr>
                                <th>Number</th>
                                <th>Status</th>
                                <th>Amount</th>
                                <th>Client</th>
                            </tr>
                            </thead>
                            <tbody>
                            <?php

                            while ($row = mysqli_fetch_assoc($sql_quotes)) {
                                $quote_id = intval($row['quote_id']);
                                $quote_prefix = escapeHtml($row['quote_prefix']);
                                $quote_number = intval($row['quote_number']);
                                $quote_amount = floatval($row['quote_amount']);
                                $quote_currency_code = escapeHtml($row['quote_currency_code']);
                                $quote_status = escapeHtml($row['quote_status']);
                                $client_id = intval($row['client_id']);
                                $client_name = escapeHtml($row['client_name']);

                                ?>
                                <tr>
                                    <td><a href="quote.php?client_id=<?= $client_id ?>&quote_id=<?= $quote_id ?>"><?= "$quote_prefix$quote_number" ?></a></td>
                                    <td><?= $quote_status ?></td>
                                    <td><?= numfmt_format_currency($currency_format, $quote_amount, $quote_currency_code) ?></td>
                                    <td><a href="client_overview.php?client_id=<?= $client_id ?>"><?= $client_name ?></a></td>
                                </tr>

                            <?php } ?>


                            </tbody>
                        </table>
                    </div>
                </div>
            </div>

        <?php } ?>

        <?php if ($sql_invoices && mysqli_num_rows($sql_invoices) > 0) { ?>

            <!-- Contacts-->

            <div class="col-sm-6">
                <div class="card card-dark mb-3">
                    <div class="card-header">
                        <h6 class="card-title"><i class="fas fa-fw fa-file-invoice me-2"></i>Invoices</h6>
                    </div>
                    <div class="card-body">
                        <table class="table table-striped table-borderless">
                            <thead>
                            <tr>
                                <th>Number</th>
                                <th>Status</th>
                                <th>Amount</th>
                                <th>Client</th>
                            </tr>
                            </thead>
                            <tbody>
                            <?php

                            while ($row = mysqli_fetch_assoc($sql_invoices)) {
                                $invoice_id = intval($row['invoice_id']);
                                $invoice_prefix = escapeHtml($row['invoice_prefix']);
                                $invoice_number = intval($row['invoice_number']);
                                $invoice_amount = floatval($row['invoice_amount']);
                                $invoice_currency_code = escapeHtml($row['invoice_currency_code']);
                                $invoice_status = escapeHtml($row['invoice_status']);
                                $client_id = intval($row['client_id']);
                                $client_name = escapeHtml($row['client_name']);

                                ?>
                                <tr>
                                    <td><a href="invoice.php?client_id=<?= $client_id ?>&invoice_id=<?= $invoice_id ?>"><?= "$invoice_prefix$invoice_number" ?></a></td>
                                    <td><?= $invoice_status ?></td>
                                    <td><?= numfmt_format_currency($currency_format, $invoice_amount, $invoice_currency_code) ?></td>
                                    <td><a href="client_overview.php?client_id=<?= $client_id ?>"><?= $client_name ?></a></td>
                                </tr>

                            <?php } ?>


                            </tbody>
                        </table>
                    </div>
                </div>
            </div>

        <?php } ?>

        <?php if ($sql_assets && mysqli_num_rows($sql_assets) > 0) { ?>

            <!-- Contacts-->

            <div class="col-sm-6">
                <div class="card card-dark mb-3">
                    <div class="card-header">
                        <h6 class="card-title"><i class="fas fa-fw fa-desktop me-2"></i>Assets</h6>
                    </div>
                    <div class="card-body">
                        <table class="table table-striped table-borderless">
                            <thead>
                            <tr>
                                <th>Asset</th>
                                <th>Type</th>
                                <th>Serial</th>
                                <th>Client</th>
                                <th>Assigned</th>
                            </tr>
                            </thead>
                            <tbody>
                            <?php

                            while ($row = mysqli_fetch_assoc($sql_assets)) {
                                $client_id = intval($row['asset_client_id']);
                                $client_name = escapeHtml($row['client_name']);
                                $asset_id = intval($row['asset_id']);
                                $asset_type = escapeHtml($row['asset_type']);
                                $asset_name = escapeHtml($row['asset_name']);
                                $asset_description = escapeHtml($row['asset_description']);
                                if (empty($asset_description)) {
                                    $asset_description_display = "-";
                                } else {
                                    $asset_description_display = $asset_description;
                                }
                                $asset_make = escapeHtml($row['asset_make']);
                                $asset_model = escapeHtml($row['asset_model']);
                                $asset_serial = escapeHtml($row['asset_serial']);
                                if (empty($asset_serial)) {
                                    $asset_serial_display = "-";
                                } else {
                                    $asset_serial_display = $asset_serial;
                                }
                                $asset_uri = escapeHtml($row['asset_uri']);
                                $asset_status = escapeHtml($row['asset_status']);
                                $asset_created_at = escapeHtml($row['asset_created_at']);
                                $asset_location_id = intval($row['asset_location_id']);
                                $asset_contact_id = intval($row['asset_contact_id']);
                                $device_icon = getAssetIcon($asset_type);

                                $contact_name = escapeHtml($row['contact_name']);
                                $contact_id = escapeHtml($row['contact_id']);
                                if (empty($contact_name)) {
                                    $contact_name_display = "-";
                                }else{
                                    $contact_name_display = "<a href='contact.php?client_id=$client_id&contact_id=$contact_id'>$contact_name</a>";
                                }
                                $contact_archived_at = escapeHtml($row['contact_archived_at']);
                                if (empty($contact_archived_at)) {
                                    $contact_archived_display = "";
                                } else {
                                    $contact_archived_display = "Archived - ";
                                }

                                ?>
                                <tr>
                                    <td>
                                        <i class="fa fa-fw text-secondary fa-<?= $device_icon ?> me-2"></i><a href="asset.php?client_id=<?= $client_id ?>&asset_id=<?= $asset_id ?>"><?= $asset_name ?></a>
                                        <?php if(!empty($asset_uri)){ ?>
                                            <a href="<?= $asset_uri ?>" target="_blank"><i class="fas fa-fw fa-external-link-alt ms-2"></i></a>
                                        <?php } ?>
                                    </td>
                                    <td><?= $asset_type ?></td>
                                    <td><?= $asset_serial_display ?></td>
                                    <td><a href="assets.php?client_id=<?= $client_id ?>"><?= $client_name ?></a></td>
                                    <td><?= $contact_name_display ?></td>
                                </tr>

                            <?php } ?>


                            </tbody>
                        </table>
                    </div>
                </div>
            </div>

        <?php } ?>

        <?php if ($sql_ticket_replies && mysqli_num_rows($sql_ticket_replies) > 0) { ?>

            <!-- Ticket Replies -->

            <div class="col-sm-6">

                <div class="card card-dark">
                    <div class="card-header">
                        <h6 class="card-title"><i class="fas fa-fw fa-reply me-2"></i>Ticket Replies</h6>
                    </div>
                    <div class="card-body">

                    <?php
                    $last_ticket_id = null; // Track the last ticket ID processed

                    while ($row = mysqli_fetch_assoc($sql_ticket_replies)) {
                        $ticket_id = intval($row['ticket_id']);

                        // Only output the ticket header if we're at a new ticket
                        if ($ticket_id !== $last_ticket_id) {
                            if ($last_ticket_id !== null) {
                                // Close the previous ticket's card (except for the very first ticket)
                                echo '</div></div>';
                            }

                            $ticket_prefix = escapeHtml($row['ticket_prefix']);
                            $ticket_number = intval($row['ticket_number']);
                            $ticket_subject = escapeHtml($row['ticket_subject']);
                            $client_id = intval($row['ticket_client_id']);
                            $client_name = escapeHtml($row['client_name']);

                            // Output the ticket header
                            ?>
                            <div class="card card-outline">
                                <div class="card-header">
                                    <h3 class="card-title">
                                        <?= "$client_name - $ticket_prefix$ticket_number - $ticket_subject" ?>
                                    </h3>
                                    <div class="card-tools">
                                        <a href="ticket.php?client_id=<?= $client_id ?>&ticket_id=<?= $ticket_id ?>" target="_blank">Open <i class="fa fa-fw fa-external-link-alt"></i></a>
                                    </div>
                                </div>
                                <div class="card-body prettyContent">
                            <?php
                        }

                        $ticket_reply = $purifier->purify($row['ticket_reply']);

                        // Output the ticket reply
                        ?>
                        <div class="d-flex">
                            <i class="fas fa-fw fa-reply me-3"></i>
                            <div class="flex-grow-1">
                                <?= $ticket_reply ?>
                            </div>
                        </div>
                        <hr>
                        <?php

                        $last_ticket_id = $ticket_id; // Update the last ticket ID
                    }

                    if ($last_ticket_id !== null) {
                        // Close the last ticket's card
                        echo '</div></div>';
                    }
                    ?>

                    </div>

                </div>

            </div>
        </div>

        <?php } ?>

    </div>

</div>

</div>

<?php

}

require_once "../includes/footer.php";

?>

<script src="../js/pretty_content.js"></script>
