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

class Jobs extends CI_Controller
{
	public function __construct()
	{ 
		parent:: __construct();
		$this->load->model('common_model');
		$this->load->library('Mobile_detect');
		$this->load->library('Utility');
	}
	public function index()
	{
		$langId = 2;
		if(get_cookie('lang'))
		{
			$langId = get_cookie('lang');
		}

		if($this->input->post())
		{
			$captcha_response = $this->utility->info_cleanQuery(trim($this->input->post('g-recaptcha-response')));
			if($captcha_response != '')
			{
				$keySecret = RECAPTCHA_SECRET_KEY;
				$check = array(
					'secret'		=>	$keySecret,
					'response'		=>	$captcha_response
				);
				$startProcess = curl_init();
				curl_setopt($startProcess, CURLOPT_URL, "https://www.google.com/recaptcha/api/siteverify");
				curl_setopt($startProcess, CURLOPT_POST, true);
				curl_setopt($startProcess, CURLOPT_POSTFIELDS, http_build_query($check));
				curl_setopt($startProcess, CURLOPT_SSL_VERIFYPEER, false);
				curl_setopt($startProcess, CURLOPT_RETURNTRANSFER, true);
				$receiveData = curl_exec($startProcess);
				$finalResponse = json_decode($receiveData, true);
				if($finalResponse['success'])
				{
					$interest = implode('<br/>', $this->input->post('Interests'));
					$surname = $this->input->post('surname');
					$name = $this->input->post('name');
					$idNumber = $this->input->post('idNumber');
					$birthday = date('d-m-Y',strtotime($this->input->post('birthday')));
					$address = $this->input->post('address');
					$nearestAirport = $this->input->post('nearestAirport');
					$phone = $this->input->post('phone');
					$eMail = $this->input->post('eMail');
					$driving_licence = ($this->input->post('driving_licence')==1)?'Yes':'No';
					$note = $this->input->post('note');
					/*if($_FILES['pics']['name']!='')
					{
						$uploadpath = 'newsletter_image/';
						$config = array(
				            'upload_path' 		=> FCPATH . $uploadpath . '/',
				            'allowed_types' 	=> 'jpg|JPG|png|PNG|jpeg|JPEG',
				            'encrypt_name' 		=> FALSE,
				            'file_ext_tolower' 	=> true,
				            'filename'			=> $_FILES['pics']['tmp_name'],
				        );

				        $this->load->library('upload', $config);
				        $this->upload->initialize($config);
				        if(!$this->upload->do_upload('pics')) {

				            $this->utility->setMsg($this->upload->display_errors(),'ERROR');	
							redirect(base_url().'jobs/');
				        }
				        $uploaddata = $this->upload->data();
				        $filename = $uploaddata['raw_name'] .'.'. substr($uploaddata['file_ext'], 1);
				        $resize = $this->utility->pic_resize($uploadpath . '/'.$uploaddata['raw_name'] .'.'. substr($uploaddata['file_ext'], 1),$uploadpath . '/'.$uploaddata['raw_name'] .'.'. substr($uploaddata['file_ext'], 1),500,500);
				    } */  

				    $dataInfo = [];
				    $files = $_FILES;
				    $cpt = count($_FILES['pics']['name']);
				    if($_FILES['pics']['name'][0]!=''){
				    	for($i=0; $i<$cpt; $i++)
					    {    
					    	//pr($_FILES);       
					        $_FILES['pics']['name']= $files['pics']['name'][$i];
					        $_FILES['pics']['type']= $files['pics']['type'][$i];
					        $_FILES['pics']['tmp_name']= $files['pics']['tmp_name'][$i];
					        $_FILES['pics']['error']= $files['pics']['error'][$i];
					        $_FILES['pics']['size']= $files['pics']['size'][$i];    

					        $uploadpath = 'newsletter_image/';
							$config = array(
					            'upload_path' 		=> FCPATH . $uploadpath . '/',
					            'allowed_types' 	=> 'jpg|JPG|png|PNG|jpeg|JPEG',
					            'encrypt_name' 		=> FALSE,
					            'file_ext_tolower' 	=> true,
					            'filename'			=> $_FILES['pics']['tmp_name'],
					        );

					        $this->load->library('upload', $config);
					        $this->upload->initialize($config);
					        if(!$this->upload->do_upload('pics')) {

					            $this->utility->setMsg($this->upload->display_errors(),'ERROR');
					        }
					        $uploaddata = $this->upload->data();
					        $resize = $this->utility->pic_resize($uploadpath . '/'.$uploaddata['raw_name'] .'.'. substr($uploaddata['file_ext'], 1),$uploadpath . '/'.$uploaddata['raw_name'] .'.'. substr($uploaddata['file_ext'], 1),500,500);
					        $dataInfo[] = $this->upload->data();
					    } 
				    }
				    
				    //pr($_FILES['pics']['name']);
					/********Admin mail***************/
					//$to = 'sayan@digitalaptech.com';
					$to = 'jobs@castleevents.com';
					//$to = 'jobs@anexus.ch';
					//$to = 'info@castleevents.com';
					$sub = 'JOB APPLICATION';
					$body= '<p>The below person applied for a job at castleevents.com</p>';
					
					$body.= '<p style="margin:0; line-height: 20px;">Surname: '.$surname.'</p>';
					$body.= '<p style="margin:0; line-height: 20px;">Name: '.$name.'</p>';
					$body.= '<p style="margin:0; line-height: 20px;">ID number: '.$idNumber.'</p>';
					$body.= '<p style="margin:0; line-height: 20px;">Birthday: '.$birthday.'</p>';
					$body.= '<p style="margin:0; line-height: 20px;">Adress: '.$address.'</p>';
					$body.= '<p style="margin:0; line-height: 20px;">Nearest airport: '.$nearestAirport.'</p>';
					$body.= '<p style="margin:0; line-height: 20px;">Driving licence : '.$driving_licence.'</p>';
					$body.= '<p style="margin:0; line-height: 20px;">Email: '.$eMail.'</p>';
					$body.= '<p style="margin:0; line-height: 20px;">Telefone: '.$phone.'</p>';
					
					$body.= '<p style="margin:0; line-height: 20px;">CHECKBOXES chosed:</p>';
					$body.= '<p style="margin:0; line-height: 20px;">'.$interest.'</p>';
					$body.= '<p style="margin:0; line-height: 20px;">Free Text: '.$note.'</p>';
					//echo count($dataInfo);
					if(count($dataInfo)>0)
					{
						$body.= '<p>Image: ';
						foreach ($dataInfo as $key => $value) {
							$body.= '<img src="'.base_url().$uploadpath.'/'.$value['file_name'].'" style="width: 200px; margin: 10px;">';
						}
						$body.= '</p>';
					}
					
					$bodys=' <table cellspacing="0" cellpadding="0" border="0" align="center" bgcolor="#fff" width="100%" style="max-width: 600px;">
		                <tr>
		                   <td>
		                      <table cellspacing="0" cellpadding="0" border="0" width="100%">
		                         <tr>
		                            <td style="font-family: sans-serif; color: #000; padding: 0px 40px 40px; font-size: 16px; line-height: 30px;">
		                               '.$body.'
		                            </td>
		                         </tr>
		                      </table>
		                   </td>
		                </tr>
		            </table>';//echo $bodys;die;
		            $this->utility->sendCIMail($to,$sub,$bodys,'','',$eMail);
		            
					/********Admin mail***************/



					$to = $eMail;
					$sub = getLang('JOB_APPLICATION');
					$body= '<p>'.getLang('Thank_you_very_much_for_your_interest_in_working').'</p>';
					$body.= '<p>'.getLang('We_will_review_your_details_and_get_back').'<p>';
					$body.= '<p>'.getLang('Looking_forward_to_maybe_welcome').'<p>';
					$bodys_user='<table cellspacing="0" cellpadding="0" border="0" align="center" bgcolor="#22222c" width="100%" style="max-width: 600px;">
		                <tr>
		                   	<td>
		                      	<table cellspacing="0" cellpadding="0" border="0" width="100%">
		                         	<tr>
			                            <td align="center" style="padding: 40px; font-family: sans-serif; font-size: 15px; mso-height-rule: exactly; line-height: 20px; color: #b3975b;">
			                               <h2>'.getLang('Hello').' <span style="text-transform:capitalize">'.$surname.' '.$name.'</span> </h2>
			                            </td>
		                         	</tr>
		                         	<tr>
			                            <td style="font-family: sans-serif; color: #fff; padding: 0px 40px 40px; font-size: 16px; line-height: 30px;">
			                               '.$body.'
			                            </td>
		                         	</tr>
		                         	<tr>
			                         	<td align="center" style="font-family: sans-serif; color: rgb(179, 151, 91); padding: 20px 40px; border-top: 1px solid rgb(79, 79, 79); background: rgb(19, 18, 24) none repeat scroll 0px 0px;">
			                         		'.getLang('Best_wishes').' <br>
			                         		<p style="margin:5px 0 0"><a href="'.base_url().'" style="text-decoration:none; color: #b3975b;">'.getLang('Your_Castleeventscom_Team').'</a></p> 
			                         	</td>
		                         	</tr>
		                      	</table>
		                   	</td>
		                </tr>
		            	</table>';
		            $this->utility->sendCIMail($eMail,$sub,$bodys_user);
					/**********************MAIL**************************/
					$this->session->set_userdata('setMSG',1);
					redirect(base_url().'job');
				} else {
					$this->session->set_userdata('errMSG',getLang('Please_complete_CAPTCHA_authentication'));
					redirect(base_url().'job');
				}
			} else {
				$this->session->set_userdata('errMSG',getLang('Please_complete_CAPTCHA_authentication'));
				redirect(base_url().'job');
			}
		}

		$content_data['loginDetails'] = $this->common_model->getAll('usr_users',array('id'=>$this->session->userdata('user_id'))) ;
		$data['upcomingEvents'] = $this->common_model->getAllMultipleJoin('evt_events',array('time_from > '=>time(),'sorting'=>1,'is_banner'=>1,'visible'=>1,'lang_id'=>$langId),array(array('evt_event_files','evt_event_files.event_id = evt_events.id'),array('evt_language_values','evt_events.id = evt_language_values.event_id')),'time_from ASC','evt_events.*,evt_event_files.*,evt_language_values.title as title','id',0,6) ;
		$data['lastEvents'] = $this->common_model->getAllMultipleJoin('evt_events',array('time_from < '=>time(),'sorting'=>1,'is_banner'=>1,'visible'=>1,'lang_id'=>$langId),array(array('evt_event_files','evt_event_files.event_id = evt_events.id'),array('evt_language_values','evt_events.id = evt_language_values.event_id')),'time_from DESC','evt_events.*,evt_event_files.*,evt_language_values.title as title','id',0,6) ;
		if(empty($data['upcomingEvents']))
		{
			$data['upcomingEvents'] = $this->common_model->getAllMultipleJoin('evt_events',array('time_from > '=>time(),'sorting'=>1,'is_banner'=>1,'visible'=>1,'lang_id'=>$langId),array(array('evt_event_files','evt_event_files.event_id = evt_events.id'),array('evt_language_values','evt_events.id = evt_language_values.event_id')),'time_from ASC','evt_events.*,evt_event_files.*,evt_language_values.title as title','id',0,6) ;
		}
		if(empty($data['lastEvents']))
		{
			$data['lastEvents'] = $this->common_model->getAllMultipleJoin('evt_events',array('time_from < '=>time(),'sorting'=>1,'is_banner'=>1,'visible'=>1,'lang_id'=>$langId),array(array('evt_event_files','evt_event_files.event_id = evt_events.id'),array('evt_language_values','evt_events.id = evt_language_values.event_id')),'time_from DESC','evt_events.*,evt_event_files.*,evt_language_values.title as title','id',0,6) ;
		}
		$data['checkinEvents'] = array();
		if($this->session->userdata('user_id'))
		{
			$data['checkinEvents'] = $this->common_model->getAllMultipleJoin('shop_orders',array('shop_orders.payed'=>1,'shop_carts.user_id'=>$this->session->userdata('user_id'),'shop_orders.checkin_payed'=>NULL,'evt_language_values.lang_id'=>$langId),array(array('shop_carts','shop_carts.id = shop_orders.cart_id'),array('shop_booking_details','shop_booking_details.cart_id = shop_carts.id'),array('evt_language_values','evt_language_values.event_id = shop_booking_details.event_id'),array('evt_events','evt_language_values.event_id = evt_events.id')),'','shop_orders.created,shop_orders.cart_id,evt_language_values.title,evt_events.alias','shop_booking_details.cart_id') ;
			$data['userDetials'] = $this->common_model->getAllRowMultipleJoin('feu_userdetails',array('feu_userdetails.user_id'=>$this->session->userdata('user_id')),array(array('sys_files','sys_files.id = feu_userdetails.avatar_file_id'))) ;
			$data['unfinishedPayment'] = $this->common_model->getAllMultipleJoin('shop_booking_details',array('is_booking'=>1,'payed'=>0,'shop_carts.user_id'=>$this->session->userdata('user_id')),array(array('shop_carts','shop_carts.id = shop_booking_details.cart_id'),array('shop_orders','shop_orders.cart_id = shop_carts.id')),'shop_orders.created','','shop_booking_details.cart_id');
		}
		$data['languages'] = $content_data['languages'] = $this->common_model->getAll('sys_languages',array('visible'=>1)) ;
		$data['cms_pages_1'] = $this->common_model->getAllMultipleJoin('cms_page',array('section_id'=>1,'language_id'=>$langId),array(array('sys_files','sys_files.id = cms_page.image_id')),'cms_page.id ASC','*','');
		$data['cms_pages_2'] = $this->common_model->getAllMultipleJoin('cms_page',array('section_id'=>2,'language_id'=>$langId),array(array('sys_files','sys_files.id = cms_page.image_id')),'cms_page.id ASC','*','');
		$data['cms_pages_3'] = $this->common_model->getAllMultipleJoin('cms_page',array('section_id'=>3,'language_id'=>$langId),array(array('sys_files','sys_files.id = cms_page.image_id')),'cms_page.id ASC','*','');
		$data['cms_pages_4'] = $this->common_model->getAllMultipleJoin('cms_page',array('section_id'=>4,'language_id'=>$langId),array(array('sys_files','sys_files.id = cms_page.image_id')),'cms_page.id ASC','*','');
		$data['device'] = ($this->mobile_detect->isMobile() ? ($this->mobile_detect->isTablet() ? 'tablet' : 'phone') : 'computer');
		$data['title'] = "Castleevents | ".getLang('Jobs');
		$data['content'] = $this->load->view('jobs', $content_data, TRUE);
		$this->load->view('template', $data);
	}
	
}
