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

class Region extends CI_Controller {

	public function __construct() {
		parent::__construct();
		date_default_timezone_set("Asia/Kolkata");
        $this->load->model('Region_model');
		$this->load->model('Dashboard_model');
		
		if(!$this->session->userdata('logged_in')) {
			redirect(base_url('Login'));
		}
 	}
	
	function listRegion(){
		$template['page'] = 'Region/viewRegionList';
        $template['menu'] = 'Region Management';
        $template['smenu'] = 'View Region List';
        $template['pTitle'] = "Region Management";
        $template['pDescription'] = "View Region List";

        $template['regionData'] = $this->Region_model->getRegionData();
		$this->load->view('template',$template);
	}

	function addRegion(){
        $this->load->model('Region_model');
        $template['regionData'] = $this->Region_model->getRegionData();

		$template['page'] = 'Region/regionAddForm';
        $template['menu'] = 'Region Management';
        $template['smenu'] = 'Add Region';
        $template['pTitle'] = "Add Region";
        $template['pDescription'] = "Create New Region";

		$this->load->view('template',$template);
	}

	function createRegion(){
		$flashMsg = array('message'=>'Something went wrong, please try again..!','class'=>'error');

		if(!isset($_POST) || empty($_POST)){
            $this->session->set_flashdata('message',$flashMsg);
            redirect(base_url('Region/addRegion'));
        }

		$err = 0;
        $errMsg = '';
        if($err == 0 && (!isset($_POST['name']) || empty($_POST['name']))){
            $err = 1;
            $errMsg = 'Provide a Region Name';
        } else if($err == 0 && (!isset($_FILES) || !isset($_FILES['region_icon']) || 
                                 empty($_FILES['region_icon']))){
            $err = 1;
            $errMsg = 'Provide a Region Icon';
        }

        if($err == 0){
            $config = set_upload_service("assets/uploads/services");
            $this->load->library('upload');
            $config['file_name'] = time()."_".$_FILES['region_icon']['name'];
            $this->upload->initialize($config);
            if(!$this->upload->do_upload('region_icon')){
                $err = 1;
                $errMsg = $this->upload->display_errors();
            }else{
                $upload_data = $this->upload->data();
                $_POST['region_icon'] = $config['upload_path']."/".$upload_data['file_name'];
            }
        }
        
        if($err == 1){
            $flashMsg['message'] = $errMsg;
            $this->session->set_flashdata('message',$flashMsg);
            redirect(base_url('Region/addRegion'));
        }

        $status = $this->Region_model->createRegion($_POST);
        if($status == 1){
            $flashMsg['class'] = 'success';
            $flashMsg['message'] = 'Region Created';

            $this->session->set_flashdata('message',$flashMsg);
            redirect(base_url('Region/listRegion'));
        }
        $this->session->set_flashdata('message',$flashMsg);
        redirect(base_url('Region/addRegion'));
	}

	function editRegion($region_id=''){
        $flashMsg = array('message'=>'Something went wrong, please try again..!','class'=>'error');
		if(empty($region_id) || empty(decode_param($region_id))){
			$this->session->set_flashdata('message',$flashMsg);
            redirect(base_url('Region/listRegion'));
		}
		$template['page'] = 'Region/regionAddForm';
        $template['menu'] = 'Region Management';
        $template['smenu'] = 'Edit Region';
        $template['pTitle'] = "Edit Region";
        $template['pDescription'] = "Update Region Data";

        $template['region_id'] = $region_id;
        $template['regionData'] = $this->Region_model->getRegionData(decode_param($region_id));
		$this->load->view('template',$template);
	}

	function updateRegion($region_id=''){
		$flashMsg = array('message'=>'Something went wrong, please try again..!','class'=>'error');

		if(!isset($_POST) || empty($_POST) || empty($region_id)){
            $this->session->set_flashdata('message',$flashMsg);
            redirect(base_url('Region/listRegion'));
        }

		$err = 0;
        $errMsg = '';
        if($err == 0 && (!isset($_POST['name']) || empty($_POST['name']))){
            $err = 1;
            $errMsg = 'Provide a Region Name';
        }

        if($err == 1){
            $flashMsg['message'] = $errMsg;
            $this->session->set_flashdata('message',$flashMsg);
            redirect(base_url('Region/editRegion/'.$region_id));
        }

        if(isset($_FILES) && isset($_FILES['region_icon']) && !empty($_FILES['region_icon'])){
            $config = set_upload_service("assets/uploads/services");
            $this->load->library('upload');
            $config['file_name'] = time()."_".$_FILES['region_icon']['name'];
            $this->upload->initialize($config);
            if($this->upload->do_upload('region_icon')){
                $upload_data = $this->upload->data();
                $_POST['region_icon'] = $config['upload_path']."/".$upload_data['file_name'];
            }
        }

        if($err == 0){
            $config = set_upload_service("assets/uploads/services");
            $this->load->library('upload');
            $config['file_name'] = time()."_".$_FILES['region_icon']['name'];
            $this->upload->initialize($config);
            if(!$this->upload->do_upload('region_icon')){
                $err = 1;
                $errMsg = $this->upload->display_errors();
            }else{
                $upload_data = $this->upload->data();
                $_POST['region_icon'] = $config['upload_path']."/".$upload_data['file_name'];
            }
        }

        $status = $this->Region_model->updateRegion(decode_param($region_id),$_POST);
        if($status == 1){
            $flashMsg['class'] = 'success';
            $flashMsg['message'] = 'Region Created';

            $this->session->set_flashdata('message',$flashMsg);
            redirect(base_url('Region/listRegion'));
        }
        $this->session->set_flashdata('message',$flashMsg);
        redirect(base_url('Region/editRegion/'.$region_id));
	}

    function changeStatus($region_id = '',$status = '1'){
        $flashMsg = array('message'=>'Something went wrong, please try again..!','class'=>'error');
        if(empty($region_id)){
            $this->session->set_flashdata('message',$flashMsg);
            redirect(base_url('Region/listRegion'));
        }
        $region_id = decode_param($region_id);
        $status = $this->Region_model->changeStatus($region_id,$status);
        if(!$status){
            $this->session->set_flashdata('message',$flashMsg);
        }
        redirect(base_url('Region/listRegion'));
    }
}
?>