<link rel="stylesheet"
	  href="<?= base_url('bower_components/bootstrap-datepicker/dist/css/bootstrap-datepicker.min.css') ?>">
<link rel="stylesheet" href="<?= base_url('bower_components/bootstrap-daterangepicker/daterangepicker.css') ?>">
<link rel="stylesheet" href="<?= base_url('bower_components/toastr/toastr.css') ?>">
<link rel="stylesheet" href="<?= base_url('<link rel="stylesheet" 
	href="https://cdn.datatables.net/1.13.6/css/dataTables.bootstrap.min.css">') ?>">
<style type="text/css">
	.table-wrapper {
		width: 500px;
		margin: 30px auto;
		background: #fff;
		padding: 20px;
		box-shadow: 0 1px 1px rgba(0,0,0,.05);
	}
	.table-title {
		padding-bottom: 10px;
		margin: 0 0 10px;
	}
	.table-title h2 {
		margin: 6px 0 0;
		font-size: 22px;
	}
	.table-title .add-new {
		float: right;
		height: 30px;
		font-weight: bold;
		font-size: 12px;
		text-shadow: none;
		min-width: 100px;
		border-radius: 50px;
		line-height: 13px;
	}
	.table-title .add-new i {
		margin-right: 4px;
	}
	table.table {
		table-layout: fixed;
	}
	table.table tr th, table.table tr td {
		border-color: #e9e9e9;
	}
	table.table th i {
		font-size: 13px;
		margin: 0 5px;
		cursor: pointer;
	}
	table.table th:last-child {
		width: 100px;
	}
	table.table td a {
		cursor: pointer;
		display: inline-block;
		margin: 0 5px;
		min-width: 24px;
	}
	table.table td a.add {
		color: #27C46B;
	}
	table.table td a.edit {
		color: #FFC107;
	}
	table.table td a.delete {
		color: #E34724;
	}
	table.table td i {
		font-size: 19px;
	}
	table.table td a.add i {
		font-size: 24px;
		margin-right: -1px;
		position: relative;
		top: 3px;
	}
	table.table .form-control {
		height: 32px;
		line-height: 32px;
		box-shadow: none;
		border-radius: 2px;
	}
	/*table.table .form-control.error {*/
	/*	border-color: #f50000;*/
	/*}*/
	.error {
		border-color: #f50000;
	}
	table.table td .add {
		display: none;
	}
</style>
<?php
use Brick\Money\CurrencyConverter;
use Brick\Money\ExchangeRateProvider\PDOProvider;
use Brick\Money\ExchangeRateProvider\PDOProviderConfiguration;
use Brick\Money\ExchangeRateProvider\BaseCurrencyProvider;
use Brick\Math\RoundingMode;
use Brick\Money\Money;

// 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
$converter = new CurrencyConverter($provider);

//$money = Money::of('1', 'USD');
//$mone2  = $converter->convert($money, 'XAF', RoundingMode::DOWN);

?>

<div class="content-wrapper">
	<!-- Content Header (Page header) -->
	<section class="content-header">

		<h1>
			<?= $this->lang->line('currency_management'). ' '. $network .' - '.$country; ?>
		</h1>
		<?php
		$site_url = base_url();

		if ($alert == "ok") {

			if (!$success == "ok") {
				?>
				<div class='alert alert-danger alert-dismissible col-xs-6'>
					<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>×</button>
					<h4><i class='icon fa fa-ban'></i> Erreur!</h4>
					<?= $message; ?>
				</div>

				<?php
			} else {
				?>
				<div class="alert alert-success alert-dismissible col-xs-6">
					<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
					<h4><i class="icon fa fa-check"></i> Success!</h4>
					<?= $message; ?>
				</div>

				<?php
			}
		}
		?>

	</section>

	<section class="content">
		<div class="row centered">
			<div class="col-lg-2 col-xs-6">
				<div class="small-box bg-red-active">
					<div class="inner">
						<h4><strong> <?= $currency_code.' - '.$currency_name_fr; ?> </strong></h4>
						<p><?= $this->lang->line('currency') ?></p>
					</div>
					</div>
			</div>
		</div>
		<div class="row">
			<div class="table-wrapper" style="overflow-x:auto;">
							<div class="table-title">
								<div class="row">
									<div class="col-sm-8">
										<label class="col-form-label"><?= $this->lang->line('conversion_rate') ; ?></label>
									</div>
								</div>
							</div>
				<table id="taux_conversion" class="table table-bordered table-striped" >
					<thead>
					<tr>
						<th width="60%"><?= $this->lang->line('currency') ; ?> </th>
						<th><?= $this->lang->line('rate') ; ?> (%) </th>
					</tr>
					</thead>
					<tbody>
					<?php
					if($currencies!=false){
						foreach($currencies->result() as $row) {
							$name = $this->session->userdata('site_lang') === 'french' ? $row->name_fr : $row->name_en;
							echo "<tr>
																	<td>".$row->code.' - '.$name."</td>
																	  <td>".round($provider->getExchangeRate($currency_code ,$row->code)->toFloat(),3)."</td>".'
																	 </tr>';
						}
					}
					?>
					</tbody>
				</table>

				<button type="button" id="save" class="btn btn-primary btn-block" >
					<b><?= $this->lang->line('update_rate'); ?></b>
				</button>
			</div>
		</div>

	</section>
</div>

<!-- jQuery 2.2.4 -->
<script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>

<!-- Bootstrap 3.3.7 -->
<script src="<?= base_url('bower_components/bootstrap/dist/js/bootstrap.min.js') ?>"></script>
<!-- DataTables -->
<script src="https://cdn.datatables.net/1.13.6/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/1.13.6/js/dataTables.bootstrap.min.js"></script>
<!-- Slimscroll -->
<script src="<?= base_url('bower_components/jquery-slimscroll/jquery.slimscroll.min.js') ?>"></script>
<!-- FastClick -->
<script src="<?= base_url('bower_components/fastclick/lib/fastclick.js') ?>"></script>
<!-- AdminLTE App -->
<script src="<?= base_url('dist/js/adminlte.min.js') ?>"></script>
<!-- AdminLTE for demo purposes -->
<script src="<?= base_url('dist/js/demo.js') ?>"></script>
<script src="<?= base_url('bower_components/toastr/toastr.js') ?>"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-multiselect/0.9.15/js/bootstrap-multiselect.js"></script>
<script src="<?= base_url('dist/js/sweetalert2.js') ?>"></script>


<script>
	$(function() {
		$('#taux_conversion').DataTable();
	})
</script>

<script>
	toastr.options.closeButton = true;
	toastr.options.closeMethod = 'fadeOut';
	toastr.options.closeDuration = 5000;
	toastr.options.closeEasing = 'swing';

	$('#save').click(function () {
		$.ajax({
			url: '<?= base_url('Gestion/fetchExchangeRates')?>',
			type: 'GET',
			dataType: 'json',
			success: function (data) {
				if(data===200){
					Swal.fire({
						icon: 'success',
						title: "<?= $this->lang->line('rates_update')?>",
						text:"<?= $this->lang->line('informations_updated')?>",
						timer: 3000
					}).then(()=>{
						location.reload();
					});
				}else{
					toastr.error("<?= $this->lang->line('error_message')?>" , "<?= $this->lang->line('request_error')?>");
				}
			},

			error: function (resultat, statut, error) {
				console.log(resultat + " " + error);
				toastr.error("<?= $this->lang->line('error_message')?>" , "<?= $this->lang->line('request_error')?>");
			}
		});
	});
</script>


<!-- Page script -->
<script src="<?= base_url('dist/js/custom.js') ?>"></script>
