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

class Balance extends CI_Controller {

	public function __construct(){
        parent::__construct();
		$this->load->model('token_model');s
    }


    public function index()
    {
		$headers = apache_request_headers();

		## METHOD INCONNU
		$method = $_SERVER['REQUEST_METHOD'];
		$this->token_model->valideMethod($method,'GET');

		## AUTHENTIFICATION 
		$login = $headers['Authorization'];
		$explode = explode(' ',$login);
		$login = trim($explode[1]);		
		$this->token_model->valideToken($login);
		
		$query = "select customers_accounts.*,country.name as country_name from customers_accounts left join country on country.currency = customers_accounts.currency and country.code = customers_accounts.country where agent_id='".$this->token_model->user_id."'";
		
		$accounts = $this->db->query($query)->result_array();
		
		/** */
		

		if(empty($accounts))
		{
			header('HTTP/1.0 401 Unauthorized');
			$return['error'] =apiError(400,"This account is not configured.");
			local_json($return);
		}
		foreach($accounts as $account)
		{
			
			uses_class('compte');
			$this->AgentCompte = new compte($account['account_number']);
			$this->AgentCompte->load();

			$code = strtoupper($account['country']);
			if(empty($code)) continue;
			$data['country_name']= $account['country_name'];
			$data['account_number']= $account['account_number'];
			$data['service']= strtolower($account['account_type']);
			$data['balance']= doubleval($account['balance']);
			$data['balance_available']= doubleval($account['balance_available']);
			
			$data['currency']= $account['currency'];
			
			$data['country_code']= strtoupper($account['country']);
			
			$result['data'][$code][strtolower($account['account_type'])]=$data;
		}
		
		$reponse['account_balance']= $account['blance'];
		$reponse['account_commission'] = floor($account['commission']*10000)/10000;
		
		local_json($result);
		
    }
	
	public function add()
    {

	}
}