<?php defined('BASEPATH') or exit('No direct script access allowed'); ?>
<?php init_head(); ?>
<link rel="stylesheet" href="<?php echo module_dir_url(PROPERTYBASE_MODULE_NAME, 'assets/css/leaflet.css'); ?>"/>
<script src="<?php echo module_dir_url(PROPERTYBASE_MODULE_NAME, 'assets/js/leaflet.js'); ?>"></script>
<div id="wrapper">
    <div class="content">
        <div class="row">
            <div class="col-md-12">
                <h4 class="tw-mt-0 tw-font-semibold tw-text-lg tw-text-neutral-700">
                    <?php echo $title; ?>
                </h4>
                <div class="panel_s">
                    <div class="panel-body">
                        <div class="row">
                            <div class="horizontal-scrollable-tabs preview-tabs-top">
                                <div class="scroller arrow-left"><i class="fa fa-angle-left"></i></div>
                                <div class="scroller arrow-right"><i class="fa fa-angle-right"></i></div>
                                <div class="horizontal-tabs">
                                    <ul class="nav nav-tabs nav-tabs-horizontal mbot15" role="tablist">

                                        <li role="presentation" class="active">
                                            <a href="#general_infor" aria-controls="general_infor" role="tab"
                                               data-toggle="tab" aria-expanded="true">
                                                <?php echo _l('propertybase_general_details'); ?> </a>
                                        </li>

                                        <li role="presentation" class="">
                                            <a href="#property_invoices" aria-controls="property_invoices" role="tab"
                                               data-toggle="tab" aria-expanded="false">
                                                <?php
                                                $totalInvoices = total_rows(
                                                    db_prefix() . 'invoices',
                                                    [
                                                        'property_id' => $property->id
                                                    ]
                                                );
                                                ?>
                                                <span class="badge <?php echo $totalInvoices === 0 ? 'hide' : ''; ?>"><?php echo $totalInvoices ?></span>
                                                <?php echo ucfirst(_l('invoices')); ?>
                                            </a>
                                        </li>

                                        <li role="presentation">
                                            <a href="#tab_tasks"
                                               onclick="init_rel_tasks_table(<?php echo $property->id; ?>,'properties'); return false;"
                                               aria-controls="tab_tasks" role="tab" data-toggle="tab">
                                                <?php
                                                $totalTasks = total_rows(
                                                    db_prefix() . 'tasks',
                                                    [
                                                        'rel_id' => $property->id,
                                                        'rel_type' => 'properties',
                                                    ]
                                                );
                                                ?>
                                                <span class="badge <?php echo $totalTasks === 0 ? 'hide' : ''; ?>"><?php echo $totalTasks ?></span>
                                                <?php echo _l('tasks'); ?>
                                            </a>
                                        </li>

                                        <li role="presentation" class="">
                                            <a href="#property_leads" aria-controls="property_leads" role="tab"
                                               onclick="initDataTable('.table-lead-properties', admin_url + 'propertybase/propertybase/property_leads/<?php echo $property->id ?>', 'undefined', 'undefined', {}, [0, 'desc']); return false"
                                               data-toggle="tab" aria-expanded="false">
                                                <?php
                                                $totalInvoices = total_rows(
                                                    db_prefix() . 'propertybase_properties_leads',
                                                    [
                                                        'property_id' => $property->id
                                                    ]
                                                );
                                                ?>
                                                <span class="badge <?php echo $totalInvoices === 0 ? 'hide' : ''; ?>"><?php echo $totalInvoices ?></span>
                                                <?php echo ucfirst(_l('leads')); ?>
                                            </a>
                                        </li>

                                        <li role="presentation" class="">
                                            <a href="#property_expenses" aria-controls="property_expenses" role="tab"
                                               data-toggle="tab" aria-expanded="false">
                                                <?php
                                                $totalInvoices = total_rows(
                                                    db_prefix() . 'expenses',
                                                    [
                                                        'property_id' => $property->id
                                                    ]
                                                );
                                                ?>
                                                <span class="badge <?php echo $totalInvoices === 0 ? 'hide' : ''; ?>"><?php echo $totalInvoices ?></span>
                                                <?php echo ucfirst(_l('expenses')); ?>
                                            </a>
                                        </li>

                                        <li role="presentation" class="">
                                            <a href="#property_events" aria-controls="property_events" role="tab"
                                               data-toggle="tab" aria-expanded="false">
                                                <?php
                                                $totalEvents = total_rows(
                                                    db_prefix() . 'propertybase_property_events',
                                                    [
                                                        'property_id' => $property->id,
                                                    ]
                                                );
                                                ?>
                                                <span class="badge <?php echo $totalEvents === 0 ? 'hide' : ''; ?>"><?php echo $totalEvents ?></span>
                                                <?php echo _l('propertybase_history_events'); ?>
                                            </a>
                                        </li>

                                        <li role="presentation" class="">
                                            <a href="#property_maintenance_requests"
                                               aria-controls="property_maintenance_requests" role="tab"
                                               onclick="initDataTable('.table-property-maintenance-requests', admin_url + 'propertybase/propertybase/property_maintenance_requests/<?php echo $property->id ?>', 'undefined', 'undefined', {}, [0, 'desc']); return false;"
                                               data-toggle="tab" aria-expanded="false">

                                                <?php
                                                $totalInvoices = total_rows(
                                                    db_prefix() . 'propertybase_properties_maintenance',
                                                    [
                                                        'property_id' => $property->id
                                                    ]
                                                );
                                                ?>
                                                <span class="badge <?php echo $totalInvoices === 0 ? 'hide' : ''; ?>"><?php echo $totalInvoices ?></span>
                                                <?php echo ucfirst(_l('propertybase_maintenance_requests')); ?>
                                            </a>
                                        </li>

                                        <li role="presentation" class="">
                                            <a href="#property_history" aria-controls="property_history" role="tab"
                                               onclick="initDataTable('.table-propertybase-history', window.location.href, [0], [0], [], [0, 'desc']); return false"
                                               data-toggle="tab" aria-expanded="false">
                                                <?php
                                                $totalHistory = total_rows(
                                                    db_prefix() . 'propertybase_property_history',
                                                    [
                                                        'property_id' => $property->id,
                                                    ]
                                                );
                                                ?>
                                                <span class="badge <?php echo $totalHistory === 0 ? 'hide' : ''; ?>"><?php echo $totalHistory ?></span>
                                                <?php echo _l('propertybase_history'); ?>
                                            </a>
                                        </li>

                                        <li role="presentation" class="">
                                            <a href="#asset_reminders" aria-controls="asset_reminders" role="tab"
                                               data-toggle="tab" aria-expanded="false"
                                               onclick="initDataTable('.table-reminders', admin_url + 'misc/get_reminders/' + <?php echo $property->id; ?> + '/' + 'property', undefined, undefined, undefined,[1,'asc']); return false;"
                                            >
                                                <?php
                                                $totalReminders = total_rows(
                                                    db_prefix() . 'reminders',
                                                    [
                                                        'rel_id' => $property->id,
                                                        'rel_type' => 'property',
                                                    ]
                                                );
                                                ?>
                                                <span class="badge <?php echo $totalReminders === 0 ? 'hide' : ''; ?>"><?php echo $totalReminders ?></span>
                                                <?php echo _l('reminders'); ?>
                                            </a>
                                        </li>

                                        <li role="presentation" class="">
                                            <a href="#tab_notes"
                                               onclick="get_sales_notes(<?php echo $property->id; ?>, 'propertybase'); return false"
                                               aria-controls="tab_notes" role="tab" data-toggle="tab">
                                                <?php
                                                $totalReminders = total_rows(
                                                    db_prefix() . 'notes',
                                                    [
                                                        'rel_id' => $property->id,
                                                        'rel_type' => 'property',
                                                    ]
                                                );
                                                ?>
                                                <span class="badge <?php echo $totalReminders === 0 ? 'hide' : ''; ?>"><?php echo $totalReminders ?></span>
                                                <?php echo _l('contract_notes'); ?>
                                            </a>
                                        </li>

                                        <li role="presentation" class="">
                                            <a href="#asset_files" aria-controls="asset_files" role="tab"
                                               data-toggle="tab" aria-expanded="false">
                                                <?php
                                                $totalFiles = total_rows(
                                                    db_prefix() . 'files',
                                                    [
                                                        'rel_id' => $property->id,
                                                        'rel_type' => 'property_attachment',
                                                    ]
                                                );
                                                ?>
                                                <span class="badge <?php echo $totalFiles === 0 ? 'hide' : ''; ?>"><?php echo $totalFiles ?></span>
                                                <?php echo ucfirst(_l('files')); ?>
                                            </a>
                                        </li>

                                    </ul>
                                </div>
                            </div>

                            <div class="tab-content">

                                <div role="tabpanel" class="tab-pane active" id="general_infor">
                                    <div class="panel panel-info">
                                        <div class="panel-body">

                                            <div class="row mtop20">
                                                <div class="col-md-3">
                                                    <h3><?php echo _l('propertybase_property_information'); ?></h3>
                                                </div>
                                                <div class="col-md-9 _buttons">
                                                    <div class="visible-xs">
                                                        <div class="mtop10"></div>
                                                    </div>
                                                    <div class="pull-right">
                                                        <a target="_blank"
                                                           href="<?php echo admin_url(PROPERTYBASE_MODULE_NAME . '/create/' . $property->id); ?>"
                                                           class="btn btn-default"><i
                                                                    class="fa-regular fa-pen-to-square"></i></a>
                                                        <span>
                                                        <a href="#" onclick="generateAssetPDF()"
                                                           class="btn btn-default"><i
                                                                    class="fa fa-file-pdf"></i> <?php echo _l('print'); ?> PDF</a>
                                                        </span>
                                                        <span>
                                                        <a href="<?php echo $generatedQr; ?>" class="btn btn-success"
                                                           download><i
                                                                    class="fa fa-qrcode"></i> <?php echo _l('download'); ?> QR</a>
                                                        </span>
                                                    </div>
                                                </div>
                                            </div>

                                            <div class="row col-md-12 mtop20">
                                                <?php
                                                foreach (propertybase_property_statuses() as $status) {

                                                    if ($status['value'] === 'available') {
                                                        continue;
                                                    }

                                                    if ($status['value'] != 'revocate') {
                                                        ?>
                                                        <button type="button" class="btn btn-secondary" id="statusBtn"
                                                                onclick="openAssetStatusModal('<?php echo $status['value']; ?>')">
                                                            <span class="<?php echo $status['icon'] ?>"></span> <?php echo $status['name']; ?>
                                                        </button>
                                                        <?php
                                                    } else {
                                                        ?>
                                                        <a href="<?php echo admin_url(PROPERTYBASE_MODULE_NAME . '/revocate_property/' . $property->id); ?>"
                                                           type="button" class="btn btn-secondary _delete"
                                                           id="statusBtn">
                                                            <span class="<?php echo $status['icon'] ?>"></span> <?php echo $status['name']; ?>
                                                        </a>
                                                        <?php
                                                    }
                                                }
                                                ?>
                                                </button>
                                                <hr>
                                            </div>

                                            <div class="row" id="propertyDetailsDiv">
                                                <div class="col-md-6">
                                                    <?php
                                                    $propertyImage = substr(module_dir_url(PROPERTYBASE_MODULE_NAME . '/uploads/default_image.jpg'), 0, -1);

                                                    if (!empty($property_main_image)) {
                                                        $assetImageUrl = substr(module_dir_url(PROPERTYBASE_MODULE_NAME . '/uploads/property_images/main_image/' . $property->id . '/' . $property_main_image[0]['file_name']), 0, -1);

                                                        $propertyImage = $assetImageUrl;
                                                    }
                                                    ?>
                                                    <div class="col-md-12">
                                                        <img src="<?php echo $propertyImage; ?>"
                                                             class="img-thumbnail img-responsive zoom propertybase-single-thumnail-size">
                                                    </div>
                                                </div>
                                                <div class="col-md-6">
                                                    <div class="col-md-12">
                                                        <img src="<?php echo $generatedQr; ?>"
                                                             class="img-thumbnail img-responsive zoom pull-right propertybase-single-qr-thumnail-size">
                                                    </div>
                                                </div>

                                                <div class="col-md-6">
                                                    <div class="col-md-12 noleftrightpadding">
                                                        <table class="table border table-striped nomargintop">
                                                            <td class="propertybase-single-table-header"><?php echo _l('propertybase_property_details'); ?></td>
                                                            <td class="propertybase-single-table-header"><a
                                                                        target="_blank"
                                                                        href="<?php echo admin_url(PROPERTYBASE_MODULE_NAME . '/create/' . $property->id); ?>"
                                                                        class="tw-text-neutral-500 hover:tw-text-neutral-700 focus:tw-text-neutral-700 pull-right">
                                                                    <i class="fa-regular fa-pen-to-square fa-lg"></i>
                                                                </a></td>
                                                            <tbody>
                                                            <tr class="project-overview">
                                                                <td class="bold"><?php echo _l('propertybase_property_id'); ?></td>
                                                                <td><?php echo $property->id; ?></td>
                                                            </tr>
                                                            <tr class="project-overview">
                                                                <td class="bold"><?php echo _l('propertybase_property_agent'); ?></td>
                                                                <td><?php
                                                                    if (!empty($property->selected_agent)) {
                                                                        echo '<a href="' . admin_url('staff/profile/' . $property->selected_agent) . '">' . staff_profile_image($property->selected_agent, [
                                                                                'staff-profile-image-small',
                                                                            ]) . ' ' . get_staff_full_name($property->selected_agent) . '</a>';
                                                                    } else {
                                                                        echo '';
                                                                    }
                                                                    ?></td>
                                                            </tr>
                                                            <?php

                                                            $status = propertybase_get_status_data($property->property_status)['badge'] ?? 'undefined';
                                                            if ($property->property_status == 2 && !empty($property_assigned)) {
                                                                $status .= propertybase_get_property_assigned_data($property_assigned);
                                                            }
                                                            ?>
                                                            <tr class="project-overview">
                                                                <td class="bold"><?php echo _l('propertybase_property_name'); ?></td>
                                                                <td><?php echo $status; ?></td>
                                                            </tr>
                                                            <tr class="project-overview">
                                                                <td class="bold"><?php echo _l('propertybase_property_name'); ?></td>
                                                                <td>
                                                                    <a href="<?php echo propertybase_get_property_single_page_url($property->id); ?>"
                                                                       target="_blank"><?php echo propertybase_formatted_property_name($property->id); ?></a>
                                                                </td>
                                                            </tr>
                                                            <tr class="project-overview">
                                                                <td class="bold"><?php echo _l('propertybase_created_at'); ?></td>
                                                                <td><?php echo _dt($property->created_at); ?></td>
                                                            </tr>
                                                            </tbody>
                                                        </table>
                                                    </div>

                                                    <div class="col-md-12 noleftrightpadding">
                                                        <table class="table border table-striped nomargintop">
                                                            <td class="propertybase-single-table-header"><?php echo _l('propertybase_property_pricing_information'); ?></td>
                                                            <td class="propertybase-single-table-header"><a
                                                                        target="_blank"
                                                                        href="<?php echo admin_url(PROPERTYBASE_MODULE_NAME . '/create/' . $property->id); ?>"
                                                                        class="tw-text-neutral-500 hover:tw-text-neutral-700 focus:tw-text-neutral-700 pull-right">
                                                                    <i class="fa-regular fa-pen-to-square fa-lg"></i>
                                                                </a></td>
                                                            <tbody>
                                                            <tr class="project-overview">
                                                                <td class="bold"><?php echo _l('propertybase_property_sale_price'); ?></td>
                                                                <td><?php echo app_format_money($property->sale_price, get_base_currency()) ?: app_format_money(0, get_base_currency()); ?></td>
                                                            </tr>
                                                            <tr class="project-overview">
                                                                <td class="bold"><?php echo _l('propertybase_property_rent_price_incl'); ?></td>
                                                                <td><?php echo app_format_money($property->rent_price_incl, get_base_currency()) ?: app_format_money(0, get_base_currency()); ?></td>
                                                            </tr>
                                                            <tr class="project-overview">
                                                                <td class="bold"><?php echo _l('propertybase_property_rent_price_excl'); ?></td>
                                                                <td><?php echo app_format_money($property->rent_price_excl, get_base_currency()) ?: app_format_money(0, get_base_currency()); ?></td>
                                                            </tr>
                                                            </tbody>
                                                        </table>
                                                    </div>

                                                    <div class="col-md-12 noleftrightpadding">
                                                        <table class="table border table-striped nomargintop">
                                                            <td class="propertybase-single-table-header"><?php echo _l('propertybase_property_location_information'); ?></td>
                                                            <td class="propertybase-single-table-header"><a
                                                                        target="_blank"
                                                                        href="<?php echo admin_url(PROPERTYBASE_MODULE_NAME . '/create/' . $property->id); ?>"
                                                                        class="tw-text-neutral-500 hover:tw-text-neutral-700 focus:tw-text-neutral-700 pull-right">
                                                                    <i class="fa-regular fa-pen-to-square fa-lg"></i>
                                                                </a></td>
                                                            <tbody>
                                                            <tr class="project-overview">
                                                                <td class="bold"><?php echo _l('propertybase_property_address'); ?></td>
                                                                <td><?php echo $property->address; ?></td>
                                                            </tr>
                                                            <tr class="project-overview">
                                                                <td class="bold"><?php echo _l('propertybase_property_city'); ?></td>
                                                                <td><?php echo $property->city; ?></td>
                                                            </tr>
                                                            <tr class="project-overview">
                                                                <td class="bold"><?php echo _l('propertybase_property_zip_code'); ?></td>
                                                                <td><?php echo $property->zip_code; ?></td>
                                                            </tr>
                                                            <tr class="project-overview">
                                                                <td class="bold"><?php echo _l('propertybase_property_country'); ?></td>
                                                                <td><?php echo get_country_name($property->country); ?></td>
                                                            </tr>
                                                            <tr class="project-overview">
                                                                <td class="bold"><?php echo _l('propertybase_property_address'); ?></td>
                                                                <td>
                                                                    <div id="map" class="propertybase-single-map-size"></div>
                                                                </td>
                                                            </tr>
                                                            </tbody>
                                                        </table>
                                                    </div>

                                                    <?php if (!empty($property->amenities_with_types)) { ?>
                                                        <?php foreach ($property->amenities_with_types as $type => $amenities): ?>
                                                            <div class="col-md-12 noleftrightpadding">
                                                                <table class="table border table-striped nomargintop">
                                                                    <td class="propertybase-single-table-header"><?php echo ucfirst($type); ?></td>
                                                                    <td class="propertybase-single-table-header"><a
                                                                                target="_blank"
                                                                                href="<?php echo admin_url(PROPERTYBASE_MODULE_NAME . '/create/' . $property->id); ?>"
                                                                                class="tw-text-neutral-500 hover:tw-text-neutral-700 focus:tw-text-neutral-700 pull-right">
                                                                            <i class="fa-regular fa-pen-to-square fa-lg"></i>
                                                                        </a></td>
                                                                    <tbody>
                                                                    <?php
                                                                    foreach ($amenities as $amenity) {
                                                                        ?>
                                                                        <tr class="project-overview">
                                                                            <td class="bold"><?php echo htmlspecialchars($amenity); ?></td>
                                                                            <td></td>
                                                                        </tr>
                                                                        <?php
                                                                    }
                                                                    ?>
                                                                    </tbody>
                                                                </table>
                                                            </div>
                                                        <?php endforeach; ?>
                                                    <?php } ?>

                                                </div>

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

                                                    <div class="col-md-12 noleftrightpadding">
                                                        <table class="table border table-striped nomargintop">
                                                            <td class="propertybase-single-table-header"><?php echo _l('propertybase_property_specifications_information'); ?></td>
                                                            <td class="propertybase-single-table-header"><a
                                                                        target="_blank"
                                                                        href="<?php echo admin_url(PROPERTYBASE_MODULE_NAME . '/create/' . $property->id); ?>"
                                                                        class="tw-text-neutral-500 hover:tw-text-neutral-700 focus:tw-text-neutral-700 pull-right">
                                                                    <i class="fa-regular fa-pen-to-square fa-lg"></i>
                                                                </a></td>
                                                            <tbody>
                                                            <tr class="project-overview">
                                                                <td class="bold"><?php echo _l('propertybase_property_purpose'); ?></td>
                                                                <td><?php echo propertybase_property_purposes($property->property_purpose, true); ?></td>
                                                            </tr>
                                                            <tr class="project-overview">
                                                                <td class="bold"><?php echo _l('propertybase_property_size'); ?></td>
                                                                <td><?php echo $property->size; ?></td>
                                                            </tr>
                                                            <tr class="project-overview">
                                                                <td class="bold"><?php echo _l('propertybase_property_bathrooms'); ?></td>
                                                                <td><?php echo $property->bathrooms; ?></td>
                                                            </tr>
                                                            <tr class="project-overview">
                                                                <td class="bold"><?php echo _l('propertybase_property_bedrooms'); ?></td>
                                                                <td><?php echo $property->bedrooms; ?></td>
                                                            </tr>
                                                            <tr class="project-overview">
                                                                <td class="bold"><?php echo _l('propertybase_property_rooms'); ?></td>
                                                                <td><?php echo $property->rooms; ?></td>
                                                            </tr>
                                                            <tr class="project-overview">
                                                                <td class="bold"><?php echo _l('propertybase_property_floor'); ?></td>
                                                                <td><?php echo $property->floor; ?></td>
                                                            </tr>
                                                            <tr class="project-overview">
                                                                <td class="bold"><?php echo _l('propertybase_property_ownership'); ?></td>
                                                                <td><?php echo propertybase_get_property_ownership_name($property->ownership); ?></td>
                                                            </tr>
                                                            </tbody>
                                                        </table>
                                                    </div>

                                                    <div class="col-md-12 noleftrightpadding">
                                                        <table class="table border table-striped nomargintop">
                                                            <td class="propertybase-single-table-header"><?php echo _l('propertybase_property_seo'); ?></td>
                                                            <td class="propertybase-single-table-header"><a
                                                                        target="_blank"
                                                                        href="<?php echo admin_url(PROPERTYBASE_MODULE_NAME . '/create/' . $property->id); ?>"
                                                                        class="tw-text-neutral-500 hover:tw-text-neutral-700 focus:tw-text-neutral-700 pull-right">
                                                                    <i class="fa-regular fa-pen-to-square fa-lg"></i>
                                                                </a></td>
                                                            <tbody>
                                                            <tr class="project-overview">
                                                                <td class="bold"><?php echo _l('propertybase_property_seo_keywords'); ?></td>
                                                                <td><?php echo $property->seo_keywords; ?></td>
                                                            </tr>
                                                            <tr class="project-overview">
                                                                <td class="bold"><?php echo _l('propertybase_property_floor_plan_image'); ?></td>
                                                                <td>
                                                                    <?php if (isset($property) && !empty($property_floor_image) != '') {
                                                                        $path = PROPERTYBASE_PROPERTY_FLOOR_IMAGE_PATH . $property->id . '/' . $property_floor_image[0]['file_name'];
                                                                        ?>
                                                                        <img src="<?php echo site_url('download/preview_image?path=' . protected_file_url_by_path($path) . '&type='); ?>"
                                                                             class="img-thumbnail img-responsive zoom propertybase-single-thumnail-size">
                                                                    <?php } ?>
                                                                </td>
                                                            </tr>
                                                            <tr class="project-overview">
                                                                <td class="bold"><?php echo _l('propertybase_property_embeded_video'); ?></td>
                                                                <td><?php echo html_entity_decode($property->embeded_video) ?? ''; ?></td>
                                                            </tr>
                                                            </tbody>
                                                        </table>
                                                    </div>

                                                    <div class="col-md-12 noleftrightpadding">
                                                        <table class="table border table-striped nomargintop">
                                                            <td class="propertybase-single-table-header"><?php echo _l('propertybase_property_status_settings_information'); ?></td>
                                                            <td class="propertybase-single-table-header"><a
                                                                        target="_blank"
                                                                        href="<?php echo admin_url(PROPERTYBASE_MODULE_NAME . '/create/' . $property->id); ?>"
                                                                        class="tw-text-neutral-500 hover:tw-text-neutral-700 focus:tw-text-neutral-700 pull-right">
                                                                    <i class="fa-regular fa-pen-to-square fa-lg"></i>
                                                                </a></td>
                                                            <tbody>
                                                            <tr class="project-overview">
                                                                <td class="bold"><?php echo _l('propertybase_property_is_featured'); ?></td>
                                                                <td><?php echo propertybase_yes_no_text($property->is_featured); ?></td>
                                                            </tr>
                                                            <tr class="project-overview">
                                                                <td class="bold"><?php echo _l('propertybase_property_is_visible_for_public'); ?></td>
                                                                <td><?php echo propertybase_yes_no_text($property->is_visible_for_public); ?></td>
                                                            </tr>
                                                            <tr class="project-overview">
                                                                <td class="bold"><?php echo _l('propertybase_property_is_activated'); ?></td>
                                                                <td><?php echo propertybase_yes_no_text($property->is_enabled); ?></td>
                                                            </tr>
                                                            <tr class="project-overview">
                                                                <td class="bold"><?php echo _l('propertybase_property_energy_effiency'); ?></td>
                                                                <td></td>
                                                            </tr>
                                                            <tr class="project-overview">
                                                                <td class="bold"><?php echo _l('propertybase_property_energy_efficient'); ?></td>
                                                                <td><?php echo $property->energy_efficient; ?></td>
                                                            </tr>
                                                            <tr class="project-overview">
                                                                <td class="bold"><?php echo _l('propertybase_property_gas_emission'); ?></td>
                                                                <td><?php echo $property->gas_emission; ?></td>
                                                            </tr>
                                                            </tbody>
                                                        </table>
                                                    </div>

                                                    <div class="col-md-12 noleftrightpadding">
                                                        <table class="table border table-striped nomargintop">
                                                            <td class="propertybase-single-table-header"><?php echo _l('propertybase_custom_fields_details'); ?></td>
                                                            <td class="propertybase-single-table-header"><a
                                                                        target="_blank"
                                                                        href="<?php echo admin_url(PROPERTYBASE_MODULE_NAME . '/create/' . $property->id); ?>"
                                                                        class="tw-text-neutral-500 hover:tw-text-neutral-700 focus:tw-text-neutral-700 pull-right">
                                                                    <i class="fa-regular fa-pen-to-square fa-lg"></i>
                                                                </a></td>
                                                            <tbody>
                                                            <?php
                                                            $cf = get_custom_fields('propertybase_pr');

                                                            foreach ($cf as $custom_field) {
                                                                $val = get_custom_field_value($property->id, $custom_field['id'], 'propertybase_pr');
                                                                if (empty($val)) {
                                                                    continue;
                                                                }
                                                                ?>
                                                                <tr class="project-overview">
                                                                    <td class="bold"><?php echo $custom_field['name']; ?></td>
                                                                    <td><?php echo $val; ?></td>
                                                                </tr>
                                                                <?php
                                                            }
                                                            ?>
                                                            </tbody>
                                                        </table>
                                                    </div>
                                                </div>
                                            </div>

                                        </div>
                                    </div>
                                </div>

                                <div role="tabpanel" class="tab-pane" id="property_events">
                                    <?php
                                    foreach ($property_events as $event) {
                                        $eventForm = json_decode($event['event_form']);

                                        unset(
                                            $eventForm->property_id,
                                            $eventForm->id,
                                            $eventForm->notify
                                        );
                                        $assetStatusData = propertybase_get_status_data($eventForm->status);
                                        $eventForm->status = '<span class="' . $assetStatusData['icon'] . '"></span> ' . ucfirst($assetStatusData['value']);
                                        ?>
                                        <table class="table table-bordered table-sm tblEvent" aria-label="Events"
                                               role="presentation">
                                            <tbody>
                                            <tr>
                                                <td class="dataCell dateCell">
                                                    <div class="text_label"><?php echo _l('propertybase_history_changed_at') ?></div><?php echo _d($event['created_at']); ?>
                                                </td>
                                                <td class="dataCell dateCell">
                                                    <div class="text_label"><?php echo _l('propertybase_history_action_by') ?></div>
                                                    <a target="_blank"
                                                       href="<?php echo admin_url('profile/' . $event['event_by']); ?>">
                                                        <?php echo staff_profile_image($event['event_by'], [
                                                            'staff-profile-image-small',
                                                            'mright5',
                                                        ]);
                                                        echo get_staff_full_name($event['event_by']); ?></a>
                                                </td>
                                                <?php
                                                $index = 0;
                                                foreach ($eventForm as $key => $value) {
                                                    $index++;

                                                    if (empty($value)) {
                                                        continue;
                                                    }

                                                    if ($key === 'status') {
                                                        $key = '';
                                                    }
                                                    if ($key === 'allocate_to') {
                                                        $key = _l('propertybase_allocate_to_event');
                                                    }

                                                    if ($key === 'date_sold') {
                                                        $key = _l('propertybase_property_date_sold');
                                                    }

                                                    if ($key === 'sold_to') {
                                                        $key = _l('propertybase_property_date_sold_to');
                                                    }

                                                    if ($key === 'sale_amount') {
                                                        $key = _l('propertybase_property_date_sale_amount');
                                                        $value = app_format_money($value, get_base_currency()->id);
                                                    }

                                                    if ($key === 'assigned_to') {
                                                        $key = _l('propertybase_property_date_assigned_to');
                                                    }

                                                    if ($key === 'signature' && !empty($value)) {
                                                        $mainImagePath = FCPATH . 'modules/' . PROPERTYBASE_MODULE_NAME . '/uploads/property_images/signatures/' . $event['id'] . '/signature.png';
                                                        $renderedImage = site_url('download/preview_image?path=' . protected_file_url_by_path($mainImagePath) . '&type=');

                                                        $assetImage = $renderedImage;
                                                        $value = '<img class="propertybase-signautre-history-size" src="' . $assetImage . '">';
                                                    }

                                                    if ($key == 'customers') {
                                                        $value = '<a href="' . admin_url('clients/client/' . $value) . '">' . get_client($value)->company . '</a>';
                                                    }
                                                    if ($key == 'staff') {
                                                        $value = '<a href="' . admin_url('staff/profile/' . $value) . '">' . get_staff_full_name($value) . '</a>';
                                                    }
                                                    if ($key == 'projects') {
                                                        $value = '<a href="' . admin_url('projects/view/' . $value) . '">' . get_project($value)->name . '</a>';
                                                    }
                                                    if ($key == 'allocation_note') {
                                                        $key = _l('propertybase_allocate_to_note');
                                                    }

                                                    ?>
                                                    <td class="dataCell dateCell">
                                                        <div class="text_label"><?php echo _l($key) ?></div>
                                                        <strong class="propertybase-events-date-cell"><?php echo ucfirst($value); ?></strong>
                                                    </td>
                                                    <?php
                                                }
                                                ?>
                                                <td class="actionCell">
                                                    <div class="tw-flex tw-items-center tw-space-x-3">
                                                        <?php
                                                        if ($assetStatusData['value'] !== 'revocate') {
                                                            ?>
                                                            <a onclick='openAssetStatusModal("<?php echo $assetStatusData["value"]; ?>", "<?php echo $event['id']; ?>")'
                                                               class="tw-text-neutral-500 hover:tw-text-neutral-700 focus:tw-text-neutral-700">
                                                                <i class="fa-regular fa-pen-to-square fa-lg"></i>
                                                            </a>
                                                            <?php
                                                        }
                                                        ?>
                                                        <?php
                                                        if (has_permission('propertybase', '', 'delete')) {
                                                            ?>
                                                            <a onclick='deleteAssetEvent("<?php echo $event['id']; ?>")'
                                                               class="tw-mt-px tw-text-neutral-500 hover:tw-text-neutral-700 focus:tw-text-neutral-700 _delete">
                                                                <i class="fa-regular fa-trash-can fa-lg"></i>
                                                            </a>
                                                            <?php
                                                        }
                                                        ?>
                                                    </div>
                                                </td>
                                            </tr>
                                            </tbody>
                                        </table>
                                        <?php
                                    }
                                    ?>
                                </div>

                                <div role="tabpanel" class="tab-pane" id="property_history">
                                    <?php
                                    $table_data = [
                                        _l('propertybase_history_changed_at'),
                                        _l('propertybase_history_event'),
                                        _l('propertybase_history_field'),
                                        _l('propertybase_history_changed_from'),
                                        _l('propertybase_history_changed_to'),
                                        _l('propertybase_history_action_by')
                                    ];

                                    render_datatable($table_data, 'propertybase-history'); ?>
                                </div>

                                <div class="tab-pane" role="tabpanel" id="asset_files">
                                    <div class="col-md-12">
                                        <?php echo form_open_multipart(admin_url(PROPERTYBASE_MODULE_NAME . '/propertybase/upload_attachment/' . $property->id), ['class' => 'dropzone', 'id' => 'client-attachments-upload']); ?>
                                        <input type="file" name="file" multiple/>
                                        <?php echo form_close(); ?>
                                        <div class="attachments">
                                            <div class="mtop25">

                                                <table class="table dt-table" data-order-col="2" data-order-type="desc">
                                                    <thead>
                                                    <tr>
                                                        <th width="30%"><?php echo _l('customer_attachments_file'); ?></th>
                                                        <th><?php echo _l('customer_attachments_show_in_customers_area'); ?></th>
                                                        <th><?php echo _l('file_date_uploaded'); ?></th>
                                                        <th><?php echo _l('options'); ?></th>
                                                    </tr>
                                                    </thead>
                                                    <tbody>
                                                    <?php foreach ($attachments as $type => $attachment) {
                                                        $download_indicator = 'file_name';
                                                        $key_indicator = 'rel_id';
                                                        $upload_path = PROPERTYBASE_PROPERTY_ATTACHMENTS_PATH . $property->id . '/';

                                                        $url = substr(module_dir_url(PROPERTYBASE_MODULE_NAME . '/uploads/property_images/attachments/' . $property->id . '/'), 0, -1);

                                                        ?>
                                                        <?php foreach ($attachment as $_att) {
                                                            ?>
                                                            <tr id="tr_file_<?php echo $_att['id']; ?>">
                                                            <td>
                                                                <?php
                                                                $path = $upload_path . $_att['file_name'];
                                                                $is_image = false;
                                                                if (!isset($_att['external'])) {
                                                                    $attachment_url = $upload_path . $_att[$download_indicator];
                                                                    $is_image = is_image($path);
                                                                    $downloadPath = substr(module_dir_url(PROPERTYBASE_MODULE_NAME . '/uploads/property_images/attachments/'), 0, -1);
                                                                    $img_url = site_url('download/preview_image?path=' . protected_file_url_by_path($attachment_url, true) . '&type=' . $_att['filetype']);

                                                                    $lightBoxUrl = site_url('download/preview_image?path=' . protected_file_url_by_path($attachment_url) . '&type=' . $_att['filetype']);
                                                                }

                                                                if (!$is_image) {
                                                                    $attachment_url = $url . $_att[$download_indicator];
                                                                }

                                                                if ($is_image) {
                                                                    echo '<div class="preview_image">';
                                                                } ?>
                                                                <a target="_blank" href="<?php if ($is_image) {
                                                                    echo isset($lightBoxUrl) ? $lightBoxUrl : $img_url;
                                                                } else {
                                                                    echo $attachment_url;
                                                                } ?>" <?php if ($is_image) { ?> data-lightbox="customer-profile" <?php } ?>
                                                                   class="display-block mbot5">
                                                                    <?php if ($is_image) { ?>
                                                                        <div class="table-image">
                                                                            <div class="text-center"><i
                                                                                        class="fa fa-spinner fa-spin mtop30"></i>
                                                                            </div>
                                                                            <img src="#" class="img-table-loading"
                                                                                 data-orig="<?php echo $img_url; ?>">
                                                                        </div>
                                                                    <?php } else { ?>
                                                                        <i class="<?php echo get_mime_class($_att['filetype']); ?>"></i>
                                                                        <?php echo $_att['file_name']; ?>
                                                                    <?php } ?>
                                                                </a>
                                                                <?php if ($is_image) {
                                                                    echo '</div>';
                                                                } ?>
                                                            </td>
                                                            <td>
                                                                <div class="onoffswitch">
                                                                    <input type="checkbox"
                                                                           id="<?php echo $_att['id']; ?>"
                                                                           data-id="<?php echo $_att['id']; ?>"
                                                                           class="onoffswitch-checkbox customer_file"
                                                                           data-switch-url="<?php echo admin_url(); ?>misc/toggle_file_visibility" <?php if (isset($_att['visible_to_customer']) && $_att['visible_to_customer'] == 1) {
                                                                        echo 'checked';
                                                                    } ?>>
                                                                    <label class="onoffswitch-label"
                                                                           for="<?php echo $_att['id']; ?>"></label>
                                                                </div>
                                                            </td>
                                                            <td data-order="<?php echo $_att['dateadded']; ?>"><?php echo _dt($_att['dateadded']); ?></td>
                                                            <td>
                                                                <div class="tw-flex tw-items-center tw-space-x-3">
                                                                    <a href="<?php echo admin_url('clients/delete_attachment/' . $_att['rel_id'] . '/' . $_att['id']); ?>"
                                                                       class="tw-mt-px tw-text-neutral-500 hover:tw-text-neutral-700 focus:tw-text-neutral-700 _delete">
                                                                        <i class="fa-regular fa-trash-can fa-lg"></i>
                                                                    </a>
                                                                </div>
                                                            </td>
                                                            <?php
                                                        } ?>
                                                        </tr>
                                                        <?php
                                                    } ?>
                                                    </tbody>
                                                </table>
                                            </div>

                                        </div>
                                    </div>
                                </div>

                                <div role="tabpanel" class="tab-pane" id="asset_reminders">
                                    <div class="col-md-12">
                                        <a href="#" data-toggle="modal" class="btn btn-default"
                                           data-target=".reminder-modal-asset-<?php echo $property->id; ?>"><i
                                                    class="fa-regular fa-bell"></i>
                                            <?php echo _l('propertybase_set_property_reminder'); ?></a>
                                        <hr/>

                                        <?php render_datatable([_l('reminder_description'), _l('reminder_date'), _l('reminder_staff'), _l('reminder_is_notified')], 'reminders'); ?>

                                        <div class="modal fade modal-reminder reminder-modal-asset-<?php echo $property->id; ?>"
                                             tabindex="-1" role="dialog"
                                             aria-labelledby="myModalLabel">
                                            <div class="modal-dialog" role="document">
                                                <div class="modal-content">
                                                    <?php echo form_open('admin/misc/add_reminder/' . $property->id . '/property', ['id' => 'form-reminder-asset']); ?>
                                                    <div class="modal-header">
                                                        <button type="button" class="close close-reminder-modal"
                                                                data-rel-id="<?php echo $property->id; ?>"
                                                                data-rel-type="asset" aria-label="Close"><span
                                                                    aria-hidden="true">&times;</span></button>
                                                        <h4 class="modal-title" id="myModalLabel"><i
                                                                    class="fa-regular fa-circle-question"
                                                                    data-toggle="tooltip"
                                                                    title="<?php echo _l('set_reminder_tooltip'); ?>"
                                                                    data-placement="bottom"></i>
                                                            <?php echo _l('propertybase_set_property_reminder'); ?></h4>
                                                    </div>
                                                    <div class="modal-body">
                                                        <div class="row">
                                                            <div class="col-md-12">
                                                                <?php echo form_hidden('rel_id', $property->id); ?>
                                                                <?php echo form_hidden('rel_type', 'property'); ?>
                                                                <?php echo render_datetime_input('date', 'set_reminder_date', '', array('data-date-min-date' => _d(date('Y-m-d')), 'data-step' => 30)); ?>
                                                                <?php echo render_select('staff', $members, array('staffid', array('firstname', 'lastname')), 'reminder_set_to', get_staff_user_id(), array('data-current-staff' => get_staff_user_id())); ?>
                                                                <?php echo render_textarea('description', 'reminder_description'); ?>
                                                                <?php if (is_email_template_active('reminder-email-staff')) { ?>
                                                                    <div class="form-group">
                                                                        <div class="checkbox checkbox-primary">
                                                                            <input type="checkbox"
                                                                                   name="notify_by_email"
                                                                                   id="notify_by_email">
                                                                            <label for="notify_by_email"><?php echo _l('reminder_notify_me_by_email'); ?></label>
                                                                        </div>
                                                                    </div>
                                                                <?php } ?>
                                                            </div>
                                                        </div>
                                                    </div>
                                                    <div class="modal-footer">
                                                        <button type="button"
                                                                class="btn btn-default close-reminder-modal"
                                                                data-rel-id="<?php echo $property->id; ?>"
                                                                data-rel-type="asset"><?php echo _l('close'); ?></button>
                                                        <button type="submit"
                                                                class="btn btn-primary"><?php echo _l('submit'); ?></button>
                                                    </div>
                                                    <?php echo form_close(); ?>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>

                                <div role="tabpanel" class="tab-pane" id="tab_notes">
                                    <div class="col-md-12">
                                        <?php echo form_open(admin_url(PROPERTYBASE_MODULE_NAME . '/propertybase/add_note/' . $property->id), ['id' => 'sales-notes', 'class' => 'contract-notes-form mtop15']); ?>
                                        <?php echo render_textarea('description'); ?>
                                        <div class="text-right">
                                            <button type="submit"
                                                    class="btn btn-primary mtop15 mbot15 addAssetNote"><?php echo _l('contract_add_note'); ?></button>
                                        </div>
                                        <?php echo form_close(); ?>
                                        <hr/>
                                        <div class="mtop20" id="sales_notes_area"></div>
                                    </div>
                                </div>

                                <div role="tabpanel" class="tab-pane" id="tab_tasks">
                                    <div class="col-md-12">
                                        <?php init_relation_tasks_table(['data-new-rel-id' => $property->id, 'data-new-rel-type' => 'properties']); ?>
                                    </div>
                                </div>

                                <div role="tabpanel" class="tab-pane" id="property_invoices">
                                    <div class="col-md-12">
                                        <div id="stats-top" class="hide">
                                            <div id="invoices_total"></div>

                                            <?php
                                            $where_all = '';
                                            $has_permission_view = has_permission('invoices', '', 'view');
                                            if (isset($property)) {
                                                $where_all .= 'property_id=' . $property->id . ' AND ';
                                            }
                                            if (!$has_permission_view) {
                                                $where_all .= get_invoices_where_sql_for_staff(get_staff_user_id());
                                            }
                                            $where_all = trim($where_all);
                                            if (endsWith($where_all, ' AND')) {
                                                $where_all = substr_replace($where_all, '', -4);
                                            }
                                            $total_invoices = total_rows(db_prefix() . 'invoices', $where_all);
                                            ?>
                                            <div class="quick-top-stats">
                                                <dl
                                                        class="tw-mt-5 tw-grid tw-grid-cols-1 tw-divide-y tw-divide-solid tw-divide-neutral-200 tw-overflow-hidden md:tw-grid-cols-3 lg:tw-grid-cols-5 md:tw-divide-y-0 md:tw-divide-x tw-mb-0">
                                                    <?php foreach ($invoices_statuses as $status) {
                                                        if ($status == Invoices_model::STATUS_CANCELLED) {
                                                            continue;
                                                        }

                                                        $where = ['status' => $status];
                                                        if (isset($property)) {
                                                            $where['property_id'] = $property->id;
                                                        }
                                                        if (!$has_permission_view) {
                                                            $where['addedfrom'] = get_staff_user_id();
                                                        }
                                                        $total_by_status = total_rows(db_prefix() . 'invoices', $where);
                                                        $percent = ($total_invoices > 0 ? number_format(($total_by_status * 100) / $total_invoices, 2) : 0); ?>

                                                        <div class="tw-px-3 tw-py-4 sm:tw-p-4">
                                                            <dt class="tw-font-medium text-<?php echo get_invoice_status_label($status); ?>">
                                                                <?php echo format_invoice_status($status, '', false); ?>
                                                            </dt>
                                                            <dd class="tw-mt-1 tw-flex tw-items-baseline tw-justify-between md:tw-block lg:tw-flex">
                                                                <div class="tw-flex tw-items-baseline tw-text-base tw-font-semibold tw-text-primary-600">
                                                                    <?php echo $total_by_status; ?>
                                                                    / <?php echo $total_invoices; ?>
                                                                    <span class="tw-ml-2 tw-text-sm tw-font-medium tw-text-neutral-500">
                            <a href="#" data-cview="invoices_<?php echo $status; ?>"
                               onclick="dt_custom_view('invoices_<?php echo $status; ?>','.table-invoices','invoices_<?php echo $status; ?>',true); return false;">
                                <?php echo _l('view'); ?>
                            </a>
                        </span>
                                                                </div>
                                                                <div class="tw-font-medium md:tw-mt-2 lg:tw-mt-0">
                                                                    <?php echo $percent; ?>%
                                                                </div>
                                                            </dd>
                                                        </div>
                                                        <?php
                                                    } ?>
                                            </div>
                                            <hr/>
                                        </div>
                                        <div class="_filters _hidden_inputs">
                                            <?php
                                            foreach ($invoices_sale_agents as $agent) {
                                                echo form_hidden('sale_agent_' . $agent['sale_agent']);
                                            }
                                            foreach ($invoices_statuses as $_status) {
                                                $val = '';
                                                if ($_status == $this->input->get('status')) {
                                                    $val = $_status;
                                                }
                                                echo form_hidden('invoices_' . $_status, $val);
                                            }
                                            foreach ($invoices_years as $year) {
                                                echo form_hidden('year_' . $year['year'], $year['year']);
                                            }
                                            foreach ($payment_modes as $mode) {
                                                echo form_hidden('invoice_payments_by_' . $mode['id']);
                                            }
                                            echo form_hidden('not_sent', $this->input->get('filter'));
                                            echo form_hidden('not_have_payment');
                                            echo form_hidden('recurring');
                                            echo form_hidden('project_id');
                                            ?>
                                        </div>

                                        <div class="tw-mb-2 sm:tw-mb-4">
                                            <div class="_buttons">
                                                <?php $this->load->view('admin/invoices/invoices_top_stats'); ?>
                                                <?php if (has_permission('invoices', '', 'create')) { ?>
                                                    <a href="<?php echo admin_url('invoices/invoice'); ?>"
                                                       class="btn btn-primary pull-left new new-invoice-list mright5">
                                                        <i class="fa-regular fa-plus tw-mr-1"></i>
                                                        <?php echo _l('create_new_invoice'); ?>
                                                    </a>
                                                <?php } ?>
                                                <?php if (!isset($property)) { ?>
                                                    <a href="<?php echo admin_url('invoices/recurring'); ?>"
                                                       class="btn btn-default pull-left mleft5">
                                                        <i class="fa-solid fa-repeat tw-mr-1"></i>
                                                        <?php echo _l('invoices_list_recurring'); ?>
                                                    </a>
                                                <?php } ?>
                                                <div class="display-block text-right">
                                                    <div class="btn-group pull-right mleft4 invoice-view-buttons btn-with-tooltip-group _filter_data"
                                                         data-toggle="tooltip"
                                                         data-title="<?php echo _l('filter_by'); ?>">
                                                        <button type="button" class="btn btn-default dropdown-toggle"
                                                                data-toggle="dropdown"
                                                                aria-haspopup="true" aria-expanded="false">
                                                            <i class="fa fa-filter" aria-hidden="true"></i>
                                                        </button>
                                                        <ul class="dropdown-menu width300">
                                                            <li>
                                                                <a href="#" data-cview="all"
                                                                   onclick="dt_custom_view('','.table-invoices',''); return false;">
                                                                    <?php echo _l('invoices_list_all'); ?>
                                                                </a>
                                                            </li>
                                                            <li class="divider"></li>
                                                            <li class="<?php if ($this->input->get('filter') == 'not_sent') {
                                                                echo 'active';
                                                            } ?>">
                                                                <a href="#" data-cview="not_sent"
                                                                   onclick="dt_custom_view('not_sent','.table-invoices','not_sent'); return false;">
                                                                    <?php echo _l('not_sent_indicator'); ?>
                                                                </a>
                                                            </li>
                                                            <li>
                                                                <a href="#" data-cview="not_have_payment"
                                                                   onclick="dt_custom_view('not_have_payment','.table-invoices','not_have_payment'); return false;">
                                                                    <?php echo _l('invoices_list_not_have_payment'); ?>
                                                                </a>
                                                            </li>
                                                            <li>
                                                                <a href="#" data-cview="recurring"
                                                                   onclick="dt_custom_view('recurring','.table-invoices','recurring'); return false;">
                                                                    <?php echo _l('invoices_list_recurring'); ?>
                                                                </a>
                                                            </li>
                                                            <li class="divider"></li>
                                                            <?php foreach ($invoices_statuses as $status) { ?>
                                                                <li class="<?php if ($status == $this->input->get('status')) {
                                                                    echo 'active';
                                                                } ?>">
                                                                    <a href="#"
                                                                       data-cview="invoices_<?php echo $status; ?>"
                                                                       onclick="dt_custom_view('invoices_<?php echo $status; ?>','.table-invoices','invoices_<?php echo $status; ?>'); return false;"><?php echo format_invoice_status($status, '', false); ?></a>
                                                                </li>
                                                            <?php } ?>
                                                            <?php if (count($invoices_years) > 0) { ?>
                                                                <li class="divider"></li>
                                                                <?php foreach ($invoices_years as $year) { ?>
                                                                    <li class="active">
                                                                        <a href="#"
                                                                           data-cview="year_<?php echo $year['year']; ?>"
                                                                           onclick="dt_custom_view(<?php echo $year['year']; ?>,'.table-invoices','year_<?php echo $year['year']; ?>'); return false;"><?php echo $year['year']; ?>
                                                                        </a>
                                                                    </li>
                                                                <?php } ?>
                                                            <?php } ?>
                                                            <?php if (count($invoices_sale_agents) > 0) { ?>
                                                                <div class="clearfix"></div>
                                                                <li class="divider"></li>
                                                                <li class="dropdown-submenu pull-left">
                                                                    <a href="#"
                                                                       tabindex="-1"><?php echo _l('sale_agent_string'); ?></a>
                                                                    <ul class="dropdown-menu dropdown-menu-left">
                                                                        <?php foreach ($invoices_sale_agents as $agent) { ?>
                                                                            <li>
                                                                                <a href="#"
                                                                                   data-cview="sale_agent_<?php echo $agent['sale_agent']; ?>"
                                                                                   onclick="dt_custom_view(<?php echo $agent['sale_agent']; ?>,'.table-invoices','sale_agent_<?php echo $agent['sale_agent']; ?>'); return false;"><?php echo $agent['full_name']; ?>
                                                                                </a>
                                                                            </li>
                                                                        <?php } ?>
                                                                    </ul>
                                                                </li>
                                                            <?php } ?>
                                                            <div class="clearfix"></div>
                                                            <?php if (count($payment_modes) > 0) { ?>
                                                                <li class="divider"></li>
                                                            <?php } ?>
                                                            <?php foreach ($payment_modes as $mode) {
                                                                if (total_rows(db_prefix() . 'invoicepaymentrecords', ['paymentmode' => $mode['id']]) == 0) {
                                                                    continue;
                                                                } ?>
                                                                <li>
                                                                    <a href="#"
                                                                       data-cview="invoice_payments_by_<?php echo $mode['id']; ?>"
                                                                       onclick="dt_custom_view('<?php echo $mode['id']; ?>','.table-invoices','invoice_payments_by_<?php echo $mode['id']; ?>'); return false;">
                                                                        <?php echo _l('invoices_list_made_payment_by', $mode['name']); ?>
                                                                    </a>
                                                                </li>
                                                                <?php
                                                            } ?>
                                                        </ul>
                                                    </div>
                                                    <a href="#" class="btn btn-default btn-with-tooltip invoices-total"
                                                       onclick="slideToggle('#stats-top'); init_property_invoices_total(true); return false;"
                                                       data-toggle="tooltip"
                                                       title="<?php echo _l('view_stats_tooltip'); ?>"><i
                                                                class="fa fa-bar-chart"></i></a>
                                                </div>
                                            </div>
                                        </div>
                                        <div class="row">
                                            <div class="col-md-12" id="small-table">
                                                <?php echo form_hidden('property_id', $property->id); ?>
                                                <?php $this->load->view('admin/invoices/table_html'); ?>
                                            </div>
                                            <div class="col-md-7 small-table-right-col">
                                                <div id="invoice" class="hide">
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>

                                <div role="tabpanel" class="tab-pane" id="property_expenses">
                                    <div class="col-md-12">
                                        <div id="propertybase_expenses_total" class="tw-mb-4"></div>

                                        <?php if (has_permission('expenses', '', 'create')) { ?>
                                            <a href="<?php echo admin_url('expenses/expense?property_id=' . $property->id); ?>"
                                               target="_blank" class="btn btn-primary tw-mb-4">
                                                <i class="fa-regular fa-plus tw-mr-1"></i>
                                                <?php echo _l('new_expense'); ?>
                                            </a>
                                        <?php } ?>
                                        <?php
                                        $data_expenses_filter['total_unbilled'] = $this->db->query('SELECT count(*) as num_rows FROM ' . db_prefix() . 'expenses WHERE (SELECT 1 from ' . db_prefix() . 'invoices WHERE ' . db_prefix() . 'invoices.id = ' . db_prefix() . 'expenses.invoiceid AND status != 2)')->row()->num_rows;
                                        $data_expenses_filter['categories'] = $expense_categories;
                                        $data_expenses_filter['filter_table_name'] = '.table-property-expenses';
                                        $data_expenses_filter['years'] = $this->expenses_model->get_expenses_years();
                                        $this->load->view('admin/expenses/filter_by_template', $data_expenses_filter); ?>
                                        <div class="clearfix"></div>
                                        <?php
                                        echo form_hidden('custom_view');
                                        propertybase_load_view(module_views_path(PROPERTYBASE_MODULE_NAME, '/admin/properties/tabs/tables/expenses_html.php'), [
                                            'class' => 'property-expenses',
                                            'withBulkActions' => false,
                                        ]);
                                        ?>
                                    </div>
                                </div>

                                <div role="tabpanel" class="tab-pane" id="property_maintenance_requests">
                                    <div class="col-md-12">
                                        <?php
                                        propertybase_load_view(module_views_path(PROPERTYBASE_MODULE_NAME, '/admin/maintenance_requests/maintenance_requests_table_html.php'));
                                        ?>
                                    </div>
                                </div>

                                <div role="tabpanel" class="tab-pane" id="property_leads">
                                    <div class="col-md-12">
                                        <?php
                                        propertybase_load_view(module_views_path(PROPERTYBASE_MODULE_NAME, '/admin/properties/tabs/leads/property_leads_html.php'));
                                        ?>
                                    </div>
                                </div>


                                <div class="modal fade" id="new-property-status-modal">
                                    <div class="modal-dialog modal-lg">
                                        <div class="modal-content">
                                            <div class="modal-header">
                                                <button type="button" class="close" data-dismiss="modal"
                                                        aria-hidden="true">
                                                    &times;
                                                </button>
                                                <h4 class="modal-title"><?php echo _l('propertybase_property_status_details') ?></h4>
                                            </div>
                                            <?php echo form_open_multipart(admin_url(PROPERTYBASE_MODULE_NAME . '/save_property_status'), array('id' => 'new-allocate-property-form')); ?>
                                            <?php echo form_hidden('id'); ?>
                                            <?php echo form_hidden('property_id', $property->id); ?>
                                            <?php echo form_hidden('status'); ?>
                                            <div class="modal-body">
                                                <div class="modalPropertyStatusForm"></div>
                                            </div>
                                            <div class="modal-footer">
                                                <button type="button" class="btn btn-default"
                                                        data-dismiss="modal"><?php echo _l('close'); ?></button>
                                                <button type="button" onclick="savePropertyStatusModal()"
                                                        class="btn btn-info btn-submit"><?php echo _l('submit'); ?></button>
                                            </div>
                                            <?php echo form_close(); ?>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <?php init_tail(); ?>
    <script src="<?php echo module_dir_url(PROPERTYBASE_MODULE_NAME, 'assets/js/jspdf.umd.min.js'); ?>"></script>
    <script src="<?php echo module_dir_url(PROPERTYBASE_MODULE_NAME, 'assets/js/html2canvas.min.js'); ?>"></script>
    <script src="<?php echo module_dir_url(PROPERTYBASE_MODULE_NAME, 'assets/js/signature_pad.min.js'); ?>"></script>
    <script>
        (function ($) {
            "use strict";

            init_tabs_scrollable();
            init_form_reminder();
            Dropzone.options.clientAttachmentsUpload = false;

            $(document).ready(function () {
                if ($('#client-attachments-upload').length > 0) {
                    new Dropzone('#client-attachments-upload', appCreateDropzoneOptions({
                        paramName: "file",
                        accept: function (file, done) {
                            done();
                        },
                        success: function (file, response) {
                            if (this.getUploadingFiles().length === 0 && this.getQueuedFiles().length === 0) {
                                window.location.reload();
                            }
                        }
                    }));
                }

                <?php if (!empty($property->lat) && !empty($property->lng)) { ?>
                initMap('<?php echo $property->lat ?>', '<?php echo $property->lng; ?>');
                <?php } ?>
            });

        })(jQuery);

        $(document).on('click', '.addAssetNote', function () {
            setTimeout(() => {
                get_sales_notes(<?php echo $property->id; ?>, 'propertybase');
            }, 300);
        });

        function openAssetStatusModal(status, event_id = '') {
            $('.modalPropertyStatusForm').empty();

            $.ajax({
                type: 'POST',
                url: admin_url + "propertybase/property_status",
                data: {status: status, id: event_id}
            }).done(function (response) {
                response = JSON.parse(response);
                $('.modalPropertyStatusForm').append(response.form);
                $('input[name="status"]').val(status);
                $('input[name="id"]').val(event_id);
                $('#new-property-status-modal').modal('show');
            }).fail(function (error) {
                alert_float('danger', JSON.parse(error.message));
            });

            return false;
        }

        function savePropertyStatusModal() {
            "use strict";

            $('#new-property-status-modal').find('button[type="submit"]').prop('disabled', true);
            var form = '#new-allocate-property-form';
            var formURL = $(form).attr('action');
            var formData = new FormData($(form)[0]);

            $.ajax({
                type: $(form).attr('method'),
                data: formData,
                mimeType: $(form).attr('enctype'),
                contentType: false,
                cache: false,
                processData: false,
                url: formURL
            }).done(function (response) {
                response = JSON.parse(response);

                if (response.success === true || response.success == 'true' || $.isNumeric(response.success)) {
                    alert_float('success', response.message);
                    $('#new-property-status-modal').modal('hide');
                    setTimeout(() => location.reload(), 500);
                } else {
                    alert_float('danger', response.message);
                }
            }).fail(function (error) {
                alert_float('danger', JSON.parse(error.message));
            });

            return false;
        }

        function initMap(lat = '51.505', lng = '-0.09') {
            var defaultLocation = [lat, lng];

            var map = L.map('map', {
                center: defaultLocation,
                zoom: 13
            });

            L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
                attribution: '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
            }).addTo(map);

            var marker = L.marker(defaultLocation, {draggable: true}).addTo(map);

            marker.on('dragend', function () {
                var latLng = marker.getLatLng();
                updateLatLngInputs(latLng.lat, latLng.lng);
            });

            map.on('click', function (e) {
                var latLng = e.latlng;
                marker.setLatLng(latLng);
                updateLatLngInputs(latLng.lat, latLng.lng);
            });
        }

        async function generateAssetPDF() {
            try {
                const {jsPDF} = window.jspdf;
                const content = document.getElementById('propertyDetailsDiv');

                if (!content) {
                    console.error("Element with id 'propertyDetailsDiv' not found.");
                    return;
                }

                const canvas = await html2canvas(content);
                const imgData = canvas.toDataURL('image/png');
                const imgWidth = 210;
                const pageHeight = 297;
                const imgHeight = (canvas.height * imgWidth) / canvas.width;
                let heightLeft = imgHeight;

                const doc = new jsPDF('p', 'mm', 'a4');
                let position = 0;

                doc.addImage(imgData, 'PNG', 0, position, imgWidth, imgHeight);
                heightLeft -= pageHeight;

                while (heightLeft >= 0) {
                    position = heightLeft - imgHeight;
                    doc.addPage();
                    doc.addImage(imgData, 'PNG', 0, position, imgWidth, imgHeight);
                    heightLeft -= pageHeight;
                }

                doc.save('Property-<?php echo $property->name; ?>.pdf');
            } catch (error) {
                console.error("Error generating PDF:", error);
            }
        }

        function deleteAssetEvent(event_id) {
            $.ajax({
                type: 'GET',
                url: admin_url + "propertybase/delete_property_event/" + event_id
            }).done(function (response) {
                response = JSON.parse(response);

                if (response.success === true || response.success == 'true' || $.isNumeric(response.success)) {
                    alert_float('success', response.message);
                    setTimeout(() => location.reload(), 500);
                } else {
                    alert_float('danger', response.message);
                }
            }).fail(function (error) {
                alert_float('danger', JSON.parse(error.message));
            });

            return false;
        }
    </script>