<?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 WalletIlinkTransaction extends CI_Controller
{
	private $converter = null;
	private $context = null;

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

		// Load member model
		$this->load->model('pagination/WalletIlinkTransaction_model', 'wit_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->wit_model->getRows($_POST);

		$i = $_POST['start'];
		foreach ($witData as $row) {
			$i++;
			$created = $row->date ; //date('jS M Y', strtotime($row->date));
//			$status = ($row->status == 1)?'Active':'Inactive';
			$operation = mb_strtoupper($row->acteur,'UTF-8') . ' - ' . ($this->session->userdata('site_lang') === 'french' ? $row->operation_fr : $row->operation_en);
			$numero_carte = join(" ", str_split($row->numero_carte, 4));


			$moneyNetInit = Money::of(round($row->montant_net, 2), $row->init_currency, $this->context);
			$agent = $row->agent ? ($row->agent . " | " . $row->code_agent . " | " . $row->phone_agent) : "";
			if ($row->destinataire_phone) {
				$destinataire = $row->destinataire_phone . ' | ' . $row->destinataire_name;
			} else {
				$destinataire = ($row->id_destinataire ? $row->id_destinataire . ' | ' : '') . ($row->nom_destinataire ?? '') . (' ' . $row->prenom_destinataire ? $row->prenom_destinataire : '');

			}
			if ($row->user_phone) {
				$emetteur = $row->user_phone . ' | ' . $row->user;
			} else {
				$emetteur = ($row->nom_emetteur ?? '') . (' ' . $row->prenom_emetteur ? $row->prenom_emetteur : '');
			}
			$moneyNetFinal = Money::of(round($row->montant_net_final_country, 2), $row->final_currency ?? 'XAF', $this->context);

			$data[] = array($row->id_transaction, $operation, $numero_carte, $moneyNetInit->formatTo('fr_FR'), $moneyNetFinal->formatTo('fr_FR'),
				Money::of(round($row->part_reseau_payeur_final_country ??  0, 2), $row->final_currency ?? 'XAF', $this->context)->formatTo('fr_FR'),
				$this->lang->line($row->status_reseau_payeur), $row->final_currency, $row->pays_init,$row->pays_final, Money::of(round($row->frais,2), $row->init_currency,$this->context)->formatTo('fr_FR'),
				Money::of(round($row->taxe, 2),$row->init_currency,$this->context)->formatTo('fr_FR'),Money::of(round($row->commission_banque ?? 0, 2),$row->init_currency,$this->context)->formatTo('fr_FR'),
				Money::of(round($row->commission_hyp ?? 0, 2),$row->init_currency,$this->context)->formatTo('fr_FR'),Money::of(round($row->commission_sup ?? 0 , 2),$row->init_currency,$this->context)->formatTo('fr_FR'),
				Money::of(round($row->commission_ag ?? 0, 2),$row->init_currency,$this->context)->formatTo('fr_FR'), $agent , $emetteur, $destinataire, $created);
		}

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

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

}
