<?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 WalletTransaction extends CI_Controller
{

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

		// Load member model
		$this->load->model('pagination/WalletTransaction_model', 'wt_model');
		$this->load->model('wallet_model');


	}


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

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

		$i = $_POST['start'];
		$fmt = new NumberFormatter( 'fr_FR', NumberFormatter::DECIMAL );
		$config = $this->wallet_model->getConfigWallet($_POST['id_network'])->first_row();
		foreach ($wtData as $row) {

			$type = $row->type_transac == 'credit' ? $this->lang->line('DEPOT')  : $this->lang->line('RETRAIT');

			$banque = 0;
			if($row->type_transac == 'credit'){
				if($row->montant < 0){
					$commission = (-$row->montant * $config->taux_com_client_depot / 100 ) + $config->frais_min_banque_depot;
					$net = $row->montant + $commission;
				}else{
					$commission = ( $row->montant * $config->taux_com_client_depot / 100 ) + $config->frais_min_banque_depot;
					$net = $row->montant - $commission;
				}
				$banque = $row->montant * (1 - ($config->taux_com_client_depot / 100));
			}else{
				$net = $row->montant ;
			}

			$disabled = $row->canceled ? "disabled" : "";
			$disabled1 = $row->deleted ? "disabled" : "";

			$data[] = array($row->id,mb_strtoupper($type,'UTF-8'), join(" ", str_split($row->numCarte, 4)), $fmt->format($net), ($row->type_transac == 'credit' ? $fmt->format($banque) : ""),
				$fmt->format( $row->commission_banque), $fmt->format( $row->commission_hyp), $fmt->format( $row->commission_sup), $fmt->format( $row->commission_ag), $row->agent ,$row->parrain, $row->date_created,
				'<button data-id-transaction="'.$row->id.'" id="cancel" class="btn btn-danger btn-block" '.$disabled.' >
												'.$this->lang->line('cancel').'
											</button>
											<button data-toggle="modal" data-target="#deleteTransaction" data-id-transaction="'.$row->id.'" class="btn btn-danger btn-block openModal" '.$disabled1.' >
												'.$this->lang->line('Supprimer').'
											</button>');
		}

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

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

}
