<?php   if ( ! defined('BASEPATH')) exit('No direct script access allowed');
//class and other component developed by nirmala 20-07-14
class Service_Details extends CI_Controller {

    var $main_menu_name = "Service";
	var $sub_menu_name = "Service Details";
	
	public function __construct()
	{
		parent::__construct();

		$this->load->model('Service_Details_Model');
		$this->load->model('Common_Model');
		$this->load->model('Sequerty_Model');
	}

	public function index()
	{
		$data['main_menu_name'] = $this->main_menu_name;
		$data['sub_menu_name'] = $this->sub_menu_name;
        $this->load->view('service_details_add',$data);
	}
	
	
	public function save_service() // new row insert and update
	{
		$type=$this->input->post('type');
		$detail_type=$this->input->post('service_type');
		$title=$this->input->post('title');
		$detail_id=$this->input->post('service_id');
      
	
   
        $data=array(
			'mstserdtpe_id'=>$detail_type,
            'mstserdtl_name'=>$title,
            'mstserdtl_status'=>1
		);
        
      
		$this->load->library('form_validation'); //form validation lib
		if($type=='A')
		{
			$this->form_validation->set_rules('title', 'title', 'required');
		}
		else if($type=='E')
		{
			$this->form_validation->set_rules('title', 'title', 'required');
		}
		
		if ($this->form_validation->run() == FALSE)
        {
           $st = array('status' =>0,'validation' => validation_errors());
           echo json_encode($st);
        }
        else
        {
		
		$_insert=$this->Service_Details_Model->save_service($data,$detail_id);
        $lastid=$this->db->insert_id();
     

		if($type=='A'){
			if ($lastid) {
				echo json_encode(array('id'=>$lastid,'type'=>$type,'status'=>1));
			} else {
				echo json_encode(array('status'=>'error'));
			}
		}
		if($type=='E'){
			echo json_encode(array('type'=>$type,'status'=>1));
		}
		}
	}

	public function add_service() //open for check model (insert and update)
	{
        if (isset($_GET['service_id'])) {
			$service_id=$_GET['service_id'];
		}
		else {
			$service_id='';
		}
		if($service_id){
			$data['service_id']=$service_id;
			$data['type']='E';
			$data['pageName']='UPDATE SERViCE';
			$data['btnText']='Update Service';
			$data['types']= $this->Service_Details_Model->get_all_types();	
			$data['services']= $this->Service_Details_Model->get_service_details($service_id);
			
			
			
		}
		else {
			$data['service_id']='';
			$data['type']='A';
			$data['pageName']='ADD SERVICE';
			$data['btnText']='Add Service';
			$data['types']= $this->Service_Details_Model->get_all_types();
			//print_r($this->Service_Details_Model->get_all_types());			
            
		}
        $this->load->view('models/create_service_details',$data);
    }
    




	public function list_details_list() // view list of check detail table
	{
	$requestData= $_REQUEST;
	
	$columns = array( 
		0 =>'mstserdtpe_id', 
        1 => 'mstserdtl_name',
		2 =>'mstserdtl_status', 
	
     
	);
	
	$data = array();
	$rows = $this->Service_Details_Model->get_all_details();
	$totalData = count($rows);
	$totalFiltered = $totalData;  


    
	foreach ($rows as $row){
		$nestedData=array(); 
		$nestedData[] = $row['mstserdtl_name'];
		$nestedData[] =$row['mstserdtpe_name'];
		$actionTxtDisble='';
		$actionTxtEnable='';
		$actionTxtUpdate='';
		$actionTxtDelete='';
		$actionTxtUpdate='<a onClick="click_page_update_btn('.$row['mstserdtl_id'].')" data-toggle="modal" href="#" class="btn btn-xs btn-blue tooltips" data-placement="top" data-original-title="Edit suppliers"><i class="glyphicon fa fa-edit"></i></a> &nbsp;';

		if($row['mstserdtl_status']==1){
			$actionTxtDisble = '<a class="btn btn-xs btn-green tooltips" data-placement="top" data-original-title="Disable supplier" onClick="disablePageData('.$row['mstserdtl_id'].')"><i class="glyphicon fa fa-check"></i></a> &nbsp;';
	}
		if($row['mstserdtl_status']==0){
			$actionTxtEnable = '<a class="btn btn-xs btn-warning tooltips" data-placement="top" data-original-title="Disable supplier" onClick="enablePageData('.$row['mstserdtl_id'].')"><i class="glyphicon fa fa-minus-circle"></i></a> &nbsp;';
	}

		$actionTxtDelete='<a class="btn btn-xs btn-bricky tooltips" data-placement="top" data-original-title="Delete supplier" onClick="deletePageData('.$row['mstserdtl_id'].')">
														<i class="glyphicon fa fa-trash-o"></i></a>';
	
	$nestedData[]=$actionTxtUpdate.$actionTxtDisble.$actionTxtEnable.$actionTxtDelete;
    $data[] = $nestedData;
    
}


	$json_data = array(
			//"draw"            => intval( $requestData['draw'] ),  
			"recordsTotal"    => intval( $totalData ),  
			"recordsFiltered" => intval( $totalFiltered ),
			"data"            => $data 
			);

	echo json_encode($json_data); 
	}


	function delete() {   // delete for  row
		$id	= $this->input->post('service_id');
		$this->Service_Details_Model->delete($id);
        if ($id) {
        	echo json_encode(array('id'=>$id));
        } else {
        	echo json_encode(array('status'=>'error'));
        }
	}


	function disable() {
		$id	= $this->input->post('service_id');
		$this->Service_Details_Model->disable($id);
        if ($id) {
        	echo json_encode(array('id'=>$id));
        } else {
        	echo json_encode(array('status'=>'error'));
        }
	}
	
	function enable() {
		$id	= $this->input->post('service_id');
		$this->Service_Details_Model->enable($id);
        if ($id) {
        	echo json_encode(array('id'=>$id));
        } else {
        	echo json_encode(array('status'=>'error'));
        }
	}



	
	

}