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

class Shop 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['shop'] = $this->common_model->getAllMultipleJoin('shop',array('lang_id'=>2,'is_default'=>1),array(array('shop_images','shop.id = shop_images.shop_id'),array('shop_lang_values','shop.id = shop_lang_values.shop_id')),'display_order ASC','*,shop.id as shop_id');
		//echo '<pre>';print_r($content_data);die;
		$data['content'] = $this->load->view('admin/shop/list', $content_data, TRUE);
		$this->load->view('admin/layout', $data);
	}
	public function add(){
		$content_data = array();
		$total_article = $this->common_model->getAll('shop');
		if($this->input->post()){
			$Details_on = array(
            	'title' => $this->input->post('title_2'),
            	'alias'=>$this->utility->slug($this->input->post('title_2')),
            	'status' => 1,
            	'display_order' => count($total_article)+1,
				'link'=> $this->input->post('link'),
				'price'=> $this->input->post('price'),
            );
            $shopId = $this->common_model->insertData('shop',$Details_on);
            $languages = $this->common_model->getAll('sys_languages');
			$arr= array();
			if(!empty($languages))
			{
				foreach ($languages as $value) {
					$arr1['shop_id'] = $shopId;
					$arr1['title'] = $this->input->post('title_'.$value['id']);
					$arr1['description'] = $this->input->post('desc_'.$value['id']);
					$arr1['meta_title'] = $this->input->post('meta_title_'.$value['id']);
					$arr1['meta_desc'] = $this->input->post('meta_description_'.$value['id']);
					$arr1['lang_id'] = $value['id'];
					$this->common_model->insertData('shop_lang_values',$arr1);
				}
			}
			//pr($_FILES);
			$cpt = count($_FILES['userfile']['name']);
			$files = $_FILES;
		    for($i=0; $i<$cpt; $i++)
		    {           
		        $_FILES['userfile']['name']= $files['userfile']['name'][$i];
		        $_FILES['userfile']['type']= $files['userfile']['type'][$i];
		        $_FILES['userfile']['tmp_name']= $files['userfile']['tmp_name'][$i];
		        $_FILES['userfile']['error']= $files['userfile']['error'][$i];
		        $_FILES['userfile']['size']= $files['userfile']['size'][$i];    

		        $imagefolder = 'img/shop/';
		        $uploadpath = 'assets/userfiles/'. $imagefolder;
				$config = array(
		            'upload_path' 		=> FCPATH . $uploadpath . '/',
		            'allowed_types' 	=> 'jpg|png|jpeg',
		            '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('userfile')) {

		            $this->utility->setMsg($this->upload->display_errors(),'ERROR');	
					redirect(base_url().'admin/shop/add/');
		        }
		        $uploaddata = $this->upload->data();
		        $file = array(
		        			'shop_id'=>$shopId,
		        			'filename'=> $uploaddata['raw_name'].'.'.substr($uploaddata['file_ext'], 1),
		        			'is_default'=> ($i==0)?1:0
		        	);
				$ImgId = $this->common_model->insertData('shop_images',$file);
		    }
			
            $this->utility->setMsg('Shop article details added','SUCCESS');	
			redirect(base_url().'admin/shop');
		}
		$content_data['language'] = $this->common_model->getAll('sys_languages');
		//echo '<pre>';print_r($content_data);die;
		$data['content'] = $this->load->view('admin/shop/add', $content_data, TRUE);
		$this->load->view('admin/layout', $data);
	}
	public function edit($edit_id=''){
		if($edit_id!=''){
			$shopId = base64_decode($edit_id);
			$content_data = array();
			if($this->input->post()){
				$Details_on = array(
	            	'title' => $this->input->post('title_2'),
	            	'alias'=>$this->utility->slug($this->input->post('title_2')),
	            	'link'=> $this->input->post('link'),
	            	'price'=> $this->input->post('price'),
	            );
	            $this->common_model->updateData('shop',$Details_on,array('id'=>$shopId));
	            $languages = $this->common_model->getAll('sys_languages');
				$arr= array();
				if(!empty($languages))
				{
					foreach ($languages as $value) {
						$arr1['title'] = $this->input->post('title_'.$value['id']);
						$arr1['description'] = $this->input->post('desc_'.$value['id']);
						$arr1['meta_title'] = $this->input->post('meta_title_'.$value['id']);
						$arr1['meta_desc'] = $this->input->post('meta_description_'.$value['id']);
						$this->common_model->updateData('shop_lang_values',$arr1,array('shop_id'=>$shopId,'lang_id'=>$value['id']));
						//echo $this->db->last_query();die;
					}
				}
				//pr($_FILES);
				$gallery_details = $this->common_model->getAllRow('shop_images',array('shop_id'=>$shopId)) ;
				$cpt = count($_FILES['userfile']['name']);
				$files = $_FILES;
			    for($i=0; $i<$cpt; $i++)
			    {           
			        if($files['userfile']['name'][$i]!='')
			        {
			        	$_FILES['userfile']['name']= $files['userfile']['name'][$i];
				        $_FILES['userfile']['type']= $files['userfile']['type'][$i];
				        $_FILES['userfile']['tmp_name']= $files['userfile']['tmp_name'][$i];
				        $_FILES['userfile']['error']= $files['userfile']['error'][$i];
				        $_FILES['userfile']['size']= $files['userfile']['size'][$i];    

				        $imagefolder = 'img/shop/';
				        $uploadpath = 'assets/userfiles/'. $imagefolder;
						$config = array(
				            'upload_path' 		=> FCPATH . $uploadpath . '/',
				            'allowed_types' 	=> 'jpg|png|jpeg',
				            '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('userfile')) {

				            $this->utility->setMsg($this->upload->display_errors(),'ERROR');	
							redirect(base_url().'admin/shop/edit/'.$edit_id);
				        }
				        $uploaddata = $this->upload->data();
				        $file = array(
				        			'shop_id'=>$shopId,
				        			'filename'=> $uploaddata['raw_name'].'.'.substr($uploaddata['file_ext'], 1),
				        			'is_default'=> ($i==0 && count($gallery_details)==0)?1:0
				        	);
						$ImgId = $this->common_model->insertData('shop_images',$file);
			        }   
			    }
	            $this->utility->setMsg('Shop article details updated','SUCCESS');	
				redirect(base_url().'admin/shop');
			}
			$content_data['shopDetails'] = $this->common_model->getAllRowMultipleJoin('shop',array('shop.id'=>$shopId),array(array('shop_lang_values','shop.id = shop_lang_values.shop_id')),'display_order DESC','*,shop.id as shop_id');
			$content_data['shopImages'] = $this->common_model->getAllMultipleJoin('shop_images',array('shop_id'=>$shopId),'','','*, shop_images.id as imgId');
			$content_data['language'] = $this->common_model->getAll('sys_languages');
			//echo '<pre>';print_r($content_data);die;
			$data['content'] = $this->load->view('admin/shop/edit', $content_data, TRUE);
			$this->load->view('admin/layout', $data);
		}else{
			//echo "Edit Failed";die;
			$this->utility->setMsg('Invalid id','ERROR');	
			redirect(base_url().'admin/shop/'.$edit_id);
		}
	}
	public function status($shop_id,$updated_status){
		$shop_id = base64_decode($shop_id);
		$update_status = array(
			'status' => $updated_status
		);
		$this->common_model->UpdateData('shop',$update_status,array('id'=>$shop_id));
		$this->utility->setMsg('Status updated','SUCCESS');	
		redirect(base_url().'admin/shop');
	}
	public function delete($shop_id='')
	{
		if($shop_id!='')
		{
			$shop_id=base64_decode($shop_id);
			$this->common_model->deleteData('shop',array('id'=>$shop_id));
			$this->common_model->deleteData('shop_lang_values',array('shop_id'=>$shop_id));
			$this->common_model->deleteData('shop_images',array('shop_id'=>$shop_id));
			$this->utility->setMsg('Shop article deleted','SUCCESS');	
			redirect(base_url().'admin/shop');
		}
		else
		{
			$this->utility->setMsg('Invalid id','ERROR');	
			redirect(base_url().'admin/shop');
		}
	}
	public function display_order($display_order,$type,$shop_id){
		if($type == 1)
		{
			$new = $display_order-1;
		}
		else
		{
			$new = $display_order+1;
		}
		$update_status = array(
				'display_order' => $new,
			);
		$get = $this->common_model->getAllRow('shop',array('display_order'=>$new));
		$this->common_model->UpdateData('shop',array('display_order'=>$display_order),array('id'=>$get['id']));
		$this->common_model->UpdateData('shop',$update_status,array('id'=>$shop_id));
		$this->utility->setMsg('Display order updated','SUCCESS');	
		redirect(base_url().'admin/shop');
	}
	public function imgDelete($img_id='',$uri='')
	{
		if($img_id!='')
		{
			$img_id=base64_decode($img_id);
			$gallery_details = $this->common_model->getAllRow('shop_images',array('id'=>$img_id)) ;
			$main_image_path = 'assets/userfiles/img/shop/'.$gallery_details['filename'];
			if(file_exists($main_image_path)){
				unlink($main_image_path);
			}
			$this->common_model->deleteData('shop_images',array('id'=>$img_id));
			$this->utility->setMsg('Shop image deleted','SUCCESS');	
			redirect(base_url().str_replace('-', '/', $uri));
		}
		else
		{
			$this->utility->setMsg('Invalid id','ERROR');	
			redirect(base_url().'admin/shop');
		}
	}
}