<?php

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

class Settings extends AdminController
{

    public function __construct()
    {
        parent::__construct();
        $this->load->model('payment_modes_model');
        $this->load->model('settings_model');
        $this->load->model('propertyamenities_model');
        $this->load->model('PropertyAmenitiesTypes_model');
        $this->load->model('PropertyAreaTypes_model');
        $this->load->model('propertycategories_model');
        $this->load->model('PropertyEnergyEfficiencyTypes_model');
        $this->load->model('PropertyOwnershipTypes_model');
        hooks()->do_action('propertybase_init');
    }

    public function index()
    {
        if (!is_admin()) {
            access_denied('propertybase');
        }

        $tab = $this->input->get('group');

        if ($this->input->post() && !$this->input->is_ajax_request()) {
            if (!has_permission('settings', '', 'edit')) {
                access_denied('settings');
            }
            $logo_uploaded = (handle_company_logo_upload() ? true : false);
            $favicon_uploaded = (handle_favicon_upload() ? true : false);
            $signatureUploaded = (handle_company_signature_upload() ? true : false);

            $post_data = $this->input->post();
            $tmpData = $this->input->post(null, false);

            if (isset($post_data['settings']['email_header'])) {
                $post_data['settings']['email_header'] = $tmpData['settings']['email_header'];
            }

            if (isset($post_data['settings']['email_footer'])) {
                $post_data['settings']['email_footer'] = $tmpData['settings']['email_footer'];
            }

            if (isset($post_data['settings']['email_signature'])) {
                $post_data['settings']['email_signature'] = $tmpData['settings']['email_signature'];
            }

            if (isset($post_data['settings']['smtp_password'])) {
                $post_data['settings']['smtp_password'] = $tmpData['settings']['smtp_password'];
            }

            $success = $this->settings_model->update($post_data);

            if ($success > 0) {
                set_alert('success', _l('settings_updated'));
            }

            if ($logo_uploaded || $favicon_uploaded) {
                set_debug_alert(_l('logo_favicon_changed_notice'));
            }

            // Do hard refresh on general for the logo
            if ($tab == 'general') {
                redirect(admin_url(PROPERTYBASE_MODULE_NAME . '/settings?group=' . $tab), 'refresh');
            } elseif ($signatureUploaded) {
                redirect(admin_url(PROPERTYBASE_MODULE_NAME . '/settings?group=pdf&tab=signature'));
            } else {
                $redUrl = admin_url(PROPERTYBASE_MODULE_NAME . '/settings?group=' . $tab);
                if ($this->input->get('active_tab')) {
                    $redUrl .= '&tab=' . $this->input->get('active_tab');
                }
                redirect($redUrl);
            }
        }

        $data['title'] = _l('propertybase') . ' ' . _l('options');

        if (!$tab) {
            $tab = 'general';
        }

        $tabData = propertybase_settings_tabs($tab);
        $data['tabs'] = propertybase_settings_tabs();

        $data['tab'] = $tabData;

        if (!$data['tab']) {
            show_404();
        }

        if ($this->input->is_ajax_request() && !empty($tabData['table_path'])) {
            $this->app->get_table_data($tabData['table_path']);
        }

        $data = hooks()->apply_filters('propertybase_settings_data', $data);
        $this->load->view('admin/settings/all', $data);
    }

    public function property_categories()
    {
        if (!is_admin()) {
            access_denied('propertybase');
        }

        if ($this->input->post()) {

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

            if ($data['id'] == '') {

                $success = $this->propertycategories_model->create($data);

                if ($success) {
                    $message = _l('added_successfully', _l('propertybase_property_category'));
                } else {
                    $message = _l('add_failure');
                }

            } else {

                $id = $data['id'];
                unset($data['id']);

                $success = $this->propertycategories_model->update($id, $data);

                if ($success) {
                    $message = _l('updated_successfully', _l('propertybase_property_category'));
                } else {
                    $message = _l('updated_fail');
                }

            }

            echo json_encode(['success' => $success, 'message' => $message]);
            die();
        }
    }

    public function delete_property_category($id)
    {
        if (!is_admin()) {
            access_denied('propertybase');
        }

        $response = $this->propertycategories_model->delete($id);

        if (is_array($response) && isset($response['referenced'])) {
            set_alert('warning', _l('is_referenced', _l('propertybase_property_category')));
        } elseif ($response == true) {
            set_alert('success', _l('deleted', _l('propertybase_property_category')));
        } else {
            set_alert('warning', _l('problem_deleting', _l('propertybase_property_category')));
        }

        redirect(admin_url(PROPERTYBASE_MODULE_NAME . '/settings?group=property_categories'));
    }

    public function get_data_property_category($id)
    {
        echo json_encode($this->propertycategories_model->get($id));
    }

    public function property_amenities_types()
    {
        if (!is_admin()) {
            access_denied('propertybase');
        }

        if ($this->input->post()) {

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

            if ($data['id'] == '') {

                $success = $this->PropertyAmenitiesTypes_model->create($data);

                if ($success) {
                    $message = _l('added_successfully', _l('propertybase_property_amenities_types'));
                } else {
                    $message = _l('add_failure');
                }

            } else {

                $id = $data['id'];
                unset($data['id']);

                $success = $this->PropertyAmenitiesTypes_model->update($id, $data);

                if ($success) {
                    $message = _l('updated_successfully', _l('propertybase_property_amenities_types'));
                } else {
                    $message = _l('updated_fail');
                }

            }

            echo json_encode(['success' => $success, 'message' => $message]);
            die();
        }
    }

    public function delete_property_amenities_types($id)
    {
        if (!is_admin()) {
            access_denied('propertybase');
        }

        $response = $this->PropertyAmenitiesTypes_model->delete($id);

        if (is_array($response) && isset($response['referenced'])) {
            set_alert('warning', _l('is_referenced', _l('propertybase_property_amenities_types')));
        } elseif ($response == true) {
            set_alert('success', _l('deleted', _l('propertybase_property_amenities_types')));
        } else {
            set_alert('warning', _l('problem_deleting', _l('propertybase_property_amenities_types')));
        }

        redirect(admin_url(PROPERTYBASE_MODULE_NAME . '/settings?group=property_amenities_types'));
    }

    public function get_data_property_amenities_types($id)
    {
        echo json_encode($this->PropertyAmenitiesTypes_model->get($id));
    }

    public function property_amenities()
    {
        if (!is_admin()) {
            access_denied('propertybase');
        }

        if ($this->input->post()) {

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

            if ($data['id'] == '') {

                $success = $this->propertyamenities_model->create($data);

                if ($success) {
                    $message = _l('added_successfully', _l('propertybase_property_amenities'));
                } else {
                    $message = _l('add_failure');
                }

            } else {

                $id = $data['id'];
                unset($data['id']);

                $success = $this->propertyamenities_model->update($id, $data);

                if ($success) {
                    $message = _l('updated_successfully', _l('propertybase_property_amenities'));
                } else {
                    $message = _l('updated_fail');
                }

            }

            echo json_encode(['success' => $success, 'message' => $message]);
            die();
        }
    }

    public function delete_property_amenities($id)
    {
        if (!is_admin()) {
            access_denied('propertybase');
        }

        $response = $this->propertyamenities_model->delete($id);

        if (is_array($response) && isset($response['referenced'])) {
            set_alert('warning', _l('is_referenced', _l('propertybase_property_amenities')));
        } elseif ($response == true) {
            set_alert('success', _l('deleted', _l('propertybase_property_amenities')));
        } else {
            set_alert('warning', _l('problem_deleting', _l('propertybase_property_amenities')));
        }

        redirect(admin_url(PROPERTYBASE_MODULE_NAME . '/settings?group=property_amenities'));
    }

    public function get_data_property_amenities($id)
    {
        echo json_encode($this->propertyamenities_model->get($id));
    }

    public function property_area_types()
    {
        if (!is_admin()) {
            access_denied('propertybase');
        }

        if ($this->input->post()) {

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

            if ($data['id'] == '') {

                $success = $this->PropertyAreaTypes_model->create($data);

                if ($success) {
                    $message = _l('added_successfully', _l('propertybase_property_area_types'));
                } else {
                    $message = _l('add_failure');
                }

            } else {

                $id = $data['id'];
                unset($data['id']);

                $success = $this->PropertyAreaTypes_model->update($id, $data);

                if ($success) {
                    $message = _l('updated_successfully', _l('propertybase_property_area_types'));
                } else {
                    $message = _l('updated_fail');
                }

            }

            echo json_encode(['success' => $success, 'message' => $message]);
            die();
        }
    }

    public function delete_property_area_types($id)
    {
        if (!is_admin()) {
            access_denied('propertybase');
        }

        $response = $this->PropertyAreaTypes_model->delete($id);

        if (is_array($response) && isset($response['referenced'])) {
            set_alert('warning', _l('is_referenced', _l('propertybase_property_area_types')));
        } elseif ($response == true) {
            set_alert('success', _l('deleted', _l('propertybase_property_area_types')));
        } else {
            set_alert('warning', _l('problem_deleting', _l('propertybase_property_area_types')));
        }

        redirect(admin_url(PROPERTYBASE_MODULE_NAME . '/settings?group=property_area_types'));
    }

    public function get_data_property_area_types($id)
    {
        echo json_encode($this->PropertyAreaTypes_model->get($id));
    }

    public function property_ownership_types()
    {
        if (!is_admin()) {
            access_denied('propertybase');
        }

        if ($this->input->post()) {

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

            if ($data['id'] == '') {

                $success = $this->PropertyOwnershipTypes_model->create($data);

                if ($success) {
                    $message = _l('added_successfully', _l('propertybase_ownership_type_name'));
                } else {
                    $message = _l('add_failure');
                }

            } else {

                $id = $data['id'];
                unset($data['id']);

                $success = $this->PropertyOwnershipTypes_model->update($id, $data);

                if ($success) {
                    $message = _l('updated_successfully', _l('propertybase_ownership_type_name'));
                } else {
                    $message = _l('updated_fail');
                }

            }

            echo json_encode(['success' => $success, 'message' => $message]);
            die();
        }
    }

    public function delete_property_ownership_types($id)
    {
        if (!is_admin()) {
            access_denied('propertybase');
        }

        $response = $this->PropertyOwnershipTypes_model->delete($id);

        if (is_array($response) && isset($response['referenced'])) {
            set_alert('warning', _l('is_referenced', _l('propertybase_ownership_type_name')));
        } elseif ($response == true) {
            set_alert('success', _l('deleted', _l('propertybase_ownership_type_name')));
        } else {
            set_alert('warning', _l('problem_deleting', _l('propertybase_ownership_type_name')));
        }

        redirect(admin_url(PROPERTYBASE_MODULE_NAME . '/settings?group=property_ownership_types'));
    }

    public function get_data_property_ownership_types($id)
    {
        echo json_encode($this->PropertyOwnershipTypes_model->get($id));
    }

    public function property_energy_efficiency_types()
    {
        if (!is_admin()) {
            access_denied('propertybase');
        }

        if ($this->input->post()) {

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

            if ($data['id'] == '') {

                $success = $this->PropertyEnergyEfficiencyTypes_model->create($data);

                if ($success) {
                    $message = _l('added_successfully', _l('propertybase_energy_efficiency_type'));
                } else {
                    $message = _l('add_failure');
                }

            } else {

                $id = $data['id'];
                unset($data['id']);

                $success = $this->PropertyEnergyEfficiencyTypes_model->update($id, $data);

                if ($success) {
                    $message = _l('updated_successfully', _l('propertybase_energy_efficiency_type'));
                } else {
                    $message = _l('updated_fail');
                }

            }

            echo json_encode(['success' => $success, 'message' => $message]);
            die();
        }
    }

    public function delete_property_energy_efficiency_types($id)
    {
        if (!is_admin()) {
            access_denied('propertybase');
        }

        $response = $this->PropertyEnergyEfficiencyTypes_model->delete($id);

        if (is_array($response) && isset($response['referenced'])) {
            set_alert('warning', _l('is_referenced', _l('propertybase_energy_efficiency_type')));
        } elseif ($response == true) {
            set_alert('success', _l('deleted', _l('propertybase_energy_efficiency_type')));
        } else {
            set_alert('warning', _l('problem_deleting', _l('propertybase_energy_efficiency_type')));
        }

        redirect(admin_url(PROPERTYBASE_MODULE_NAME . '/settings?group=energy_efficiency_types'));
    }

    public function get_data_property_energy_efficiency_types($id)
    {
        echo json_encode($this->PropertyEnergyEfficiencyTypes_model->get($id));
    }
}