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

class Admin extends CI_Controller {

	public function __construct()
	{
		
		parent::__construct();
		$this->load->model('adminmodel');
		$this->load->helper(array('url','form'));
		$this->load->library('form_validation','session');
		
		$this->load->helper('ckeditor_helper');
		$data 	= 	array();
 
		//Ckeditor's configuration
		$this->data['ckeditor'] = array(
 
			//ID of the textarea that will be replaced
			'id' 	=> 	'content',
			'path'	=>	'js/ckeditor',
 
			//Optionnal values
			'config' => array(
				'toolbar' 	=> 	"Full", 	//Using the Full toolbar
				'width' 	=> 	"550px",	//Setting a custom width
				'height' 	=> 	'100px',	//Setting a custom height
 
			),
 
			//Replacing styles from the "Styles tool"
			'styles' => array(
 
				//Creating a new style named "style 1"
				'style 1' => array (
					'name' 		=> 	'Blue Title',
					'element' 	=> 	'h2',
					'styles' => array(
						'color' 	=> 	'Blue',
						'font-weight' 	=> 	'bold'
					)
				),
 
				//Creating a new style named "style 2"
				'style 2' => array (
					'name' 	=> 	'Red Title',
					'element' 	=> 	'h2',
					'styles' => array(
						'color' 		=> 	'Red',
						'font-weight' 		=> 	'bold',
						'text-decoration'	=> 	'underline'
					)
				)				
			)
		);
 
		$this->data['ckeditor_2'] = array(
 
			//ID of the textarea that will be replaced
			'id' 	=> 	'content_2',
			'path'	=>	'js/ckeditor',
 
			//Optionnal values
			'config' => array(
				'width' 	=> 	"100%",	//Setting a custom width
				'height' 	=> 	'100px',	//Setting a custom height
				'toolbar' 	=> 	array(	//Setting a custom toolbar
					array('Bold', 'Italic'),
					array('Underline', 'Strike', 'FontSize'),
					array('Smiley'),
					'/'
				)
			),
 
			//Replacing styles from the "Styles tool"
			'styles' => array(
 
				//Creating a new style named "style 1"
				'style 3' => array (
					'name' 		=> 	'Green Title',
					'element' 	=> 	'h3',
					'styles' => array(
						'color' 	=> 	'Green',
						'font-weight' 	=> 	'bold'
					)
				)
 
			)
		);		
 
	}
	
	
	public function index()
	{
		$this->load->view('login');
	}

	public function check_admin()
	{
		$this->form_validation->set_rules('username','Username','required');
		$this->form_validation->set_rules('password','Password','required');
		if($this->form_validation->run())
		{
			$username=$this->input->post('username');
			$password=$this->input->post('password');
			$result=$this->adminmodel->check_admin($username,$password);
			if($result==true)
			{ 
				$sess_data = array(
					//'id' => $uid,
					'username'=>$username
				);
				$this->session->set_userdata($sess_data);
				header('location:'.SITE_PATH.'user');
			}
			else
			{
				$this->session->set_flashdata('error', "Incorrect Username/Password");
				header('location:'.SITE_PATH.'index');
			}
		}
		
		else
		{
			$this->session->set_flashdata('error', "Username & Password Required");
			header('location:'.SITE_PATH.'index');
		}
	}
	public function  enter()
	{
		if($this->session->userdata('username')!='')
		{

		}
		else{
			header('location:'.SITE_PATH.'index');
		}
	}	

	public function logout()
	{
		$this->session->unset_userdata('username');
		header('location:'.SITE_PATH.'index');
	}

	public function tax()
	{
		$this->enter();
		$result['side']=$this->adminmodel->activeService();
		$this->load->view('side_bar',$result);
		$result['data']=$this->adminmodel->fetch_tax();
		//$result['country']=$this->adminmodel->getcountry();
		$this->load->view('tax',$result);
	}
	/*public function fetch_state()
	{
		if($this->input->post('country_id'))
		{
			echo $this->adminmodel->fetchState($this->input->post('country_id'));
		}
	}*/
	public function add_tax()
	{
		$this->enter();
		$this->form_validation->set_rules('date','Date','required');
		$this->form_validation->set_rules('amount','Amount','required');
		//$this->form_validation->set_rules('country','Country','required');
		//$this->form_validation->set_rules('state','State','required');
		if($this->form_validation->run())
		{
			$data['date'] = date('Y-m-d', strtotime($this->input->post('date')));
			//$data['date']=$this->input->post('date');
			$data['rate']=$this->input->post('amount');
		//	$data['country']=$this->input->post('country');
			//$data['state']=$this->input->post('state');
			$this->adminmodel->taxAdd($data);
			header('location:'.SITE_PATH.'tax');
		}
		else
		{
			header('location:'.SITE_PATH.'tax');
		}
	}
	public function inactiveTax($id)
	{
		$this->adminmodel->inactive($id);
		header('location:'.SITE_PATH.'tax');
	}
	public function activeTax($id)
	{
		$this->adminmodel->active($id);
		header('location:'.SITE_PATH.'tax');
	}
	public function user()
	{
		$this->enter();
		$result['side']=$this->adminmodel->activeService();
		$this->load->view('side_bar',$result);
		$result['data']=$this->adminmodel->user_fetch();
		$this->load->view('users',$result);
	}
	public function profile()
	{
		$this->enter();
		$result['side']=$this->adminmodel->activeService();
		$this->load->view('side_bar',$result);
		$id=$this->uri->segment(2);
		$result['data']=$this->adminmodel->fetchProfile($id);
		
		$this->load->view('user-profile',$result);
	}
	//public function email()
	//{
		//$this->load->view('side_bar');
		//$this->load->view('user_email');
	//}
	public function service()
	{
		$this->enter();
		$result['side']=$this->adminmodel->activeService();
		$this->load->view('side_bar',$result);
		$result['data']=$this->adminmodel->fetchService();
		$this->load->view('service',$result);
	}
	public function addService()
	{
		$this->enter();
		$this->form_validation->set_rules('title','Title','required');
		if($this->form_validation->run())
		{
			$data['service_name']=$this->input->post('title');
			$this->adminmodel->serviceAdd($data);
			header('location:'.SITE_PATH.'service');
		}
		else
		{
			header('location:'.SITE_PATH.'service');
		}
	}
	public function editService()
	{
		$this->enter();
		$result['side']=$this->adminmodel->activeService();
		$this->load->view('side_bar',$result);
		$result['data']=$this->adminmodel->fetchService();
		$id=$this->uri->segment(2);
		$result['edi']=$this->adminmodel->singleService($id);
		$this->load->view('service',$result);
		if($this->input->post('update'))
		{
			$id=$this->input->post('sid');
			$data['status']=$this->input->post('status');
			$data['service_name']=$this->input->post('title');
			$this->adminmodel->EditService($id,$data);
			header('location:'.SITE_PATH.'service');
		}
	}
	public function servicePlan()
	{
		$this->enter();
		$result['side']=$this->adminmodel->activeService();
		$this->load->view('side_bar',$result);
		$id=$this->uri->segment(2);
		$result['ser']=$this->adminmodel->serviceName($id);
		$result['plan']=$this->adminmodel->planListing($id);
		$result['inact']=$this->adminmodel->inactivePalns($id);
		$this->load->view('service_plans',$result);
	}
	public function serviceDetails()
	{
		$this->enter();
		$result['side']=$this->adminmodel->activeService();
		$this->load->view('side_bar',$result);
		$id=$this->uri->segment(2);
		$result['ser']=$this->adminmodel->serviceName($id);
		$this->load->view('service_details',$result);
	}
	
	public function billing()
	{
		$this->enter();
		$result['side']=$this->adminmodel->activeService();
		$this->load->view('side_bar',$result);
		$this->load->view('user-billing');
	}
	public function Edit_ServiceDetails()
	{
		$this->enter();
		$result['side']=$this->adminmodel->activeService();
		$this->load->view('side_bar',$result);
		$id=$this->uri->segment(2);
		//$result['ser']=$this->adminmodel->serviceName($id);
		$result['data']=$this->adminmodel->fetchSingleData($id);
		$this->load->view('service_details',$result);
		if($this->input->post('update'))
		{
			$pid=$this->input->post('pln_id');
			$data['plan_name']=$this->input->post('planName');
			$data['price']=$this->input->post('price');
			$data['descp']=$this->input->post('description');
			$data['status']=$this->input->post('status');
			$data['duration']=$this->input->post('duration');
			$this->adminmodel->updatePlan($data,$pid);
			header('location:'.SITE_PATH.'service');	
		}
	
	}
	public function add_plan()
	{
		$this->enter();
		$this->form_validation->set_rules('planName','PlanName','required');
		$this->form_validation->set_rules('price','Price','required');
		$this->form_validation->set_rules('duration','Duration','required');
		$this->form_validation->set_rules('description','Description','required');
		$this->form_validation->set_rules('status','Status','required');
		if($this->form_validation->run())
		{
			$data['service_id']=$this->input->post('ser_id');
			$data['plan_name']=$this->input->post('planName');
			$data['price']=$this->input->post('price');
			$data['descp']=$this->input->post('description');
			$data['status']=$this->input->post('status');
			$data['duration']=$this->input->post('duration');
			$this->adminmodel->addPlanDetails($data);
			header('location:'.SITE_PATH.'service');
		}
		else
		{
			header('location:'.SITE_PATH.'service');
		}
	}
	public function Orders()
	{
		$this->enter();
		$result['side']=$this->adminmodel->activeService();
		$this->load->view('side_bar',$result);
		$result['data']=$this->adminmodel->fechOrder();
		$this->load->view('order',$result);
	}
	public function orderDetails()
	{
		$this->enter();
		$result['side']=$this->adminmodel->activeService();
		$this->load->view('side_bar',$result);
		$id=$this->uri->segment(2);
		$result['data']=$this->adminmodel->Orderdetails($id);
		$this->load->view('order_details',$result);
	}

}
