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

class PoliciesAndProcedures extends MY_Controller
{
	function __construct()
	{
		parent::__construct();

		if($this->logged_user = $this->session->userdata('user')){
			$this->login->checkModuleAccess(4);
			
            $this->logged_module = $this->session->userdata('module_data');
            
            $this->load->model('hr/model_form');
            $this->load->model('hr/model_policy');
			$this->load->model('hr/model_tnc');
			$this->load->model('hr/model_manual');
			$this->load->model('admin/model_company');
			// $this->load->model('Admin/model_company','company');
			$this->load->model('Admin/model_user','user');
			$this->load->model('analysis/model_analysis','model_analysis');


		}else{
			redirect(base_url());
		}
    }
    
    function index()
    
    {
		// $data['categories'] 	= $this->model_general->fetchAll('hr__policyprocedure_category', array(), array('is_delete' => 0));
		$data['categories'] 	= $this->model_general->fetchAll('hr__policyprocedure_category', array(), array('parent_id' => 0, 'is_delete' => 0));
        //		var_dump($data['categories']); 
		// $data['sub_categories'] = $this->model_general->fetchAll('hr__policyprocedure_category', array(), array('parent_id !=' => 0, 'is_delete' => 0));

		$data['forms']      = $this->model_form->getForms();
		$data['policies']   = $this->model_policy->getPolicy();
		$data['tncs']       = $this->model_tnc->getTncs();
		$data['manuals']	= $this->model_manual->getManual();

		$data['branches'] 	 = $this->model_company->getBranches();
		$data['departments'] = $this->model_company->getDepartments();

        $data['page_setup'] = $this->template->pageSetup('HR', '', 'Policies & Procedures');
		
		$this->model_analysis->add_access($this->logged_user['id'],7); //Fakru Add for analysis purpose
		 
		$this->template->view('hr/policyprocedure/index', $data);
	}
	
	// API
	function getCategories()
    {
        $data = $this->input->post();

        if(isset($data['id']))
		{
            $category = $this->model_general->fetchSingle('hr__policyprocedure_category', array(), array('id' => $data['id'], 'is_delete' => 0));

            print json_encode(array(
				"status" => "Success",
				"category"	 => $category,
			));
        }
        else
        {
            print json_encode(array(
				"status" => "Error",
				"data"	 => "Data is missing."
			));
        }
	}

	function createCategory()
	{
		$data = $this->input->post();

		if($data)
		{
			$data['created_by'] = $this->logged_user['id'];

			$id = $this->model_general->insert('hr__policyprocedure_category', $data);

			if($id)
			{
				print json_encode(array(
					"status" => "Success",
					"msg"	 => "Category created successfully."
				));
			}
			else
			{
				print json_encode(array(
					"status" => "Error",
					"msg"	 => "System failed to create Category."
				));
			}
		}
		else
		{
			print json_encode(array(
				"status" => "Error",
				"msg"	 => "Data is missing."
			));
		}
	}

	function updateCategory()
	{
		$data = $this->input->post();

		if(isset($data['id']))
		{
			$id = $data['id'];
			unset($data['id']);

			$data['updated_by'] = $this->logged_user['id'];
			$data['updated_ts'] = date('Y-m-d H:i:s');

			$ok = $this->model_general->update('hr__policyprocedure_category', $data, array('id'=>$id));

			if($ok)
			{
				print json_encode(array(
					"status" => "Success",
					"msg"	 => "Category updated successfully."
				));
			}
			else
			{
				print json_encode(array(
					"status" => "Error",
					"msg"	 => "System failed to update Category."
				));
			}
		}
		else
		{
			print json_encode(array(
				"status" => "Error",
				"msg"	 => "Data is missing."
			));
		}
	}

	function deleteCategory()
	{
		$data = $this->input->post();

		if(isset($data['id']))
		{
			$data['deleted_by'] = $this->logged_user['id'];
			$data['deleted_ts'] = date('Y-m-d H:i:s');

			$ok = $this->model_general->update('hr__policyprocedure_category', array('is_delete'=>1), array('id'=>$data['id']));

			if($ok)
			{
				print json_encode(array(
					"status" => "Success",
					"msg"	 => "Category deleted successfully."
				));
			}
			else
			{
				print json_encode(array(
					"status" => "Error",
					"msg"	 => "Failed to delete Category."
				));
			}
		}
		else
		{
			print json_encode(array(
				"status" => "Error",
				"msg"	 => "Data is missing."
			));
		}
	}

    function setting()
    {
        $data['page_setup'] = $this->template->pageSetup('Policies & Procedures', '', 'Setting');
		$this->template->view('hr/policyprocedure/index', $data);
	}

	// public function onlineForm($formName = null)
	// {
	// 	$data['page_setup'] = $this->template->pageSetup('Online Form', '', $formName);
	// 	$this->template->view('HR/policyprocedure/form/'.$formName, $data);
	// }

	// public function carbooking_calendar()
	// {
	// 	$data['page_setup'] = $this->template->pageSetup('Car Booking Calendar', '', '');
	// 	$this->template->view('HR/policyprocedure/carbooking_calendar', $data);
	// }

	// public function meeting_calendar()
	// {
	// 	$data['page_setup'] = $this->template->pageSetup('Meeting Calendar', '', '');
	// 	$this->template->view('HR/policyprocedure/meeting_calendar', $data);
	// }


}