<?php
defined('BASEPATH') or exit('No direct script access allowed');
class Contact 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('Content-Type: application/json');
        header('Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE');
        $method = $_SERVER['REQUEST_METHOD'];
        if ($method == "OPTIONS") {
            die();
        }

        $this->load->helper('form');
        $this->load->helper(['jwt', 'authorization']);
       
    }

    

  
    

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

            $user_info = array(

                'name' => $name,
                'email' => $email,
              
                'message' => $json_data['message'],
                'address' => $json_data['address'],
                'date_created'=>date('Y-m-d'),
                'phone_number' => $json_data['phone_number']
            );
            
            if ($this->db->insert('contact_us', $user_info)) {
                
                return $this->output
                        ->set_content_type('application/json')
                        ->set_status_header(200)
                        ->set_output(json_encode(array(

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


public function add_news_letter()
    {
        
        $json_data = json_decode(@file_get_contents('php://input'), true);
        
            $name =  $json_data['full_name'];
         
            $email = $json_data['email'];
            
            $check_email = $this->db->get_where("newsletter",array("email"=>$email))->row();


if($check_email !=null){
       return $this->output
                        ->set_content_type('application/json')
                        ->set_status_header(200)
                        ->set_output(json_encode(array(

                            'success' => false,
                            'message' => 'ALREADY_SUBSCRIBED',
                         )));
}
            $user_info = array(

                'full_name' => $name,
                'email' => $email,
               'date_created'=>date('Y-m-d')
                
            );
            
            if ($this->db->insert('newsletter', $user_info)) {
                
                
                 
   
    



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

<head>
    
    <title>Welcome to the Ihamba Newsletter</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 $name,</h1>
                                       <p style='color: grey;'>
                                        Welcome to our monthly newsletter! We are thrilled to have you as a part of our tour company's community and look forward to sharing our latest safari news, travel tips, and exclusive promotions with you.
</p>
<p style='color: grey;'>
As fellow adventurers, we understand the importance of planning your dream safari vacation, and our newsletter is designed to make that process as easy and enjoyable as possible. Each month, we'll be sharing insider tips from our experienced guides, updates on new safari destinations and experiences, and inspiring stories from fellow travelers.
</p>
<p style='color: grey;'>
Rest assured that we will never overload your inbox with countless emails. Our newsletter is sent out just once a month, allowing you to stay up-to-date on all the latest safari news without any unnecessary clutter. Plus, as a valued subscriber, you'll be the first to know about our exclusive promotions and discounts.
</p>

 

<p style='color: grey;'>

Thank you for joining our community, and we can't wait to help you plan your next unforgettable safari adventure.
</p>

<p style='color: grey;'>
Best regards,
</p>

<p style='color: #1a2a5e; font-size:1.2em'>
Joseph Byamukama
</p>
<p style='margin-top:-15px'>
      <img src='https://api.ihambaadventures.com/destinations/jbi.jpg' alt='Signture.' width='20%' style='display: block; border-radius:5px' />
</p>
<p style='color: grey;'><b>CEO - Ihamba Adventures</b></p>
<p style='color: grey; font-size:11px'>Plot 600 Zika Close Kisubi, Entebbe ,Uganda</p>
<p style='color: grey; font-size:11px'>For more information, please visit our website at <a href='https://www.ihambaadventures.com'>www.ihambaadventures.com<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 = 'Welcome To our Newsletter';
$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' => "ADDED_SUCCESSFULLY"
            )));
}
            }
                
                
                
                        
           
    }

public function get_all_contact(){

    $this->db->select('id,name,company,email,phone_number,message,date_created');
        $this->db->from('contact_us');
        $query = $this->db->get();

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

public function get_one_contact_us($id){

    $this->db->where('id', $id);
    $this->db->select('id,name,company,email,phone_number,message,date_created');

        $this->db->from('contact_us');
        $query = $this->db->get();

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

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

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

}
