<?php
defined('BASEPATH') or exit('No direct script access allowed');
class Enquiry extends CI_Controller
{

    public function __construct()
    {
        parent::__construct();
        header('Access-Control-Allow-Origin: *');
        header('Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, Access-Control-Request-Method');
        header('Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE');
        // // avoiding preflight requests
        $method = $_SERVER['REQUEST_METHOD'];
        if ($method == "OPTIONS") {
            die();
        }

        $this->load->helper('form');
        $this->load->library('upload');
    }

    public function add_enquiry()
    {
        $json_data = json_decode(@file_get_contents('php://input'), true);
            $data = array(
                "name" => $json_data['name'],
                "email" => $json_data['email'],
                "nationality" => $json_data['nationality'],
                "travel_period" => $json_data['travel_period'],
                "number_of_days" => $json_data['number_of_days'],
                "number_of_persons" => $json_data['number_of_persons'],
                "accommodation" => $json_data['accommodation'],
                "budget_indication" => $json_data['budget_indication'],
                "interests" => $json_data['interests'],
                "comments" => $json_data['comments'],
                'date_created' => time()
            );

  
           
            if ($this->db->insert('enquiry', $data)) {
                return $this->output
                    ->set_content_type('application/json')
                    ->set_status_header(200)
                    ->set_output(json_encode(array(
                        'success' => true,
                        'message' => 'ENQUIRY_ADDED_SUCCESSFULLY'
                    )));
            } else {
                return $this->output
                    ->set_content_type('application/json')
                    ->set_status_header(200)
                    ->set_output(json_encode(array(
                        'success' => false,
                        'message' => 'enquiry not added successfully'
                    )));
            }
        
    }
  

    public function edit_enquiry($id)
    {

        $json_data = json_decode(@file_get_contents('php://input'), true);


            $data = array(
                "name" => $json_data['name'],
                "email" => $json_data['email'],
                "nationality" => $json_data['nationality'],
                "travel_period" => $json_data['travel_period'],
                "number_of_days" => $json_data['number_of_days'],
                "country" => $json_data['country'],
                "number_of_persons" => $json_data['number_of_persons'],
                "acommodation" => $json_data['accomodation'],
                "budget_indication" => $json_data['budget_indication'],
                "interests" => $json_data['interests'],
                "comments" => $json_data['comments'],
                'date_created' => time()
            );
  
           $this->db->where('id',$id);
            if ($this->db->update('enquiry', $data)) {
                return $this->output
                    ->set_content_type('application/json')
                    ->set_status_header(200)
                    ->set_output(json_encode(array(
                        'success' => true,
                        'message' => 'enquiry edited successfully'
                    )));
            } else {
                return $this->output
                    ->set_content_type('application/json')
                    ->set_status_header(200)
                    ->set_output(json_encode(array(
                        'success' => false,
                        'message' => 'enquiry not edited successfully'
                    )));
            }
        
    }

    public function get_enquiries()
    {
        $this->db->select('*');
        $this->db->from('enquiry');
        $query = $this->db->get();

        return $this->output
            ->set_content_type('application/json')
            ->set_status_header(200)
            ->set_output(json_encode(array(
                'success' => true,
                'messeage'=>'FETCHED_SUCCESSFULLY',
                'enquiries' => $query->result_array()
            )));
    }

    public function get_enquiry($id)
    {
        $this->db->where('id',$id);
        $this->db->from('enquiry');
        $query = $this->db->get();

        return $this->output
            ->set_content_type('application/json')
            ->set_status_header(200)
            ->set_output(json_encode(array(
                'success' => true,
                'enquiry' => $query->row()
            )));
    }
   

    // function to upload an edit enquiry photo 

    public function delete_enquiry($id)
    {
        if ($this->input->method() != 'delete') {
            return $this->output
                ->set_content_type('application/json')
                ->set_status_header(200)
                ->set_output(json_encode(array(
                    'success' => false,
                    'message' => 'wrong request method used'
                )));
        }

        $enquiry = $this->db->get_where('enquiry', array('id' => $id))->row();
        if ($enquiry == null) {
            return $this->output
                ->set_content_type('application/json')
                ->set_status_header(200)
                ->set_output(json_encode(array(
                    'success' => false,
                    'message' => 'enquiry not found'
                )));
        }
        // else delete
        $this->db->where('id', $id);
        if ($this->db->delete('enquiry')) {

            return $this->output
                ->set_content_type('application/json')
                ->set_status_header(200)
                ->set_output(json_encode(array(
                    'success' => true,
                    'message' => 'ENQUIRY_DELETED_SUCCESSFULLY'
                )));
        } else {
            return $this->output
                ->set_content_type('application/json')
                ->set_status_header(200)
                ->set_output(json_encode(array(
                    'success' => true,
                    'message' => 'ENQUIRY_NOT_DELETED'
                )));
        }
    }

}
