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

class Game extends CI_Controller {

	function __construct() 
	{
        error_reporting(0);
        parent::__construct();
        $this->load->model('CommonModel');
        $this->load->helper('Security');
        $this->load->library('form_validation');

       
    }

	public function index()
	{ 
		if(isAdmin())
		{ 
			$data['recordSet']=$this->db->get('sports')->result_array();
			$data['content'] = 'Game/Listing';
			$this->load->view('layout_home', $data);
		}
	}

	public function add()
	{
		if(isAdmin())
		{
			//$data['recordSet']=$this->db->get('gallery')->result_array();
			// $data['category']=$this->db->get('doctor_category')->result_array();
			$data['content'] = 'Game/Add';
			$this->load->view('layout_home', $data);
		}
	}
	public function edit($g_id)
	{
		if(isAdmin())
		{
			$data['gallery'] = $this->db->where('id', $g_id)->get('sports')->result_array();
            $data['content']  ="Game/Edit";
            $this->load->view('layout_home',$data);
		}
	}
	public function save()
	{
		if(isAdmin())
		{
			$input_data = $this->input->post();
            $input_data = $this->security->xss_clean($input_data);
            
			/////// Image Upload ///////////////////////
        
        		$_FILES['userfile']['name']     = $_FILES['image']['name'];
		        $_FILES['userfile']['type']     = $_FILES['image']['type'];
		        $_FILES['userfile']['tmp_name'] = $_FILES['image']['tmp_name'];
		        $_FILES['userfile']['error']    = $_FILES['image']['error'];
		        $_FILES['userfile']['size']     = $_FILES['image']['size'];
				$config = array('upload_path' => "./../uploads/game/",
								'allowed_types' => "gif|jpg|png|jpeg",
								'overwrite' => TRUE,
								'encrypt_name' => TRUE
								);
				$this->load->library('upload', $config);
				$this->upload->initialize($config);
				if($this->upload->do_upload())
				{
					$data = array('upload_data' => $this->upload->data());
					$file_name=$data['upload_data']['file_name'];
					$insert_data['image'] = $file_name;
					$config["source_image"] = './../uploads/game/'.$file_name;
					$config['new_image'] = './../uploads/game/thumbnail/';
					$config['image_library']  = 'gd2';
					$config['create_thumb'] =  FALSE;
					$config['overwrite'] = TRUE;
					$config['maintain_ratio'] = FALSE;
					$config["width"] = 210;
					$config["height"] = 110;
					$this->load->library('image_lib', $config);
					$this->image_lib->initialize($config);
					$this->image_lib->resize();
					$this->image_lib->clear();
				}
				else
				{
					$data['er']=$this->upload->display_errors();
					print_r($data['er']);
				}
        	
            	$insert_data['name'] = $input_data['name'];
	            $this->db->insert('sports',$insert_data);
            echo 'Success';
        }
		
	}

	public function Edit_data()
	{
	    if(isAdmin())
	    {  
        	$input_data = $this->input->post();
            $input_data = $this->security->xss_clean($input_data);

            
			/////// Image Upload ///////////////////////
            	if (empty($_FILES['image']['name']))
	        	{
	        		$insert_data['image'] = $input_data['old_image'];
	        	}
	        	else
	        	{
        		$_FILES['userfile']['name']     = $_FILES['image']['name'];
		        $_FILES['userfile']['type']     = $_FILES['image']['type'];
		        $_FILES['userfile']['tmp_name'] = $_FILES['image']['tmp_name'];
		        $_FILES['userfile']['error']    = $_FILES['image']['error'];
		        $_FILES['userfile']['size']     = $_FILES['image']['size'];
				$config = array('upload_path' => "./../uploads/game/",
								'allowed_types' => "gif|jpg|png|jpeg",
								'overwrite' => TRUE,
								'encrypt_name' => TRUE
								);
				$this->load->library('upload', $config);
				$this->upload->initialize($config);
				if($this->upload->do_upload())
				{
					

					$data = array('upload_data' => $this->upload->data());
					$file_name=$data['upload_data']['file_name'];
					$insert_data['image'] = $file_name;
					$config["source_image"] = './../uploads/game/'.$file_name;
					$config['new_image'] = './../uploads/game/thumbnail/';
					$config['image_library']  = 'gd2';
					$config['create_thumb'] =  FALSE;
					$config['overwrite'] = TRUE;
					$config['maintain_ratio'] = FALSE;
					$config["width"] = 323;
					$config["height"] = 416;
					

					$this->load->library('image_lib', $config);
					$this->image_lib->initialize($config);
					$this->image_lib->resize();
					$this->image_lib->clear();




				}
				}
            	$insert_data['name'] = $input_data['name'];
            	$this->db->where('id',$input_data['id']);
	            $this->db->update('sports',$insert_data);
	            echo 'success';
	    }      
	}
	public function status_change()
	{
	    if(isAdmin())
	    {  
	        	$input_data = $this->input->post();
	            $input_data = $this->security->xss_clean($input_data);
	        	$edit_data['status'] = $input_data['new_status'];
	        	
	            $this->db->where('prd_id',$input_data['id']);
	            $this->db->update('product',$edit_data);
	            echo 'success';
	    }      
	}

	public function Delete()
    {
        if(isAdmin())
        {
        	$input_data = $this->input->post();
        	$this->db->where('id', $input_data['id']);
			$this->db->delete('sports');
             // / ---------------------------Insert Audit trail--------------------
            $audit_trail_table='audit_trail';
	        $audit_trail_array=array(
	                                    'user_name'=>$this->session->userdata('admin_name'),
	                                    'action'=>'Action -> "Delete Game"',
	                                    'action_time'=>date('Y-m-d H:i:s')
	                                );
        	$this->CommonModel->insert($audit_trail_table,$audit_trail_array);
        }
    }


	public function review_delete()
    {
        if(isAdmin())
        {
        	$input_data = $this->input->post();
        	$this->db->where('rev_id', $input_data['id']);
			$this->db->delete('review');
        }
    }

    public function export()
	{
		if(isAdmin())
		{ 
			$project_array=$this->db->select('d.prd_id,d.vt_id,d.project_id,d.d_code,d.d_name,d.working_project,d.d_pass,d.date,vt.vehicle_type, p.project_code, p.project_name')->from('product as d')->join('project as p', 'd.project_id = p.project_id', 'LEFT')->join('vehicle_type as vt', 'd.vt_id = vt.vt_id', 'LEFT')->order_by('prd_id','desc')->get()->result_array();
			header("Content-type: application/csv");
		    header("Content-Disposition: attachment; filename=\"product-".date('d-m-Y').".csv\"");
		    header("Pragma: no-cache");
		    header("Expires: 0");

		     $handle = fopen('php://output', 'w');
			$excel_heding=array();
			array_push($excel_heding, 'SL', 'SAP ID', 'Product Name', 'Working Project', 'Product Paassword', 'Vehicle Type', 'Status','Date');
			fputcsv($handle, $excel_heding);
			foreach ($project_array as $key => $project_data) {
			$export_req = array();
			if ($project_data['d_status']=='1') {
			$status='Active';
			}
			else
			{
			$status='Inactive';
			}
			$registered_data=date('Y-m-d', strtotime($project_data['date']));
			array_push($export_req, $key+1, $project_data['d_code'], $project_data['d_name'], $project_data['project_code'], $project_data['d_pass'], $project_data['vehicle_type'], $status, $registered_data);

			//print_r($call_det);die;
			fputcsv($handle, $export_req);
			} 
			 fclose($handle);
		     exit;
		
		}
	}

	public function import(){

        $data = array();

        $memData = array();

                $insertCount = $updateCount = $rowCount = $notAddCount = 0;

                if(is_uploaded_file($_FILES['import_data']['tmp_name'])){

                    $this->load->library('CSVReader');

                    $csvData = $this->csvreader->parse_csv($_FILES['import_data']['tmp_name']);
                    
                    if(!empty($csvData)){

                        foreach($csvData as $row){ 
                        	$rowCount++;
                        	$duplicate_data=$this->db->where('pin', $row['Project Code'])->get('active_pin')->result_array();
                        	// echo '<pre>';
                        	// print_r($row);
				            if(empty($duplicate_data))
				            {
							$insert_data['pin'] = $row['Project Code'];
							$insert_data['delevery_charge'] = $row['Delivery Charge'];
							
							$this->db->insert('active_pin',$insert_data);
							}

	
                          $insertCount++;  

                        }
                        $notAddCount = ($rowCount - ($insertCount + $updateCount));
                    }
                }

    }

}