<?php

defined('BASEPATH') or exit('No direct script access allowed');

class Maintenance_requests extends AdminController
{
    public function __construct()
    {
        parent::__construct();
        $this->load->model('propertybase_model');
        hooks()->do_action('propertybase_init');
    }

    public function index($property_id = '')
    {
        if (!has_permission('propertybase_maintenance_requests', '', 'view')) {
            access_denied('propertybase');
        }

        if ($this->input->is_ajax_request()) {
            $this->app->get_table_data(module_views_path(PROPERTYBASE_MODULE_NAME, 'admin/maintenance_requests/maintenance_requests_table'), [
                'property_id' => $property_id,
                'postData' => $_POST
            ]);
        }

        $data['title'] = _l('propertybase_maintenance_requests');

        $data['properties'] = $this->propertybase_model->get_all();
        $data['request_statuses'] = propertybase_maintenance_request_statuses();

        $this->load->view('admin/maintenance_requests/manage', $data);
    }

    public function view($maintenance_id)
    {
        if (!has_permission('propertybase_maintenance_requests', '', 'edit')) {
            access_denied('propertybase');
        }

        $this->load->model('PropertyMaintenance_model');
        $data['maintenance_data'] = $this->PropertyMaintenance_model->get($maintenance_id);
        $data['property_data'] = $this->propertybase_model->get($data['maintenance_data']->property_id);

        if ($this->input->post() && isset($maintenance_id)) {

            $postData = $this->input->post();

            $response = $this->PropertyMaintenance_model->update($maintenance_id, ['status' => $postData['status']]);

            if ($response == true) {

                if (get_staff_user_id() != $data['property_data']->selected_agent) {
                    $notified = add_notification([
                        'description' => 'propertybase_request_maintenance_status_update',
                        'touserid' => $data['property_data']->selected_agent,
                        'fromcompany' => 1,
                        'fromuserid' => 0,
                        'link' => PROPERTYBASE_MODULE_NAME . '/maintenance_requests/view/' . $maintenance_id,
                        'additional_data' => serialize([
                            $data['maintenance_data']->maintenance_title,
                            propertybase_get_request_status_data($postData['status'])['name'],
                            get_staff_full_name(get_staff_user_id())
                        ]),
                    ]);

                    if ($notified) {
                        pusher_trigger_notification([$postData['audit_by']]);
                    }
                }

                if ($data['maintenance_data']->requested_by_type == 'customer' && $data['maintenance_data']->requested_by !== 0) {

                    $sendToContactEmail = propertybase_get_contact_data($data['maintenance_data']->requested_by)->email;
                    if (!empty($sendToContactEmail)) {

                        $this->load->model('emails_model');
                        $this->emails_model->send_simple_email(
                            $sendToContactEmail,
                            $data['maintenance_data']->maintenance_title . ' - Maintenance Request Changed Status To ' . propertybase_get_request_status_data($postData['status'])['name'] ?? 'undefined' . ' -' . get_option('companyname'),
                            '
            Hello,
<br>
<br>
Maintenance Request with title <strong>' . $data['maintenance_data']->maintenance_title . '</strong> has changed status to <strong>' . propertybase_get_request_status_data($postData['status'])['name'] . '</strong> by staff member <strong>' . get_staff_full_name(get_staff_user_id()) . '</strong>.
<br>
<br>
<a href="' . site_url(PROPERTYBASE_MODULE_NAME . '/propertybase_client/manage_maintenance_requests/' . $data['property_data']->id) . '">More Details</a>
<br>
<br>
Best regards,<br>
' . get_option('companyname') . '
            '
                        );

                    }

                }

                set_alert('success', _l('propertybase_maintenance_status_updated_success'));
            } else {
                set_alert('warning', _l('propertybase_maintenance_status_updated_failed'));
            }

            redirect(admin_url(PROPERTYBASE_MODULE_NAME . '/maintenance_requests/view/' . $maintenance_id));

        }

        $data['title'] = _l('propertybase_maintenance_requests') . ' - ' . $data['maintenance_data']->maintenance_title;
        $this->load->view('admin/maintenance_requests/view', $data);
    }

    public function delete($maintenance_id)
    {
        if (!has_permission('propertybase_maintenance_requests', '', 'delete')) {
            access_denied('propertybase');
        }
        $this->load->model('PropertyMaintenance_model');

        $response = $this->PropertyMaintenance_model->delete($maintenance_id);

        if ($response == true) {
            set_alert('success', _l('deleted', _l('propertybase_maintenance_requests')));
        } else {
            set_alert('warning', _l('problem_deleting', _l('propertybase_maintenance_requests')));
        }

        redirect(admin_url(PROPERTYBASE_MODULE_NAME . '/maintenance_requests'));
    }

}