<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('<link rel="stylesheet" 
	href="https://cdn.datatables.net/1.13.6/css/dataTables.bootstrap.min.css">') ?>">
<link rel="stylesheet" href="<?= base_url('bower_components/toastr/toastr.css') ?>">
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">


<?php

use Brick\Money\Money;

$context = new \Brick\Money\Context\AutoContext();
?>

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

		<h1>
			Nano crédit <?= $network . ' - ' . $country; ?>
		</h1>
		<?php
		

		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-lg-4 col-xs-6">
				<div class="small-box bg-green-active">
					<div class="inner">
						<h3><?= Money::of(round($configWallet->limite_credit_min, 2), $currency_code, $context)->formatTo('fr_FR') ?></h3>
						<p><?= $this->lang->line('minimum_credit_limit') ?></p>
					</div>
					<div class="icon">
						<i class="ion ion-cash"></i>
					</div>
				</div>
			</div>

			<div class="col-lg-4 col-xs-6">
				<div class="small-box bg-red-active">
					<div class="inner">
						<h3><?= Money::of(round($configWallet->limite_credit_max, 2), $currency_code, $context)->formatTo('fr_FR'); ?></h3>
						<p><?= $this->lang->line('maximum_credit_limit') ?></p>
					</div>
					<div class="icon">
						<i class="ion ion-cash"></i>
					</div>
				</div>
			</div>

		</div>
		<div class="row">
			<div class="col-lg-4">
				<div class="margin">
					<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#modal-limit"
							style="width: 100%"><?= $this->lang->line('change_the_credit_limit') ?></button>
				</div>
			</div>
			<div class="col-lg-4">
				<div class="margin">
					<a class="btn btn-info"
					   href="<?= current_url() . ($network_id ? '?config=nano_credit' : '') ?>"
					   style="width: 100%"><?= $this->lang->line('edit_nano_credit') ?></a>
				</div>
			</div>
			<div class="col-lg-4">
				<div class="margin">
					<a href="<?= current_url() . ($network_id ? '?config=epargne' : '') ?>"
					   class="btn btn-success"
					   style="width: 100%"><?= $this->lang->line('edit_savings') ?></a>
				</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('users_groups') ?></h3>
						<div class="box-tools">
							<a class="btn btn-success" href="
		<?= current_url() . ($network_id ? '?history=savings' : '') ?>">
								<?= $this->lang->line('savings_history'); ?>
							</a>
							<a class="btn btn-primary" href="
		<?= current_url() . ($network_id ? '?history=nano_credit' : '') ?>">
								<?= $this->lang->line('nano_credit_history'); ?>
							</a>
						</div>
					</div>
					<div class="box-body" style="overflow-x:auto;">
						<table id="example1" class="table table-bordered table-striped">
							<thead>
							<tr>
								<?php
								echo "<th>" . $this->lang->line('group_code') . "</th>
											<th>" . $this->lang->line('Nom') . "</th>
											<th>" . $this->lang->line('creator') . "</th>
											<th>" . $this->lang->line('credit_limit') . "</th>
											<th> Status</th>
											<th>" . $this->lang->line('number_of_users') . "</th>
											<th>" . $this->lang->line('created_date') . "</th>
											<th> Action </th>";
								?>
							</tr>
							</thead>
							<tbody>
							<?php
							if ($groups) {
								foreach ($groups->result() as $row) {
									echo "<tr>
		                                                    <td>" . $row->code_groupe . "</td>
		                                                    <td>" . $row->nom . "</td>
		                                                      <td>" . $row->createur . "</td>
		                                                      <td>" . Money::of(round($row->limite_credit, 2), $row->currency_code, $context)->formatTo('fr_FR') . "</td>
														";
									?>
									<?php
									if ($row->actif) {
										?>
										<td>
											<span class="label label-success"><?= $this->lang->line('Actif') ?></span>
										</td>
										<?php
									} else {
										?>
										<td>
											<span class="label label-danger"><?= $this->lang->line('Inactif') ?></span>
										</td>
										<?php
									}
									?>
									<td> <?= $row->nombre_utilisateurs?> </td>

									<td> <?= $row->date_creation ?> </td>
									<td>
										<a href="<?= current_url().('?group='.$row->code_groupe)?>" class="btn btn-primary btn-block" <?php if(!$row->id_network) echo "disabled"?> >
											<b><?= $this->lang->line('Voir plus...'); ?></b>
										</a>
									</td>
									</tr>
									<?php
								}
								?>
								<?php
							}
							?>
							</tbody>
						</table>
					</div>
				</div>
			</div>
		</div>
		<div class="modal fade" id="modal-limit">
			<div class="modal-dialog">
				<div class="modal-content">
					<div class="modal-header">
						<button type="button" class="close" data-dismiss="modal" aria-label="Close">
							<span aria-hidden="true">&times;</span>
						</button>
						<h3 class="modal-title"><?= $this->lang->line('set_credit_limit'); ?></h3>
					</div>
					<div class="modal-body">
						<form id="limitForm">
							<div class="row">
								<div class="col-lg-12">
									<div class="form-group">
										<label for="limit_min"
											   class="col-form-label"><?= $this->lang->line('minimum_credit_limit'); ?></label>
										<input type="number" min="0" step=".01" required class="form-control"
											   id="limit_min" name="limit_min"
											   value="<?= $configWallet->limite_credit_min ?>">
									</div>
									<div class="form-group">
										<label for="limit_max"
											   class="col-form-label"><?= $this->lang->line('maximum_credit_limit'); ?></label>
										<input type="number" min="0" step=".01" required class="form-control"
											   name="limit_max" id="limit_max"
											   value="<?= $configWallet->limite_credit_max ?>">
									</div>
								</div>
							</div>
						</form>
					</div>
					<div class="modal-footer">
						<button type="button" class="btn btn-default pull-left"
								data-dismiss="modal"><?= $this->lang->line('Fermer'); ?></button>
						<button type="button" id="saveLimit" data-network-id="<?= $network_id ?>"
								class="btn btn-primary"><?= $this->lang->line('save'); ?></button>
					</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/moment/min/moment.min.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-datetimepicker/4.17.47/js/bootstrap-datetimepicker.min.js"></script>
<!-- ChartJS -->
<script src="<?= base_url('bower_components/chart.js/Chart.js') ?>"></script>
<script src="<?= base_url('bower_components/toastr/toastr.js') ?>"></script>
<script src="<?= base_url('dist/js/sweetalert2.js') ?>"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/momentjs/latest/moment-with-locales.min.js"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/daterangepicker/daterangepicker.min.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/plug-ins/1.10.19/sorting/datetime-moment.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/plug-ins/1.10.20/dataRender/datetime.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/buttons/1.6.2/js/dataTables.buttons.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.1.3/jszip.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.53/pdfmake.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.53/vfs_fonts.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/buttons/1.6.2/js/buttons.html5.min.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/buttons/1.6.2/js/buttons.print.min.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/buttons/1.6.2/js/buttons.colVis.min.js"></script>
<script>
	$(function () {
		const lang = "<?= $this->session->userdata('site_lang') ?>";
		const format = lang === 'french' ? 'fr' : 'en';
		moment.updateLocale(moment.locale(format), {invalidDate: ""});

		$('#example1').DataTable({
			"aaSorting": [[6, "desc"]],
			"columnDefs": [{
				targets: 6,
				render: $.fn.dataTable.render.moment('YYYY-MM-DD HH:mm:ss', 'D MMMM YYYY HH:mm:ss', format)
			}]
		});
	})
</script>

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


	$('#saveLimit').click(function () {
		const limit_min = parseFloat($('#limit_min').val());
		const limit_max = parseFloat($('#limit_max').val());
		const network_id = $(this).data('network-id');

		if (limit_max > limit_min) {
			if ($('#limitForm')[0].checkValidity()) {

				$.ajax({
					url: '<?= base_url('Hyperviseur_dash/saveCreditLimit')?>',
					type: 'POST',
					dataType: 'json',
					data: {
						"limit_min": limit_min,
						"limit_max": limit_max,
						"id_network": network_id,
					},
					success: function (data) {
						if (data == '200') {
							Swal.fire({
								icon: 'success',
								title: "<?= $this->lang->line('update_credit_limit')?>",
								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')?>");
					}
				});


			} else {
				$('#limitForm')[0].reportValidity();
			}
		} else {
			toastr.error("<?= $this->lang->line('credit_limit_rule')?>", "<?= $this->lang->line('request_error')?>");
		}

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