<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') ?>">

<?php

use Brick\Money\Money;

$context = new \Brick\Money\Context\AutoContext();
?>
<div class="content-wrapper">
	<!-- Content Header (Page header) -->
	<section class="content-header">

		<h1>
			<?= $this->lang->line('management_regulatory'); ?>
		</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">
			<div class="col-xs-12">
				<div class="box">
					<div class="box-header">
						<h3 class="box-title"><?= $this->lang->line('activated_countries'); ?></h3>
					</div>
					<div class="box-body" style="overflow-x:auto;">

						<?php

						$sql2 = $countries;
						if($sql2!=false){
							$numrows=$sql2->num_rows();
							$num = 0;
							if ($numrows > 0) {
								?>
								<table id="validated" class="table table-bordered table-striped">
									<thead>
									<tr>
										<th align='center'>N°</th>
										<th><?= $this->lang->line('Pays'); ?></th>
										<th align='center'><?= $this->lang->line('max_amount_per_day').' national'; ?></th>
										<th align='center'><?= $this->lang->line('max_amount_per_week').' national'; ?></th>
										<th align='center'><?= $this->lang->line('max_amount_per_month').' national'; ?></th>
										<th align='center'><?= $this->lang->line('max_amount_per_day').' international'; ?></th>
										<th align='center'><?= $this->lang->line('max_amount_per_week').' international'; ?></th>
										<th align='center'><?= $this->lang->line('max_amount_per_month').' international'; ?></th>
										<th align='center'> 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->country</td>";
										?>
										<?php
											?>
											<td> <?php if($row->id_country) echo Money::of(round($row->montant_max_jour_national, 2), $row->currency_code, $context)->formatTo('fr_FR') ?></td>
											<td><?php  if($row->id_country) echo Money::of(round($row->montant_max_hebdo_national, 2), $row->currency_code, $context)->formatTo('fr_FR') ?></td>
											<td><?php  if($row->id_country) echo Money::of(round($row->montant_max_mensuel_national, 2), $row->currency_code, $context)->formatTo('fr_FR'); ?></td>
											<td> <?php if($row->id_country) echo Money::of(round($row->montant_max_jour_international, 2), $row->currency_code, $context)->formatTo('fr_FR') ?></td>
											<td><?php  if($row->id_country) echo Money::of(round($row->montant_max_hebdo_international, 2), $row->currency_code, $context)->formatTo('fr_FR') ?></td>
											<td><?php  if($row->id_country) echo Money::of(round($row->montant_max_mensuel_international, 2), $row->currency_code, $context)->formatTo('fr_FR'); ?></td>
												<?php

										?>
										<td>
											<button  type="button" data-country-id="<?= $row->id ?>"
													 data-num ="<?=$num?>" class="btn btn-primary btn-block openModal"
													 data-toggle="modal" data-target="<?='#configureLimits'.$num?>" >
												<b><?= $this->lang->line('define_the_limits'); ?></b>
											</button>
										</td>

										<div class="modal fade" id="<?='configureLimits'.$num?>" tabindex="-1" role="dialog" aria-hidden="true">
											<div class="modal-dialog modal-lg" role="document">
												<div class="modal-content">
													<div class="modal-header">
														<h3 class="modal-title"><?= $this->lang->line('define_the_limits'); ?></h3>
													</div>
													<div class="modal-body">
														<form id="<?='limitsForm'.$num?>" class="row">
																<div class="col-lg-6 col-md-6">
																	<div class="form-group">
																		<label for="rate"
																			   class="col-form-label"><?= $this->lang->line('max_amount_per_day').' national' ?></label>
																		<input type="number" min="0" step=".01" required class="form-control"
																			   name="max_day_national" id="<?='max_day_national'.$num?>" value="<?=$row->montant_max_jour_national?>">
																	</div>
																	<div class="form-group">
																		<label for="url"
																			   class="col-form-label"><?= $this->lang->line('max_amount_per_week').' national' ?></label>
																		<input type="number" min="0" step=".01" required class="form-control" value="<?=$row->montant_max_hebdo_national?>"
																			   id="<?='max_week_national'.$num?>" name="max_week_national">
																	</div>

																	<div class="form-group">
																		<label for="url"
																			   class="col-form-label"><?= $this->lang->line('max_amount_per_month').' national' ?></label>
																		<input  type="number" min="0" step=".01" required class="form-control" value="<?=$row->montant_max_mensuel_national?>"
																				id="<?='max_month_national'.$num?>" name="max_month_national">
																	</div>
																</div>
																<div class="col-lg-6 col-md-6">
																	<div class="form-group">
																		<label for="rate"
																			   class="col-form-label"><?= $this->lang->line('max_amount_per_day').' international' ?></label>
																		<input type="number" min="0" step=".01" required class="form-control"
																			   name="max_day_international" id="<?='max_day_international'.$num?>" value="<?=$row->montant_max_jour_international?>">
																	</div>
																	<div class="form-group">
																		<label for="url"
																			   class="col-form-label"><?= $this->lang->line('max_amount_per_week').' international' ?></label>
																		<input type="number" min="0" step=".01" required class="form-control" value="<?=$row->montant_max_hebdo_international?>"
																			   id="<?='max_week_international'.$num?>" name="max_week_international">
																	</div>

																	<div class="form-group">
																		<label for="url"
																			   class="col-form-label"><?= $this->lang->line('max_amount_per_month').' international' ?></label>
																		<input  type="number" min="0" step=".01" required class="form-control" value="<?=$row->montant_max_mensuel_international?>"
																				id="<?='max_month_international'.$num?>" name="max_month_international">
																	</div>
																</div>
														</form>
													</div>
													<div class="modal-footer">
														<button type="button" class="btn btn-secondary"
																data-dismiss="modal"><?= $this->lang->line('Fermer'); ?></button>
														<button type="button" class="btn btn-primary setLimits">
															<?= $this->lang->line('Valider'); ?></button>
													</div>
												</div>
											</div>
										</div>
										<?php
											}
										?>
									</tbody>
								</table>

								<?php

							}

							else {
								echo $this->lang->line('Aucun pays');
							}
						}else {
							echo $this->lang->line('Aucun pays');
						}
						?>
					</div>
				</div>
			</div>
		</div>
<!--		<div class="alert alert-info alert-dismissible" aria-hidden="true">-->
<!--			<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>-->
<!--			<h4><i class="icon fa fa-info"></i>Info!</h4>-->
<!--			Data is Saved-->
<!--		</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() {
		$('#validated').DataTable();
	})
</script>

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

	var country_id = null;
	var num = null;
	$(document).on("click", ".openModal", function () {
		country_id = $(this).data('country-id');
		num = $(this).data('num');
	})

	$(document).on("click", ".setLimits", function () {
		if ($('#limitsForm'+num)[0].checkValidity()) {

			const max_day_national = parseFloat($('#max_day_national'+num).val());
			const max_week_national = parseFloat($('#max_week_national'+num).val());
			const max_month_national = parseFloat($('#max_month_national'+num).val());

			const max_day_international = parseFloat($('#max_day_international'+num).val());
			const max_week_international = parseFloat($('#max_week_international'+num).val());
			const max_month_international = parseFloat($('#max_month_international'+num).val());
			$.ajax({
				url: '<?= base_url('Gestion/setRegulationsLimits')?>',
				type: 'POST',
				dataType: 'json',
				data: {"country_id": country_id,
					"max_day_national": max_day_national, "max_week_national": max_week_national,"max_month_national" : max_month_national,
					"max_day_international": max_day_international, "max_week_international": max_week_international,"max_month_international" : max_month_international
				},
				success: function (data) {
					if (data == '200') {
						Swal.fire({
							icon: 'success',
							title: "<?= $this->lang->line('regulations_updated')?>",
							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 {
			$('#limitsForm'+num)[0].reportValidity();
		}

	});

</script>
