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

class Contact extends CI_Controller {
	

	private $cart_qty   = '' ;
	private $logged 	= 0  ;	
	private $user 		= '' ;
	

	public function __construct()
	{
		parent::__construct();
		$this->load->model('Db_model');
		$this->load->library(array('cart','form_validation','encrypt'));
		
		$this->load->helper('basic_helper');
		
		$this->load->library('user_loginfunctions');
		
		
				$ic=0;
		foreach ($this->cart->contents() as $items):
		$ic++;
		endforeach;
		$this->cart_qty = ''.$ic.'';
		//$this->cart_qty = ''.$this->cart->total_items().'';
		//$this->cart_qty = ''.$this->cart->total_items().'';
		
		if($this->session->userdata('shop_user2'))
		{
			$this->load->library('encrypt') ;
			$this->user = $this->encrypt->decode($this->session->userdata('shop_user2'));
			$this->logged = 1;
		}
		
		
		if($_COOKIE['username_m'] !='' && $_COOKIE['password_m']!='' )
		{	
		if($this->user_loginfunctions->mobile_log($_COOKIE['username_m'],$_COOKIE['password_m']))
		{
			// echo $_COOKIE['username_m'];
			$this->load->library('encrypt') ;
			$this->user = $_COOKIE['username_m'];
			$this->logged = 1;
		}	
		}
	
		
		
	}
	
	public function index()
	{
		$this->home() ;
	}
	
	public function view()
	{
		$data['result'] = array() ;
		$data['cart_qty'] = $this->cart_qty;
		$data['email'] = $this->user;
		$data['logged'] = $this->logged;
		
		//$data['result'] = $this->Db_model->get_where_by_order('shop_pages',array('type'=>'about','active'=>'Y'),'order','desc') ;
		
		
				$data['category']=$data['result'] = $this->Db_model->get_where_by_order('shop_category',array(),'order','asc') ;	
		$data['gift'] = $this->Db_model->get_where_by_order('shop_category',array(),'order','asc') ;
		$data['allproduct']= $this->Db_model->get_where_by_order('shop_product',array('active'=>'Y'),'order','asc') ;
		$data['service'] = $this->Db_model->get_where_by_order('shop_pages',array('active'=>'Y','type'=>'service'),'order','asc') ;
		//$books = $this->Db_model->get_where_by_order('shop_product',array('offer'=>1,'active'=>'Y'),'order','asc') ;
	
		$product = $this->Db_model->get_all_by_order('shop_product','order','asc') ;
		if($product!==FALSE)
		{
			$data['product'] = $product;
		}
		$data['contactus'] = $this->Db_model->get_where_by_order('shop_contact',array('status'=>'1'),'id','desc') ;
		$data['site'] = $this->Db_model->get_all('site',array()) ;
		$data['category'] = $this->Db_model->get_all_by_order('shop_category','order','asc') ;
		
		if($books!==FALSE)
		{
			$data['offer'] = $books;
		}
		else
		{
			$data['offer'] = array() ;
		}
		
		$this->_contact_form($data);
		
	}
	
	
	
	
	
	
	// login page
	public function home()
	{
		
		$a = rand(1,9);
		$b = rand(1,9);
		$capimage = '<b>'.$a.'&nbsp; + &nbsp;'.$b.'&nbsp; = &nbsp;</b>' ;
		$cap_result = $a + $b ;
		
		$data = array(
				'cart_qty'=>$this->cart_qty,	  
				'user' => $this->user,
				'email' => $this->user,
				'logged' => $this->logged,
				'capimage' => $capimage,
				'c_val' => $this->encrypt->encode($cap_result),
				'suc_msg' => '',
				'result' => '',
				'offer' => '',
				'error' => ''
				);
		
		
		/*$data['cart_qty'] = $this->cart_qty;
		$data['email'] = $this->user;
		$data['logged'] = $this->logged;
		$data['error'] = '';
		$data['suc_msg'] = '';
		$data['capimage'] = $capimage ;
		$data['c_val'] = $this->encrypt->encode($cap_result);
		*/
		$data['result'] = array() ;
		$data['offer'] = array() ;
		//$data['result'] = $this->Db_model->get_where_by_order('shop_pages',array('type'=>'about','active'=>'Y'),'order','desc') ;
		
		$data['offer']= $this->Db_model->get_all_by_order('shop_category','order','asc') ;
		$data['contactus']= $this->Db_model->get_all_by_order('shop_contact','order','asc') ;
		//$data['category'] = $this->Db_model->get_all_by_order('shop_category','order','asc') ;
		
		$data['service'] = $this->Db_model->get_where_by_order('shop_pages',array('active'=>'Y','type'=>'service'),'order','asc') ;
		$data['category']=$data['result'] = $this->Db_model->get_where_by_order('shop_category',array(),'order','asc') ;	
		$data['gift'] = $this->Db_model->get_where_by_order('shop_category',array(),'order','asc') ;
		//check if submit button clicked
		if($this->input->post('submit'))
		{
	
			$this->form_validation->set_rules('name', 'Name', 'trim|required');
			//$this->form_validation->set_rules('subject', 'Subject', 'trim');
			//$this->form_validation->set_rules('email', 'Email', 'trim');
			//$this->form_validation->set_rules('tel', 'Telephone Number', 'trim');
			//$this->form_validation->set_rules('comments', 'Comment', 'trim');
			//$this->form_validation->set_rules('captext', 'Security Text', 'trim|callback_captcha_check');
			
			//if there are form validation errors
			if ($this->form_validation->run() === FALSE)
			{
				//$this->_contact_form($data);
			}
			
			else
			{
				//send email
				
			
		echo "send email";		
				
				
		$this->load->config('email');
        $this->load->library('email');
        
        $from = $this->config->item('smtp_user');
        $to = $this->input->post('to');
        $subject = $this->input->post('subject');
        $message = $this->input->post('message');

        $this->email->set_newline("\r\n");
       $this->email->from('info@keralapropertyfinder.com', 'Kerala Property Finder');
		//$this->email->fromname('Asian');
        $this->email->to('majojohny13@gmail.com');
				
				
				
				
				
				
				
				
				$this->email->subject($this->input->post('subject'));
				
				
				$msg='
				<div style="width:630px; padding:10px ; background-color:#E0E0E0;">
				
				<table width="630"  border="0" cellpadding="0" cellspacing="0" align="center" bgcolor="#FBFBFB" 
				style="padding:20px;font-family:Arial, Helvetica, sans-serif; color:#2C2C2C; font-size:14px;">
				
				<tr><td colspan="2"><font style="font-size:18px;">'.$this->input->post('subject').'</font></td></tr>
				<tr height="20"><td colspan="2"></td></tr>
				<tr><td width="30%" height="30">Name : </td><td width="70%">'.$this->input->post('name').'</td></tr>
				<tr><td height="30">Phone : </td><td>'.$this->input->post('tel').'</td></tr>
				<tr><td height="30">Email id : </td><td>'.$this->input->post('email').'</td></tr>
				<tr><td height="30">Enquiry Details : </td><td>'.$this->input->post('comments').'</td></tr>
				<tr height="40"></tr>
				<tr><td colspan="2">
				
				<a href="">
				<img src=""  border="0" width="300" height="60" alt="logo"/>
				</a>
			
				<div style="font-family:Arial, Helvetica, sans-serif; font-size:10px; color:#575757;">
				<p></p>
				<p></p>
				<p></p>
				</div>
				
				</td></tr>
				</table>
				
				</div>    
				';
				
				$this->email->message($msg);
				
				//email not sent
				if(! $this->email->send())
				{
					$data['error'] = 'Problem on sending mail , Try again.';
					
					$this->_contact_form($data);
				}
				else
				{
					$data['suc_msg'] = 'Mail has been sent successfully.' ;
					
					$this->_contact_form($data);	
					
					$this->output->set_header('refresh:5;url="https://www.keralapropertyfinder.com"');  
				}
			}
		
		}//if($this->input->post('login'))
		
		else
		{
			/**
			*
			*	show login form at first time page loads,
			*	ie, before clicking submit button
			*
			*/
			
			$this->_contact_form($data);
		}
			

		
	}
	

	
	private function _contact_form($data=array())
	{
		$this->load->view('website/header',$data);
	
		$this->load->view('website/menu',$data);
		$this->load->view('website/contact',$data);
		$this->load->view('website/footer');
	}
	
	
	public function captcha_check($captext)
	{
		if( $captext==$this->encrypt->decode($this->input->post('c_val')) )
		{
			return true;
		}
		else
		{
			$this->form_validation->set_message('captcha_check','Capcha Error') ;
			return false;
		}
		
	}

	
}
