<link rel="stylesheet"
	  href="<?= base_url('<link rel="stylesheet" 
	href="https://cdn.datatables.net/1.13.6/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="<?= base_url('bower_components/toastr/toastr.css') ?>">
<div class="content-wrapper">
	<!-- Content Header (Page header) -->
	<section class="content-header">

		<h1>
			<?= $this->lang->line('paying_and_transmitting_networks_accounts')?>
		</h1>
		<?php

		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>
					<?= $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">
			<div class="col-xs-12">
				<div class="box">
					<div class="box-header">
						<h3 class="box-title"><?= $this->lang->line('paying_networks_accounts'); ?></h3>
						<div class="box-tools">
							<a class="btn btn-success" href="<?= current_url() . '?history=refund_history' ?>">
								<?= $this->lang->line('refund_history'); ?>
							</a>
						</div>
					</div>
					<div class="box-body" style="overflow-x:auto;">

						<?php
						$sql2 = $networks;
						if ($sql2) {
							$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><?= $this->lang->line('Nom'); ?></th>
										<th><?= $this->lang->line('Pays'); ?></th>
										<th align='center'>Wallet</th>
										<th align='center'> <?= $this->lang->line('share_rate') ?></th>
										<th><?= $this->lang->line('Solde Commission') ?></th>
										<th><?= $this->lang->line('compensation_balance') ?></th>
										<th>Action</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'>".mb_strtoupper($row->type,'UTF-8')."</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;"><?= $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>
										<td>
											<?= Money::of(round($row->balance_compensation, 2), $row->currency_code ? $row->currency_code : 'XAF', $context)->formatTo('fr_FR') ?>
										</td>
										<td>
											<button class="btn btn-primary btn-block openModal" data-toggle="modal"
													data-target="#refundModal" data-network-id="<?= $row->id ?>"
													data-config-id="<?= $row->config_id ?>"
													<?php if ($row->balance_compensation == 0) echo "disabled" ?>>
												<b><?= $this->lang->line('refund_compensation'); ?></b>
											</button>
										</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="row">
			<div class="col-xs-12">
				<div class="box">
					<div class="box-header">
						<h3 class="box-title"><?= $this->lang->line('transmitting_networks_accounts'); ?></h3>
						<div class="box-tools">
							<a class="btn btn-success" href="<?= current_url() . '?history=refund_history' ?>">
								<?= $this->lang->line('refund_history'); ?>
							</a>
						</div>
					</div>
					<div class="box-body" style="overflow-x:auto;">

						<?php
						$sql2 = $transmitting_networks;
						if ($sql2) {
							$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><?= $this->lang->line('Nom'); ?></th>
										<th><?= $this->lang->line('Pays'); ?></th>
										<th align='center'>Wallet</th>
										<th align='center'> <?= $this->lang->line('share_rate') ?></th>
										<th><?= $this->lang->line('Solde Commission') ?></th>
										<th><?= $this->lang->line('compensation_balance') ?></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'>".mb_strtoupper($row->type,'UTF-8')."</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;"><?= $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>
										<td>
											<?= Money::of(round($row->balance_compensation, 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="refundModal" 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"><?= $this->lang->line('refund_compensation'); ?></h3>
					</div>
					<div class="modal-body">
						<?php if ($walletPassword != null) { ?>
							<form id="refundForm">
								<div class="form-group">
									<label for="montant"
										   class="col-form-label"><?= $this->lang->line('Montant') ?></label>
									<input type="text" required class="form-control" id="montant" name="montant">
								</div>
								<div class="form-group">
									<label><?= $this->lang->line('mot de passe') ?></label>
									<div class="input-group" id="show_hide_password">
										<input class="form-control" name="password" id="password" type="password">
										<div class="input-group-addon">
											<a href=""><i class="fa fa-eye-slash" aria-hidden="true"></i></a>
										</div>
									</div>
								</div>
								<div class="clearfix">
									<a href="#" id="resetPassword" class="pull-right forgot-password"
									   data-wallet_password_id="<?= $walletPassword->id ?>"
									   data-network="<?= $network ?>" data-country="<?= $country ?>"
									   data-email="<?= $walletPassword->email ?>">
										<?= $this->lang->line('i_forgot_password') ?>
									</a>
								</div>
							</form>
						<?php } else { ?>
							<div class="text-center">
								<h4> <?= $this->lang->line('no_wallet_password') ?></h4>
								<a href="<?= base_url('Hyperviseur_dash/walletPassword') ?>" alt=""
								   class="btn btn-primary"><?= $this->lang->line('click_here') ?></a>
							</div>
						<?php } ?>
					</div>
					<div class="modal-footer">
						<button type="button" class="btn btn-secondary"
								data-dismiss="modal"><?= $this->lang->line('Fermer'); ?></button>
						<?php if ($walletPassword != null) { ?>
							<button type="button" class="btn btn-primary" data-wallet_id="<?= 12 ?>"
									data-salt="<?= $walletPassword->salt ?>"
									data-encrypted_password="<?= $walletPassword->encrypted_password ?>"
									id="refund">
								<?= $this->lang->line('refund'); ?>
							</button>
						<?php } ?>
					</div>
				</div>
			</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="<?= base_url('bower_components/bootstrap-daterangepicker/daterangepicker.js') ?>"></script>
<script src="<?= base_url('bower_components/bootstrap-datepicker/dist/js/bootstrap-datepicker.min.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 src="https://cdn.jsdelivr.net/npm/autonumeric@4.5.4"></script>

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

		anElement = new AutoNumeric('#montant', '', {
			digitGroupSeparator: ' ',
			decimalPlaces: '0',
			minimumValue: '1',
			maximumValue: '99999999999999999999999999'
		});

		$("#show_hide_password a").on('click', function (event) {
			event.preventDefault();
			if ($('#show_hide_password input').attr("type") == "text") {
				$('#show_hide_password input').attr('type', 'password');
				$('#show_hide_password i').addClass("fa-eye-slash");
				$('#show_hide_password i').removeClass("fa-eye");
			} else if ($('#show_hide_password input').attr("type") == "password") {
				$('#show_hide_password input').attr('type', 'text');
				$('#show_hide_password i').removeClass("fa-eye-slash");
				$('#show_hide_password i').addClass("fa-eye");
			}
		});
	})
</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');
	});

	$('#refund').click(function () {

		const salt = $(this).data('salt');
		const encrypted_password = $(this).data('encrypted_password');

		if ($('#refundForm')[0].checkValidity()) {
			const montant = anElement.getNumber();  //parseFloat($('#montant').val());
			const password = $('#password').val();

			$.ajax({
				url: '<?= base_url('Hyperviseur_dash/rembourser_compensation')?>',
				type: 'POST',
				dataType: 'json',
				data: {
					"id_config": config_id,
					"id_network": network_id,
					"montant": montant,
					"password": password,
					"salt": salt,
					"encrypted_password": encrypted_password
				},
				success: function (data) {
					if (data == '200') {
						Swal.fire({
							icon: 'success',
							title: "<?= $this->lang->line('refund_made')?>",
							text: "<?= $this->lang->line('informations_updated')?>",
							timer: 3000
						}).then(() => {
							location.reload();
						});
						// alert("Les informations ont été mises à jour.") ? "" :
					} else if (data == '400') {
						toastr.error("<?= $this->lang->line('incorrect_password')?>", "<?= $this->lang->line('request_error')?>");
					} else if (data == '401') {
						toastr.error("<?= $this->lang->line('amount_greater_than_balance')?>", "<?= $this->lang->line('request_error')?>");
					} 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')?>");
				}
			});


		} else {
			$('#refundForm')[0].reportValidity();
		}

	});

	$('#resetPassword').click(function () {
		const wallet_password_id = $(this).data('wallet_password_id');
		const network = $(this).data('network');
		const country = $(this).data('country');
		const email = $(this).data('email');
		$.ajax({
			url: '<?= base_url('Hyperviseur_dash/resetWalletPassword')?>',
			type: 'POST',
			dataType: 'json',
			data: {
				"wallet_password_id": wallet_password_id,
				"email": email,
				"network": network,
				"country": country
			},
			success: function (data) {
				if (data == '200') {
					Swal.fire({
						icon: 'success',
						title: "<?= $this->lang->line('password_has_been_reset')?>",
						text: "<?= $this->lang->line('informations_updated')?>",
						timer: 3000
					}).then(() => {
						location.reload();
					});
					// alert("Les informations ont été mises à jour.") ? "" :
				} 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>
