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

class End_Category extends CI_Controller {
	// Session Authoriesed
	public function __construct() {
		parent::__construct();
		
		$admin = $this->session->userdata('admin');
		$column['id'] = $admin['id'];
		$table = 'tbl_user';
		
	    // loading common model
	    $this->load->model('Common_model');
		$result = $this->Common_model->Result($column,$table);
		if($result['password'] == $admin['password']){
		    $login = true;
		} else {
		    $login = false;
		}
		
		if (empty($admin) || $login==false) {
			$this->session->set_flashdata('msg','Your Session Has Been Expired');
			redirect(base_url().'master/login/index');
		}
		
        // loading common model
        $this->load->model('Common_model');
        $this->load->helper('common_helper');
	}

	public function index()
	{
        // we are setting data for get site data
		$column['id'] = '1';
		$table = 'tbl_settings';
		
        // we are getting results from common modal
		$result = $this->Common_model->Result($column,$table);
		
        // we are setting result in data array as $data
		$data['data'] = $result;
		$data['page'] = 'End Category';
		
		
		
		$table1 = 'tbl_top_category';
        // we are getting results from common modal
		$result1 = $this->Common_model->Results($table1);
		
		$data['top_category'] = $result1;
		
		
		$table = 'tbl_mid_category';
        // we are getting results from common modal
		$result = $this->Common_model->Results($table);
		$data['mid_category'] = $result;
		
		$table2 = 'tbl_end_category';
        // we are getting results from common modal
		$result2 = $this->Common_model->Results($table2);
		$data['end_category'] = $result2;
		
        // we are loading admin dashboard
		$this->load->view('master/end_category/list',$data);	
	}


	public function edit($id)
	{
        // we are setting data for get site data
		$column['id'] = '1';
		$table = 'tbl_settings';
		
        // we are getting results from common modal
		$result = $this->Common_model->Result($column,$table);
		
        // we are setting result in data array as $data
		$data['data'] = $result;
		$data['page'] = 'End Category';
		
		
		$table1 = 'tbl_top_category';
        // we are getting results from common modal
		$result1 = $this->Common_model->Results($table1);
		
		$data['top_category'] = $result1;
		
		$table2 = 'tbl_end_category';
		$column2['ecat_id'] = $id;
        // we are getting results from common modal
		$result2 = $this->Common_model->Result($column2,$table2);
		$data['end_category'] = $result2;
		
		
		$table3 = 'tbl_mid_category';
		$column3['mcat_id'] = $result2['mcat_id'];
        // we are getting results from common modal
		$result3 = $this->Common_model->Result($column3,$table3);
		$data['mid_category'] = $result3;
		
		
        // we are loading admin dashboard
		$this->load->view('master/end_category/edit',$data);	
	}

	public function create()
	{
        // we are setting data for get site data
		$column['id'] = '1';
		$table = 'tbl_settings';
		
        // we are getting results from common modal
		$result = $this->Common_model->Result($column,$table);
		
        // we are setting result in data array as $data
		$data['data'] = $result;
		$data['page'] = 'End Category';
		
		
		$table1 = 'tbl_top_category';
        // we are getting results from common modal
		$result1 = $this->Common_model->Results($table1);
		
		$data['top_category'] = $result1;
		
		
        // we are getting results from common modal
		$result = $this->Common_model->Results($table);
		
		$data['end_category'] = $result;
		
        // we are loading admin dashboard
		$this->load->view('master/end_category/create',$data);	
	}
	
	public function edit_content($id){
	    
        foreach ($_POST as $key => $value) {
            if($key == 'table'){$table = $value;}
            if($key == 'page'){$page = $value;}
            
            if($key != 'table' && $key != 'page'){
                $data[$key] = $value;
            }
        }
        
        $column['ecat_id']= $id;
        $table='tbl_end_category';
            
        if (!empty($_FILES['ecat_pic']['name'])) {
            $result = $this->Common_model->Result($column,$table);
            $current_img = $result['ecat_pic'];
            $current_img ='./uploads/shop/end_category/'.$current_img;
            if(file_exists($current_img)){
                unlink($current_img);
            }
            
            $path = $_FILES['ecat_pic']['name'];
            $ext = pathinfo( $path, PATHINFO_EXTENSION );
            
            $ecat_pic_name = 'end_category-'.$id.'.'.$ext;
            $ecat_pic_name2 = 'end_category-'.$id.'-'.rand().'.'.$ext;
            
            $config['upload_path'] = 'uploads/shop/end_category/';
            $config['allowed_types'] = 'jpg|jpeg|png';
            $config['file_name'] = $ecat_pic_name2;
            
            $this->load->library('upload',$config);
            
            if($this->upload->do_upload('ecat_pic')){
                $ecat_pic = $this->upload->data();
                
                // Resizeing Image Part
				resizeImage($config['upload_path'].$ecat_pic_name2,$config['upload_path'].$ecat_pic_name,225,225,FALSE);
				
				if(file_exists($config['upload_path'].$ecat_pic_name2)){
                    unlink($config['upload_path'].$ecat_pic_name2);
                }
                
                $data['ecat_pic'] = $ecat_pic_name;
    
            } else{
                $this->session->set_flashdata('error','Something went Wrong Image is Not Uploaded.');
			    redirect(base_url('master/end_category/edit/'.$id));
			    exit();
            }
           
        }
        
        // we are updating data from tbl_settings
        $result  = $this->Common_model->Edit($column,$data,$table);
        if($result == true){
            $this->session->set_flashdata('success','SUCCESS!! End Category is Updated Successfully.');
		    redirect(base_url('master/end_category/edit/'.$id));
		    exit();
        } else{
            $this->session->set_flashdata('error','Something went Wrong.');
		    redirect(base_url('master/end_category/edit/'.$id));
		    exit();
        }
        
        
	}
	
	public function add(){
	    
        foreach ($_POST as $key => $value) {
            if($key == 'table'){$table = $value;}
            if($key == 'page'){$page = $value;}
            
            if($key != 'table' && $key != 'page'){
                $data[$key] = $value;
            }
        }
        
        $column_name = 'ecat_id';
        $table='tbl_end_category';
        
        $ecat_id = $this->Common_model->Results_last_id($column_name, $table);
        $id = $ecat_id+1;
            
        if (!empty($_FILES['ecat_pic']['name'])) {
            
            $path = $_FILES['ecat_pic']['name'];
            $ext = pathinfo( $path, PATHINFO_EXTENSION );
            
            $ecat_pic_name = 'end_category-'.$id.'.'.$ext;
            $ecat_pic_name2 = 'end_category-'.$id.'-'.rand().'.'.$ext;
            
            $config['upload_path'] = 'uploads/shop/end_category/';
            $config['allowed_types'] = 'jpg|jpeg|png';
            $config['file_name'] = $ecat_pic_name2;
            
            $this->load->library('upload',$config);
            
            if($this->upload->do_upload('ecat_pic')){
                $ecat_pic = $this->upload->data();
                
                // Resizeing Image Part
				resizeImage($config['upload_path'].$ecat_pic_name2,$config['upload_path'].$ecat_pic_name,225,225,FALSE);
				
				if(file_exists($config['upload_path'].$ecat_pic_name2)){
                    unlink($config['upload_path'].$ecat_pic_name2);
                }
                
                $data['ecat_pic'] = $ecat_pic_name;
    
            } else{
                $this->session->set_flashdata('error','Something went Wrong Image is Not Uploaded.');
			    redirect(base_url('master/end_category/create/'));
			    exit();
            }
           
        }
        
        // we are updating data from tbl_settings
        $result  = $this->Common_model->Create($data,$table);
        if($result == true){
            $this->session->set_flashdata('success','SUCCESS!! End Category is Added Successfully.');
		    redirect(base_url('master/end_category/create/'));
		    exit();
        } else{
            $this->session->set_flashdata('error','Something went Wrong.');
		    redirect(base_url('master/end_category/create/'));
		    exit();
        }
        
        
	}

	
	public function get_mid_category() {

		$tcat_id = $_POST['tcat_id'];


		$column['tcat_id'] = $tcat_id;
		$table = 'tbl_mid_category';
		// we are getting results from common modal
		$result4 = $this->Common_model->ResultsWhere2($column, $table);


		echo '<option value="" selected>Select Mid Category </option>';
		for ($x = 0; $x < count($result4); $x++) {
			echo '<option value="' . $result4[$x]['mcat_id'] . '">' . $result4[$x]['mcat_name'] . '</option>';
		}
	}

	
}

?>