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

class Duration extends CI_Controller {

    var $main_menu_name = "settings";
	var $sub_menu_name = "duration";
	
	public function __construct()
	{
		parent::__construct();

		$this->load->model('Duration_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('duration',$data);
	}
	
	
	public function save_duration()
	{
		$duration_id=$this->input->post('duration_id');
		$type=$this->input->post('type');
		$duration_value=$this->input->post('duration_value');	
		
		$data=array(
			'duration_value'=>$duration_value,			
		);		
       	$_insert=$this->Duration_Model->save_duration($data,$duration_id);
		$lastid=$this->db->insert_id();
		if($type=='A'){
			if ($lastid) {
				echo json_encode(array('id'=>$lastid,'type'=>$type));
			} else {
				echo json_encode(array('status'=>'error'));
			}
		}
		if($type=='E'){
			echo json_encode(array('type'=>$type));
		}		
	}

	public function add_duration()
	{
        if (isset($_GET['duration_id'])) {
			$duration_id=$_GET['duration_id'];
		}
		else {
			$duration_id='';
		}
		if($duration_id){
			$data['duration_id']=$duration_id;
			$data['type']='E';
			$data['pageName']='UPDATE Duration';
			$data['btnText']='Update Duration';
			$data['suppliyer']= $this->Duration_Model->get_duration_info($duration_id);	
		}
		else {
			$data['duration_id']='';
			$data['type']='A';
			$data['pageName']='ADD Duration';
			$data['btnText']='Add Duration';
			$data['suppliyer']=array();
		}
        $this->load->view('models/create_duration',$data);
	}

	public function list_duration()
	{
	$requestData= $_REQUEST;
	$data = array();
	$duration = $this->Duration_Model->get_all_duration();
	$totalData = count($duration);
	$totalFiltered = $totalData; 	
	foreach ($duration as $row){
		$nestedData=array(); 		
		$nestedData[] = $row['duration_value'];
		$actionTxtDisble='';
		$actionTxtEnable='';
		$actionTxtUpdate='';
		$actionTxtDelete='';
		$actionTxtUpdate='<a onClick="click_duration_update_btn('.$row['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['status']==1){
			$actionTxtDisble = '<a class="btn btn-xs btn-green tooltips" data-placement="top" data-original-title="Disable supplier" onClick="disabledurationData('.$row['id'].')"><i class="glyphicon fa fa-check"></i></a> &nbsp;';
	}
		if($row['status']==0){
			$actionTxtEnable = '<a class="btn btn-xs btn-warning tooltips" data-placement="top" data-original-title="Disable supplier" onClick="enabledurationData('.$row['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="deletedurationData('.$row['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_duration() {
		$duration_id	= $this->input->post('duration_id');
		$this->Duration_Model->delete_duration($duration_id);
        if ($duration_id) {
        	echo json_encode(array('id'=>$duration_id));
        } else {
        	echo json_encode(array('status'=>'error'));
        }
	}
	
	function disable_duration() {
		$duration_id	= $this->input->post('duration_id');
		$this->Duration_Model->disable_duration($duration_id);
        if ($duration_id) {
        	echo json_encode(array('id'=>$duration_id));
        } else {
        	echo json_encode(array('status'=>'error'));
        }
	}
	
	function enable_duration() {
		$duration_id	= $this->input->post('duration_id');
		$this->Duration_Model->enable_duration($duration_id);
        if ($duration_id) {
        	echo json_encode(array('id'=>$duration_id));
        } else {
        	echo json_encode(array('status'=>'error'));
        }
	}
}