<?php defined('BASEPATH') or exit('No direct script access allowed'); ?>
<?php init_head(); ?>
<div id="wrapper">
    <div class="content">
        <div class="row">
            <div class="col-md-12">
                <div class="_buttons">
                    <?php if (has_permission('propertybase', '', 'create')) { ?>
                        <a href="<?php echo admin_url(PROPERTYBASE_MODULE_NAME . '/create'); ?>"
                           class="btn btn-primary">
                            <i class="fa-regular fa-plus tw-mr-1"></i>
                            <?php echo _l('propertybase_add_property'); ?>
                        </a>
                    <?php } ?>
                    <?php
                    if (is_admin()) {
                        ?>
                        <a href="<?php echo admin_url(PROPERTYBASE_MODULE_NAME . '/import_properties'); ?>"
                           class="btn btn-success">
                            <i class="fa-solid fa-upload tw-mr-1"></i>
                            <?php echo _l('propertybase_import_properties'); ?></a>

                        <a href="<?php echo admin_url(PROPERTYBASE_MODULE_NAME . '/bulk_property_agent_transfer'); ?>"
                           class="btn btn-secondary">
                            <i class="fa-solid fa-exchange-alt tw-mr-1"></i>
                            <?php echo _l('propertybase_bulk_property_agent_transfer'); ?></a>

                        <?php
                    }
                    ?>
                </div>
            </div>
            <br>
            <br>
            <br>
            <div class="col-md-12">
                <div class="panel_s">
                    <div class="panel-body panel-table-full">
                        <div class="display-block">
                            <div class="col-md-4 col-xs-6 border-right">
                                <?php echo render_select('property_categories', $property_categories, ['id', 'name'], 'propertybase_property_categories', '', ['multiple' => true, 'data-actions-box' => true], [], '', '', false); ?>
                            </div>
                            <div class="col-md-4 col-xs-6 border-right">
                                <?php echo render_select('property_country', $property_countries, ['country_id', ['short_name'], 'iso2'], 'propertybase_property_country', '', ['multiple' => true, 'data-actions-box' => true], [], '', '', false); ?>
                            </div>
                            <div class="col-md-4 col-xs-6">
                                <?php echo render_select('property_agents', $members, ['staffid', ['firstname', 'lastname']], 'propertybase_property_agent', '', ['multiple' => true, 'data-actions-box' => true], [], '', '', false); ?>
                            </div>
                            <div class="col-md-4 col-xs-6 border-right">
                                <?php echo render_select('ownership', $property_ownership_types, ['id', 'name'], 'propertybase_property_ownership', '', ['multiple' => true, 'data-actions-box' => true], [], '', '', false); ?>
                            </div>
                            <div class="col-md-4 col-xs-6 border-right">
                                <?php echo render_select('property_purpose', propertybase_property_purposes(null, true), ['id', 'purpose'], 'propertybase_property_purpose', '', ['multiple' => true, 'data-actions-box' => true], [], '', '', false); ?>
                            </div>
                            <div class="col-md-4 col-xs-6">
                                <?php echo render_select('energy_effiency', $property_energy_efficiency_types, ['id', 'name'], 'propertybase_property_energy_effiency', '', ['multiple' => true, 'data-actions-box' => true], [], '', '', false); ?>
                            </div>
                            <div class="col-md-4 col-xs-6 border-right">
                                <?php echo render_input('size', 'propertybase_property_size'); ?>
                            </div>
                            <div class="col-md-4 col-xs-6 border-right">
                                <?php echo render_input('bathrooms', 'propertybase_property_bathrooms'); ?>
                            </div>
                            <div class="col-md-4 col-xs-6">
                                <?php echo render_input('bedrooms', 'propertybase_property_bedrooms'); ?>
                            </div>
                            <div class="col-md-4 col-xs-6 border-right">
                                <?php echo render_input('rooms', 'propertybase_property_rooms'); ?>
                            </div>
                            <div class="col-md-4 col-xs-6 border-right">
                                <?php echo render_input('floor', 'propertybase_property_floor'); ?>
                            </div>
                            <div class="col-md-4 col-xs-6">
                                <?php echo render_input('city', 'propertybase_property_city'); ?>
                            </div>
                            <div class="col-md-4 col-xs-6">
                                <?php echo render_select('projects', $projects_list, ["id", "name"], 'projects', '', ['multiple' => true, 'data-actions-box' => true], [], '', '', false); ?>
                            </div>
                            <div class="col-md-4 col-xs-6">
                                <?php echo render_select('customers', $client_list, ["userid", "company"], 'customers', '', ['multiple' => true, 'data-actions-box' => true], [], '', '', false); ?>
                            </div>
                            <div class="col-md-4 col-xs-6">
                                <?php echo render_select("allocate_to", propertybase_allocate_to_options(), ["value", "name"], "propertybase_allocate_to_event", '', ['multiple' => true, 'data-actions-box' => true], [], '', ''); ?>
                            </div>
                            <div class="col-md-12 col-xs-6">
                                <?php echo render_select("property_status", propertybase_property_statuses(), ["id", "name"], "propertybase_property_status", '', ['multiple' => true, 'data-actions-box' => true], [], '', ''); ?>
                            </div>
                            <div class="col-md-6 col-xs-6">
                                <label for="price_from"><?php echo _l('propertybase_price_from'); ?></label>
                                <div class="input-group">
                                    <input type="text" class="form-control" name="price_from">
                                    <div class="input-group-addon">
                                        <?php echo get_base_currency()->symbol; ?>
                                    </div>
                                </div>
                            </div>
                            <div class="col-md-6 col-xs-6">
                                <label for="price_to"><?php echo _l('propertybase_price_to'); ?></label>
                                <div class="input-group">
                                    <input type="text" class="form-control" name="price_to">
                                    <div class="input-group-addon">
                                        <?php echo get_base_currency()->symbol; ?>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-md-12">
                <div class="panel_s">
                    <div class="panel-body panel-table-full">
                        <?php render_datatable([
                            _l('propertybase_property_id'),
                            _l('propertybase_property_image'),
                            _l('propertybase_property_name'),
                            _l('propertybase_property_agent'),
                            _l('propertybase_property_status'),
                            _l('propertybase_property_category'),
                            _l('propertybase_property_purpose'),
                            _l('propertybase_property_ownership'),
                            _l('propertybase_property_location_information'),
                            _l('propertybase_property_pricing_information'),
                            _l('propertybase_created_at'),
                        ], 'propertybase-properties'); ?>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
</div>
<?php init_tail(); ?>
<script>
    (function ($) {
        "use strict";

        var fnServerParams;
        let debounceTimer;

        $(function () {
            generate_table();

            fnServerParams = {
                "property_categories": '[name="property_categories"]',
                "property_country": '[name="property_country"]',
                "property_agents": '[name="property_agents"]',
                "ownership": '[name="ownership"]',
                "property_purpose": '[name="property_purpose"]',
                "energy_effiency": '[name="energy_effiency"]',
                "size": '[name="size"]',
                "bathrooms": '[name="bathrooms"]',
                "bedrooms": '[name="bedrooms"]',
                "rooms": '[name="rooms"]',
                "floor": '[name="floor"]',
                "city": '[name="city"]',
                "price_from": '[name="price_from"]',
                "price_to": '[name="price_to"]',
                'property_projects': '[name="projects"]',
                'property_customers': '[name="customers"]',
                'property_allocate_to': '[name="allocate_to"]',
                'property_status': '[name="property_status"]'
            };

            $(document).on('change', 'select[name="property_categories"], select[name="property_country"], select[name="property_agents"], select[name="ownership"], select[name="projects"], select[name="customers"], select[name="allocate_to"], select[name="property_status"], select[name="property_purpose"], select[name="energy_effiency"]', function () {
                generate_table();
            });

            $(document).on('keyup', 'input[name="size"], input[name="bathrooms"], input[name="bedrooms"], input[name="rooms"], input[name="floor"], input[name="city"], input[name="price_from"], input[name="price_to"]', function () {
                clearTimeout(debounceTimer);
                debounceTimer = setTimeout(generate_table, 2000);
            });
        });

        function generate_table() {
            const tableClass = $('.table-propertybase-properties');

            if ($.fn.DataTable.isDataTable(tableClass)) {
                tableClass.DataTable().destroy();
            }
            initDataTable(tableClass, window.location.href, [], [], fnServerParams, [0, 'desc']);
        }

    })(jQuery);
</script>