<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('edit_paying_and_transmitting_networks') . ' - ' . $country->name; ?>
		</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('paying_networks'); ?></h3>
					</div>
					<div class="box-body" style="overflow-x:auto;">

						<?php
						$sql2 = $networks;
						if ($sql2) {
							$numrows = sizeof($sql2);
							$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('Nom'); ?></th>
										<th align='center'>Wallet</th>
										<th align='center'> <?= $this->lang->line('share_rate') ?></th>
										<th align='center'> URL</th>
										<th align='center'> Action</th>
									</tr>
									</thead>
									<tbody>
									<?php
									foreach ($sql2 as $row) {

										$num++;
										//$member_code = randomString1(10);
										echo "<tr>
                                                  <td align='center'>$num</td>
                                                  <td>$row->name</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><?= $row->url ?></td>
										<td>
											<button type="button" data-network-id="<?= $row->id ?>"
													data-config-id="<?= $idConfig ?>" data-num="<?= $num ?>"
													data-toggle="modal" data-target="<?= '#configureRate' . $num ?>"
													class="btn btn-primary btn-block openModal">
												<b><?= $this->lang->line('configure_paying_network'); ?></b>
											</button>
										</td>
										<div class="modal fade" id="<?= 'configureRate' . $num ?>" 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('configure_paying_network'); ?></h3>
													</div>
													<div class="modal-body">
														<form id="<?= 'rateForm' . $num ?>">
															<div class="form-group">
																<label for="rate"
																	   class="col-form-label"><?= $this->lang->line('share_rate') . ' (%)'; ?></label>
																<input type="number" min="0" step=".01" max="100"
																	   required class="form-control"
																	   name="rate" id="<?= 'rate' . $num ?>"
																	   value="<?= $row->rate ?>">
															</div>
															<div class="form-group">
																<label for="url"
																	   class="col-form-label">URL</label>
																<input type="text" required class="form-control"
																	   value="<?= $row->url ?>"
																	   id="<?= 'url' . $num ?>" name="url">
															</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 enableRate">
															<?= $this->lang->line('Valider'); ?></button>
													</div>
												</div>
											</div>
										</div>
										<?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'); ?></h3>
					</div>
					<div class="box-body" style="overflow-x:auto;">

						<?php
						$sql2 = $networks;
						if ($sql2) {
							$sql2 = array_filter($sql2, function ($val) {
								return $val->type == 'autre';
							});
							$numrows = sizeof($sql2);
							$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('Nom'); ?></th>
										<th align='center'>Wallet</th>
										<th align='center'><?= $this->lang->line('Statut'); ?></th>
										<th align='center'> <?= $this->lang->line('share_rate') ?></th>
										<th align='center'> URL</th>
										<th style="text-align: center"> Action</th>
									</tr>
									</thead>
									<tbody>
									<?php
									foreach ($sql2 as $row) {

										$num++;
										//$member_code = randomString1(10);
										echo "<tr>
                                                  <td align='center'>$num</td>
                                                  <td>$row->name</td>
										 	     <td><span class='label label-success'>" . mb_strtoupper($row->type,'UTF-8') . "</span></td>";
										?>
										<?php
										if ($row->is_transmitting_network) {
											?>
											<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
										}
										?>
										<?php
										if ($row->rate_transmitting != null) {
											?>
											<td>
												<span class="label label-success"
													  style="font-size: 1.1em;"><?= $row->rate_transmitting . ' %' ?></span>
											</td>
											<?php
										} else {
											?>
											<td>
												<span class="label label-danger"
													  style="font-size: 1em;"><?= $this->lang->line('not_defined'); ?></span>
											</td>
											<?php
										}
										?>
										<td><?= $row->url_transmitting ?></td>
										<?php
										if ($row->is_transmitting_network) {
											?>
											<td>
												<button type="button" data-network-id="<?= $row->id ?>"
														data-config-id="<?= $idConfig ?>" data-num="<?= $num ?>"
														data-toggle="modal"
														data-target="<?= '#configureTransmittingRate' . $num ?>"
														class="btn btn-primary openModal">
													<b><?= $this->lang->line('configure_transmitter_network'); ?></b>
												</button>
												<button type="button" data-network-id="<?= $row->id ?>"
														data-config-id="<?= $idConfig ?>" data-num="<?= $num ?>"
														class="btn btn-danger openModal disableTransmitting"><?= $this->lang->line('Désactiver'); ?></button>
											</td>
											<?php
										} else {
											?>
											<td>
												<button type="button" data-network-id="<?= $row->id ?>"
														data-toggle="modal"
														data-target="<?= '#configureTransmittingRate' . $num ?>"
														data-config-id="<?= $idConfig ?>" data-num="<?= $num ?>"
														class="btn btn-primary openModal"><?= $this->lang->line('Activer'); ?></button>
											</td>
											<?php
										}
										?>
										<div class="modal fade" id="<?= 'configureTransmittingRate' . $num ?>"
											 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('configure_transmitter_network'); ?></h3>
													</div>
													<div class="modal-body">
														<form id="<?= 'rateTransmittingForm' . $num ?>">
															<div class="form-group">
																<label for="rate"
																	   class="col-form-label"><?= $this->lang->line('share_rate') . ' (%)'; ?></label>
																<input type="number" min="0" step=".01" max="100"
																	   required class="form-control"
																	   name="rate" id="<?= 'rateTransmitting' . $num ?>"
																	   value="<?= $row->rate_transmitting ?>">
															</div>
															<div class="form-group">
																<label for="url"
																	   class="col-form-label">URL</label>
																<input type="text" required class="form-control"
																	   value="<?= $row->url_transmitting ?>"
																	   id="<?= 'urlTransmitting' . $num ?>" name="url">
															</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" data-is-transmitting-network="<?=$row->is_transmitting_network?>"
																class="btn btn-primary enableTransmitting">
															<?= $this->lang->line('Valider'); ?></button>
													</div>
												</div>
											</div>
										</div>
										<?php
									}
									?>
									</tbody>
								</table>

								<?php

							} else {
								echo $this->lang->line('Aucun réseau');
							}
						} else {
							echo $this->lang->line('Aucun réseau');
						}
						?>
					</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('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;
	var num = null;
	$(document).on("click", ".openModal", function () {
		network_id = $(this).data('network-id');
		config_id = $(this).data('config-id');
		num = $(this).data('num');
	})

	$(document).on("click", ".enableRate", function () {
		if ($('#rateForm' + num)[0].checkValidity()) {
			const rate = parseFloat($('#rate' + num).val());
			const url = $('#url' + num).val();
			$.ajax({
				url: '<?= base_url('Hyperviseur_dash/config_sharing_rate')?>',
				type: 'POST',
				dataType: 'json',
				data: {"network_id": network_id, "rate": rate, "url": url, "config_id": config_id},
				success: function (data) {
					if (data == '200') {
						Swal.fire({
							icon: 'success',
							title: "<?= $this->lang->line('paying_network_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 {
			$('#rateForm' + num)[0].reportValidity();
		}

	});

	$(document).on("click", ".enableTransmitting", function () {
		if ($('#rateTransmittingForm' + num)[0].checkValidity()) {
			const rate = parseFloat($('#rateTransmitting' + num).val());
			const url = $('#urlTransmitting' + num).val();
			const  is_enabled = $(this).data('is-transmitting-network');
			$.ajax({
				url: '<?= base_url('Hyperviseur_dash/active_transmitting_network')?>',
				type: 'POST',
				dataType: 'json',
				data: {"network_id": network_id, "config_id": config_id , "rate": rate, "url": url , 'is_enabled' : is_enabled},
				success: function (data) {
					if (data == '200') {
						Swal.fire({
							icon: 'success',
							title: "<?= $this->lang->line('transmitter_network_activated')?>",
							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')?>");
				}
			});
		} else {
			$('#rateTransmittingForm' + num)[0].reportValidity();
		}
	});

	$(document).on("click", ".disableTransmitting", function () {
		$.ajax({
			url: '<?= base_url('Hyperviseur_dash/deactive_transmitting_network')?>',
			type: 'POST',
			dataType: 'json',
			data: {"network_id": network_id, "config_id": config_id},
			success: function (data) {
				if (data == '200') {
					Swal.fire({
						icon: 'success',
						title: "<?= $this->lang->line('transmitter_network_deactivated')?>",
						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>
