<?php

use Brick\Money\Context\AutoContext;
use Brick\Money\Money;

defined('BASEPATH') or exit('No direct script access allowed');

class Agent extends CI_Controller
{

	public function __construct()
	{
		parent::__construct();
		$this->load->model('wallet_model');
		$this->load->model('nano_credit_model');
		$this->load->model('Users_simple');
		$this->load->model('User_model');
	}

	public function index()
	{
		if ($this->isLogged()) {

			$data['active'] = "health_care_sheets";
			
			$data['member_code'] = $this->session->userdata('member_code');
			$data['category'] = $this->session->userdata('category');
			$data['network'] = $this->session->userdata('network');
			$data['code_parrain'] = $this->session->userdata('parrain');
			$data['network_agent_id'] = $this->session->userdata('network_agent_id');
			$data['network_id'] = $data['id_network'] = $this->session->userdata('network_id');
			$data['hasWallet'] = $this->wallet_model->getConfigWallet($data['network_id']);
			$data['show_health_care_sheet_buttons'] = true;

			if ($this->input->get('history')) {
				$history = $this->input->get('history');
				if($this->input->get('id') !== null){
					if($history == 'insurance-health_care_sheets'){
						$data['active'] = "health_care_sheets";
						$data['health_care_sheet_id'] = $this->input->get('id');
						$data['health_care_sheet'] = $this->nano_health_model->getInfosHealthCareSheetById($this->input->get('id'));
						$data['performances'] = $this->nano_health_model->getHealthCareSheetPerformances($data['health_care_sheet']->id ?? null);
						$data['exams'] = $this->nano_health_model->getHealthCareSheetExams($data['health_care_sheet']->id ?? null);
						$data['prescriptions'] = $this->nano_health_model->getHealthCareSheetPrescriptions($data['health_care_sheet']->id ?? null);

						// Calcul par assuré et assurance
//						$data['amounts'] = $this->nano_health_model->calculateSheetAmountsParts($data['health_care_sheet']->id ?? null, $data['health_care_sheet']->type ?? null);

						$this->load->view('header_agent', $data);
						$this->load->view('nano_health/hyper/infos_health_care_sheet');
					}
					$this->load->view('footer');
					return;
				}else{
					$this->historique($this->input->get('d'), $this->input->get('f'), $this->input->get('history') , $data);
				}
			}else if($this->input->get('action')) {

				$action = $this->input->get('action');
				$nh_config = $this->nano_health_model->getConfig($data['network_id']);
				$data['nh_config'] = $nh_config ? $nh_config->first_row() : null ;
				$data['config_id'] = $data['nh_config'] ? $data['nh_config']->id : null ;
				$data['acts'] = $this->nano_health_model->getConfigActs($data['config_id']);
				$data['provider_classes'] = $this->nano_health_model->getProviderClasses($data['config_id']);
				$data['drugs_and_devices'] =  $this->nano_health_model->getDrugAndDevices($data['network_id']);

				$this->load->view('header_agent', $data);
				if($action == 'new_consultation'){
					$this->load->view('nano_health/agent/health_care_sheet_consultation_form');
				} else if($action == 'update_consultation'){
					$this->load->view('nano_health/agent/health_care_sheet_update_consultation_form');
				} else if($action == 'update_execution'){
					$this->load->view('nano_health/agent/health_care_sheet_update_execution_form');
				} else {
					$this->load->view('nano_health/agent/health_care_sheet_execution_form');
				}
				$this->load->view('footer');
			}else{
				$this->historique($this->input->get('d'), $this->input->get('f'), 'insurance-health_care_sheets' , $data);
			}
		}
	}


	// Wallet
	public function wallet()
	{
		if (!$this->session->userdata('email')) {
			$this->session->set_flashdata('error', 'log in first');

			$data['alert'] = "ok";
			$data['message'] = "Login first!";
			$this->load->view('login', $data);
		} else {
			$id_network = $this->session->userdata('network_id');
			$data["commission"] = "";
			$data["principal"] = "";
			$data["transactions"] = "";
			$agent_id = $this->session->userdata('agent_id');
			$data['hasWallet'] = $this->wallet_model->getConfigWallet($this->session->userdata('network_id'));

			if ($this->input->get('history')) {
				$this->historique($id_network, $this->input->get('d'), $this->input->get('f'), $this->session->userdata('member_code'),
					$this->input->get('history'));
			} else {
				$context = new AutoContext();
				//Create wallet if it not exist

			}

		}
	}

	private function historique($startDate, $endDate,$type , $data)
	{

		$format = $this->session->userdata('site_lang') === 'french' ? 'd-m-Y' : 'Y-m-d';
		$data['startDate'] = $startDate ? date($format, strtotime($startDate)) : null;
		$data['endDate'] = $endDate ? date($format, strtotime($endDate)) : null;
		$endDate = Date('Y-m-d', strtotime($endDate . "+1 day"));

		if ($type == 'insurance-invoices')
			$data['active'] = 'wallet_invoices';
		else if ($type == 'insurance-health_care_sheets')
			$data['active'] = 'wallet_health_care_sheets';

		$this->load->view('header_agent', $data);
		if ($type == 'insurance-invoices')
			$this->load->view('nano_health/hyper/insurances_invoices');
		else if ($type == 'insurance-health_care_sheets')
			$this->load->view('nano_health/hyper/insurances_health_care_sheets');
		$this->load->view('footer');
	}

	private function isLogged()
	{
		if (!$this->session->userdata('email')) {
			$this->session->set_flashdata('error', 'log in first');

			$data['alert'] = "ok";
			$data['message'] = "Login first!";
			$this->load->view('login', $data);
			return false;
		}
		return true;
	}
}
