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

class Zakatsetup extends CI_Controller {
	
	public function __construct(){
		parent::__construct();
		$this->load->model('Useraccess');
		$query = $this->Useraccess->useraccess_id_module2('7', 'setup');
		if(!$query){
			echo '<script>alert("Not authorised..");
				window.location.replace("'.base_url().'Error");
				</script>';
		}
		$this->load->library('layouts');
		$this->load->model('menu');
	}
	
	public function Group(){
 	  $this->load->model('ZakatGroup');
	  if($this->input->post('mysubmit')){
		  if($this->input->post('ID')==NULL){
			  $query = $this->ZakatGroup->ZakatGroup_insert();
			  if($query){
				echo '<script>alert("Insert Data Success..");
				window.location.replace("../ZakatReport/groupList");
				</script>';
			  }else{
				echo '<script>alert("Insert Data Failed.");
				history.go(-1);</script>';
			  }
		  }else{
			  $updatedata = array(
				'name' => $this->input->post('name'),
				'description' => $this->input->post('description'),
				'parentID' => $this->input->post('parentID'),
				'sequence' => $this->input->post('sequence'),
			  	'updatedBy' => element('role', $this->session->userdata('logged_in')),
			  );
			  $query = $this->ZakatGroup->ZakatGroup_save($this->input->post('ID'), $updatedata);
			  if($query){
				echo '<script>alert("Insert Data Success..");
				window.location.replace("groupList");
				</script>';
			  }else{
				echo '<script>alert("Insert Data Failed.");
				history.go(-1);</script>';
			  }
		  }
	  }else{
		  $ID = $this->uri->segment(3);;
		$this->lang->load("zakatGroup",$this->session->userdata('language'));
		$this->layouts->add_includes('js/validationengine/languages/jquery.validationEngine-'.$this->session->userdata('language').'.js')
				->add_includes('js/validationengine/jquery.validationEngine.js');
		$data['zakatGroup'] = $this->ZakatGroup->group_all();
		if($ID){
			$query = $this->ZakatGroup->ZakatGroup_ID($ID);
			$data['zakatData'] = $query[0];
			$this->layouts->view('zakat/setups/groupEdit', array('latest' => 'sidebar/latest'), $data);
		}else{
			$data['zakatData'] = NULL;
			$this->layouts->view('zakat/setups/group', array('latest' => 'sidebar/latest'), $data);
		}
		
	  }
	}
	
	public function groupDelete(){
		$this->lang->load("zakatGroup",$this->session->userdata('language'));
 	  	$this->load->model('ZakatGroup');
		$query = $this->ZakatGroup->ZakatGroup_Delete($this->input->post('fldid'));
		if($query){
   			return TRUE;
		}else{
   			return FALSE;
		}
	}
	public function NisabDetails(){
		$ID = $this->uri->segment(3);;
		$this->lang->load("zakatDetails",$this->session->userdata('language'));
 	  	$this->load->model('ZakatGroup');
		$data['zakatGroup'] = $this->ZakatGroup->group_all();
		$this->layouts->add_includes('js/validationengine/languages/jquery.validationEngine-'.$this->session->userdata('language').'.js')
				->add_includes('js/validationengine/jquery.validationEngine.js')
				->add_includes('js/datatables/jquery.dataTables.min.js');
		if($ID){
			$query = $this->ZakatGroup->ZakatNisabDetails_GroupID($ID);
			$data['zakatData'] = $query[0];
		}else{
			$query = $this->ZakatGroup->ZakatNisabDetails_GroupID(1);
			if($query){
				$data['zakatData'] = $query[0];
			}else{
				$data['zakatData'] = NULL;
			}
		}
		$this->layouts->view('zakat/setups/nisabDetails', array('latest' => 'sidebar/latest'), $data);
	}
	public function nisabDetailsSearch(){
 	  	$this->load->model('ZakatGroup');
		$query = $this->ZakatGroup->ZakatNisabDetails_GroupID($this->input->post('ID'));
		if($query){
   			print json_encode(array("status"=>"success", "message"=>$query));
		}else{
   			print json_encode(array("status"=>"failed", "message"=>''));
		}
	}
	public function nisabDetailsDelete(){
 	  	$this->load->model('ZakatGroup');
		$query = $this->ZakatGroup->ZakatNisabDetails_Delete($this->input->post('fldid'));
		if($query){
   			print json_encode(array("status"=>"success", "message"=>$query));
		}else{
   			print json_encode(array("status"=>"failed", "message"=>''));
		}
	}
	public function nisabDetailsInsert(){
	  	$this->load->model('ZakatGroup');
		$query = $this->ZakatGroup->ZakatNisabDetails_Insert();
		if($query){
 			$query1 = $this->ZakatGroup->ZakatNisabDetails_ID($query);
  			print json_encode(array("status"=>"success", "message"=>$query1[0]));
		}else{
   			print json_encode(array("status"=>"failed", "message"=>'Journal details saved..'));
		}
	}
	public function Nisab(){
 	  $this->load->model('ZakatNisab');
	  if($this->input->post('mysubmit')){
		  if($this->input->post('ID')==NULL){
			  $query = $this->ZakatNisab->ZakatNisab_insert();
			  if($query){
				echo '<script>alert("Insert Data Success..");
				window.location.replace("'. base_url().'zakatReport/nisabList");
				</script>';
			  }else{
				echo '<script>alert("Insert Data Failed.");
				history.go(-1);</script>';
			  }
		  }else{
			  $updatedata = array(
				'year' => $this->input->post('year'),
				'nisab' => $this->input->post('nisab'),
			  	'updatedBy' => element('role', $this->session->userdata('logged_in')),
			  );
			  $query = $this->ZakatNisab->ZakatNisab_save($this->input->post('ID'), $updatedata);
			  if($query){
				echo '<script>alert("Insert Data Success..");
				window.location.replace("'. base_url().'zakatReport/nisabList");
				</script>';
			  }else{
				echo '<script>alert("Insert Data Failed.");
				history.go(-1);</script>';
			  }
		  }
	  }else{
		  $ID = $this->uri->segment(3);;
		$this->lang->load("zakatNisab",$this->session->userdata('language'));
		$this->layouts->add_includes('js/validationengine/languages/jquery.validationEngine-'.$this->session->userdata('language').'.js')
				->add_includes('js/validationengine/jquery.validationEngine.js');
 	  	$this->load->model('ZakatCenter');
		$data['ZakatCenter'] = $this->ZakatCenter->group_all();
		if($ID){
			$query = $this->ZakatNisab->ZakatNisab_ID($ID);
			$data['zakatData'] = $query[0];
			$this->layouts->view('zakat/setups/nisabEdit', array('latest' => 'sidebar/latest'), $data);
		}else{
			$data['selyear'] = date("Y");
			$data['zakatData'] = NULL;
			$this->layouts->view('zakat/setups/nisab', array('latest' => 'sidebar/latest'), $data);
		}
		
	  }
	}
	public function nisabDelete(){
 	  	$this->load->model('ZakatNisab');
		$query = $this->ZakatNisab->ZakatNisab_Delete($this->input->post('fldid'));
		if($query){
   			print json_encode(array("status"=>"success", "message"=>$query));
		}else{
   			print json_encode(array("status"=>"failed", "message"=>''));
		}
	}
// Zakat Center

	public function Center(){
 	  $this->load->model('ZakatCenter');
	  if($this->input->post('mysubmit')){
		  if($this->input->post('ID')==NULL){
			  $query = $this->ZakatCenter->ZakatCenter_insert();
			  if($query){
				echo '<script>alert("Insert Data Success..");
				window.location.replace("'. base_url().'zakatReport/centerList");
				</script>';
			  }else{
				echo '<script>alert("Insert Data Failed.");
				history.go(-1);</script>';
			  }
		  }else{
			  $updatedata = array(
				'shortName' => $this->input->post('shortName'),
				'name' => $this->input->post('name'),
				'line1' => $this->input->post('line1'),
				'line2' => $this->input->post('line2'),
				'line3' => $this->input->post('line3'),
				'city' => $this->input->post('city'),
				'postCode' => $this->input->post('postCode'),
				'stateID' => $this->input->post('state'),
				'countryID' => $this->input->post('country'),
				'noTel' => $this->input->post('noTel'),
				'noFax' => $this->input->post('noFax'),
				'email' => $this->input->post('email'),
				'website' => $this->input->post('website'),
			  	'updatedBy' => element('role', $this->session->userdata('logged_in')),
			  );
			  $query = $this->ZakatCenter->ZakatCenter_save($this->input->post('ID'), $updatedata);
			  if($query){
				echo '<script>alert("Insert Data Success..");
				window.location.replace("'. base_url().'zakatReport/centerList");
				</script>';
			  }else{
				echo '<script>alert("Insert Data Failed.");
				history.go(-1);</script>';
			  }
		  }
	  }else{
		  $ID = $this->uri->segment(3);;
		$this->lang->load("zakatCenter",$this->session->userdata('language'));
		$this->layouts->add_includes('js/validationengine/languages/jquery.validationEngine-'.$this->session->userdata('language').'.js')
				->add_includes('js/validationengine/jquery.validationEngine.js');
		$this->load->model('Country');
		$this->load->model('State');
		$data['country'] = $this->Country->country_all();
//		$data['ZakatCenter'] = $this->ZakatCenter->group_all();
		if($ID){
			$query = $this->ZakatCenter->ZakatCenter_ID($ID);
			$data['zakatData'] = $query[0];
			$data['state'] = $this->State->state_region($query[0]->countryID);
			$this->layouts->view('zakat/setups/centerEdit', array('latest' => 'sidebar/latest'), $data);
		}else{
			$data['state'] = $this->State->state_region('458');
			$data['zakatData'] = NULL;
			$this->layouts->view('zakat/setups/center', array('latest' => 'sidebar/latest'), $data);
		}
		
	  }
	}

}