<?php
	class Documents extends Admin_Controller{
		public function __construct(){
			parent::__construct();

			$this->load->model("Hc_documents", "documents");
		}

		public function index(){

			$this->data["access_right"] = "view_right";
			$this->data["page_name"] = "documents";
			$this->data['meta_title'] = 'Manage documents';

			$access_control = $this->admin_user->get_admin_rights($this->data);
			if(!$access_control){
				$this->data['subview'] = $this->config->item('admin_folder').'/access_denied';
				$this->load->view($this->config->item('admin_folder').'/_layout_main', $this->data);
			}else{
				$this->data['subview'] = $this->config->item('admin_folder').'/documents/index';
				$this->load->view($this->config->item('admin_folder').'/_layout_main', $this->data);

			}
		}


		public function ajax_load_data(){
			if(isset($_POST) && !empty($_POST)){

				$search_params = array();
				$like_params = array();

				if(isset($_POST['search']['value']) && !empty($_POST['search']['value'])){
					$like_params = array(
						"hc_documents.document_name" => $_POST['search']['value']
					);

					$this->db->group_start();
					foreach ($like_params as $key => $value) {
						$this->db->or_like($key, $value);
					}
					$this->db->group_end();
		        }

				$search_params["hc_documents.is_active != "] = 2;
				$this->db->select("hc_documents.*");
				$this->db->where($search_params);
				$this->db->limit($_POST['length'], $_POST['start']);
				$this->db->order_by("display_order", "asc");
				$result = $this->documents->get();

				$data = array();
	        	$no = $_POST['start'];

				foreach ($result as $key => $value) {

					
					$no++;
		            $row = array();
		            
		            $row[] = "<strong>".$no.".</strong>";
		            $row[] = $value->title;
		            $row[] = $value->slug;
		            
		            if($value->is_active == 1){
		            	$row[] = '<span class="text-success">Active</span>';
		            }else{
		            	$row[] = '<span class="text-danger">In-active</span>';
		            }

		            $a = "";
		            $a = btn_edit(base_url().$this->config->item('admin_folder').'/documents/edit/'.$value->id);
		            $a .= ' '.btn_delete($this->config->item('admin_folder').'/documents/delete/'.$value->id);
	            	
	            	
	            	$row[] = $a;

		            $data[] = $row;
				}

				$output = array(
	                "draw" => $_POST['draw'],
	                "recordsTotal" => $this->documents->get_search_count($search_params, array()),
	                "recordsFiltered" => $this->documents->get_search_count($search_params, $like_params),
	                "data" => $data,
	        	);
		        //output to json format
		        echo json_encode($output);

			}
		}

		public function edit($id = NULL){
			if($id){
				$this->data['documents'] = $this->documents->get_by(array("id" => $id), TRUE);
				$this->data["meta_title"] = "Edit documents";

				//------------------------------------Activity logs--------------------------------------
				$logs_data["action_type"] = "Edit documents";
				$logs_data["action_identifier"] = "documents";

				$this->data["access_right"] = "edit_right";

			}else{
				$this->data['documents'] = $this->documents->get_new();
				$this->data["meta_title"] = "Add documents";

				//------------------------------------Activity logs--------------------------------------
				$logs_data["action_type"] = "Add documents";
				$logs_data["action_identifier"] = "documents";

				$this->data["access_right"] = "create_right";
			}

			
			$this->data["page_name"] = "documents";
			$access_control = $this->admin_user->get_admin_rights($this->data);
			if(!$access_control){
				$this->data['subview'] = $this->config->item('admin_folder').'/access_denied';
				$this->load->view($this->config->item('admin_folder').'/_layout_main', $this->data);
			}else{

				if(isset($_POST) && !empty($_POST)){
					// Set up the form
					$rules = $this->documents->rules;
					
					$this->form_validation->set_rules($rules);

					if($this->form_validation->run() === TRUE){
						$data = $this->documents->array_from_post(array('document_name', 'display_order','has_lock', 'is_active'));

						

						if(!$id){
							
							$data["created_at"] = Date('Y-m-d H:i:s');
							$data["created_by"] = isset($_SESSION["id"]) ? $_SESSION["id"] : 0;
						}

						$data["updated_at"] = Date('Y-m-d H:i:s');
						$data["updated_by"] = isset($_SESSION["id"]) ? $_SESSION["id"] : 0;

						$logs_data["request"] = json_encode($data, TRUE);

						$result_id = $this->documents->save($data, $id);
						$returnArr = array();

						if($result_id){
							$returnArr["status"] = TRUE;
							$returnArr["message"] = "Details saved successfully.";
							$returnArr["redirect_url"] = base_url().$this->config->item('admin_folder').'/documents/edit/'.$result_id;
						}else{
							$returnArr["status"] = FALSE;
							$returnArr["message"] = "Failed to perform this action.";
						}

						//--------------------------------------------Save Activity Logs-----------------------------
						$logs_data["response"] = json_encode($returnArr, TRUE);
						$log_id = $this->logs->add_log($logs_data);

	    				echo json_encode($returnArr);
		            	exit;

					}else{
						$returnArr["status"] = false;
						$returnArr["message"] = "Validation Failed.";
						$returnArr["errors"] = validation_errors();

						//--------------------------------------------Save Activity Logs-----------------------------
						$logs_data["response"] = json_encode($returnArr, TRUE);
						$log_id = $this->logs->add_log($logs_data);

		    			echo json_encode($returnArr);
		            	exit;
					}
				}else{

					if($id){
						$this->data["documents"] = $this->documents->get_data(array("id" => $id));
					}

					$this->data['subview'] = $this->config->item('admin_folder').'/documents/edit';
					$this->load->view($this->config->item('admin_folder').'/_layout_main', $this->data);
				}
			}
		}


		public function delete($id){

        	$returnArr = array();

        	$this->data["page_name"] = "documents";
			$this->data["access_right"] = "delete_right";
			$access_control = $this->admin_user->get_admin_rights($this->data);
			if(!$access_control){
				$returnArr["status"] = FALSE;
				$returnArr["message"] = "Access denied!";

				echo json_encode($returnArr, TRUE);
			}else{

	        	//------------------------------------Activity logs--------------------------------------
				$logs_data["action_type"] = "Delete documents";
				$logs_data["action_identifier"] = "documents";

	        	if($id){
	        		$update_val["is_active"] = 2;
					$result = $this->documents->save($update_val,$id);

					if($result){
						$returnArr["status"] = TRUE;
						$returnArr["message"] = "Row deleted successfully.";
						$returnArr["redirect"] = base_url().$this->config->item('admin_folder').'/documents/index';
					}else{

						$returnArr["status"] = FALSE;
						$returnArr["message"] = "Failed to delete row.";
						$returnArr["redirect"] = base_url().$this->config->item('admin_folder').'/documents/index';
					}
				}else{

			    	$returnArr["status"] = FALSE;
					$returnArr["message"] = "Validation Error";
					$returnArr["errors"] = "ID is required/Invalid ID";
					
				}

				//--------------------------------------------Save Activity Logs-----------------------------
				$logs_data["response"] = json_encode($returnArr, TRUE);
				$log_id = $this->logs->add_log($logs_data);
				
				echo json_encode($returnArr, TRUE);
			}
		}

		public function sorting_ajax(){

			if(isset($_POST['sortable'])){
				$this->documents->save_order($_POST['sortable']);
			}

			$this->db->where("is_active < 2");
			$this->db->order_by('display_order', 'asc');
			$this->data['data_array'] = $this->documents->get_data_for_sorting();
			

			$this->load->view($this->config->item('admin_folder').'/documents/sorting_ajax', $this->data);
		}

	}
?>