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

class Checkin extends CI_Controller {
	public function __construct(){
		parent::__construct();
		$this->load->model('common_model');
		if(get_cookie('admin_id'))
		{
			$this->session->set_userdata('admin_id',get_cookie('admin_id'));
		}
		if(!$this->session->userdata('admin_id'))
		{
			redirect('admin/login');
		}
	}
	public function index()
	{
		$content_data = array();
		$data['loginDetails'] = $this->common_model->getAllRow('feu_userdetails',array('id'=>$this->session->userdata('admin_id')),array('usr_users','feu_userdetails.user_id = usr_users.id')) ;
		$content_data['checkin_steps'] = $this->common_model->getAllMultipleJoin('checkin_steps','',array(array('sys_files','sys_files.id = checkin_steps.image_id')),'checkin_steps.id DESC','checkin_steps.*,sys_files.path,sys_files.filename,sys_files.extension');
		//print_r($content_data['checkin_steps']);
		$data['content'] = $this->load->view('admin/checkin_steps/list', $content_data, TRUE);
		$this->load->view('admin/layout', $data);
	}
	public function checkin_steps_add()
	{
		if($this->input->post())
		{
			$arr['name'] = $this->input->post('details_name_2');
			$arr['created'] = time();
			//$arr['code'] = str_replace(' ','_',strtolower($this->input->post('details_name_2')));
			$arr['code'] = 'additional_step';
			$checkin_stepsId = $this->common_model->insertData('checkin_steps',$arr);
			//echo $this->db->last_query();die;
			$languages = $this->common_model->getAll('sys_languages');
			$arr= array();
			if(!empty($languages))
			{
				foreach ($languages as $value) {
					$arr['checkin_step_id'] = $checkin_stepsId;
					$arr['lang_id'] = $value['id'];
					$arr['details_name'] = $this->input->post('details_name_'.$value['id']);
					$arr['details_desc'] = $this->input->post('details_desc_'.$value['id']);
					$this->common_model->insertData('checkin_steps_details',$arr);
				}
			}
			if($_FILES['image']['name']!='')
			{
				$imagefolder = 'img/checkin_steps/';
		        $uploadpath = 'assets/userfiles/'. $imagefolder;
				$config = array(
		            'upload_path' 		=> FCPATH . $uploadpath . '/',
		            'allowed_types' 	=> '*',
		            'encrypt_name' 		=> true,
		            'file_ext_tolower' 	=> true,
		            //'max_size' 			=> (int)MSysSettings::getValue('upload_max_filesize') * 1024,
		        );

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

		            $this->utility->setMsg($this->upload->display_errors(),'ERROR');	
					redirect(base_url().'admin/checkin/checkin_steps_add/');
		        }

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

		        $file = array(
		            'user_id' 	=> 1,
		            'path' 		=> $imagefolder,
		            'filename' 	=> $uploaddata['raw_name'],
		            'extension' => substr($uploaddata['file_ext'], 1),
		            'mimetype' 	=> $uploaddata['file_type'],
		            'is_image' 	=> $uploaddata['is_image'],
		            //'attr_alt' 	=> 'Bild von ' . $username,
		        );

		        $imagesize = getimagesize($uploaddata['full_path']);

		        $file['width'] 	= $imagesize[0];
		        $file['height'] = $imagesize[1];

		        $bannerId = $this->common_model->insertData('sys_files',$file);
		        $this->common_model->UpdateData('checkin_steps',array('image_id'=>$bannerId),array('id'=>$checkin_stepsId));
			
			}
			redirect(base_url().'admin/checkin/');
		}
			
		$content_data = array();
		$data['loginDetails'] = $this->common_model->getAllRow('feu_userdetails',array('id'=>$this->session->userdata('admin_id')),array('usr_users','feu_userdetails.user_id = usr_users.id')) ;
		$content_data['languages'] = $this->common_model->getAll('sys_languages') ;
		$data['content'] = $this->load->view('admin/checkin_steps/checkin_steps_add', $content_data, TRUE);
		$this->load->view('admin/layout', $data);
	}
	public function checkin_steps_delete($enc_stepID = '')
	{
		if($enc_stepID!='')
		{
			$checkin_stepId=base64_decode($enc_stepID);
			$this->common_model->deleteData('checkin_steps',array('id'=>$checkin_stepId));
			$this->common_model->deleteData('checkin_steps_details',array('checkin_step_id'=>$checkin_stepId));
			$this->utility->setMsg('Entry deleted','SUCCESS');	
			redirect(base_url().'admin/checkin');
		}
		else
		{
			$this->utility->setMsg('Invalid id','ERROR');	
			redirect(base_url().'admin/checkin');
		}
	}
	
	public function checkin_steps_edit($enc_stepID = '')
	{//pr($this->input->post());
		$id = base64_decode($enc_stepID);
		if($this->input->post())
		{
			$arr['name'] = $this->input->post('details_name_2');
			$entryId = $this->common_model->UpdateData('checkin_steps',$arr,array('id'=>$id));

			$languages = $this->common_model->getAll('sys_languages');
			$arr= array();
			if(!empty($languages))
			{
				foreach ($languages as $value) {
					$arr['details_name'] = $this->input->post('details_name_'.$value['id']);
					$arr['details_desc'] = $this->input->post('details_desc_'.$value['id']);
					$exist = $this->common_model->getAllRow('checkin_steps_details',array('checkin_step_id'=>$id,'lang_id'=>$value['id']));
					if(empty($exist))
					{
						$arr1['checkin_step_id'] = $id;
						$arr1['lang_id'] = $value['id'];
						$arr['details_name'] = $this->input->post('details_name_'.$value['id']);
						$arr['details_desc'] = $this->input->post('details_desc_'.$value['id']);
						$this->common_model->insertData('checkin_steps_details',$arr1);
					}
					else
					{
						$this->common_model->UpdateData('checkin_steps_details',$arr,array('checkin_step_id'=>$id,'lang_id'=>$value['id']));
					}
					
				}
			}
			if($this->input->post('price')){
				$price = $this->input->post('price');
				if(!empty($price))
				{
					$this->common_model->deleteData('checkin_steps_event',array('checkin_step_id'=>$id));
					foreach ($price as $pkey=>$pvalue) {
						$arr1['checkin_step_id'] = $id;
						$arr1['event_id'] = $this->input->post('event')[$pkey];
						$arr1['price'] = $pvalue;
						$arr1['status'] = isset($this->input->post('status')[$pkey]) ? 1 : 0;
						//print_r($arr1);
						$this->common_model->insertData('checkin_steps_event',$arr1);

					}
				}
			}//die;
			
			if($_FILES['image']['name']!='')
			{
				$imagefolder = 'img/checkin_steps/';
		        $uploadpath = 'assets/userfiles/'. $imagefolder;
				$config = array(
		            'upload_path' 		=> FCPATH . $uploadpath . '/',
		            'allowed_types' 	=> '*',
		            'encrypt_name' 		=> true,
		            'file_ext_tolower' 	=> true,
		            //'max_size' 			=> (int)MSysSettings::getValue('upload_max_filesize') * 1024,
		        );

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

		            $this->utility->setMsg($this->upload->display_errors(),'ERROR');	
					redirect(base_url().'admin/checkin/checkin_steps_edit/'.$enc_stepID);
		        }

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

		        $file = array(
		            'user_id' 	=> 1,
		            'path' 		=> $imagefolder,
		            'filename' 	=> $uploaddata['raw_name'],
		            'extension' => substr($uploaddata['file_ext'], 1),
		            'mimetype' 	=> $uploaddata['file_type'],
		            'is_image' 	=> $uploaddata['is_image'],
		            //'attr_alt' 	=> 'Bild von ' . $username,
		        );

		        $imagesize = getimagesize($uploaddata['full_path']);

		        $file['width'] 	= $imagesize[0];
		        $file['height'] = $imagesize[1];

		        $bannerId = $this->common_model->insertData('sys_files',$file);
		        $this->common_model->UpdateData('checkin_steps',array('image_id'=>$bannerId),array('id'=>$id));
			
			}
			redirect(base_url().'admin/checkin/checkin_steps_edit/'.$enc_stepID);
		}
			
		$content_data = array();
		$data['loginDetails'] = $this->common_model->getAllRow('feu_userdetails',array('id'=>$this->session->userdata('admin_id')),array('usr_users','feu_userdetails.user_id = usr_users.id')) ;
		$content_data['languages'] = $this->common_model->getAll('sys_languages') ;
		$content_data['checkin_stepsDetails'] = $this->common_model->getAllRowMultipleJoin('checkin_steps',array('checkin_steps.id'=>$id),array(array('sys_files','sys_files.id = checkin_steps.image_id')),'checkin_steps.created DESC','checkin_steps.*,sys_files.path,sys_files.filename,sys_files.extension');

		$content_data['checkin_events'] = $this->common_model->getAll('checkin_steps_event',array('checkin_step_id'=>$content_data['checkin_stepsDetails']['id']),'','id ASC');

		$content_data['events'] = $this->common_model->getAll('evt_events',array('time_from >'=>time()));
		//print_r($content_data['entryDetails']); die;
		$data['content'] = $this->load->view('admin/checkin_steps/checkin_steps_edit', $content_data, TRUE);
		$this->load->view('admin/layout', $data);
	}
}