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

class Interest_Rate extends CI_Controller {

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

		$this->load->model('Interest_Rate_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('interest_rate',$data);
	}
	
	
	public function save_interest_rate()
	{
		$interest_rate_id=$this->input->post('rate_id');
		$type=$this->input->post('type');
		$interest_rate_name=$this->input->post('interest_rate');
		$loan_payment_term=$this->input->post('loan_payment_term');		
		$data=array(
			'interest_rate_value'=>$interest_rate_name,	
			'preferd_paymet_type'=>$loan_payment_term		
		);		
       	$_insert=$this->Interest_Rate_Model->save_interest_rate($data,$interest_rate_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_interest_rate()
	{
        if (isset($_GET['rate_id'])) {
			$interest_rate_id=$_GET['rate_id'];
		}
		else {
			$interest_rate_id='';
		}
		if($interest_rate_id){
			$data['rate_id']=$interest_rate_id;
			$data['type']='E';
			$data['pageName']='UPDATE Rate';
			$data['btnText']='Update Rate';
			$data['suppliyer']= $this->Interest_Rate_Model->get_interest_rate_info($interest_rate_id);	
		}
		else {
			$data['rate_id']='';
			$data['type']='A';
			$data['pageName']='ADD Rate';
			$data['btnText']='Add Rate';
			$data['suppliyer']=array();
		}
		
		$this->load->model('Loan_Model');
		$data['payment_term_list']= $this->Loan_Model->get_loan_payments_term();
        $this->load->view('models/create_interest_rate',$data);
	}

	public function list_interest_rate()
	{
	$requestData= $_REQUEST;
	
	
	
	$data = array();
	$interest_rate = $this->Interest_Rate_Model->get_all_interest_rate();
	$totalData = count($interest_rate);
	$totalFiltered = $totalData;  
	
	foreach ($interest_rate as $row){
		$nestedData=array(); 
		
		$nestedData[] = $row['interest_rate_value'];
		$actionTxtDisble='';
		$actionTxtEnable='';
		$actionTxtUpdate='';
		$actionTxtDelete='';
		$actionTxtUpdate='<a onClick="click_interest_rate_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="disableinterest_rateData('.$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="enableinterest_rateData('.$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="deleteinterest_rateData('.$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_interest_rate() {
		$interest_rate_id	= $this->input->post('rate_id');
		$this->Interest_Rate_Model->delete_interest_rate($interest_rate_id);
        if ($interest_rate_id) {
        	echo json_encode(array('id'=>$interest_rate_id));
        } else {
        	echo json_encode(array('status'=>'error'));
        }
	}
	
	function disable_interest_rate() {
		$interest_rate_id	= $this->input->post('rate_id');
		$this->Interest_Rate_Model->disable_interest_rate($interest_rate_id);
        if ($interest_rate_id) {
        	echo json_encode(array('id'=>$interest_rate_id));
        } else {
        	echo json_encode(array('status'=>'error'));
        }
	}
	
	function enable_interest_rate() {
		$interest_rate_id	= $this->input->post('rate_id');
		$this->Interest_Rate_Model->enable_interest_rate($interest_rate_id);
        if ($interest_rate_id) {
        	echo json_encode(array('id'=>$interest_rate_id));
        } else {
        	echo json_encode(array('status'=>'error'));
        }
	}
}