<link rel="stylesheet"
	  href="<?php echo base_url('bower_components/datatables.net-bs/css/dataTables.bootstrap.min.css') ?>">
<link rel="stylesheet"
	  href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-multiselect/0.9.15/css/bootstrap-multiselect.css">
<link rel="stylesheet" href="<?php echo base_url('bower_components/toastr/toastr.css') ?>">
<div class="content-wrapper">
	<!-- Content Header (Page header) -->
	<section class="content-header">

		<h1>
			<?php echo $this->lang->line('paying_networks_accounts')?>
		</h1>
		<?php

		use Brick\Money\Context\CustomContext;
		use Brick\Money\Money;
		$context = new \Brick\Money\Context\AutoContext();
		$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>
					<?php echo $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>
					<?php echo $message; ?>
				</div>

				<?php
			}
		}
		?>
	</section>

	<section class="content">
		<div class="row">
			<div class="col-xs-12">
				<div class="box">
					<div class="box-header">
						<h3 class="box-title"><?php echo $this->lang->line('paying_networks'); ?></h3>
					</div>
					<div class="box-body" style="overflow-x:auto;">

						<?php
						$sql2 = $networks;
						if ($sql2 != false) {
							$numrows = $networks->num_rows();
							$num = 0;
							if ($numrows > 0) {
								$fmt = new NumberFormatter( 'fr_FR', NumberFormatter::DECIMAL );
								?>
								<table id="validated" class="table table-bordered table-striped">
									<thead>
									<tr>
										<th align='center'>N°</th>
										<th><?php echo $this->lang->line('Nom'); ?></th>
										<th><?php echo $this->lang->line('Pays'); ?></th>
										<th align='center'>Wallet</th>
										<th align='center'> <?php echo $this->lang->line('share_rate')?></th>
										<th><?=$this->lang->line('Solde Commission')?></th>
									</tr>
									</thead>
									<tbody>
									<?php
									foreach ($sql2->result() as $row) {

										$num++;
										//$member_code = randomString1(10);
										echo "<tr>
                                                  <td align='center'>$num</td>
                                                  <td>$row->name</td>
                                                    <td>$row->country</td>
										 	     <td><span class='label label-success'>".strtoupper($row->type)."</span></td>";
										?>
										<?php
										if ($row->rate != null ) {
											?>
											<td>
												<span class="label label-success" style="font-size: 1.1em;"><?= $row->rate . ' %'?></span>
											</td>
											<?php
										} else {
											?>
											<td>
												<span class="label label-danger" style="font-size: 1em;" ><?php echo $this->lang->line('not_defined'); ?></span>
											</td>
											<?php
										}
										?>
										<td>
											<?= Money::of(round($row->balance_com,2 ),$row->currency_code ? $row->currency_code  : 'XAF',$context)->formatTo('fr_FR')?>
										</td>
										<?php
									}
									?>
									</tbody>
								</table>

								<?php

							} else {
								echo $this->lang->line('Aucun réseau');
							}
						} else {
							echo $this->lang->line('Aucun réseau');
						}
						?>
					</div>
				</div>
			</div>
		</div>
		<div class="modal fade" id="configureRate" tabindex="-1" role="dialog" aria-hidden="true">
			<div class="modal-dialog" role="document">
				<div class="modal-content">
					<div class="modal-header">
						<h3 class="modal-title"><?php echo $this->lang->line('configure_share_rate'); ?></h3>
					</div>
					<div class="modal-body">
						<form id="rateForm">
							<div class="form-group">
								<label for="rate"
									   class="col-form-label"><?php echo $this->lang->line('share_rate') . ' (%)'; ?></label>
								<input type="number" min="0" step=".01" max="100" required class="form-control"
									   name="rate" id="rate" value="0">
							</div>
						</form>
					</div>
					<div class="modal-footer">
						<button type="button" class="btn btn-secondary"
								data-dismiss="modal"><?php echo $this->lang->line('Fermer'); ?></button>
						<button type="button" class="btn btn-primary"
								id="enableRate"> <?php echo $this->lang->line('Valider'); ?></button>
					</div>
				</div>
			</div>
		</div>
	</section>
</div>

<!-- jQuery 3 -->
<script src="<?php echo base_url('bower_components/jquery/dist/jquery.min.js') ?>"></script>
<!-- Bootstrap 3.3.7 -->
<script src="<?php echo base_url('bower_components/bootstrap/dist/js/bootstrap.min.js') ?>"></script>
<!-- DataTables -->
<script src="<?php echo base_url('bower_components/datatables.net/js/jquery.dataTables.min.js') ?>"></script>
<script src="<?php echo base_url('bower_components/datatables.net-bs/js/dataTables.bootstrap.min.js') ?>"></script>
<!-- Slimscroll -->
<script src="<?php echo base_url('bower_components/jquery-slimscroll/jquery.slimscroll.min.js') ?>"></script>
<!-- FastClick -->
<script src="<?php echo base_url('bower_components/fastclick/lib/fastclick.js') ?>"></script>
<!-- AdminLTE App -->
<script src="<?php echo base_url('dist/js/adminlte.min.js') ?>"></script>
<!-- AdminLTE for demo purposes -->
<script src="<?php echo base_url('dist/js/demo.js') ?>"></script>
<script src="<?php echo 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="<?php echo base_url('dist/js/sweetalert2.js') ?>"></script>

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

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

	var network_id = null;
	var config_id = null;
	$(document).on("click", ".openModal", function () {
		network_id = $(this).data('network-id');
		config_id = $(this).data('config-id');
	})

	$(document).on("click", "#enableRate", function () {
		// const network_id = $(this).data('network-id');
		if ($('#rateForm')[0].checkValidity()) {
			const rate = parseFloat($('#rate').val());
			$.ajax({
				url: '<?php echo base_url('index.php/Hyperviseur_dash/config_sharing_rate')?>',
				type: 'POST',
				dataType: 'json',
				data: {"network_id": network_id, "rate": rate , "config_id" : config_id},
				success: function (data) {
					if (data == '200') {
						Swal.fire({
							icon: 'success',
							title: "<?php echo $this->lang->line('sharing_updated')?>",
							text: "<?php echo $this->lang->line('informations_updated')?>",
							timer: 3000
						}).then(() => {
							location.reload();
						});

					} else {
						toastr.error("<?php echo $this->lang->line('error_message')?>", "<?php echo $this->lang->line('request_error')?>");
					}
				},
				error: function (resultat, statut, error) {
					console.log(resultat + " " + error);
					toastr.error("<?php echo $this->lang->line('error_message')?>", "<?php echo $this->lang->line('request_error')?>");
				}
			});
		} else {
			$('#rateForm')[0].reportValidity();
		};
	});

</script>
