<?php 
Class Account extends CI_Controller
{
	function __construct(){
		parent::__construct();
	} 

	function login(){ 
		$this->load->helper('custom_helper');

		api_header();  

		$resp=array(); 
		$resp['code'] = 200;  
		$resp['message'] = 'Success!';

		if(!empty($_POST['username']) && !empty($_POST['password'])){ 
			$condition=array();
			$condition[] = array('name'=>'username','value'=>$_POST['username']);
			$condition[] = array('name'=>'password','value'=>md5($_POST['password']));
			$select = array('admin_id','username');
			$res= $this->setting_model->get_setting('wb_admin',$condition,false,false,$select);

			if ($res) {  
				$api_token= implode('-', str_split(substr(strtolower(md5(microtime().rand(1000, 9999))), 0, 30), 6));

				$save=array(); 
				$save['admin_id']=$res['admin_id']; 
				$save['api_token'] =$api_token;   
				$this->setting_model->save_setting('admin',$save);  

				$resp['admin_id'] = $res['admin_id'];
				$resp['username'] =$res['username'];	
				$resp['api_token'] =$api_token;   

				$resp['status'] = 'success'; 
				$resp['code'] = 200;  
				$resp['message'] = 'User logged in successfully!';		
			}
			else{
				$resp['status'] = 'error'; 
				$resp['code'] = 203;  
				$resp['message'] = 'Invalid username/password!';
			} 
		}
		else{
			$resp['status'] = 'error'; 
			$resp['code'] = 400;  
			$resp['message'] = 'Please fill mandatory fields!';    
		} 

		echo json_encode($resp); 
	} 

	function dashboard(){
		$this->load->helper('custom_helper');

		api_header();  
		token_verify();

		$resp=array();
		$condition = array();
		$condition[] = array('name'=>'deleted','value'=>'0'); 
		$select =array("count(*) as tot_rec");
		$res = $this->setting_model->get_setting('entry',$condition,false,false,$select);

		if($res)
			$tot_entry=$res['tot_rec'];
		else
			$tot_entry=0;

		$condition = array();
		$condition[] = array('name'=>'deleted','value'=>'0'); 
		$select =array("sum(net_amt) as tot_amt");
		$res = $this->setting_model->get_setting('entry',$condition,false,false,$select);

		if($res)
			$tot_amt=$res['tot_amt'];
		else
			$tot_amt=0; 

		$data['tot_entry']=$tot_entry;
		$data['tot_amt']=$tot_amt;

		$q = "SELECT ip_address,port FROM wb_printer";
		$res = $this->db->query($q)->row_array();   
		$data['ip_address']=$res['ip_address'];
		$data['port']=$res['port'];

		$resp['res'] =$data;
		$resp['status'] = 'success'; 
		$resp['code'] = 200;  
		$resp['message'] = 'Success!';		

		echo json_encode($resp); 
	}


}
?>