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

use Brick\Money\Context\AutoContext;
use Brick\Money\CurrencyConverter;
use Brick\Money\ExchangeRateProvider\BaseCurrencyProvider;
use Brick\Money\ExchangeRateProvider\PDOProvider;
use Brick\Money\ExchangeRateProvider\PDOProviderConfiguration;
use Brick\Money\Money;

class Insurances  extends CI_Controller
{
//	private $converter = null;
	private $context = null;

	function __construct()
	{
		parent::__construct();

		// Load member model
		$this->load->model('pagination/Insurances_model', 'model');

		// set to whatever your rates are relative to
		$baseCurrency = 'USD';

		// use your own credentials, or re-use your existing PDO connection
		$pdo = new PDO('mysql:host=' . $this->db->hostname . ';dbname=' . $this->db->database, $this->db->username, $this->db->password);

		$configuration = new PDOProviderConfiguration();

		$configuration->tableName = 'exchange_rate';
		$configuration->exchangeRateColumnName = 'exchange_rate';
		$configuration->targetCurrencyColumnName = 'target_currency';
		$configuration->sourceCurrencyCode = $baseCurrency;

		// this provider loads exchange rates from your database
		$provider = new PDOProvider($pdo, $configuration);

		// this provider calculates exchange rates relative to the base currency
		$provider = new BaseCurrencyProvider($provider, $baseCurrency);

		// this currency converter can now handle any currency pair
//		$this->converter = new CurrencyConverter($provider);
		$this->context = new AutoContext();

	}


	function getLists()
	{
		$data = $row = array();

		// Fetch member's records
		$witData = $this->model->getRows($_POST);

		$i = $_POST['start'];
		$current_url = $_POST['currentURL'];
		foreach ($witData as $row) {
			$i++;

			$bonus_amount =  Money::of(round($row->total_bonus_amount, 2), $row->currency_code, $this->context)->formatTo('fr_FR');

			$data[] = array($row->insured_id , $row->lastname,  $row->phone, $row->number_of_months, $row->number_of_beneficiaries,
				$bonus_amount, $this->lang->line($row->payment_period) , $row->start_at, $row->end_at, $row->created_at,
				'<a href="'.$current_url.'?history=insurance-insured&id='.$row->insured_id.'" class="btn btn-primary" > '.$this->lang->line('Voir plus...').'</a>');
		}

		$output = array(
			"draw" => $_POST['draw'],
			"recordsTotal" => $this->model->countAll($_POST),
			"recordsFiltered" => $this->model->countFiltered($_POST),
			"data" => $data,
		);

		// Output to JSON format
		echo json_encode($output);
	}

}
