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

class Maintenance extends Admin_Controller {
	
	function __construct() {
		parent::__construct(TRUE);
		$this->load->model('maintenance_model','',TRUE);
	}
	function index()
	{
		$this->load->view( 'maintenance' );
	}
	function add_mainteance($data=null)
	{
		$this->load->view( 'add_maintenance' );
		if(isset($data)){
			$this->maintenance_model->save($data);
		}
	} 
	
	function addmainteance()
	{
		$this->load->view( 'add_maintenance' );

		if(sizeof($_POST) > 0){
			unset($_POST['type_maintenance']);
			$_POST['user_id'] = $this->session->userdata('user_id');
			$this->maintenance_model->save($_POST);
		}
	}	
	
	function operation()
	{
		$this->load->view( 'operation' );

	}
	
	
	function check_duplicates()
	{
		$operationName=uri_assoc("name");
		$id=uri_assoc("id");
		if(!$this->maintenance_model->checkOperationDuplicate($operationName,$id))
		{
			//echo "Operation Name Already Exist.";
			$this->output->set_output("Operation Name Already Exist.");
		}
	}
	
	
	function loadTypeOperation()
	{
		$data = $this->maintenance_model->typeOperationgetAllData();
		$responce->page = $data['page'];
		$responce->total = $data['total_pages'];
		$responce->records = $data['count'];
		$i=0;
		foreach($data['result'] as $row) {
			$responce->rows[$i] = $row;
			$i++;
		}
		//echo json_encode($responce);
		$this->output->set_output(json_encode($responce));
	}	
	
	function getnameoperation($type)
	{
		$data = $this->maintenance_model->typeOperationgetAllDataBYtype($type);
		$i=0;
		$options = "";
		foreach($data as $row) {
			$options .= '<option value=\'' . $row->id . '\'>' . $row->operation_name . '</option>';
			$i++;
		}
		
		$this->output->set_output($options);
	}
	
		
	function add_operation()
	{
		$this->load->view( 'add_operation' );

		if(sizeof($_POST) > 0){
			$this->maintenance_model->saveOperationType($_POST);
		}
	} 
	
	function loadData(){
		$data = $this->maintenance_model->getAllData();
		$responce->page = $data['page'];
		$responce->count_pay = $data['count_pay'];
		$responce->total = $data['total_pages'];
		$responce->records = $data['count'];
		
		$i=0;  
		foreach($data['result'] as $row) {  
			$row->kms_debut_alerte = $row->kms_debut_alerte." Km";
			$row->kms_fin_alerte = $row->kms_fin_alerte." Km";
			$row->cout = $row->cout." DZD";
			$responce->rows[$i] = $row;
			$i++; 
		} 
		//echo json_encode($responce);
		$this->output->set_output(json_encode($responce));
	}
	// function export(){
		
		// $this->load->plugin('to_excel'); 
		// $this->form_model->export();
	// }	 
}
?>