<?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-6">
                <div class="panel_s">
                    <div class="panel-body">

                        <div class="row">
                            <div class="row col-md-12">

                                <div class="col-md-12 panel-padding">
                                    <?php if (has_permission('tasks', '', 'create')) { ?>
                                        <a href="#" class="btn btn-success btn-sm pull-right mbot30"
                                           onclick="convert_maintenance_request_to_task(); return false;"><?php echo _l('convert_to_task'); ?></a>
                                    <?php } ?>
                                    <table class="table border table-striped table-margintop">
                                        <tbody>
                                        <tr class="project-overview">
                                            <td class="bold"
                                                width="30%"><?php echo _l('propertybase_property'); ?></td>
                                            <td><?php echo propertybase_formatted_property_name($property_data->id, true); ?></td>
                                        </tr>
                                        <tr class="project-overview">
                                            <td class="bold"
                                                width="30%"><?php echo _l('propertybase_maintenance_title'); ?></td>
                                            <td><?php echo $maintenance_data->maintenance_title; ?></td>
                                        </tr>
                                        <tr class="project-overview">
                                            <td class="bold"><?php echo _l('propertybase_maintenance_description'); ?></td>
                                            <td><?php echo $maintenance_data->maintenance_description; ?></td>
                                        </tr>
                                        <tr class="project-overview">
                                            <td class="bold"><?php echo _l('propertybase_maintenance_status'); ?></td>
                                            <td><?php echo propertybase_get_request_status_data($maintenance_data->status)['badge'] ?? 'undefined'; ?>
                                                &nbsp;
                                            </td>
                                        </tr>
                                        <tr class="project-overview">
                                            <td class="bold"><?php echo _l('propertybase_created_at'); ?></td>
                                            <td><?php echo $maintenance_data->created_at; ?></td>
                                        </tr>
                                        <tr class="project-overview">
                                            <td class="bold"><?php echo _l('propertybase_requested_by'); ?></td>
                                            <td>
                                                <span class="label label-tag tag-id-1"><?php echo propertybase_get_requested_by_maintenance_request_data($maintenance_data->requested_by, $maintenance_data->requested_by_type, $maintenance_data->user_id); ?></span>&nbsp;
                                            </td>
                                        </tr>
                                        </tbody>
                                    </table>
                                </div>

                                <br>
                            </div>
                        </div>

                        <div class="row">
                            <div class="col-md-12">
                                <h4 class="h4-color"><?php echo _l('propertybase_request_attachments'); ?></h4>
                                <hr class="hr-color">
                                <?php

                                if (empty($maintenance_data->attachments)) {
                                    echo _l('propertybase_request_empty_attachments');
                                }

                                foreach ($maintenance_data->attachments as $image) {
                                    $path = PROPERTYBASE_PROPERTY_MAINTENANCE_REQUESTS_PATH . $maintenance_data->id . '/' . $image['filename'];
                                    $url = substr(module_dir_url(PROPERTYBASE_MODULE_NAME . '/uploads/property_images/maintenance_requests/' . $maintenance_data->id . '/' . $image['filename']), 0, -1);
                                    $is_image = is_image($path);

                                    if (endsWith($url, '.webp')) {
                                        $is_image = true;
                                    }

                                    if ($is_image) {
                                        echo '<div class="preview_image">';
                                    } ?>
                                    <a target="_blank" href="<?php echo $url; ?>"
                                       class="display-block mbot5" <?php if ($is_image) { ?>
                                        data-lightbox="attachment-reply-" <?php } ?>>
                                        <i class=""></i>
                                        <?php echo $image['filename']; ?>
                                        <?php if ($is_image) { ?>
                                            <img class="mtop5"
                                                 src="<?php echo $url; ?>">
                                        <?php } ?>
                                    </a>
                                    <?php if ($is_image) {
                                        echo '</div>';
                                    }
                                }
                                ?>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="col-md-6">
                <div class="panel_s">
                    <div class="panel-body">
                        <div class="col-md-12 mbot10">
                            <h4 class="text-center"><?php echo _l('propertybase_update_maintenance_request_status'); ?></h4>
                        </div>

                        <?php echo form_open(current_full_url(), ['id' => 'maintenanceForm']); ?>

                        <?php echo render_select('status', propertybase_maintenance_request_statuses(), ['id', 'name'], 'propertybase_maintenance_status', $maintenance_data->status, [], [], '', '', false); ?>

                        <div class="text-right">
                            <button type="submit" class="btn btn-primary pull-right"><?php echo _l('save'); ?></button>
                        </div>
                        <?php
                        echo form_close();
                        ?>
                    </div>
                </div>

                <div class="panel_s">
                    <div class="panel-body">
                        <div class="col-md-12">
                            <?php init_relation_tasks_table(['data-new-rel-id' => $maintenance_data->id, 'data-new-rel-type' => 'maintenance_request']); ?>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<?php init_tail(); ?>
<script>
    (function ($) {
        "use strict";

        $(function () {
            init_rel_tasks_table(<?php echo $maintenance_data->id; ?>, 'maintenance_request');

            appValidateForm($('#maintenanceForm'), {
                status: 'required'
            });
        });
    })(jQuery);

    function convert_maintenance_request_to_task() {
        var new_task_url = admin_url +
            'tasks/task?rel_id=<?php echo $maintenance_data->id; ?>&rel_type=maintenance_request&ticket_to_task=true';
        new_task(new_task_url);
    }
</script>

