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

class Propertybase_client extends ClientsController
{

    public function __construct()
    {
        parent::__construct();
        $this->load->model('propertybase_model');
    }

    public function index()
    {
        show_404();
    }

    public function properties()
    {
        if (!has_contact_permission('properties')) {
            set_alert('warning', _l('access_denied'));
            redirect(site_url());
        }

        $data = [];

        $data['title'] = _l('propertybase_properties');
        $data['properties_list'] = $this->propertybase_model->get_assigned_properties_for_client(get_client_user_id());

        $data = hooks()->apply_filters('propertybase_client_manage_properties_page', $data);

        $this->data($data);
        $this->view('client/properties/manage');
        $this->layout();
    }

    public function manage_maintenance_requests($property_id)
    {
        if (!has_contact_permission('properties')) {
            set_alert('warning', _l('access_denied'));
            redirect(site_url());
        }

        $this->load->model('PropertyMaintenance_model');

        $data = [];

        $data['title'] = _l('propertybase_your_maintenance_requests_for', propertybase_formatted_property_name($property_id));
        $data['property_id'] = $property_id;
        $data['maintenance_requests'] = $this->PropertyMaintenance_model->get_client_property_maintenance_requests($property_id);

        $this->data($data);
        $this->view('client/properties/manage_maintenance_requests');
        $this->layout();
    }

    public function request_maintenance($property_id)
    {
        if (!has_contact_permission('properties')) {
            set_alert('warning', _l('access_denied'));
            redirect(site_url());
        }

        $this->load->model('PropertyMaintenance_model');

        if ($this->input->post()) {
            $this->form_validation->set_rules('maintenance_title', _l('customer_ticket_subject'), 'required');
            $this->form_validation->set_rules('maintenance_description', _l('propertybase_request_description'), 'required');

            if ($this->form_validation->run() !== false) {

                $data = $this->input->post();
                $propertyData = $this->propertybase_model->get($property_id);

                $id = $this->PropertyMaintenance_model->create([
                    'property_id' => $property_id,
                    'maintenance_title' => $data['maintenance_title'],
                    'maintenance_description' => $data['maintenance_description'],
                    'user_id' => get_client_user_id(),
                    'requested_by' => get_contact_user_id(),
                    'requested_by_type' => 'customer',
                    'status' => '1',
                    'created_at' => date('Y-m-d H:i:s')
                ]);

                if ($id) {

                    $notified = add_notification([
                        'description' => 'propertybase_maintenance_request_assigned_to_you',
                        'touserid' => $propertyData->selected_agent,
                        'fromcompany' => 1,
                        'fromuserid' => 0,
                        'link' => PROPERTYBASE_MODULE_NAME . '/maintenance_requests/view/' . $id,
                        'additional_data' => serialize([
                            $data['maintenance_title'],
                        ]),
                    ]);

                    if ($notified) {
                        pusher_trigger_notification([$data['assigned']]);
                    }

                    $this->load->model('emails_model');
                    $this->emails_model->send_simple_email(
                        get_staff($propertyData->selected_agent)->email,
                        'New Property Maintenance Request - ' . $propertyData->name,
                        '
            Hello,
<br>
<br>
For Property with title <strong>' . $propertyData->name . '</strong> has been created new maintenance request to you by : <strong>' . get_contact_full_name(get_contact_user_id()) . '</strong>
<br>
<br>
<a href="' . admin_url(PROPERTYBASE_MODULE_NAME . '/maintenance_requests/view/' . $id) . '">More Details</a>
<br>
<br>
Best regards,<br>
' . get_option('companyname') . '
            '
                    );

                    set_alert('success', _l('propertybase_new_maintenance_request'));
                    redirect(site_url(PROPERTYBASE_MODULE_NAME . '/propertybase_client/manage_maintenance_requests/' . $property_id));
                }
            }
        }

        $data = [];
        $data['title'] = _l('propertybase_new_request_maintenance');
        $data['property_id'] = $property_id;

        $data = hooks()->apply_filters('propertybase_client_request_maintenance_page', $data);

        $this->data($data);
        $this->view('client/properties/request_maintenance');
        $this->layout();
    }

}