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

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

	public function index()
	{
		if ($this->isLogged()){
			$data['active'] = "dashboard";
			$data['network_id'] = $data['id_network'] = $this->session->userdata('network_id');

			if ($this->input->get('history')) {
				$history_type = $this->input->get('history');
				$id = $this->input->get('id');
				if($history_type == 'requests' && !empty($id)){
					$data['active'] = "requests";
					$data['invoice'] = $this->opening_account_model->getInfosOpeningAccountRequestId($id);
					$data['agent'] = $this->db->get_where('agent_plus',['code_membre'=> $data['invoice']->institution_code ?? null])->first_row();
					$data['health_care_sheets'] = $this->db->get_where('nh_infos_health_care_sheets',['invoice_id'=> $id]);
					$this->load->view('account_opening_agent/header', $data);
					$this->load->view('account_opening_agent/infos_invoice', $data);
					$this->load->view('footer');
				}else{
					$this->history($this->input->get('d'), $this->input->get('f'), $this->input->get('history'), $data);
				}
			}else{

				$data['count_accepted'] = $this->opening_account_model->getCountRequests(null,$this->session->userdata('agent_id'), 'ACCEPTED');
				$data['count_rejected'] = $this->opening_account_model->getCountRequests(null,$this->session->userdata('agent_id'), 'REJECTED');
				$data['count_under_validation'] = $this->opening_account_model->getCountRequests($data['network_id'],null, 'UNDER_VALIDATION');

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

	}

	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');

//		if ($type == 'customer_account_opening_requests'){
//			$data['active'] = 'requests';
//		}


		$this->load->view('account_opening_agent/header', $data);
		if ($type == 'customer_account_opening_requests')
			$this->load->view('account_opening_agent/opening_account_requests');
		$this->load->view('footer');
	}

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

			if ($this->input->get('history')) {
				if ($this->input->get('id') !== null) {
					$data['request'] = $this->opening_account_model->getInfosOpeningAccountRequestId($this->input->get('id'));
					$data['user'] = $this->db->get_where('user_infos', ['user_id' => $data['subscription']->user_id ?? null])->first_row();
					$data['documents'] = $this->opening_account_model->getOpeningAccountRequestDocuments($this->input->get('id'));
					$this->load->view('account_opening_agent/header', $data);
					$this->load->view('account_opening_agent/infos_opening_account_request');
					$this->load->view('footer');
				} else {
					$this->history($data['id_network'], $this->input->get('d'), $this->input->get('f'), $this->input->get('history'));
				}
			}
		}
	}

	public function treatRequest($request_id)
	{
		echo makeRequest('user-service', 'PUT','/customers_accounts_requests/'.$request_id, $_POST);
	}

	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;
	}
}
