<?php
defined('BASEPATH') or exit('No direct script access allowed');
class Safari_tours 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');
    }
// destinations
// sub destinations
// trips


// public function add_destination(){
//     $check_name =   $this->db->get_where('destinations', array("title" => $_POST['title']))->row();

//     if (($check_name != null)) {
      
//         return $this->output
//             ->set_content_type('application/json')
//             ->set_status_header(200)
//             ->set_output(json_encode(array(
//                 'success' => false,
//                 'message' => 'DESTINATION_EXISTS'
//             )));
//     } else {

//         $image_url =  $this->upload_image($_FILES["image"]['name'], "image","destinations");

//         $data = array(
//             "title" => $_POST['title'],
//             'image' => $image_url,
//             'description' => $_POST['description'],
//             'date_created' => date('Y-m-d\TH:i:s.u\Z')
//         );

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

public function add_reciept(){
    $data['name'] = 'John Doe';
    $data['amount'] = '$100';
    $data['description'] = '$100';
    $data['balance'] = '$100';
    $data['item'] = 'Safari Tour To Kampala, Uganda';
    $data['date'] = '23th/12/23';
    

    $this->load->view('receipt', $data);
}


public function add_destination_country(){
     $check_name =   $this->db->get_where('destination_country', array("country_title" => $_POST['country_title']))->row();

    if (($check_name != null)) {
      
        return $this->output
            ->set_content_type('application/json')
            ->set_status_header(200)
            ->set_output(json_encode(array(
                'success' => false,
                'message' => 'DESTINATION_COUNTRY_EXISTS'
            )));
    }
    
    //functions to add an image
    
       $africa_country_location_url =  $this->upload_image($_FILES["africa_country_location"]['name'], "africa_country_location","destination_country");
       
       
       
        $featured_photo_url =  $this->upload_image($_FILES["featured_photo"]['name'], "featured_photo","destination_country");
       
        $country_map_url =  $this->upload_image($_FILES["country_map"]['name'], "country_map","destination_country");
       
    
    $data = array(
        "country_title"=>$_POST['country_title'],
        "overview"=>$_POST['overview'],
        "africa_country_location"=>$africa_country_location_url,
        "featured_video_url"=>$_POST['featured_video_url'],
        "featured_photo"=>$featured_photo_url,
        "country_map"=>$country_map_url,
        "rates_usd"=>$_POST['rates_usd'],
        "best_time_to_go"=>$_POST['best_time_to_go'],
        "high_season"=>$_POST['high_season'],
        "size"=>$_POST['size'],
        "pros_and_cons"=>$_POST['pros_and_cons'],
        "wild_life"=>$_POST['wild_life'],
        "weather_and_climate"=>$_POST['weather_and_climate'],
        "best_time_to_visit"=>$_POST['best_time_to_visit'],
        "premier_parks_and_reserves"=>$_POST['premier_parks_and_reserves'],
        // "want_to_visit_uganda"=>$_POST['want_to_visit_uganda'],
        "date_created"=>date('Y-m-d\TH:i:s.u\Z')
        );
        
        if($this->db->insert('destination_country',$data)){
             return $this->output
            ->set_content_type('application/json')
            ->set_status_header(200)
            ->set_output(json_encode(array(
                'success' => true,
                'message' => 'COUNTRY_ADDED_SUCCESSFULLY'
            )));
        };
        
        
    
    
    
}

public function get_country_destinations($country_id){
    
    $this->db->select("id,title,description,image,slide_image_1,slide_image_2,slide_image_3");
    $this->db->where("country_id",$country_id);
     $destinations = $this->db->get('destinations')->result_array();
     
     $prepared_destinations =[];
     
      foreach($destinations as $destination){
          
        $packages = $this->db->get_where('destination_packages',array('destination_id'=>$destination['id']))->result_array();
        
        array_push($prepared_destinations,array("id"=>$destination['id'],"title"=>$destination['title'],"description"=>$destination['description'],"image"=>$destination['image'],"slide_image_1"=>$destination['slide_image_1'],"slide_image_2"=>$destination['slide_image_2'],"slide_image_3"=>$destination['slide_image_3'],'package_count'=>count($packages)));
    }
     
     return $this->output
        ->set_content_type('application/json')
        ->set_status_header(200)
        ->set_output(json_encode(array(
            'success' => true,
            'message'=>'FETCHED_SUCCESSFULLY',
            'destinations' => $prepared_destinations
        )));

}

public function get_destination_countries(){
    $this->db->select('id,country_title,featured_photo,overview');
    $this->db->from('destination_country');
    $countries = $this->db->get()->result_array();
    
    $prepared_countries = [];
    
  foreach($countries as $country){
        $destinations = $this->db->get_where('destinations',array('country_id'=>$country['id']))->result_array();
        
        array_push($prepared_countries,array("id"=>$country['id'],"country_title"=>$country['country_title'],"featured_photo"=>$country['featured_photo'],"overview"=>$country['overview'],'destination_count'=>count($destinations)));
    }


    return $this->output
        ->set_content_type('application/json')
        ->set_status_header(200)
        ->set_output(json_encode(array(
            'success' => true,
            'message'=>'FETCHED_SUCCESSFULLY',
            'countries' => $prepared_countries
        )));

}

public function get_destination_countries_admin(){
    $this->db->select('id,country_title,featured_photo, overview');
    $this->db->from('destination_country');
    $query = $this->db->get();

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

}

public function get_countries_admin(){
    
    $this->db->select("id,country_title");
    $countries = $this->db->get("destination_country")->result_array();
    
    
    return $this->output
            ->set_content_type('application/json')
            ->set_status_header(200)
            ->set_output(json_encode(array(
                'success' => true,
                'message' => "FETCHED_SUCCESSFULLY",
                'countries'=>$countries
            )));
    
    
}

public function edit_destination_country($country_id){
    $destination_country = $this->db->get_where('destination_country', array('id' => $country_id))->row();
        
        
    if ($destination_country == null) {
        return $this->output
            ->set_content_type('application/json')
            ->set_status_header(200)
            ->set_output(json_encode(array(
                'success' => false,
                'message' => "DESTINATION_COUNTRY_NOT_FOUND"
            )));
    }
    //functions to add an image
    
    
    $africa_country_location_url = $destination_country->africa_country_location;
           if(!empty($_FILES["africa_country_location"]['name'])){
       $africa_country_location_url =  $this->upload_image($_FILES["africa_country_location"]['name'], "africa_country_location","destination_country");
           }
       
       
       $featured_photo_url=$destination_country->featured_photo;
       
          if(!empty($_FILES["featured_photo"]['name'])){
        $featured_photo_url =  $this->upload_image($_FILES["featured_photo"]['name'], "featured_photo","destination_country");
          }
       
       
       $country_map_url = $destination_country->country_map;
          if(!empty($_FILES["country_map"]['name'])){
        $country_map_url =  $this->upload_image($_FILES["country_map"]['name'], "country_map","destination_country");
          }
       
    
    $data = array(
        "country_title"=>$_POST['country_title'],
        "overview"=>$_POST['overview'],
        "africa_country_location"=>$africa_country_location_url,
        "featured_video_url"=>$_POST['featured_video_url'],
        "featured_photo"=>$featured_photo_url,
        "country_map"=>$country_map_url,
        "rates_usd"=>$_POST['rates_usd'],
        "best_time_to_go"=>$_POST['best_time_to_go'],
        "high_season"=>$_POST['high_season'],
        "size"=>$_POST['size'],
        "pros_and_cons"=>$_POST['pros_and_cons'],
        "wild_life"=>$_POST['wild_life'],
        "weather_and_climate"=>$_POST['weather_and_climate'],
        "best_time_to_visit"=>$_POST['best_time_to_visit'],
        "premier_parks_and_reserves"=>$_POST['premier_parks_and_reserves'],
        // "want_to_visit_uganda"=>$_POST['want_to_visit_uganda']
        
        );
        
        $this->db->where("id",$country_id);
        if($this->db->update('destination_country',$data)){
             return $this->output
            ->set_content_type('application/json')
            ->set_status_header(200)
            ->set_output(json_encode(array(
                'success' => true,
                'message' => 'DESTINATION_COUNTRY_EDITED_SUCCESSFULLY'
            )));
        };
        
        
    
    
    
}

public function delete_destination_country($country_id){
      $destination_country = $this->db->get_where('destination_country', array('id' => $country_id))->row();
        
        
    if ($destination_country == null) {
        return $this->output
            ->set_content_type('application/json')
            ->set_status_header(200)
            ->set_output(json_encode(array(
                'success' => false,
                'message' => "DESTINATION_COUNTRY_NOT_FOUND"
            )));
    }
    
    $this->db->where("id",$country_id);
    if($this->db->delete('destination_country')){
           return $this->output
            ->set_content_type('application/json')
            ->set_status_header(200)
            ->set_output(json_encode(array(
                'success' => true,
                'message' => "DESTINATION_COUNTRY_DELETED"
            )));
    }
    
    
}

public function get_destination_country($country_id){
        $destination_country = $this->db->get_where('destination_country', array('id' => $country_id))->row();
        
        
    if ($destination_country == null) {
        return $this->output
            ->set_content_type('application/json')
            ->set_status_header(200)
            ->set_output(json_encode(array(
                'success' => false,
                'message' => "DESTINATION_COUNTRY_NOT_FOUND"
            )));
    }
    
    // get all other destinations inside
     // get all the sub destinations too

    $this->db->select('id,title,description,image,slide_image_1,slide_image_2,slide_image_3');
    $this->db->where("country_id",$destination_country->id);
    $this->db->from('destinations');
    $sub_destinations = $this->db->get()->result_array();
    
    return $this->output
    ->set_content_type('application/json')
    ->set_status_header(200)
    ->set_output(json_encode(array(
        'success' => true,
        'destination'=>$destination_country,
        'sub_destinations'=>$sub_destinations

    )));

    
    
}

public function get_destination_packages($id){
    
     $this->db->select("id,title,image");
    $destination = $this->db->get("destinations")->row();
     
     
   
     
     if($destination == null){
            return $this->output
            ->set_content_type('application/json')
            ->set_status_header(200)
            ->set_output(json_encode(array(
                'success' => false,
                'message' => "DESTINATION_NOT_FOUND"
            )));
     }
     
     
    // get packages in the destination
    
           $this->db->where('destination_packages.destination_id', $id);
          $this->db->select('safari_tour_packages.id,safari_tour_packages.title,safari_tour_packages.image,safari_tour_packages.brief_description,safari_tour_packages.destination_country');

       
         $this->db->from('destination_packages');
          $this->db->join('safari_tour_packages', 'safari_tour_packages.id = destination_packages.package_id');
        $packages = $this->db->get()->result_array();
        
        
          return $this->output
            ->set_content_type('application/json')
            ->set_status_header(200)
            ->set_output(json_encode(array(
                'success' => true,
                'message' => "FETCHED_SUCCESSFULLY",
                'packages'=>$packages,
                
            )));
    
    
    
    
    
    
}

public function add_destination_to_country(){
    
     $check_name =   $this->db->get_where('destinations', array("title" => $_POST['title']))->row();

    if (($check_name != null)) {
      
        return $this->output
            ->set_content_type('application/json')
            ->set_status_header(200)
            ->set_output(json_encode(array(
                'success' => false,
                'message' => 'DESTINATION_EXISTS'
            )));
    } 
    
    
     $check_country =   $this->db->get_where('destination_country', array("id" => $_POST['country_id']))->row();

    if (($check_country == null)) {
      
        return $this->output
            ->set_content_type('application/json')
            ->set_status_header(200)
            ->set_output(json_encode(array(
                'success' => false,
                'message' => 'COUNTRY_NOT_FOUND'
            )));
    } 
    
    

        $image_url =  $this->upload_image($_FILES["image"]['name'], "image","destinations");
        
        //check if the slide images are sent or not
        // $slide_image_1 ='';
        // $slide_image_2 ='';
        // $slide_image_3 ='';
        // // $slide_image_4 ='';
        
        // if(!empty($_FILES["slide_image_1"]['name'])){
        //      $slide_image_1 =  $this->upload_image($_FILES["slide_image_1"]['name'], "image","destinations");
        // }
        
        // if(!empty($_FILES["slide_image_2"]['name'])){
        //      $slide_image_2 =  $this->upload_image($_FILES["slide_image_2"]['name'], "image","destinations");
        // }
        
        //  if(!empty($_FILES["slide_image_3"]['name'])){
        //      $slide_image_3 =  $this->upload_image($_FILES["slide_image_3"]['name'], "image","destinations");
        // }
        
        //  if(!empty($_FILES["slide_image_4"]['name'])){
        //      $slide_image_4 =  $this->upload_image($_FILES["slide_image_4"]['name'], "image","destinations");
        // }
        
        

        $data = array(
            "title" => $_POST['title'],
            'image' => $image_url,
            'country_id' => $_POST['country_id'],
         
                
            'description' => $_POST['description'],
            'date_created' => date('Y-m-d\TH:i:s.u\Z')
        );

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

public function edit_destination_to_country($id){
    
     $check_destination =   $this->db->get_where('destinations', array("id" => $id))->row();

    if (($check_destination == null)) {
      
        return $this->output
            ->set_content_type('application/json')
            ->set_status_header(200)
            ->set_output(json_encode(array(
                'success' => false,
                'message' => 'DESTINATION_NOT_FOUND'
            )));
    } else {
        
        $image_url ='';
        if(!empty($_FILES["image"]['name'])){
                $image_url =  $this->upload_image($_FILES["image"]['name'], "image","destinations");
        }else{
            $image_url = $check_destination->image;
        }

       
        
        //check if the slide images are sent or not
        // $slide_image_1 ='';
        // $slide_image_2 ='';
        // $slide_image_3 ='';
        // $slide_image_4 ='';
        
        // if(!empty($_FILES["slide_image_1"]['name'])){
        //      $slide_image_1 =  $this->upload_image($_FILES["slide_image_1"]['name'], "slide_image_1","destinations");
        // }
        
        // if(!empty($_FILES["slide_image_2"]['name'])){
        //      $slide_image_2 =  $this->upload_image($_FILES["slide_image_2"]['name'], "slide_image_2","destinations");
        // }
        
        //  if(!empty($_FILES["slide_image_3"]['name'])){
        //      $slide_image_3 =  $this->upload_image($_FILES["slide_image_3"]['name'], "slide_image_3","destinations");
        // }
        
        //  if(!empty($_FILES["slide_image_4"]['name'])){
        //      $slide_image_4 =  $this->upload_image($_FILES["slide_image_4"]['name'], "slide_image_4","destinations");
        // }
        
        

        $data = array(
            "title" => $_POST['title'],
            'image' => $image_url,
             
            'description' => $_POST['description']
           
        );
        
        $this->db->where("id",$id);
        
        if ($this->db->update('destinations', $data)) {
            return $this->output
                ->set_content_type('application/json')
                ->set_status_header(200)
                ->set_output(json_encode(array(
                    'success' => true,
                    'message' => 'DESTINATION_EDITED_SUCCESSFULLY'
                )));
        } else {
            return $this->output
                ->set_content_type('application/json')
                ->set_status_header(200)
                ->set_output(json_encode(array(
                    'success' => false,
                    'message' => 'DESTINATION_NOT_EDITED'
                )));
        }
    }
    
}

public function get_destination($id){
    $destination =   $this->db->get_where('destinations', array("id" => $id))->row();
    
     // get packages in the destination
    
           $this->db->where('destination_packages.destination_id', $id);
          $this->db->select('safari_tour_packages.id,safari_tour_packages.title,safari_tour_packages.image,safari_tour_packages.brief_description,safari_tour_packages.destination_country');

       
         $this->db->from('destination_packages');
          $this->db->join('safari_tour_packages', 'safari_tour_packages.id = destination_packages.package_id');
        $packages = $this->db->get()->result_array();

    if (($destination == null)) {
      
        return $this->output
            ->set_content_type('application/json')
            ->set_status_header(200)
            ->set_output(json_encode(array(
                'success' => false,
                'message' => 'DESTINATION_NOT_FOUND'
            )));
    }
    
    
     return $this->output
                ->set_content_type('application/json')
                ->set_status_header(200)
                ->set_output(json_encode(array(
                    'success' => true,
                    'message' => 'FETCHED_SUCCESSFULLY',
                    'destination'=>$destination,
                    'packages'=>$packages
                )));
    
}

public function delete_destination($id){
    
    $destination =   $this->db->get_where('destinations', array("id" => $id))->row();

    if (($destination == null)) {
      
        return $this->output
            ->set_content_type('application/json')
            ->set_status_header(200)
            ->set_output(json_encode(array(
                'success' => false,
                'message' => 'DESTINATION_NOT_FOUND'
            )));
    }
    
    $this->db->where('id',$id);
    if($this->db->delete('destinations')){
       
        return $this->output
            ->set_content_type('application/json')
            ->set_status_header(200)
            ->set_output(json_encode(array(
                'success' => true,
                'message' => 'DESTINATION_DELETED'
            ))); 
    }
    
    
}

public function get_tours_by_country(){
    $country_id = $this->input->get("id");
    
    $destination_country = $this->db->get_where('destination_country', array('id' => $country_id))->row();
        
        
    if ($destination_country == null) {
        return $this->output
            ->set_content_type('application/json')
            ->set_status_header(200)
            ->set_output(json_encode(array(
                'success' => false,
                'message' => "DESTINATION_COUNTRY_NOT_FOUND"
            )));
    }
    
    
    
    
    // now get tours by country
    
     $this->db->select('id,image,title,safari_type,amount,destination_country,brief_description,status');
        $this->db->where('status','true');
        $this->db->like('destination_country', $destination_country->country_title, 'both');
        
        $this->db->from('safari_tour_packages');
        $query = $this->db->get();

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

    
}

public function get_tours_by_country_back_office(){
    $country_id = $this->input->get("id");
    
    $destination_country = $this->db->get_where('destination_country', array('id' => $country_id))->row();
        
        
    if ($destination_country == null) {
        return $this->output
            ->set_content_type('application/json')
            ->set_status_header(200)
            ->set_output(json_encode(array(
                'success' => false,
                'message' => "DESTINATION_COUNTRY_NOT_FOUND"
            )));
    }
    
    
    
    
    // now get tours by country
    
     $this->db->select('id,title');
        $this->db->where('status','true');
        $this->db->like('destination_country', $destination_country->country_title, 'both');
        
        $this->db->from('safari_tour_packages');
        $query = $this->db->get();

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

    
}

public function get_tours_by_where_to(){
    
    $country_title = $this->input->get("v");
    
 
    $destination_country = $this->db->get_where('destination_country', array('country_title' => trim($country_title)))->row();
    
    
    if ($destination_country == null) {
        return $this->output
            ->set_content_type('application/json')
            ->set_status_header(200)
            ->set_output(json_encode(array(
                'success' => false,
                'message' => "DESTINATION_COUNTRY_NOT_FOUND"
            )));
    }
    
    
    
    
    // now get tours by country
    
     $this->db->select('id,image,title,safari_type,amount,destination_country,brief_description,status');
        $this->db->where('status','true');
        $this->db->like('destination_country', $destination_country->country_title, 'both');
        
        $this->db->from('safari_tour_packages');
        $safari_tours = $this->db->get()->result_array();
        
            $prepared_tours =[];
        
        foreach($safari_tours as $safari_tour){
        //   $safari_type ='';
        //   if(is_int($safari_tour['safari_type'])){
            
        //     }
            
            $safari_type = $this->db->get_where("safari_types",array("id"=>$safari_tour['safari_type']))->row()->title;
            
            $each_safari = array(
                'id'=>$safari_tour['id'],
                'image'=>$safari_tour['image'],
                'title'=>$safari_tour['title'],
                'safari_type'=>$safari_type,
                'amount'=>$safari_tour['amount'],
                'brief_description'=>$safari_tour['brief_description'],
                'destination_country'=>$safari_tour['destination_country'],
                'status'=>($safari_tour['status']=='true'?true:false),
                );
                
                array_push($prepared_tours,$each_safari);
        }
        
        $this->db->select('id,title,image,description');
        $this->db->where('country_id',$destination_country->id);
        $destinations = $this->db->get('destinations')->result_array();

         return $this->output
            ->set_content_type('application/json')
            ->set_status_header(200)
            ->set_output(json_encode(array(
                'success' => true,
                'message'=>"FETCHED_SUCCESSFULLY",
                'country_overview'=>$destination_country->overview,
                'featured_image'=>$destination_country->featured_photo,
                'safari_tours' => $prepared_tours,
                'destinations'=>$destinations
            )));
    
    
    
}


public function add_safari_type(){
     $json_data = json_decode(@file_get_contents('php://input'), true);
     
     $check_safari_type = $this->db->get_where('safari_types',array("title"=>$json_data['title']))->row();
     
     if($check_safari_type != null){
            return $this->output
            ->set_content_type('application/json')
            ->set_status_header(200)
            ->set_output(json_encode(array(
                'success' => false,
                'message' => "SAFARI_TYPE_EXISTS"
            )));
     }
     
     if($this->db->insert('safari_types',array('title'=>$json_data['title'],'description'=>$json_data['description'],'date_created'=> date('Y-m-d\TH:i:s.u\Z')))){
            return $this->output
            ->set_content_type('application/json')
            ->set_status_header(200)
            ->set_output(json_encode(array(
                'success' => true,
                'message' => "ADDED_SUCCESSFULLY"
            )));
     }
}

public function get_safari_types(){
// Get the number of packages for each safari type
$this->db->select('safari_types.id, safari_types.title, safari_types.description, COUNT(safari_tour_packages.id) as package_count');
$this->db->from('safari_types');
$this->db->join('safari_tour_packages', 'safari_types.id = safari_tour_packages.safari_type', 'left');
$this->db->group_by('safari_types.id');
$safari_package_counts = $this->db->get()->result_array();

// Organize the results by safari type
$safari_types = array();
foreach ($safari_package_counts as $count) {
$safari_type_id = $count['id'];
$safari_types[] = array(
'id' => $safari_type_id,
'title' => $count['title'],
'description' => $count['description'],
'package_count' => (int) $count['package_count']
);

}

return $this->output
    ->set_content_type('application/json')
    ->set_status_header(200)
    ->set_output(json_encode(array(
        'success' => true,
        'message' => 'FETCHED_SUCCESSFULLY',
        'safari_types' => $safari_types
    )));

}

public function edit_safari_type($id){
     $json_data = json_decode(@file_get_contents('php://input'), true);
     
     $check_safari_type = $this->db->get_where('safari_types',array("id"=>$id))->row();
     
     if($check_safari_type == null){
            return $this->output
            ->set_content_type('application/json')
            ->set_status_header(200)
            ->set_output(json_encode(array(
                'success' => false,
                'message' => "SAFARI_TYPE_NOT_FOUND"
            )));
     }
     $this->db->where("id",$id);
     $data = array('title'=>$json_data['title'],'description'=>$json_data['description']);
     
     if($this->db->update('safari_types', $data)){
            return $this->output
            ->set_content_type('application/json')
            ->set_status_header(200)
            ->set_output(json_encode(array(
                'success' => true,
                'message' => "EDITED_SUCCESSFULLY"
            )));
     }
}


public function delete_safari_type($id){
     $json_data = json_decode(@file_get_contents('php://input'), true);
     
     $check_safari_type = $this->db->get_where('safari_types',array("id"=>$id))->row();
     
     if($check_safari_type == null){
            return $this->output
            ->set_content_type('application/json')
            ->set_status_header(200)
            ->set_output(json_encode(array(
                'success' => false,
                'message' => "SAFARI_TYPE_NOT_FOUND"
            )));
     }
     $this->db->where("id",$id);
     if($this->db->delete('safari_types')){
         
            return $this->output
            ->set_content_type('application/json')
            ->set_status_header(200)
            ->set_output(json_encode(array(
                'success' => true,
                'message' => "DELETED_SUCCESSFULLY"
            )));
     }
}

public function edit_experience($id){

    $destination = $this->db->get_where('experiences', array('id' => $id))->row();
    if ($destination == null) {
        return $this->output
            ->set_content_type('application/json')
            ->set_status_header(200)
            ->set_output(json_encode(array(
                'success' => false,
                'message' => "EXPERIENCE_NOT_FOUND"
            )));
    }


        $data = array(
            'title' => $_POST['title'],
            'description' => $_POST['description']
           
        
        );


    // update the safari_tour image link of those files and field names passed accordingly
    if (!empty($_FILES['image']['name'])) {
        // assign the returned image url(s) after deleting the old image and add the value to the data array

        $data['image'] = $this->upload_edit_experience($_FILES['image']['name'], $id, 'image');
    }

   

    // now update the data

    $this->db->set($data);
    $this->db->where('id', $id);
    if ($this->db->update('experiences', $data)) {

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

public function add_experience_package(){
    
    $json_data = json_decode(@file_get_contents('php://input'), true);
    $experience_id = $json_data['experience_id'];
    
    // first check if the experience exists
    $check_experience = $this->db->get_where('experiences',array('id'=>$experience_id))->row();
    
    if($check_experience == null){
         return $this->output
            ->set_content_type('application/json')
            ->set_status_header(200)
            ->set_output(json_encode(array(
                'success' => false,
                'message' => "EXPERIENCE_NOT_FOUND"
            )));
    }
    
    
    $packages = $json_data['packages'];
    
    foreach($packages as $package){
        
        $check_package = $this->db->get_where('experience_packages', array("package_id"=>$package,"experience_id"=>$experience_id))->row();
        
        if($check_package == null){
            //if its not existing add
            $this->db->insert('experience_packages',array("package_id"=>$package,"experience_id"=>$experience_id));
        }
    }
    
    
      return $this->output
            ->set_content_type('application/json')
            ->set_status_header(200)
            ->set_output(json_encode(array(
                'success' => true,
                'message' => "ADDED_SUCCESSFULY"
            )));
}


public function add_blog_package(){
    
    $json_data = json_decode(@file_get_contents('php://input'), true);
    $blog_id = $json_data['blog_id'];
    
    // first check if the blog exists
    $check_blog = $this->db->get_where('blogs',array('id'=>$blog_id))->row();
    
    if($check_blog == null){
         return $this->output
            ->set_content_type('application/json')
            ->set_status_header(200)
            ->set_output(json_encode(array(
                'success' => false,
                'message' => "BLOG_NOT_FOUND"
            )));
    }
    
    
    $packages = $json_data['packages'];
    
    foreach($packages as $package){
        
        $check_package = $this->db->get_where('blog_packages', array("package_id"=>$package,"blog_id"=>$blog_id))->row();
        
        if($check_package == null){
            //if its not existing add
            $this->db->insert('blog_packages',array("package_id"=>$package,"blog_id"=>$blog_id));
        }
    }
    
    
      return $this->output
            ->set_content_type('application/json')
            ->set_status_header(200)
            ->set_output(json_encode(array(
                'success' => true,
                'message' => "ADDED_SUCCESSFULY"
            )));
}

public function delete_blog_package(){
    
    $json_data = json_decode(@file_get_contents('php://input'), true);
    $blog_id = $json_data['blog_id'];
    
    // first check if the blog exists
    $check_blog = $this->db->get_where('blogs',array('id'=>$blog_id))->row();
    
    if($check_blog == null){
         return $this->output
            ->set_content_type('application/json')
            ->set_status_header(200)
            ->set_output(json_encode(array(
                'success' => false,
                'message' => "BLOG_NOT_FOUND"
            )));
    }
    
    
    $packages = $json_data['packages'];
    
    foreach($packages as $package){
        
        $check_package = $this->db->get_where('blog_packages', array("package_id"=>$package,"blog_id"=>$blog_id))->row();
        
        if($check_package != null){
            //if its not existing add
            $this->db->where("package_id",$package);
            $this->db->where("blog_id",$blog_id);
            $this->db->delete('blog_packages');
        }
    }
    
    
      return $this->output
            ->set_content_type('application/json')
            ->set_status_header(200)
            ->set_output(json_encode(array(
                'success' => true,
                'message' => "DELETED_SUCCESSFULY"
            )));
}
public function add_experience(){



if (empty($_FILES["image"]['name'])) {
      
        return $this->output
            ->set_content_type('application/json')
            ->set_status_header(200)
            ->set_output(json_encode(array(
                'success' => false,
                'message' => 'PLEASE_ADD_IMAGE'
            )));
    }
    
        $check_name =   $this->db->get_where('experiences', array("title" => $_POST['title']))->row();
    
    if (($check_name != null)) {
      
        return $this->output
            ->set_content_type('application/json')
            ->set_status_header(200)
            ->set_output(json_encode(array(
                'success' => false,
                'message' => 'EXPERIENCE_EXISTS'
            )));
    } else {

        $image_url =  $this->upload_image($_FILES["image"]['name'], "image","experiences");

        $data = array(
            "title" => $_POST['title'],
            'image' => $image_url,
            'description' => $_POST['description'],
            'date_created' => date('Y-m-d\TH:i:s.u\Z')
        );

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

public function add_destination_package(){
    
    $json_data = json_decode(@file_get_contents('php://input'), true);
    $destination_id = $json_data['destination_id'];
    
    // first check if the destination exists
    $check_destination = $this->db->get_where('destinations',array('id'=>$destination_id))->row();
    
    if($check_destination == null){
         return $this->output
            ->set_content_type('application/json')
            ->set_status_header(200)
            ->set_output(json_encode(array(
                'success' => false,
                'message' => "DESTINATION_NOT_FOUND"
            )));
    }
    
    
    $packages = $json_data['packages'];
    
    foreach($packages as $package){
        
        $check_package = $this->db->get_where('destination_packages', array("package_id"=>$package,"destination_id"=>$destination_id))->row();
        
        if($check_package == null){
            //if its not existing add
            $this->db->insert('destination_packages',array("package_id"=>$package,"destination_id"=>$destination_id));
        }
    }
    
    
      return $this->output
            ->set_content_type('application/json')
            ->set_status_header(200)
            ->set_output(json_encode(array(
                'success' => true,
                'message' => "ADDED_SUCCESSFULY"
            )));
}


public function delete_destination_package(){
    
    $json_data = json_decode(@file_get_contents('php://input'), true);
    $destination_id = $json_data['destination_id'];
    
    // first check if the experience exists
    $check_destination = $this->db->get_where('destinations',array('id'=>$destination_id))->row();
    
    if($check_destination == null){
         return $this->output
            ->set_content_type('application/json')
            ->set_status_header(200)
            ->set_output(json_encode(array(
                'success' => false,
                'message' => "DESTINATION_NOT_FOUND"
            )));
    }
    
    
    $packages = $json_data['packages'];
    
    foreach($packages as $package){
        
        $check_package = $this->db->get_where('destination_packages', array("package_id"=>$package,"destination_id"=>$destination_id))->row();
        
        if($check_package != null){
            //if its not existing add
            $this->db->where("package_id",$package);
            $this->db->where("destination_id",$destination_id);
            $this->db->delete('destination_packages');
        }
    }
    
    
      return $this->output
            ->set_content_type('application/json')
            ->set_status_header(200)
            ->set_output(json_encode(array(
                'success' => true,
                'message' => "DELETED_SUCCESSFULY"
            )));
}
public function add_safari_type_package(){
    
    $json_data = json_decode(@file_get_contents('php://input'), true);
    $safari_type_id = $json_data['safari_type_id'];
    
    // first check if the safari_type exists
    $check_safari_type = $this->db->get_where('safari_types',array('id'=>$safari_type_id))->row();
    
    if($check_safari_type == null){
         return $this->output
            ->set_content_type('application/json')
            ->set_status_header(200)
            ->set_output(json_encode(array(
                'success' => false,
                'message' => "SAFARI_TYPE_NOT_FOUND"
            )));
    }
    
    
    $packages = $json_data['packages'];
    
    foreach($packages as $package){
        
        $check_package = $this->db->get_where('safari_types_packages', array("package_id"=>$package,"safari_type_id"=>$safari_type_id))->row();
        
        if($check_package == null){
            //if its not existing add
            $this->db->insert('safari_types_packages',array("package_id"=>$package,"safari_type_id"=>$safari_type_id, 'date_created'=>date('Y-m-d\TH:i:s.u\Z')));
        }
    }
    
    
      return $this->output
            ->set_content_type('application/json')
            ->set_status_header(200)
            ->set_output(json_encode(array(
                'success' => true,
                'message' => "ADDED_SUCCESSFULY"
            )));
}

public function delete_safari_type_package(){
    
    $json_data = json_decode(@file_get_contents('php://input'), true);
     $safari_type_id = $json_data['safari_type_id'];

    
 // first check if the safari_type exists
    $check_safari_type = $this->db->get_where('safari_types',array('id'=>$safari_type_id))->row();
    
    if($check_safari_type == null){
         return $this->output
            ->set_content_type('application/json')
            ->set_status_header(200)
            ->set_output(json_encode(array(
                'success' => false,
                'message' => "SAFARI_TYPE_NOT_FOUND"
            )));
    }
    
    $packages = $json_data['packages'];
    
    foreach($packages as $package){
        
         $check_package = $this->db->get_where('safari_types_packages', array("package_id"=>$package,"safari_type_id"=>$safari_type_id))->row();
        
        if($check_package != null){
            //if its not existing add
            $this->db->where("package_id",$package);
            $this->db->where("safari_type_id",$safari_type_id);
            $this->db->delete('safari_types_packages');
        }
    }
    
    
      return $this->output
            ->set_content_type('application/json')
            ->set_status_header(200)
            ->set_output(json_encode(array(
                'success' => true,
                'message' => "DELETED_SUCCESSFULY"
            )));
}

public function delete_experience_package(){
    
    $json_data = json_decode(@file_get_contents('php://input'), true);
    $experience_id = $json_data['experience_id'];
    
    // first check if the experience exists
    $check_experience = $this->db->get_where('experiences',array('id'=>$experience_id))->row();
    
    if($check_experience == null){
         return $this->output
            ->set_content_type('application/json')
            ->set_status_header(200)
            ->set_output(json_encode(array(
                'success' => false,
                'message' => "EXPERIENCE_NOT_FOUND"
            )));
    }
    
    
    $packages = $json_data['packages'];
    
    foreach($packages as $package){
        
        $check_package = $this->db->get_where('experience_packages', array("package_id"=>$package,"experience_id"=>$experience_id))->row();
        
        if($check_package != null){
            //if its not existing add
            $this->db->where("package_id",$package);
            $this->db->where("experience_id",$experience_id);
            $this->db->delete('experience_packages');
        }
    }
    
    
      return $this->output
            ->set_content_type('application/json')
            ->set_status_header(200)
            ->set_output(json_encode(array(
                'success' => true,
                'message' => "DELETED_SUCCESSFULY"
            )));
}
public function delete_experience($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'
            )));
    }

    $destination = $this->db->get_where('experiences', array('id' => $id))->row();

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

        // delete / unlink the old destination photos
        $image_location = 'experiences/' . explode('experiences/', $destination->image)[1];
        

        if (file_exists($image_location)) {
            // if it exists delete it
            $this->unlink_files($image_location);
            } 

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



public function get_experience($id){
    
    $this->db->select('id,title,image,description');
    $this->db->where("id",$id);
   $experience = $this->db->get('experiences')->row();
    
    // $experience = $this->db->get_where('experiences', array('id' => $id))->row();

    if ($experience == null) {
        return $this->output
            ->set_content_type('application/json')
            ->set_status_header(200)
            ->set_output(json_encode(array(
                'success' => false,
                'message' => "EXPERIENCE_NOT_FOUND"
            )));
    }

    // get all the packages in the experience
    
    // get
    
    
          $this->db->where('experience_packages.experience_id', $id);
          $this->db->select('safari_tour_packages.id,safari_tour_packages.title,safari_tour_packages.image,safari_tour_packages.brief_description,safari_tour_packages.destination_country');

       
         $this->db->from('experience_packages');
          $this->db->join('safari_tour_packages', 'safari_tour_packages.id = experience_packages.package_id');
        $packages = $this->db->get()->result_array();
    

   
    return $this->output
    ->set_content_type('application/json')
    ->set_status_header(200)
    ->set_output(json_encode(array(
        'success' => true,
        'experience'=>$experience,
        'packages'=>$packages
       
    )));


}
public function get_experiences(){
    $this->db->select('id,title,description,image');
    $this->db->from('experiences');
    $experiences = $this->db->get()->result_array();
    
    $prepared_experiences = [];
    
    foreach($experiences as $experience){
        $packages = $this->db->get_where('experience_packages',array('experience_id'=>$experience['id']))->result_array();
        
        array_push($prepared_experiences,array("id"=>$experience['id'],"title"=>$experience['title'],"description"=>$experience['description'],"image"=>$experience['image'],'package_count'=>count($packages)));
    }

    return $this->output
        ->set_content_type('application/json')
        ->set_status_header(200)
        ->set_output(json_encode(array(
            'success' => true,
            'experiences' => $prepared_experiences
        )));

}

public function get_destinations(){
    
    $this->db->select('id,title,description,image');
    $this->db->from('destinations');
    $destinations = $this->db->get()->result_array();
    
    $prepared_destinations = [];
    
    foreach($destinations as $destination){
        $packages = $this->db->get_where('destination_packages',array('destination_id'=>$destination['id']))->result_array();
        
        array_push($prepared_destinations,array("id"=>$destination['id'],"title"=>$destination['title'],"description"=>$destination['description'],"image"=>$destination['image'],'package_count'=>count($packages)));
    }

    return $this->output
        ->set_content_type('application/json')
        ->set_status_header(200)
        ->set_output(json_encode(array(
            'success' => true,
            'destinations' => $prepared_destinations
        )));

}

public function get_safari_type($id){
    $this->db->select('id,title,description');
   $safari_type = $this->db->get('safari_types')->row();
    
    // $safari_type = $this->db->get_where('safari_types', array('id' => $id))->row();

    if ($safari_type == null) {
        return $this->output
            ->set_content_type('application/json')
            ->set_status_header(200)
            ->set_output(json_encode(array(
                'success' => false,
                'message' => "safari_type_NOT_FOUND"
            )));
    }

    // get all the packages in the safari_type
    
    // get
    
    
          $this->db->where('safari_types_packages.safari_type_id', $id);
          $this->db->select('safari_tour_packages.id,safari_tour_packages.title,safari_tour_packages.image,safari_tour_packages.brief_description,safari_tour_packages.destination_country');

       
         $this->db->from('safari_types_packages');
          $this->db->join('safari_tour_packages', 'safari_tour_packages.id = safari_types_packages.package_id');
        $packages = $this->db->get()->result_array();
    

   
    return $this->output
    ->set_content_type('application/json')
    ->set_status_header(200)
    ->set_output(json_encode(array(
        'success' => true,
        'safari_type'=>$safari_type,
        'packages'=>$packages
       
    )));


}







public function get_tours_by_safari_type(){
        $safari_type = $this->input->get("safari_type");
        
        $safari_type_data =  $this->db->get_where('safari_types', ['id'=>$safari_type])->row();
    
    // now get tours by safari type
    
     $this->db->select('id,image,title,amount,destination_country,brief_description,status');
        $this->db->where('status','true');
        $this->db->where('safari_type',$safari_type);
        // $this->db->like('safari_type', $safari_type, 'both');
        
        $this->db->from('safari_tour_packages');
        $query = $this->db->get();

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


public function get_tours_by_wild_life(){
        $wild_life = $this->input->get("wild_life");
    // now get tours by safari type
    
     $this->db->select('id,image,title,safari_type,amount,destination_country,brief_description,status');
        $this->db->where('status','true');
        $this->db->like('title', $wild_life, 'both');
        
        $this->db->from('safari_tour_packages');
        $query = $this->db->get();

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

public function get_quote(){
        $json_data = json_decode(@file_get_contents('php://input'), true);
    
    $tour_id = $json_data['tour_id'];
    
    // get tour id
    
         $this->db->select('*');
        $this->db->where('id', $tour_id);
        $safari_tour = $this->db->get('safari_tour_packages')->row();
        
          

        if (($safari_tour == null)) {
          
            return $this->output
                ->set_content_type('application/json')
                ->set_status_header(200)
                ->set_output(json_encode(array(
                    'success' => false,
                    'message' => 'SAFARI_TOUR_DOESNT_EXIST'
                )));
        }
        
        $full_name = $json_data['full_name'];
        $email = $json_data['email'];
        $country = $json_data['country'];
        $phone = $json_data['phone'];
        $client_message = $json_data['message'];
        $start_date = $json_data['start_date'];
        $number_of_adults =$json_data['travellers']['number_of_adults'];
        $number_of_children=$json_data['travellers']['number_of_children'];
        
        
    
    $data = array(
        "full_name"=>$json_data['full_name'],
        "tour_id"=>$json_data['tour_id'],
        "email"=>$json_data['email'],
         "number_of_adults"=>$json_data['travellers']['number_of_adults'],
         "number_of_children"=>$json_data['travellers']['number_of_children'],
         "start_date"=>$json_data['start_date'],
         "tour_rate"=>$json_data['tour_rate'],
        "country"=>$json_data['country'],
        "phone"=>$json_data['phone'],
        "message"=>$json_data['message'],
        "date_created"=>date('Y-m-d\TH:i:s.u\Z')
    
        
        );
        
        // send the client an email
        
        //store quote details
        
        
        
        $message = "<!DOCTYPE html>
<html lang='en'>

<head>
    <meta http-equiv='Content-Type' content='text/html; charset=UTF-8' />
    <title>Safari Quote</title>
    <meta name='viewport' content='width=device-width, initial-scale=1.0' />
    <link rel='stylesheet' href='https://use.fontawesome.com/releases/v5.3.1/css/all.css' integrity='sha384-mzrmE5qonljUremFsqc01SB46JvROS7bZs3IO2EmfFsd15uHvIt+Y8vEf7N7fWAU' crossorigin='anonymous'>

    <style type='text/css'>
        a[x-apple-data-detectors] {
            color: inherit !important;
        }
        
        .previous_lecture_table {
            margin-top: 30px;
            font-size: 1em !important;
        }
    </style>

</head>

<body style='margin: 0; padding: 0;'>
    <table role='presentation' border='0' cellpadding='0' cellspacing='0' width='100%' style='background-color: #eeeeee;'>
        <tr>
            <td style='padding: 20px 0 30px 0;'>

                <table align='center' border='0' cellpadding='0' cellspacing='0' width='600' style='border-collapse: collapse; border: 1px solid #cccccc; border-top: 10px solid #083628; background-color: #fff;'>
                    <tr>
                        <td align='center' style='padding: 40px 0 30px 0;'>
                            <img src='https://d3v0px0pttie1i.cloudfront.net/uploads/user/logo/12740338/369b543a.png' alt='Creating Email Magic.' width='20%' style='display: block; border-radius:10px' />
                        </td>
                    </tr>
                    <tr>
                        <td bgcolor='#ffffff' style='padding: 20px 30px 40px 30px;'>
                            <table border='0' cellpadding='0' cellspacing='0' width='100%' style='border-collapse: collapse;'>
                                <tr>
                                    <td style='color: #002F6C; font-family: Arial, sans-serif;'>
                                        <h1 style='font-size: 20px; margin: 0;'>Hello $full_name,</h1>
                                        <p style='color: grey;'>
                                            
                                            Below is a copy of your quote request. 
                                            You should receive a response from us in a few. If not, please check your spam folder.

                                        </p>
                                    </td>
                                </tr>
                                <tr>
                                    <td style='color: grey; font-family: Arial, sans-serif;'>
                                        <div style='margin-top: 20px;'>
                                            <h3>Details</h3>
                                            <hr>
                                            <p style='color: grey;'>
                                                <span><strong>Name:	</strong> $full_name</span><br/><br/>
                                                <span><strong>Email:	</strong> $email</span> <br/><br/>
                                                <span><strong>Country:</strong> $country</span><br/><br/>
                                                <span><strong>Phone No:</strong>$phone</span><br/><br/>
                                                
                                                <span><strong>Number of Adults:</strong> $number_of_adults</span><br/><br/>
                                                
                                                <span><strong>Number of Children:</strong> $number_of_children</span><br/><br/>
                                                
                                                    <span><strong>Tour:</strong><a href='https://www.ihambaadventures.com/package-detail/$safari_tour->title/$safari_tour->id'> $safari_tour->title</a></span><br/>
                                                
                                                
                                            </p>
                                        </div>
                                    </td>
                                </tr>
                                <tr>
                                    <td style='color: grey; font-family: Arial, sans-serif;'>
                                        <div class='previous_lecture_table' style='margin-top: 20px;'>
                                            <p style='color: grey;'>
                                                <span><strong>Your Message</strong>: $client_message</span>
                                            </p>
                                        </div>
                                    </td>
                                </tr>
                            </table>
                        </td>
                    </tr>
                    <tr>
                        <td style='padding: 30px 30px;'>
                            <table border='0' cellpadding='0' cellspacing='0' width='100%' style='border-collapse: collapse;'>
                                <tr>
                                    <td align='center' style='color: #153643; font-family: Arial, sans-serif;'>
                                        <small style='margin: 0;'>
                                            If you have any questions or issues with this email, please contact us at 
                                            <a mailto='info@ihambaadventures.com'>info@ihambaadventures.com</a>
                                        </small>
                                    </td>
                                </tr>
                            </table>
                        </td>
                    </tr>
                </table>

            </td>
        </tr>
    </table>
</body>

</html>";


        $config = Array(
        
          'mailtype' => 'html',
          'charset' => 'iso-8859-1',
          'wordwrap' => TRUE
        );


      $this->load->library('email', $config);

      $this->email->from('no-reply@ihambaadventures.com','Ihamba Adventures'); // change it to yours
       $this->email->set_header('Content-Type', 'text/html');
      $this->email->to($email);// change it to yours
      $this->email->subject('Copy of your quote request ');
      $this->email->set_header('X-Priority', 1);
    $this->email->set_header('X-MSMail-Priority', 'High');

      $this->email->message($message);
     
        
    if($this->db->insert("tour_qoutes",$data)){
        $this->email->send();
         return $this->output
                ->set_content_type('application/json')
                ->set_status_header(200)
                ->set_output(json_encode(array(
                    'success' => true,
                    'message' => 'QUOTE_SENT_SUCCESSFULLY'
                )));
    }
    
}

public function send_emails(){
    $json_data = json_decode(@file_get_contents('php://input'), true);
    
    
    $full_name = "Kimbowa Andrew";
    $time = "12:00pm EST";
    $clients_number ="+256 703 657 397";
    $date = "Tuesday 25th August 2023";
    $email = "andrewkimbowa2021@gmail.com";
    
    
     $message = "<!DOCTYPE html>
<html lang='en'>

<head>
    <meta http-equiv='Content-Type' content='text/html; charset=UTF-8' />
    <title>Safari Quote</title>
    <meta name='viewport' content='width=device-width, initial-scale=1.0' />
    <link rel='stylesheet' href='https://use.fontawesome.com/releases/v5.3.1/css/all.css' integrity='sha384-mzrmE5qonljUremFsqc01SB46JvROS7bZs3IO2EmfFsd15uHvIt+Y8vEf7N7fWAU' crossorigin='anonymous'>

    <style type='text/css'>
        a[x-apple-data-detectors] {
            color: inherit !important;
        }
        
        .previous_lecture_table {
            margin-top: 30px;
            font-size: 1em !important;
        }
    </style>

</head>

<body style='margin: 0; padding: 0;'>
    <table role='presentation' border='0' cellpadding='0' cellspacing='0' width='100%' style='background-color: #eeeeee;'>
        <tr>
            <td style='padding: 20px 0 30px 0;'>

                <table align='center' border='0' cellpadding='0' cellspacing='0' width='600' style='border-collapse: collapse; border: 1px solid #cccccc; border-top: 10px solid #083628; background-color: #fff;'>
                    <tr>
                        <td align='center' style='padding: 40px 0 30px 0;'>
                            <img src='https://d3v0px0pttie1i.cloudfront.net/uploads/user/logo/12740338/369b543a.png' alt='Creating Email Magic.' width='20%' style='display: block; border-radius:10px' />
                        </td>
                    </tr>
                    <tr>
                        <td bgcolor='#ffffff' style='padding: 20px 30px 40px 30px;'>
                            <table border='0' cellpadding='0' cellspacing='0' width='100%' style='border-collapse: collapse;'>
                                <tr>
                                    <td style='color: #002F6C; font-family: Arial, sans-serif;'>
                                        <h1 style='font-size: 20px; margin: 0;'>Hi $full_name,</h1>
                                        <p style='color: grey;'>
                                            
                                     
I look forward to our Safari Planning Session on $date. It will be a zoom meeting and the meeting link has been provided in the previous email.

$time on $clients_number or via zoom, kindly let me know what's more convenient for you.
Please take Four minutes to tell me more about the safari you're planning so that I can best prepare for our meeting. <a href='https://forms.gle/MqZziWbBjQQestUW8'> Head over here to answer five quick questions</a>
Thank you! I'm excited to meet with you to see if  we are a good fit to work together on this.
</p>

<p>Cheers,</p>

<p>Joseph Byamukama</p>


<p>
P.S. I've planned so many African Safari vacations for my clients. I absolutely
love the work I do! To read the story of how I became an African Safari Expert, go to <a href='https://www.ihambaadventures.com/'>my bio </a>

                                        </p>
                                    </td>
                                </tr>
                               
                                    
                               
                                
                            </table>
                        </td>
                    </tr>
                    <tr>
                        <td style='padding: 30px 30px;'>
                            <table border='0' cellpadding='0' cellspacing='0' width='100%' style='border-collapse: collapse;'>
                                <tr>
                                    <td align='center' style='color: #153643; font-family: Arial, sans-serif;'>
                                        <small style='margin: 0;'>
                                            If you have any questions or issues with this email, please contact us at 
                                            <a mailto='info@ihambaadventures.com'>info@ihambaadventures.com</a>
                                        </small>
                                    </td>
                                </tr>
                            </table>
                        </td>
                    </tr>
                </table>

            </td>
        </tr>
    </table>
</body>

</html>";






$config = Array( 
                    'protocol' => 'smtp', 
                    'smtp_host' => 'ihambaadventures.com', 
                      'smtp_port' => '465', 
                    'smtp_user' => 'sales@ihambaadventures.com', 
                    'smtp_pass' => 'sales@ihamba',
                     'mailtype' => 'html',
                     'wordwrap' => TRUE,
                        'charset' => 'iso-8859-1'); 

      

      $this->load->library('email', $config);

      $this->email->from('sales@ihambaadventures.com','Ihamba Adventures'); // change it to yours
      $this->email->set_header('Content-Type', 'text/html');
      $this->email->to('andrew@ovalspace.co');// change it to yours
      $this->email->subject('Safari Planning Session');
    //   $this->email->set_header('X-Priority', 1);
    //   $this->email->set_header('X-MSMail-Priority', 'High');

      $this->email->message($message);

      if($this->email->send()){
          echo "done";
      }else{
         show_error($this->email->print_debugger());
         
      }
}

public function send_test_email(){
    
    
    $message = "<h1>hello</h1>";
    
$config = Array( 
                    'protocol' => 'smtp', 
                    'smtp_host' => 'premium133.web-hosting.com', 
                    'smtp_port' => '465', 
                    'smtp_user' => 'sales@ihambaadventures.com', 
                    'smtp_pass' => 'sales@ihamba',
                    'mailtype' => 'html',
                    'wordwrap' => TRUE,
                        'charset' => 'iso-8859-1'); 

      

      $this->load->library('email', $config);

      $this->email->from('sales@ihambaadventures.com','Ihamba Adventures'); // change it to yours
      $this->email->set_header('Content-Type', 'text/html');
      $this->email->to('andrewkimbowa2021@gmail.com');// change it to yours
      $this->email->subject('Copy of your quote request ');
      $this->email->set_header('X-Priority', 1);
      $this->email->set_header('X-MSMail-Priority', 'High');

      $this->email->message($message);
      if($this->email->send()){
          echo "done";
      }else{
         show_error($this->email->print_debugger());
         
      }
    
}

public function send_email(){
    
     
    $full_name = "Kimbowa Andrew";
    $time = "12:00pm EST";
    $clients_number ="+256 703 657 397";
    $date = "Tuesday 25th August 2023";
    $email = "andrewkimbowa2021@gmail.com";
    
    
     $message = "<!DOCTYPE html>
<html lang='en'>

<head>
    
    <title>Safari Quote</title>
    <meta name='viewport' content='width=device-width, initial-scale=1.0' />
    <link rel='stylesheet' href='https://use.fontawesome.com/releases/v5.3.1/css/all.css' integrity='sha384-mzrmE5qonljUremFsqc01SB46JvROS7bZs3IO2EmfFsd15uHvIt+Y8vEf7N7fWAU' crossorigin='anonymous'>

    <style type='text/css'>
        a[x-apple-data-detectors] {
            color: inherit !important;
        }
        
        .previous_lecture_table {
            margin-top: 30px;
            font-size: 1em !important;
        }
    </style>

</head>

<body style='margin: 0; padding: 0;'>
    <table role='presentation' border='0' cellpadding='0' cellspacing='0' width='100%' style='background-color: #eeeeee;'>
        <tr>
            <td style='padding: 20px 0 30px 0;'>

                <table align='center' border='0' cellpadding='0' cellspacing='0' width='600' style='border-collapse: collapse; border: 1px solid #cccccc; border-top: 10px solid #083628; background-color: #fff;'>
                    <tr>
                        <td align='center' style='padding: 40px 0 30px 0;'>
                            <img src='https://d3v0px0pttie1i.cloudfront.net/uploads/user/logo/12740338/369b543a.png' alt='Creating Email Magic.' width='20%' style='display: block; border-radius:10px' />
                        </td>
                    </tr>
                    <tr>
                        <td bgcolor='#ffffff' style='padding: 20px 30px 40px 30px;'>
                            <table border='0' cellpadding='0' cellspacing='0' width='100%' style='border-collapse: collapse;'>
                                <tr>
                                    <td style='color: #002F6C; font-family: Arial, sans-serif;'>
                                        <h1 style='font-size: 20px; margin: 0;'>Hi $full_name,</h1>
                                        <p style='color: grey;'>
                                            
                                     
I look forward to our Safari Planning Session on $date. I will call you at $clients_number, at $time.
Please take Four minutes to tell me more about the safari you're planning so that I can best prepare for our meeting. <a href='https://forms.gle/MqZziWbBjQQestUW8'> Head over here to answer five quick questions</a>
Thank you! I'm excited to meet with you to see if  we are a good fit to work together on this.
</p>

<p>Cheers,</p>

<p>Joseph Byamukama</p>


<p>
P.S. I've planned so many African Safari vacations for my clients. I absolutely
love the work I do! To read the story of how I became an African Safari Expert, go to <a href='https://www.ihambaadventures.com/'>my bio </a>

                                        </p>
                                    </td>
                                </tr>
                               
                                    
                               
                                
                            </table>
                        </td>
                    </tr>
                    <tr>
                        <td style='padding: 30px 30px;'>
                            <table border='0' cellpadding='0' cellspacing='0' width='100%' style='border-collapse: collapse;'>
                                <tr>
                                    <td align='center' style='color: #153643; font-family: Arial, sans-serif;'>
                                        <small style='margin: 0;'>
                                            If you have any questions or issues with this email, please contact us at 
                                            <a mailto='info@ihambaadventures.com'>info@ihambaadventures.com</a>
                                        </small>
                                    </td>
                                </tr>
                            </table>
                        </td>
                    </tr>
                </table>

            </td>
        </tr>
    </table>
</body>

</html>";
    
    $this->email->from('sales@ihambaadventures.com', 'Sales');
$this->email->to('andrewkimbowa2021@gmail.com');
$this->email->subject('Customer');
 $this->email->set_header('Content-Type', 'text/html');
$this->email->message($message);

$this->email->send();

}

public function send_em(){
    
    // $full_name = "Kimbowa Andrew";
    // $time = "12:00pm EST";
    // $clients_number ="+256 703 657 397";
    // $date = "Tuesday 25th August 2023";
    // $email = "andrewkimbowa2021@gmail.com";
    
   
    
    $full_name = $_POST['fullname'];
    $time =  $_POST['time'];
    $clients_number = $_POST['phone'];
    $email =    $_POST['email'];
    
$dateString = $_POST['date'];


$datee = DateTime::createFromFormat('D M d Y H:i:s e+', $dateString);
$date = $datee->format('D M d Y');

     $message = "<!DOCTYPE html>
<html lang='en'>

<head>
    
    <title>Safari Planning Session</title>
    <meta name='viewport' content='width=device-width, initial-scale=1.0' />
    <link rel='stylesheet' href='https://use.fontawesome.com/releases/v5.3.1/css/all.css' integrity='sha384-mzrmE5qonljUremFsqc01SB46JvROS7bZs3IO2EmfFsd15uHvIt+Y8vEf7N7fWAU' crossorigin='anonymous'>

    <style type='text/css'>
        a[x-apple-data-detectors] {
            color: inherit !important;
        }
        
        .previous_lecture_table {
            margin-top: 30px;
            font-size: 1em !important;
        }
    </style>

</head>

<body style='margin: 0; padding: 0;'>
    <table role='presentation' border='0' cellpadding='0' cellspacing='0' width='100%' style='background-color: #eeeeee;'>
        <tr>
            <td style='padding: 20px 0 30px 0;'>

                <table align='center' border='0' cellpadding='0' cellspacing='0' width='600' style='border-collapse: collapse; background-color: #fff;'>
                    <tr>
                        <td align='left' style='padding: 40px 0 30px 35px;'>
                            <img src='https://api.ihambaadventures.com/destinations/ihamba.png' alt='Creating Email Magic.' width='20%' style='display: block; border-radius:5px' />


                        </td>
                    </tr>
                    <tr>
                        <td bgcolor='#ffffff' style='padding: 20px 30px 40px 30px;'>
                            <table border='0' cellpadding='0' cellspacing='0' width='100%' style='border-collapse: collapse;'>
                                <tr>
                                    <td style='color: #002F6C; font-family: Arial, sans-serif;'>
                                        <h1 style='font-size: 20px; margin: 0;'>Hi $full_name,</h1>
                                        <p style='color: grey;'>
                                            
                                     
I look forward to our Safari Planning Session on $date at $time sharp. It will be a zoom meeting and attached is the link <a href='$clients_number'>here.</a>
</p>

<p style='color: grey;'>
Please take Four minutes to tell me more about the safari you're planning so that I can best prepare for our meeting. <a href='https://forms.gle/MqZziWbBjQQestUW8'>Head over here </a>to answer five quick questions
</p>
<p style='color: grey;'>
Thank you! I'm excited to meet with you to see if  we are a good fit to work together on this.
</p>

<p style='color: grey;'>Cheers,</p>
<img style='width:10%' src='https://api.ihambaadventures.com/files/josephpng.png'>
<p style='color: grey; margin-top:-10px'>Joseph Byamukama</p>


<p style='color: grey;'>
P.S. I've planned so many African Safari vacations for my clients. I absolutely
love the work I do! To read the story of how I became an African Safari Expert, go to <a href='https://www.ihambaadventures.com/about-us?bio=joseph'>my bio </a>

                                        </p>
                                    </td>
                                </tr>
                               
                                    
                               
                                
                            </table>
                        </td>
                    </tr>
                    <tr>
                        <td style='padding: 30px 30px;'>
                            <table border='0' cellpadding='0' cellspacing='0' width='100%' style='border-collapse: collapse;'>
                                <tr>
                                    <td align='center' style='color: #153643; font-family: Arial, sans-serif;'>
                                        <small style='margin: 0;'>
                                            If you have any questions or issues with this email, please contact us at 
                                            <a mailto='info@ihambaadventures.com'>info@ihambaadventures.com</a>
                                        </small>
                                    </td>
                                </tr>
                            </table>
                        </td>
                    </tr>
                </table>

            </td>
        </tr>
    </table>
</body>

</html>";
    
    
    $this->load->library('phpmailer_lib');

    
    // PHPMailer object
        $mail = $this->phpmailer_lib->load();

$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'premium133.web-hosting.com';                       // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'sales@ihambaadventures.com';             // SMTP username
$mail->Password = 'sales@ihamba';              // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;                                    // TCP port to connect to

$mail->setFrom('sales@ihambaadventures.com', 'Ihamba Adventures Marketing');
$mail->addAddress($email);           // Add a recipient
$mail->addReplyTo('sales@ihambaadventures.com', 'Ihamba Adventures');
$mail->isHTML(true);                                  // Set email format to HTML
$mail->Subject = 'Safari Planning Session';
$mail->Body    = $message;
$mail->AltBody = 'Hello, this is a test email!';       // Plain-text message

if (!$mail->send()) {
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    return $this->output
            ->set_content_type('application/json')
            ->set_status_header(200)
            ->set_output(json_encode(array(
                'success' => true,
                'message' => "EMAIL_SENT_SUCCESSFULLY"
            )));
}
}

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

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

}

// public function get_destinations(){
//     $this->db->select('id,title,description,image');
//     $this->db->from('destinations');
//     $query = $this->db->get();

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

// }

// public function get_destination($id){
//     $destination = $this->db->get_where('destinations', array('id' => $id))->row();

//     if ($destination == null) {
//         return $this->output
//             ->set_content_type('application/json')
//             ->set_status_header(200)
//             ->set_output(json_encode(array(
//                 'success' => false,
//                 'message' => "DESTINATION_NOT_FOUND"
//             )));
//     }

//     // get all the sub destinations too

//     $this->db->select('id,title,description,image');
//     $this->db->where("destination_id",$destination->id);
//     $this->db->from('sub_destinations');
//     $sub_destinations = $this->db->get()->result_array();

//     return $this->output
//     ->set_content_type('application/json')
//     ->set_status_header(200)
//     ->set_output(json_encode(array(
//         'success' => true,
//         'destination'=>$destination,
//         'sub_destinations' => $sub_destinations
//     )));


// }


public function add_day(){
      $check_name =   $this->db->get_where('safari_tour_days', array("day" => $_POST['day'],"safari_tour_id"=>$_POST['safari_tour_id']))->row();

    if (($check_name != null)) {
      
        return $this->output
            ->set_content_type('application/json')
            ->set_status_header(200)
            ->set_output(json_encode(array(
                'success' => false,
                'message' => 'DAY_EXISTS'
            )));
    } else {

        $image_url = '';
        
        if(!empty($_FILES["image"]['name'])){
        $image_url =  $this->upload_image($_FILES["image"]['name'], "image","safari_tour_days");
            }
            
        $data = array(
            "day" => $_POST['day'],
            "main_destination" => $_POST['main_destination'],
            'featuring_photo' => $image_url,
            'safari_tour_id' => $_POST['safari_tour_id'],
            'meals'=>$_POST['meals'],
            'accomodation' => $_POST['accomodation'],
            'description' => $_POST['description'],
            'date_added' => date('Y-m-d\TH:i:s.u\Z')
        );

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

public function add_activity(){
    
    $json_data = json_decode(@file_get_contents('php://input'), true);
    
       $check_package = $this->db->get_where('safari_tour_packages', array("id"=>$json_data['package_id']))->row();
      
       if (($check_package == null)) {
      
        return $this->output
            ->set_content_type('application/json')
            ->set_status_header(200)
            ->set_output(json_encode(array(
                'success' => false,
                'message' => 'PACKAGE_NOT_FOUND'
            )));
    }
    
      $check_name =   $this->db->get_where('day_activities', array("title" => $json_data['title'],"package_id"=>$json_data['package_id']))->row();
      
   

    if (($check_name != null)) {
      
        return $this->output
            ->set_content_type('application/json')
            ->set_status_header(200)
            ->set_output(json_encode(array(
                'success' => false,
                'message' => 'ACTIVITY_EXISTS'
            )));
    }
    
    $data = array(
        "title"=>$json_data['title'],
        "package_id"=>$json_data['package_id'],
        "date_added"=>date('Y-m-d\TH:i:s.u\Z')
        );
        
         

      if($this->db->insert('day_activities',$data)){
            return $this->output
                ->set_content_type('application/json')
                ->set_status_header(200)
                ->set_output(json_encode(array(
                    'success' => true,
                    'message' => 'ACTIVITY_ADDED_SUCCESSFULLY'
                )));
      }
}

public function edit_activity($id){
    
    $json_data = json_decode(@file_get_contents('php://input'), true);
    
       $check_activity = $this->db->get_where('day_activities', array("id"=>$id))->row();
      
       if (($check_activity == null)) {
      
        return $this->output
            ->set_content_type('application/json')
            ->set_status_header(200)
            ->set_output(json_encode(array(
                'success' => false,
                'message' => 'ACTIVITY_NOT_FOUND'
            )));
    }
    
    
    
    $data = array(
        "title"=>$json_data['title'],
        "package_id"=>$json_data['package_id'],
        
        );
        
         
        $this->db->where("id",$id);
      if($this->db->update('day_activities',$data)){
            return $this->output
                ->set_content_type('application/json')
                ->set_status_header(200)
                ->set_output(json_encode(array(
                    'success' => true,
                    'message' => 'ACTIVITY_EDITED_SUCCESSFULLY'
                )));
      }
}


public function delete_activity($id){
 $check_activity = $this->db->get_where('day_activities', array("id"=>$id))->row();
      
       if (($check_activity == null)) {
      
        return $this->output
            ->set_content_type('application/json')
            ->set_status_header(200)
            ->set_output(json_encode(array(
                'success' => false,
                'message' => 'ACTIVITY_NOT_FOUND'
            )));
    }
    
    $this->db->where('id',$id);
    if($this->db->delete('day_activities')){
         return $this->output
                ->set_content_type('application/json')
                ->set_status_header(200)
                ->set_output(json_encode(array(
                    'success' => true,
                    'message' => 'ACTIVITY_DELETED_SUCCESSFULLY'
                )));
    }
}
public function edit_day($id){
    
      $safari_day =   $this->db->get_where('safari_tour_days', array("id" =>$id))->row();

    if (($safari_day == null)) {
      
        return $this->output
            ->set_content_type('application/json')
            ->set_status_header(200)
            ->set_output(json_encode(array(
                'success' => false,
                'message' => 'DAY_DOESNT_EXISTS'
            )));
    } else {
        

        $data = array(
            "day" => $_POST['day'],
            "main_destination" => $_POST['main_destination'],
            'safari_tour_id' => $_POST['package_id'],
            'accomodation' => $_POST['accomodation'],
            'meals'=>$_POST['meals'],
            'description' => $_POST['description'],
            'date_added' => date('Y-m-d\TH:i:s.u\Z')
        );
        
        //check if the image was sent
        
         if (!empty($_FILES['image']['name'])) {
             $data['featuring_photo'] = $this->upload_image($_FILES["image"]['name'], "image","safari_tour_days");
             
             // delete the old image too
             
             $image_location = './safari_tour_days' . explode('safari_tour_days/', $safari_day->featuring_photo)[1];
         }

        $this->db->set($data);
        $this->db->where('id', $id);
        if ($this->db->update('safari_tour_days', $data)) {
            return $this->output
                ->set_content_type('application/json')
                ->set_status_header(200)
                ->set_output(json_encode(array(
                    'success' => true,
                    'message' => 'DAY_EDITED_SUCCESSFULLY'
                )));
        } else {
            return $this->output
                ->set_content_type('application/json')
                ->set_status_header(200)
                ->set_output(json_encode(array(
                    'success' => false,
                    'message' => 'DAY_NOT_EDITED'
                )));
        }
    }
}

public function delete_day($id){
      $safari_day =   $this->db->get_where('safari_tour_days', array("id" =>$id))->row();


 if (($safari_day == null)) {
      
        return $this->output
            ->set_content_type('application/json')
            ->set_status_header(200)
            ->set_output(json_encode(array(
                'success' => false,
                'message' => 'DAY_DOESNT_EXISTS'
            )));
    }

   $this->db->where('id', $id);
        if ($this->db->delete('safari_tour_days')) {
            return $this->output
                ->set_content_type('application/json')
                ->set_status_header(200)
                ->set_output(json_encode(array(
                    'success' => true,
                    'message' => 'DAY_DELETED_SUCCESSFULLY'
                )));
        } else {
            return $this->output
                ->set_content_type('application/json')
                ->set_status_header(200)
                ->set_output(json_encode(array(
                    'success' => false,
                    'message' => 'DAY_NOT_DELETED'
                )));
        }
}

public function set_package_status(){
     $package_id = $this->input->get("package_id");
     $safari_package =   $this->db->get_where('safari_tour_packages', array("id" =>$package_id))->row();
     
      if (($safari_package == null)) {
      
        return $this->output
            ->set_content_type('application/json')
            ->set_status_header(200)
            ->set_output(json_encode(array(
                'success' => false,
                'message' => 'PACKAGE_DOESNT_EXISTS'
            )));
    }
    
    
      $package_status="";
        if($safari_package->status =='true'){
            $package_status='false';
        }else{
               $package_status='true';
        }
        
        
         $this->db->where("id",$package_id);
        $this->db->set("status",$package_status);
        
        
          if($this->db->update("safari_tour_packages")){
        
           if($package_status =='true'){
                
                return $this->output
                ->set_content_type('application/json')
                ->set_status_header(200)
                ->set_output(json_encode(array(
                    'success' => true,
                    'message' => 'PACKAGE_SET_ONLINE'
                )));
            }else{
                 return $this->output
                ->set_content_type('application/json')
                ->set_status_header(200)
                ->set_output(json_encode(array(
                    'success' => true,
                    'message' => 'PACKAGE_SET_OFFLINE'
                )));
            }
          }
    
     
     
}

public function set_package_featured_status(){
     $package_id = $this->input->get("package_id");
     $safari_package =   $this->db->get_where('safari_tour_packages', array("id" =>$package_id))->row();
     
      if (($safari_package == null)) {
      
        return $this->output
            ->set_content_type('application/json')
            ->set_status_header(200)
            ->set_output(json_encode(array(
                'success' => false,
                'message' => 'PACKAGE_DOESNT_EXISTS'
            )));
    }
    
    
      $package_status="";
        if($safari_package->isFeatured =="true"){
            $package_status="false";
        }else{
               $package_status="true";
        }
        
        
         $this->db->where("id",$package_id);
        $this->db->set("isFeatured",$package_status);
        
        
          if($this->db->update("safari_tour_packages")){
        
           if($package_status =="true"){
                
                return $this->output
                ->set_content_type('application/json')
                ->set_status_header(200)
                ->set_output(json_encode(array(
                    'success' => true,
                    'message' => 'PACKAGE_ADDED_TO_FEATURED'
                )));
            }else{
                 return $this->output
                ->set_content_type('application/json')
                ->set_status_header(200)
                ->set_output(json_encode(array(
                    'success' => true,
                    'message' => 'PACKAGE_REMOVED_FROM_FEATURED'
                )));
            }
          }
    
     
     
}




// public function edit_destination($id){

//     $destination = $this->db->get_where('destinations', array('id' => $id))->row();
//     if ($destination == null) {
//         return $this->output
//             ->set_content_type('application/json')
//             ->set_status_header(200)
//             ->set_output(json_encode(array(
//                 'success' => false,
//                 'message' => "DESTINATION_NOT_FOUND"
//             )));
//     }


//         $data = array(
//             'title' => $_POST['title'],
//             'description' => $_POST['description']
           
        
//         );


//     // update the safari_tour image link of those files and field names passed accordingly
//     if (!empty($_FILES['image']['name'])) {
//         // assign the returned image url(s) after deleting the old image and add the value to the data array

//         $data['image'] = $this->upload_edit_destination($_FILES['image']['name'], $id, 'image');
//     }

   

//     // now update the data

//     $this->db->set($data);
//     $this->db->where('id', $id);
//     if ($this->db->update('destinations', $data)) {

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

// public function delete_destination($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'
//             )));
//     }

//     $destination = $this->db->get_where('destinations', array('id' => $id))->row();

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

//         // delete / unlink the old destination photos
//         $image_location = 'destinations/' . explode('destinations/', $destination->image)[1];
        

//         if (file_exists($image_location)) {
//             // if it exists delete it
//             $this->unlink_files($image_location);
//             } 

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

// sub destinations
// get sub destination
// sub destination has more tours/adventures

// public function add_sub_destination(){
//     $check_name =   $this->db->get_where('sub_destinations', array("title" => $_POST['title']))->row();

//     if (($check_name != null)) {
      
//         return $this->output
//             ->set_content_type('application/json')
//             ->set_status_header(200)
//             ->set_output(json_encode(array(
//                 'success' => false,
//                 'message' => 'SUB_DESTINATION_EXISTS'
//             )));
//     } else {

//         $image_url =  $this->upload_image($_FILES["image"]['name'], "image","sub_destinations");

//         $data = array(
//             "title" => $_POST['title'],
//             "destination_id" => $_POST['destination_id'],
//             'image' => $image_url,
//             'description' => $_POST['description'],
//             'date_created' => date('Y-m-d\TH:i:s.u\Z')
//         );

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

// public function edit_sub_destination($id){

//     $destination = $this->db->get_where('sub_destinations', array('id' => $id))->row();
//     if ($destination == null) {
//         return $this->output
//             ->set_content_type('application/json')
//             ->set_status_header(200)
//             ->set_output(json_encode(array(
//                 'success' => false,
//                 'message' => "SUB_DESTINATION_EDITED_SUCCESSFULLY"
//             )));
//     }


//         $data = array(
//             'title' => $_POST['title'],
//             'description' => $_POST['description'],
//             "destination_id" => $_POST['destination_id'],
//             'last_updated'=>date('Y-m-d\TH:i:s.u\Z')
        
//         );


//     // update the safari_tour image link of those files and field names passed accordingly
//     if (!empty($_FILES['image']['name'])) {
//         // assign the returned image url(s) after deleting the old image and add the value to the data array

//         $data['image'] = $this->upload_edit_sub_destination($_FILES['image']['name'], $id, 'image');
//     }


//     // now update the data

   

//     $this->db->set($data);
//     $this->db->where('id', $id);
//     if ($this->db->update('sub_destinations', $data)) {

//         return $this->output
//             ->set_content_type('application/json')
//             ->set_status_header(200)
//             ->set_output(json_encode(array(
//                 'success' => true,
//                 'message' => 'SUB_DESTINATION_EDITED_SUCCESSFULLY'
//             )));
//     } else {
//         return $this->output
//             ->set_content_type('application/json')
//             ->set_status_header(200)
//             ->set_output(json_encode(array(
//                 'success' => false,
//                 'message' => 'SUB_DESTINATION_NOT_EDITED_SUCCESSFULLY'
//             )));
//     }
// }
 
// public function get_sub_destination($id){
    
//         $destination = $this->db->get_where('destinations', array('id' => $id))->row();
    
//         if ($destination == null) {
//             return $this->output
//                 ->set_content_type('application/json')
//                 ->set_status_header(200)
//                 ->set_output(json_encode(array(
//                     'success' => false,
//                     'message' => "DESTINATION_NOT_FOUND"
//                 )));
//         }
    
//         // get all the adventures/tours in this sub destination
    
//         // $this->db->select('id,title,description,image');
//         $this->db->select('*');
//         $this->db->where("sub_destination_id",$destination->id);
//         $this->db->from('safari_tours');
//         $sub_destinations = $this->db->get()->result_array();
    
//         return $this->output
//         ->set_content_type('application/json')
//         ->set_status_header(200)
//         ->set_output(json_encode(array(
//             'success' => true,
//             'destination'=>$destination,
//             'adventures' => $sub_destinations
//         )));
    
    
   
// }

// public function delete_sub_destination($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'
//             )));
//     }

//     $destination = $this->db->get_where('sub_destinations', array('id' => $id))->row();

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

//         // delete / unlink the old destination photos
//         $image_location = 'sub_destinations/' . explode('sub_destinations/', $destination->image)[1];
        

//         if (file_exists($image_location)) {
//             // if it exists delete it
//             $this->unlink_files($image_location);
//             } 

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

    public function add_safari_tour()
    {

        $check_name =   $this->db->get_where('safari_tours', array("heading" => $_POST['heading']))->row();

        if (($check_name != null)) {
          
            return $this->output
                ->set_content_type('application/json')
                ->set_status_header(200)
                ->set_output(json_encode(array(
                    'success' => false,
                    'message' => 'safari_tour  exists'
                )));
        } else {

           

            $image_url =  $this->upload_image($_FILES["image"]['name'], "image","safari_tours");

            $data = array(
                "heading" => $_POST['heading'],
                'image' => $image_url,
                "amount_range" => $_POST['amount_range'],
                'brief_description' => $_POST['brief_description'],
                'safari_type' => $_POST['safari_type'],
                'overview_visit' => $_POST['overview_visit'],
                'destination_country' => $_POST['destination_country'],
                'tour_budget' => $_POST['tour_budget'],
                'date_created' => date('Y-m-d\TH:i:s.u\Z')
            );

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

        $check_name =   $this->db->get_where('safari_tour_packages', array("title" => $_POST['title']))->row();

        if (($check_name != null)) {
          
            return $this->output
                ->set_content_type('application/json')
                ->set_status_header(200)
                ->set_output(json_encode(array(
                    'success' => false,
                    'message' => 'safari_tour  exists'
                )));
        } else {

           

            $image_url =  $this->upload_image($_FILES["image"]['name'], "image","safari_tours");

            $data = array(
                "title" => $_POST['title'],
                'image' => $image_url,
                "amount" => $_POST['amount'],
                'brief_description' => $_POST['brief_description'],
                'safari_type' => $_POST['safari_type'],
                'inclusions_and_exclusions' => $_POST['inclusions_and_exclusions'],
                'getting_there' => $_POST['getting_there'],
                
                'destination_country' => $_POST['destination_country'],
                'added_by' => $_POST['added_by'],
                
                'date_created' => date('Y-m-d\TH:i:s.u\Z')
            );

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

        $check_name =   $this->db->get_where('blogs', array("title" => $_POST['title']))->row();

        if (($check_name != null)) {
          
            return $this->output
                ->set_content_type('application/json')
                ->set_status_header(200)
                ->set_output(json_encode(array(
                    'success' => false,
                    'message' => 'SAFARI_BLOG_EXISTS'
                )));
        } else {

           

            $image_url =  $this->upload_image($_FILES["image"]['name'], "image","blogs");

            $data = array(
                "title" => $_POST['title'],
                'featured_image' => $image_url,
                // 'brief_description' => $_POST['brief_description'],
                'author' => $_POST['author'],
                
                'content' => $_POST['content'],
                'is_active'=>false,
                
                'added_by' => $_POST['added_by'],
                'date_added' => date('Y-m-d\TH:i:s.u\Z')
            );

  
           
            if ($this->db->insert('blogs', $data)) {
                return $this->output
                    ->set_content_type('application/json')
                    ->set_status_header(200)
                    ->set_output(json_encode(array(
                        'success' => true,
                        'message' => 'BLOG_ADDED_SUCCESSFULLY'
                    )));
            } else {
                return $this->output
                    ->set_content_type('application/json')
                    ->set_status_header(200)
                    ->set_output(json_encode(array(
                        'success' => false,
                        'message' => 'BLOG_NOT_ADDED_SUCCESSFULLY'
                    )));
            }
        }
    }
    
  public function get_blogs() {
    $this->db->select("blogs.id,title,author,featured_image,is_active,date_added, 
        (SELECT COUNT(*) FROM blog_packages WHERE blog_packages.blog_id = blogs.id) AS package_count");
    $this->db->order_by("id",'desc');
    $blogs = $this->db->get("blogs")->result_array();

    // Loop through each blog in the array and add an isActive key with the correct boolean value
    foreach ($blogs as &$blog) { // The & symbol makes $blog a reference to the original array element instead of a copy
        $blog['is_active'] = ($blog['is_active'] == 'true') ? true : false;
    }

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


  public function get_blogs_client() {
    $this->db->select("id,title,author,featured_image,is_active,date_added");
    $this->db->where('is_active','true');
    $this->db->order_by("id",'desc');
    
    
    $blogs = $this->db->get("blogs")->result_array();

    // Loop through each blog in the array and add an isActive key with the correct boolean value
    foreach ($blogs as &$blog) { // The & symbol makes $blog a reference to the original array element instead of a copy
        $blog['is_active'] = ($blog['is_active'] == 'true') ? true : false;
    }

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

    
    public function set_blog_status(){
     $blog_id = $this->input->get("blog_id");
     $safari_blog =   $this->db->get_where('blogs', array("id" =>$blog_id))->row();
     
      if (($safari_blog == null)) {
      
        return $this->output
            ->set_content_type('application/json')
            ->set_status_header(200)
            ->set_output(json_encode(array(
                'success' => false,
                'message' => 'BLOG_DOESNT_EXISTS'
            )));
    }
    
    
      $blog_status="";
        if($safari_blog->is_active =='true'){
            $blog_status='false';
        }else{
               $blog_status='true';
        }
        
        
         $this->db->where("id",$blog_id);
        $this->db->set("is_active",$blog_status);
        
        
          if($this->db->update("blogs")){
        
           if($blog_status =='true'){
                
                return $this->output
                ->set_content_type('application/json')
                ->set_status_header(200)
                ->set_output(json_encode(array(
                    'success' => true,
                    'message' => 'BLOG_SET_ONLINE'
                )));
            }else{
                 return $this->output
                ->set_content_type('application/json')
                ->set_status_header(200)
                ->set_output(json_encode(array(
                    'success' => true,
                    'message' => 'BLOG_SET_OFFLINE'
                )));
            }
          }
    
     
     
}


    public function get_blog_post($id){
        
        $blog_post = $this->db->get_where("blogs",array("id"=>$id))->row();
        
          $this->db->where('blog_packages.blog_id', $id);
          $this->db->select('safari_tour_packages.id,safari_tour_packages.title,safari_tour_packages.image,safari_tour_packages.brief_description,safari_tour_packages.destination_country');

       
         $this->db->from('blog_packages');
          $this->db->join('safari_tour_packages', 'safari_tour_packages.id = blog_packages.package_id');
        $packages = $this->db->get()->result_array();
    
        
        if(!$blog_post){
        
         return $this->output
                    ->set_content_type('application/json')
                    ->set_status_header(200)
                    ->set_output(json_encode(array(
                        'success' => false,
                        'message' => 'BLOG_NOT_FOUND'
                    )));
            }else{
                
                   return $this->output
                    ->set_content_type('application/json')
                    ->set_status_header(200)
                    ->set_output(json_encode(array(
                        'success' => true,
                        'blog_post' => $blog_post,
                        'packages'=>$packages
                    )));
                
            }
            
        
    }
    
    public function edit_blog_post($id){
        
        $check_blog =  $this->db->get_where('blogs', array("id" => $id))->row();

        if (($check_blog == null)) {
          
            return $this->output
                ->set_content_type('application/json')
                ->set_status_header(200)
                ->set_output(json_encode(array(
                    'success' => false,
                    'message' => 'BLOG_DOES_NOT_EXIST'
                )));
        } else {


      $data = array(
                "title" => $_POST['title'],
               
                // 'brief_description' => $_POST['brief_description'],
                'author' => $_POST['author'],
                'content' => $_POST['content'],
                'added_by' => $_POST['added_by']
                
            );

           
        if(!empty($_FILES["image"]['name'])){
            
            $image_url =  $this->upload_image($_FILES["image"]['name'], "image","blogs"); 
            $data['featured_image'] = $image_url;
        }
           

      
         $this->db->set($data);
        $this->db->where('id', $id);
        if ($this->db->update('blogs', $data)) {
           
                return $this->output
                    ->set_content_type('application/json')
                    ->set_status_header(200)
                    ->set_output(json_encode(array(
                        'success' => true,
                        'message' => 'BLOG_EDITED_SUCCESSFULLY'
                    )));
            } else {
                return $this->output
                    ->set_content_type('application/json')
                    ->set_status_header(200)
                    ->set_output(json_encode(array(
                        'success' => false,
                        'message' => 'BLOG_NOT_EDITED_SUCCESSFULLY'
                    )));
            }
        }
    }
    
      public function delete_safari_blog($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'
                )));
        }

        $safari_blog = $this->db->get_where('blogs', array('id' => $id))->row();
        if ($safari_blog == null) {
            return $this->output
                ->set_content_type('application/json')
                ->set_status_header(200)
                ->set_output(json_encode(array(
                    'success' => false,
                    'message' => 'BLOG_NOT_FOUND'
                )));
        }
        // else delete
        $this->db->where('id', $id);
        if ($this->db->delete('blogs')) {
            // will work on this later
            // // delete / unlink the old safari_tour photos
            $image_location = 'blogs/' . explode('blogs/', $safari_blog->featured_image)[1];
            
            
            if (file_exists($image_location)) {
                // if it exists delete it
                $this->unlink_files($image_location);
            } 

            return $this->output
                ->set_content_type('application/json')
                ->set_status_header(200)
                ->set_output(json_encode(array(
                    'success' => true,
                    'message' => 'BLOG_DELETED'
                )));
        } else {
            return $this->output
                ->set_content_type('application/json')
                ->set_status_header(200)
                ->set_output(json_encode(array(
                    'success' => true,
                    'message' => 'BLOG_NOT_DELETED'
                )));
        }
    
    
    }
    
    public function add_safari_tour_day(){
        // add safari tour day
       

            $image_url =  $this->upload_image($_FILES["featuring_photo"]['name'], "featuring_photo","safari_tour_days");

            $data = array(
                "day" => $_POST['day'],
                'featuring_photo' => $image_url,
                "meals" => $_POST['meals'],
                "main_destination" => $_POST['main_destination'],
                'detailed_description' => $_POST['detailed_description'],
                'accomodation' => $_POST['accomodation'],
                'safari_tour_id' => $_POST['safari_tour_id'],
                'date_added' => date('Y-m-d\TH:i:s.u\Z')
            );

  
           
            if ($this->db->insert('safari_tour_days', $data)) {
                return $this->output
                    ->set_content_type('application/json')
                    ->set_status_header(200)
                    ->set_output(json_encode(array(
                        'success' => true,
                        'message' => 'safari_tour_day added successfully'
                    )));
            } else {
                return $this->output
                    ->set_content_type('application/json')
                    ->set_status_header(200)
                    ->set_output(json_encode(array(
                        'success' => false,
                        'message' => 'safari_tour_day not added successfully'
                    )));
            }
        
    }
     public function get_newsletter_subscribers(){
         $subs = $this->db->get_where("newsletter")->result_array();
          return $this->output
                    ->set_content_type('application/json')
                    ->set_status_header(200)
                    ->set_output(json_encode(array(
                        'success' => true,
                        'subs' => array_reverse($subs)
                    )));
     }
  
    // function to upload images
    public function upload_image($image_name, $field_name,$directory)
    {

        $config['upload_path'] = "./$directory/";
        $config['allowed_types'] = '*';
        $new_name = time() . '-' . $image_name;
        $config['file_name'] = $new_name;

        $this->load->library('upload', $config);
        $this->upload->initialize($config);

        if (!$this->upload->do_upload($field_name) && !empty($image_name)) {
            $error = array('error' => $this->upload->display_errors());
           
              if($error['error'] == "<p>The filetype you are attempting to upload is not allowed.</p>")
            {
                
                header('Content-type: application/json');
                http_response_code(200);
                echo json_encode(array(
                    'success' => false,
                    'message' => 'PLEASE_TRY_ANOTHER_IMAGE'
                ));
           
                die;
            }
        }


        $upload_data = $this->upload->data();

        $image_url = base_url() . "$directory/" . $upload_data['file_name'];
        return $image_url;
    }

    public function get_safari_tour($id)
    {

        $this->db->select('*');
        $this->db->where('id', $id);
        $safari_tour = $this->db->get('safari_tour_packages')->row();
        
          

        if (($safari_tour == null)) {
          
            return $this->output
                ->set_content_type('application/json')
                ->set_status_header(200)
                ->set_output(json_encode(array(
                    'success' => false,
                    'message' => 'SAFARI_TOUR_DOESNT_EXIST'
                )));
        }
        
        
          $this->db->select('id, day,accomodation,description,featuring_photo,meals,main_destination');
          $this->db->where('safari_tour_id', $id);
        
          
        
       $safari_tour_days = $this->db->get('safari_tour_days')->result_array();
       
       foreach ($safari_tour_days as &$day) {
    // Use preg_replace() to remove all white spaces from the 'day' value
    $day['day'] = preg_replace('/\s+/', '', $day['day']);
    }
    unset($day);

    // Define the custom comparison function for sorting by day
    // function compare_days($a, $b) {
    //     // Extract the numeric day values from the input arrays
    //     $a_day = explode('-', $a['day'])[0];
    //     $b_day = explode('-', $b['day'])[0];

    //     // Compare the day values numerically
    //     return intval($a_day) - intval($b_day);
    // }
    
//     function compare_days($a, $b) {
//     // Extract the first numeric value from the day string
//     preg_match('/\d+\s*,?\s*\d*/', $a['day'], $matches_a);
//     $a_day = str_replace(',', '', $matches_a[0]);
//     $a_day = intval($a_day);

//     preg_match('/\d+\s*,?\s*\d*/', $b['day'], $matches_b);
//     $b_day = str_replace(',', '', $matches_b[0]);
//     $b_day = intval($b_day);

//     // Compare the day values numerically
//     return $a_day - $b_day;
// }

//coool
// function compare_days($a, $b) {
//     // Split the day strings into arrays of individual days
//     $a_days = preg_split('/[,\s]/', $a['day']);
//     $b_days = preg_split('/[,\s]/', $b['day']);

//     // Convert the day strings to arrays of integers
//     $a_days_int = array_map(function($day) { 
//         return (int)$day; 
//     }, $a_days);
//     $b_days_int = array_map(function($day) { 
//         return (int)$day; 
//     }, $b_days);

//     // Find the minimum and maximum days for each tour
//     $a_min_day = min($a_days_int);
//     $a_max_day = max($a_days_int);
//     $b_min_day = min($b_days_int);
//     $b_max_day = max($b_days_int);

//     // Compare the minimum days of each tour
//     if ($a_min_day < $b_min_day) {
//         return -1;
//     } elseif ($a_min_day > $b_min_day) {
//         return 1;
//     }

//     // If the minimum days are equal, compare the maximum days
//     if ($a_max_day < $b_max_day) {
//         return -1;
//     } elseif ($a_max_day > $b_max_day) {
//         return 1;
//     }

//     // If both minimum and maximum days are equal, sort by ID
//     if ($a['id'] < $b['id']) {
//         return -1;
//     } elseif ($a['id'] > $b['id']) {
//         return 1;
//     }

//     // If everything is equal, return 0
//     return 0;
// } 


function compare_days($a, $b) {
    // Split the day strings into arrays of individual days
    $a_days = preg_split('/[,\\s-]/', $a['day']);
    $b_days = preg_split('/[,\\s-]/', $b['day']);

    // Convert the day strings to arrays of integers
    $a_days_int = array_map(function($day) { 
        return (int)$day; 
    }, $a_days);
    $b_days_int = array_map(function($day) { 
        return (int)$day; 
    }, $b_days);

    // Find the minimum and maximum days for each tour
    $a_min_day = min($a_days_int);
    $a_max_day = max($a_days_int);
    $b_min_day = min($b_days_int);
    $b_max_day = max($b_days_int);

    // Compare the minimum days of each tour
    if ($a_min_day < $b_min_day) {
        return -1;
    } elseif ($a_min_day > $b_min_day) {
        return 1;
    }

    // If the minimum days are equal, compare the maximum days
    if ($a_max_day < $b_max_day) {
        return -1;
    } elseif ($a_max_day > $b_max_day) {
        return 1;
    }

    // If both minimum and maximum days are equal, sort by ID
    if ($a['id'] < $b['id']) {
        return -1;
    } elseif ($a['id'] > $b['id']) {
        return 1;
    }

    // If everything is equal, return 0
    return 0;
}











    // Sort the safari tour days array by day using the custom comparison function
    usort($safari_tour_days, 'compare_days');

        
        // print_r($safari_tour_days);
        
          $safari_tour_activities = $this->db->get_where('day_activities',array("package_id"=>$id))->result_array();
          
           $this->db->select('number_of_travellers,rate_amount');
          $this->db->where('package_id', $id);
          $this->db->order_by('number_of_travellers','ASC');
          
            $safari_tour_rates = $this->db->get('safari_package_rates')->result_array();
          
       


        return $this->output
            ->set_content_type('application/json')
            ->set_status_header(200)
            ->set_output(json_encode(array(
                'success' => true,
                'safari_tour' => $safari_tour,
                "safari_tour_days"=> $safari_tour_days,
               'activities'=> $safari_tour_activities,
               'safari_rates'=>$safari_tour_rates
                

            )));
    }
    
    // Define a comparison function that compares two day strings and returns the sort order
public function compareDays($a, $b) {
    // Convert '1', '2', ... to integers for easy comparison
    $aDay = ($a['day'] != null) ? (int)$a['day'] : 0;
    $bDay = ($b['day'] != null) ? (int)$b['day'] : 0;

    // If the days are equal, return 0
    if ($aDay == $bDay) {
        return 0;
    }
    
    // If both days are numeric, compare them numerically
    if (is_numeric($aDay) && is_numeric($bDay)) {
        return ($aDay < $bDay) ? -1 : 1;
    }
    
    // Otherwise, if one or both days is not numeric, compare them as strings
    return strcmp($a['day'], $b['day']);
}

// Call usort() with the comparison function to sort the array by the 'day' field


    
    public function like_safari_tour()
    {
        // first check if that safari_tour ain't in the user liked safari_tours
        $json_data = json_decode(@file_get_contents('php://input'), true);
        $safari_tou_id =$json_data['safari_tou_id'];
        $user_id =$json_data['user_id'];
        $query = $this->db->get_where('user_liked_safari_tours', array('safari_tou_id' => $safari_tou_id, 'user_id' => $user_id))->row();
        if ($query == null) {

            $safari_tour = $this->db->get_where('safari_tours', array('id' => $safari_tou_id))->row();
           
            $this->db->where('id', $safari_tou_id);
            
            if ($this->db->update('safari_tours', array('likes' =>  $safari_tour->likes+1))) {
                // also add to user_liked safari_tours
                $data = array(
                    'safari_tou_id' => $safari_tou_id,
                    'user_id' => $user_id,
                    'time_added' => date('Y-m-d\TH:i:s.u\Z')

                );
                // now insert the safari_tour into the user liked safari_tours
                if ($this->db->insert('user_liked_safari_tours', $data)) {
                    // return true;
                    return $this->output
                    ->set_content_type('application/json')
                    ->set_status_header(200)
                    ->set_output(json_encode(array(
                        'success' => true,
                        'message'=>'safari_tour liked'
                        
                    )));
                } else {
                    return false;
                }
            } else {
                return false;
            }
        }else{
            // reduce the like
            $this->db->where('id',$query->id);
            if($this->db->delete('user_liked_safari_tours')){
                // reduce the like
                $safari_tour = $this->db->get_where('safari_tours', array('id' => $safari_tou_id))->row();
                 $this->db->where('id', $safari_tou_id);
            // reduce the like
            if($safari_tour->likes !=0){
            if ($this->db->update('safari_tours', array('likes' =>  $safari_tour->likes-1))) {
                return $this->output
            ->set_content_type('application/json')
            ->set_status_header(200)
            ->set_output(json_encode(array(
                'success' => true,
                'message' => 'safari_tour unliked'
            )));
            }
            }else {
                return false;
            }
        }
    }
    }

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

        return $this->output
            ->set_content_type('application/json')
            ->set_status_header(200)
            ->set_output(json_encode(array(
                'success' => true,
                'safari_tours' => $query->result_array()
            )));
    }
    
        public function get_safari_tours_2()
    {
        $this->db->select('id,image,title,safari_type,amount,destination_country,brief_description,status');
        $this->db->where('status','true');
        $this->db->from('safari_tour_packages');
        $query = $this->db->get();

        return $this->output
            ->set_content_type('application/json')
            ->set_status_header(200)
            ->set_output(json_encode(array(
                'success' => true,
                'safari_tours' => $query->result_array()
            )));
    }
    
        public function get_safari_tours_admin()
    {
        $this->db->select('id,image,title,safari_type,amount,destination_country,brief_description,status,isFeatured');
        $this->db->order_by('id','desc');
        $this->db->from('safari_tour_packages');
        $safari_tours = $this->db->get()->result_array();
        
        $prepared_tours =[];
        
        foreach($safari_tours as $safari_tour){
           $safari_type ='';
          if(is_int($safari_tour['safari_type'])){
            $safari_type = $this->db->get_where("safari_types",array("id"=>$safari_tour['safari_type']))->row()->title;
            }
            
            $each_safari = array(
                'id'=>$safari_tour['id'],
                'image'=>$safari_tour['image'],
                'title'=>$safari_tour['title'],
                'safari_type'=>$safari_type,
                'amount'=>$safari_tour['amount'],
                'brief_description'=>$safari_tour['brief_description'],
                'destination_country'=>$safari_tour['destination_country'],
                'status'=>($safari_tour['status']=='true'?true:false),
                'isFeatured'=>($safari_tour['isFeatured']=='true'?true:false),
                );
                
                array_push($prepared_tours,$each_safari);
        }

        return $this->output
            ->set_content_type('application/json')
            ->set_status_header(200)
            ->set_output(json_encode(array(
                'success' => true,
                'safari_tours' => $prepared_tours
            )));
    }
    
    
    
         public function get_featured_packages()
    {
        $this->db->select('id,image,title,safari_type,amount,destination_country,brief_description,status');
        $this->db->where('isFeatured','true');
         $this->db->where('status','true');
        // $this->db->limit(3);
        $this->db->from('safari_tour_packages');
        $featured_tours =  $query = $this->db->get()->result_array();
        
        // // first check if featured tours are less than six
        //  $prepared_featured_tours = [];
        
        // $featured_tours_difference =  6 - count($featured_tours);
        
       
        // if($featured_tours_difference !=0 ){
        
       
        
        // if($featured_tours_difference <6 && $featured_tours_difference !=0){
             
        
        //     array_push($prepared_featured_tours ,...$featured_tours);
            
            
        //     // get other safaris and concatenate them 
        //      $this->db->select('id,image,title,safari_type,amount,destination_country,brief_description,status');
        //       $this->db->where('status','true');
        //       $this->db->where('isFeatured!=','true');
             
        //      $this->db->limit($featured_tours_difference);
        //      $this->db->from('safari_tour_packages');
        //   $more_safaris = $this->db->get()->result_array();
        //   if(count($more_safaris)>0){
        //     array_push($prepared_featured_tours,...$more_safaris);
        //   }
        
        
        
        // }
        // }else{
        //     array_push($prepared_featured_tours,...$featured_tours);
        // }

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

    
         public function get_days($package_id)
    {
        $this->db->select('*');
        $this->db->where("safari_tour_id",$package_id);
        $this->db->from('safari_tour_days');
        $safari_tour_days = $this->db->get()->result_array();
        
          foreach ($safari_tour_days as &$day) {
    // Use preg_replace() to remove all white spaces from the 'day' value
    $day['day'] = preg_replace('/\s+/', '', $day['day']);
    }
    unset($day);
    
    

function compare_days($a, $b) {
    // Split the day strings into arrays of individual days
    $a_days = preg_split('/[,\\s-]/', $a['day']);
    $b_days = preg_split('/[,\\s-]/', $b['day']);

    // Convert the day strings to arrays of integers
    $a_days_int = array_map(function($day) { 
        return (int)$day; 
    }, $a_days);
    $b_days_int = array_map(function($day) { 
        return (int)$day; 
    }, $b_days);

    // Find the minimum and maximum days for each tour
    $a_min_day = min($a_days_int);
    $a_max_day = max($a_days_int);
    $b_min_day = min($b_days_int);
    $b_max_day = max($b_days_int);

    // Compare the minimum days of each tour
    if ($a_min_day < $b_min_day) {
        return -1;
    } elseif ($a_min_day > $b_min_day) {
        return 1;
    }

    // If the minimum days are equal, compare the maximum days
    if ($a_max_day < $b_max_day) {
        return -1;
    } elseif ($a_max_day > $b_max_day) {
        return 1;
    }

    // If both minimum and maximum days are equal, sort by ID
    if ($a['id'] < $b['id']) {
        return -1;
    } elseif ($a['id'] > $b['id']) {
        return 1;
    }

    // If everything is equal, return 0
    return 0;
}





    // Sort the safari tour days array by day using the custom comparison function
    usort($safari_tour_days, 'compare_days');


        return $this->output
            ->set_content_type('application/json')
            ->set_status_header(200)
            ->set_output(json_encode(array(
                'success' => true,
                'safari_tour_days' => $safari_tour_days
            )));
    }
    
    public function activities(){
         $this->db->select('*');
        $this->db->from('day_activities');
        $query = $this->db->get();

        return $this->output
            ->set_content_type('application/json')
            ->set_status_header(200)
            ->set_output(json_encode(array(
                'success' => true,
                'activities' => $query->result_array()
            )));
    }
    
    
    
      public function get_activities($package_id){
         $this->db->select('*');
          $this->db->where("package_id",$package_id);
        $this->db->from('day_activities');
        $query = $this->db->get();

        return $this->output
            ->set_content_type('application/json')
            ->set_status_header(200)
            ->set_output(json_encode(array(
                'success' => true,
                'activities' => $query->result_array()
            )));
    }
    
    
    public function get_user_liked_safari_tours($user){
        
        $this->db->where('user_liked_safari_tours.user_id', $user);
          $this->db->select('safari_tours.brief_description,safari_tours.heading,safari_tours.image,safari_tours.safari_tou_price');

        $this->db->order_by('user_liked_safari_tours.id', 'DESC'); // order safari_tours in DESC order by id
         $this->db->from('user_liked_safari_tours');
          $this->db->join('safari_tours', 'safari_tours.id = user_liked_safari_tours.safari_tou_id');
        $query = $this->db->get();
         return $this->output
            ->set_content_type('application/json')
            ->set_status_header(200)
            ->set_output(json_encode(array(
                'success' => true,
                'liked_safari_tours' => $query->result_array()
            )));
        
    }

    public function get_category_safari_tours($category)
    {
        $this->db->select('*');
        $this->db->where('category_id', $category);
        $this->db->from('safari_tour');
        $query = $this->db->get();

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

    public function edit_safari_tour($id)
    {

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

    

            $data = array(
                "heading" => $_POST['heading'],
                
                "amount_range" => $_POST['amount_range'],
                'brief_description' => $_POST['brief_description'],
                'safari_type' => $_POST['safari_type'],
                'overview_visit' => $_POST['overview_visit'],
                'destination_country' => $_POST['destination_country'],
                'tour_budget' => $_POST['tour_budget'],
            
            );


        // update the safari_tour image link of those files and field names passed accordingly
        if (!empty($_FILES['image']['name'])) {
            // assign the returned image url(s) after deleting the old image and add the value to the data array

            $data['image'] = $this->upload_edit_safari_tour($_FILES['image']['name'], $id, 'image');
        }

       

        // now update the data

        $this->db->set($data);
        $this->db->where('id', $id);
        if ($this->db->update('safari_tours', $data)) {

            return $this->output
                ->set_content_type('application/json')
                ->set_status_header(200)
                ->set_output(json_encode(array(
                    'success' => true,
                    'message' => 'safari_tour edited successfully'
                )));
        } else {
            return $this->output
                ->set_content_type('application/json')
                ->set_status_header(200)
                ->set_output(json_encode(array(
                    'success' => false,
                    'message' => 'something wrong happened'
                )));
        }
    }
    
    public function edit_safari_tour_2($id)
    {

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

    
    $data = array(
                "title" => $_POST['title'],
                
                "amount" => $_POST['amount'],
                'brief_description' => $_POST['brief_description'],
                'safari_type' => $_POST['safari_type'],
                'location' => $_POST['location'],
                'inclusions_and_exclusions' => $_POST['inclusions_and_exclusions'],
                'getting_there' => $_POST['getting_there'],
                'destination_country' => $_POST['destination_country'],
         
            );


        // update the safari_tour image link of those files and field names passed accordingly
        if (!empty($_FILES['image']['name'])) {
            // assign the returned image url(s) after deleting the old image and add the value to the data array

            $data['image'] = $this->upload_edit_safari_tour_2($_FILES['image']['name'], $id, 'image');
        }

       

        // now update the data

        // $this->db->set($data);
        $this->db->where('id', $id);
        if ($this->db->update('safari_tour_packages', $data)) {

            return $this->output
                ->set_content_type('application/json')
                ->set_status_header(200)
                ->set_output(json_encode(array(
                    'success' => true,
                    'message' => 'safari_tour edited successfully'
                )));
        } else {
            return $this->output
                ->set_content_type('application/json')
                ->set_status_header(200)
                ->set_output(json_encode(array(
                    'success' => false,
                    'message' => 'something wrong happened'
                )));
        }
    }
    
    public function delete_safari_tour_2($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'
                )));
        }

        $safari_tour = $this->db->get_where('safari_tour_packages', array('id' => $id))->row();
        if ($safari_tour == null) {
            return $this->output
                ->set_content_type('application/json')
                ->set_status_header(200)
                ->set_output(json_encode(array(
                    'success' => false,
                    'message' => 'safari_tour not found'
                )));
        }
        // else delete
        $this->db->where('id', $id);
        if ($this->db->delete('safari_tour_packages')) {
// will work on this later
            // // delete / unlink the old safari_tour photos
            // $image_location = 'safari_tours/' . explode('safari_tours/', $safari_tour->image)[1];
            // // $safari_tou_image_1 = 'safari_tours/' . explode('safari_tours/', $safari_tour->safari_tou_image_1)[1];
            // // $safari_tou_image_2 = 'safari_tours/' . explode('safari_tours/', $safari_tour->safari_tou_image_2)[1];
            // // $safari_tou_image_3 = 'safari_tours/' . explode('safari_tours/', $safari_tour->safari_tou_image_3)[1];

            // if (file_exists($image_location)) {
            //     // if it exists delete it
            //     $this->unlink_files($image_location);
            // } 

            return $this->output
                ->set_content_type('application/json')
                ->set_status_header(200)
                ->set_output(json_encode(array(
                    'success' => true,
                    'message' => 'safari_tour deleted successfully'
                )));
        } else {
            return $this->output
                ->set_content_type('application/json')
                ->set_status_header(200)
                ->set_output(json_encode(array(
                    'success' => true,
                    'message' => 'safari_tour not deleted'
                )));
        }
    }
    
    public function add_safari_package_rate(){
        
        $json_data = json_decode(@file_get_contents('php://input'), true);
        
        //first check for a package if it exists
        
         $safari_tour = $this->db->get_where('safari_tour_packages', array('id' => $json_data['package_id']))->row();
         
        if ($safari_tour == null) {
            return $this->output
                ->set_content_type('application/json')
                ->set_status_header(200)
                ->set_output(json_encode(array(
                    'success' => false,
                    'message' => "SAFARI_PACKAGE_NOT_FOUND"
                )));
        }
        
        // check if package_rate exists
        
        $package_rate = $this->db->get_where('safari_package_rates', array('package_id' => $json_data['package_id'],'number_of_travellers'=>$json_data['number_of_travellers']))->row();
         
        if ($package_rate != null) {
            return $this->output
                ->set_content_type('application/json')
                ->set_status_header(200)
                ->set_output(json_encode(array(
                    'success' => false,
                    'message' => "SAFARI_PACKAGE_RATE_EXISTS"
                )));
        }
        
        
        $data = array(
            "package_id"=>$json_data['package_id'],
            "number_of_travellers"=>$json_data['number_of_travellers'],
            "rate_amount"=>$json_data['rate_amount'],
            "date_added"=>date('Y-m-d\TH:i:s.u\Z')
            );
            
            if($this->db->insert('safari_package_rates',$data)){
                 return $this->output
                            ->set_content_type('application/json')
                            ->set_status_header(200)
                            ->set_output(json_encode(array(
                                'success' => true,
                                'message' => 'SAFARI_RATE_ADDED'
                            )));
            }
    }
    
     public function edit_safari_package_rate($rate_id){
        
        $json_data = json_decode(@file_get_contents('php://input'), true);
        
        
          
         //first check for a rate if it exists
        
         $safari_tour_rate = $this->db->get_where('safari_package_rates', array('id' => $rate_id))->row();
         
        if ($safari_tour_rate == null) {
            return $this->output
                ->set_content_type('application/json')
                ->set_status_header(200)
                ->set_output(json_encode(array(
                    'success' => false,
                    'message' => "SAFARI_RATE_NOT_FOUND"
                )));
        }
        
        
        //first check for a package if it exists
        
         $safari_tour = $this->db->get_where('safari_tour_packages', array('id' => $json_data['package_id']))->row();
         
        if ($safari_tour == null) {
            return $this->output
                ->set_content_type('application/json')
                ->set_status_header(200)
                ->set_output(json_encode(array(
                    'success' => false,
                    'message' => "SAFARI_PACKAGE_NOT_FOUND"
                )));
        }
        
        
        
        
        $data = array(
            "package_id"=>$json_data['package_id'],
            "number_of_travellers"=>$json_data['number_of_travellers'],
            "rate_amount"=>$json_data['rate_amount']
          
            );
            
            $this->db->where('id',$rate_id);
            if($this->db->update('safari_package_rates',$data)){
                 return $this->output
                            ->set_content_type('application/json')
                            ->set_status_header(200)
                            ->set_output(json_encode(array(
                                'success' => true,
                                'message' => 'SAFARI_RATE_EDITED_SUCCESSFULLY'
                            )));
            }
    }
    
    public function edit_safari_package_rates(){
        
        
        $json_data = json_decode(@file_get_contents('php://input'), true);
        $package_rates = $json_data['package_rates'];
        
        foreach($package_rates as $package_rate){
            // update each package rate
            
             $data = array(
            
            "number_of_travellers"=>$package_rate['number_of_travellers'],
            "rate_amount"=>$package_rate['rate_amount']
          
            );
            
            $this->db->where('id',$package_rate['id']);
            $this->db->update('safari_package_rates',$data);
        }
        
         return $this->output
                            ->set_content_type('application/json')
                            ->set_status_header(200)
                            ->set_output(json_encode(array(
                                'success' => true,
                                'message' => 'SAFARI_RATES_EDITED_SUCCESSFULLY'
                            )));
    
    }
    
    public function delete_safari_package_rate($rate_id){
        
         //first check for a rate if it exists
        
         $safari_tour_rate = $this->db->get_where('safari_package_rates', array('id' => $rate_id))->row();
         
        if ($safari_tour_rate == null) {
            return $this->output
                ->set_content_type('application/json')
                ->set_status_header(200)
                ->set_output(json_encode(array(
                    'success' => false,
                    'message' => "SAFARI_RATE_NOT_FOUND"
                )));
        }
        
         $this->db->where('id',$rate_id);
        if($this->db->delete('safari_package_rates')){
            return $this->output
                ->set_content_type('application/json')
                ->set_status_header(200)
                ->set_output(json_encode(array(
                    'success' => true,
                    'message' => "SAFARI_RATE_DELETED"
                )));
        }
        
    
    
}


public function get_package_rates($package_id){
     // get safari rates for each tour
            
              $this->db->select('id,number_of_travellers,rate_amount');
          $this->db->where('package_id', $package_id);
            $this->db->order_by('number_of_travellers','ASC');
            $safari_tour_rates = $this->db->get('safari_package_rates')->result_array();
            
             return $this->output
                            ->set_content_type('application/json')
                            ->set_status_header(200)
                            ->set_output(json_encode(array(
                                'success' => true,
                                'message'=>"RATES_FETCHED_SUCCESSFULLY",
                                'safari_tour_rates' =>$safari_tour_rates
                            )));
}



    // function to upload an edit safari_tour photo 

    public function upload_edit_safari_tour($file_name, $safari_tou_id, $field_name)
    {

        if (!empty($file_name)) {
            $config['upload_path'] = './safari_tours/';

            $config['allowed_types'] = '*';


            // $config['max_size'] = 0;    


            $new_name = date('Y-m-d\TH:i:s.u\Z') . '-' . $file_name;

            //Added time function in image name for no duplicate image 

            $config['file_name'] = $new_name;

            //Stored the new name into $config['file_name']

            $this->load->library('upload', $config);
            $this->upload->initialize($config);


            if (!$this->upload->do_upload($field_name) && !empty($file_name)) {
                $error = array('error' => $this->upload->display_errors());
                return $this->output
                    ->set_content_type('application/json')
                    ->set_status_header(200)
                    ->set_output(json_encode(
                        $error
                    ));
            } else {

                //got the safari_tour using its id
                $safari_tour = $this->db->get_where('safari_tours', array('id' => $safari_tou_id))->row();
                // exploded the url which is like "http://api-url/safari_tours/image.jpg"
                if ($safari_tour->$field_name != null) {
                    $old_image_file_name = explode("safari_tours/", $safari_tour->$field_name)[1];
                    $old_image_location = "safari_tours/" . $old_image_file_name;
                    // first check if the file 
                    if (file_exists($old_image_location)) {
                        // if it exists delete it
                        unlink($old_image_location);
                    } else {
                        return $this->output
                            ->set_content_type('application/json')
                            ->set_status_header(200)
                            ->set_output(json_encode(array(
                                'success' => false,
                                'message' => 'old image not found, failed to be deleted'
                            )));
                    }
                }

                $upload_data = $this->upload->data();

                $image_url = base_url() . "safari_tours/" . $upload_data['file_name'];
                return $image_url;
            }
        }
    }
    
      public function upload_edit_safari_tour_2($file_name, $safari_tou_id, $field_name)
    {

        if (!empty($file_name)) {
            $config['upload_path'] = './safari_tours/';

            $config['allowed_types'] = '*';


            // $config['max_size'] = 0;    


            $new_name = date('Y-m-d\TH:i:s.u\Z') . '-' . $file_name;

            //Added time function in image name for no duplicate image 

            $config['file_name'] = $new_name;

            //Stored the new name into $config['file_name']

            $this->load->library('upload', $config);
            $this->upload->initialize($config);


            if (!$this->upload->do_upload($field_name) && !empty($file_name)) {
                $error = array('error' => $this->upload->display_errors());
                return $this->output
                    ->set_content_type('application/json')
                    ->set_status_header(200)
                    ->set_output(json_encode(
                        $error
                    ));
            } else {

                //got the safari_tour using its id
                $safari_tour = $this->db->get_where('safari_tour_packages', array('id' => $safari_tou_id))->row();
                // exploded the url which is like "http://api-url/safari_tours/image.jpg"
                if ($safari_tour->$field_name != null) {
                  
                    $old_image_file_name = explode("safari_tours/", $safari_tour->$field_name)[1];
                    $old_image_location = "safari_tours/" . $old_image_file_name;
                    
                     
                    // first check if the file 
                    if (file_exists($old_image_location)) {
                        // if it exists delete it
                        unlink($old_image_location);
                    } 
                    
                  
                }

                $upload_data = $this->upload->data();

                $image_url = base_url() . "safari_tours/" . $upload_data['file_name'];
                return $image_url;
            }
        }
    }

   public function upload_edit_experience($file_name, $safari_tou_id, $field_name)
    {

        if (!empty($file_name)) {
            $config['upload_path'] = './experiences/';

            $config['allowed_types'] = '*';


            // $config['max_size'] = 0;    


            $new_name = date('Y-m-d\TH:i:s.u\Z') . '-' . $file_name;

            //Added time function in image name for no duplicate image 

            $config['file_name'] = $new_name;

            //Stored the new name into $config['file_name']

            $this->load->library('upload', $config);
            $this->upload->initialize($config);


            if (!$this->upload->do_upload($field_name) && !empty($file_name)) {
                $error = array('error' => $this->upload->display_errors());
                return $this->output
                    ->set_content_type('application/json')
                    ->set_status_header(200)
                    ->set_output(json_encode(
                        $error
                    ));
            } else {

                //got the safari_tour using its id
                $safari_tour = $this->db->get_where('experiences', array('id' => $safari_tou_id))->row();
                // exploded the url which is like "http://api-url/safari_tours/image.jpg"
                if ($safari_tour->$field_name != null) {
                  
                    $old_image_file_name = explode("experiences/", $safari_tour->$field_name)[1];
                    $old_image_location = "experiences/" . $old_image_file_name;
                    
                     
                    // first check if the file 
                    if (file_exists($old_image_location)) {
                        // if it exists delete it
                        unlink($old_image_location);
                    } 
                    
                  
                }

                $upload_data = $this->upload->data();

                $image_url = base_url() . "experiences/" . $upload_data['file_name'];
                return $image_url;
            }
        }
    }


    public function upload_edit_destination($file_name, $safari_tou_id, $field_name)
    {

        if (!empty($file_name)) {
            $config['upload_path'] = './destinations/';

            $config['allowed_types'] = '*';


            // $config['max_size'] = 0;    


            $new_name = date('Y-m-d\TH:i:s.u\Z') . '-' . $file_name;

            //Added time function in image name for no duplicate image 

            $config['file_name'] = $new_name;

            //Stored the new name into $config['file_name']

            $this->load->library('upload', $config);
            $this->upload->initialize($config);


            if (!$this->upload->do_upload($field_name) && !empty($file_name)) {
                $error = array('error' => $this->upload->display_errors());
                return $this->output
                    ->set_content_type('application/json')
                    ->set_status_header(200)
                    ->set_output(json_encode(
                        $error
                    ));
            } else {

                //got the safari_tour using its id
                $safari_tour = $this->db->get_where('destinations', array('id' => $safari_tou_id))->row();
                // exploded the url which is like "http://api-url/safari_tours/image.jpg"
                if ($safari_tour->$field_name != null) {
                    $old_image_file_name = explode("destinations/", $safari_tour->$field_name)[1];
                    $old_image_location = "destinations/" . $old_image_file_name;
                    // first check if the file 
                    if (file_exists($old_image_location)) {
                        // if it exists delete it
                        unlink($old_image_location);
                    } else {
                        return $this->output
                            ->set_content_type('application/json')
                            ->set_status_header(200)
                            ->set_output(json_encode(array(
                                'success' => false,
                                'message' => 'old image not found, failed to be deleted'
                            )));
                    }
                }

                $upload_data = $this->upload->data();

                $image_url = base_url() . "destinations/" . $upload_data['file_name'];
                return $image_url;
            }
        }
    }

    public function upload_edit_sub_destination($file_name, $safari_tou_id, $field_name)
    {

        if (!empty($file_name)) {
            $config['upload_path'] = './sub_destinations/';

            $config['allowed_types'] = '*';


            // $config['max_size'] = 0;    


            $new_name = date('Y-m-d\TH:i:s.u\Z') . '-' . $file_name;

            //Added time function in image name for no duplicate image 

            $config['file_name'] = $new_name;

            //Stored the new name into $config['file_name']

            $this->load->library('upload', $config);
            $this->upload->initialize($config);


            if (!$this->upload->do_upload($field_name) && !empty($file_name)) {
                $error = array('error' => $this->upload->display_errors());
                return $this->output
                    ->set_content_type('application/json')
                    ->set_status_header(200)
                    ->set_output(json_encode(
                        $error
                    ));
            } else {

                //got the safari_tour using its id
                $safari_tour = $this->db->get_where('sub_destinations', array('id' => $safari_tou_id))->row();
                // exploded the url which is like "http://api-url/safari_tours/image.jpg"
                if ($safari_tour->$field_name != null) {
                    $old_image_file_name = explode("sub_destinations/", $safari_tour->$field_name)[1];
                    $old_image_location = "sub_destinations/" . $old_image_file_name;
                    // first check if the file 
                    if (file_exists($old_image_location)) {
                        // if it exists delete it
                        unlink($old_image_location);
                    } else {
                        return $this->output
                            ->set_content_type('application/json')
                            ->set_status_header(200)
                            ->set_output(json_encode(array(
                                'success' => false,
                                'message' => 'old image not found, failed to be deleted'
                            )));
                    }
                }

                $upload_data = $this->upload->data();

                $image_url = base_url() . "sub_destinations/" . $upload_data['file_name'];
                return $image_url;
            }
        }
    }
    public function delete_safari_tour($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'
                )));
        }

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

            // delete / unlink the old safari_tour photos
            $image_location = 'safari_tours/' . explode('safari_tours/', $safari_tour->image)[1];
            $safari_tou_image_1 = 'safari_tours/' . explode('safari_tours/', $safari_tour->safari_tou_image_1)[1];
            $safari_tou_image_2 = 'safari_tours/' . explode('safari_tours/', $safari_tour->safari_tou_image_2)[1];
            $safari_tou_image_3 = 'safari_tours/' . explode('safari_tours/', $safari_tour->safari_tou_image_3)[1];

            if (file_exists($image_location)) {
                // if it exists delete it
                $this->unlink_files($image_location);
            }  if (file_exists($safari_tou_image_1)) {
                $this->unlink_files($safari_tou_image_1);
            }  if (file_exists($safari_tou_image_2)) {
                $this->unlink_files($safari_tou_image_2);
            }  if (file_exists($safari_tou_image_3)) {
                $this->unlink_files($safari_tou_image_3);
            }

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

    public function unlink_files($location)
    {
        return  unlink($location);
    }
}
