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

class ValidatingAgent extends CI_Controller
{
	public function __construct()
	{
		parent::__construct();
		$this->load->model('nano_health_model');
	}

	public function index(){
		if ($this->isLogged()){
			$data['active'] = "dashboard";
			$data['agent_id'] = $this->session->userdata('agent_id');
			$data['id_network'] = $this->session->userdata('network_id');
			$data['acceptedRequests'] = $this->nano_health_model->getNhValidatingAgentSubscriptionHistory($data['agent_id'], 'ACCEPTED');
			$data['rejectedRequests'] = $this->nano_health_model->getNhValidatingAgentSubscriptionHistory($data['agent_id'], 'REJECTED');
			$data['untreatedRequests'] = $this->nano_health_model->getNhInsurancesSubscriptionCount($data['id_network'],'UNDER_VALIDATION');

			$this->load->view('nano_health/validating_agent/header', $data);
			$this->load->view('nano_health/validating_agent/dashboard', $data);
			$this->load->view('footer');
		}
	}

	public function requests(){
		if ($this->isLogged()) {
			$data['id_network'] = $this->session->userdata('network_id');
			$data['active'] = "subscription_requests";
			$data['category'] = $this->session->userdata('role');

			if ($this->input->get('history')) {
				if ($this->input->get('id') !== null) {
					$data['insurance_subscription_id'] = $this->input->get('id');
					$data['subscription'] = $this->nano_health_model->getInfosInsuranceSubscriptionById($this->input->get('id'));
					$data['beneficiaries'] = $this->nano_health_model->getSubscriptionBeneficiaries($data['subscription']->id ?? null);
					$data['user'] = $this->db->get_where('user_infos', ['user_id' => $data['subscription']->user_id ?? null])->first_row();
					$this->load->view('nano_health/validating_agent/header', $data);
					$this->load->view('nano_health/hyper/infos_insurance_subscription');
					$this->load->view('footer');
					return;
				} else {
					$this->history($data['id_network'], $this->input->get('d'), $this->input->get('f'), $this->input->get('history'));
				}
			}
			return;
		}
	}

	public function history($networkId , $startDate , $endDate , $type){

		$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"));

		$data['active'] = "subscription_requests";
		$data['network'] = $this->session->userdata('network');
		$data['id_network'] = $this->session->userdata('network_id');
		$data['country'] = $this->session->userdata('current_pays');
		$data['category'] = $this->session->userdata('role');

		$this->load->view('nano_health/validating_agent/header', $data);
		$this->load->view('nano_health/hyper/insurances_subscriptions.php');
		$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!";

			redirect('index.php', $data);
			return false;
		}
		return true;
	}
}
