<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">
<link rel="stylesheet" href="<?= base_url('dist/css/custom/levels-table.css') ?>">


<?php

use Brick\Money\Context\CustomContext;
use Brick\Money\Money;

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

$fmt = new NumberFormatter('fr_FR', NumberFormatter::DECIMAL);
?>

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

		<h1>
			<?= $this->lang->line('config_agent').' - '.$this->lang->line('config_ilink_agent_remove_cash'); ?>
		</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-12">
				<div class="margin">
					<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#modal-update"
							style="width: 100%"><?= $this->lang->line('Modifier la configuration') ?></button>
				</div>
			</div>
		</div>
		<div class="callout callout-info">
			<p>Note</p>
			<h4><?= $this->lang->line('note_config_wallet_agent_remove_cash') ?></h4>
		</div>
		<div class="row">
			<div class="col-lg-6 col-xs-12">
				<div class="box">
					<div class="box-header">
						<h3 class="box-title"><?= $this->lang->line('rate_com_agent_wallet_cash_international'); ?></h3>
					</div>
					<!-- /.box-header -->
					<div class="box-body table-responsive" style="overflow-x:auto;">
						<table class="table table-hover">
							<tbody>
							<tr>
								<th>Min</th>
								<th>Max</th>
								<th>Taux (%)</th>
								<th><?= $this->lang->line('ceiling')?></th>
							</tr>
							<?php
							if ($plr_agent_wallet_cash_international) {
								foreach ($plr_agent_wallet_cash_international->result() as $row) {
									echo "<tr>
											<td>" . $fmt->format($row->min) . "</td>
											<td>" . $fmt->format($row->max) . "</td>
											  <td>" . $row->taux . "</td>
											  <td>" . (isset($row->plafond) ? floatval($row->plafond) : '') . "</td>" . '
											 </tr>';
								}
							}
							?>
							</tbody>
						</table>
					</div>
					<!-- /.box-body -->
				</div>
				<div class="box">
					<div class="box-header">
						<h3 class="box-title"><?= $this->lang->line('rate_com_agent_wallet_cash_national'); ?></h3>
					</div>
					<!-- /.box-header -->
					<div class="box-body table-responsive" style="overflow-x:auto;">
						<table class="table table-hover">
							<tbody>
							<tr>
								<th>Min</th>
								<th>Max</th>
								<th>Taux (%)</th>
								<th><?= $this->lang->line('ceiling')?></th>
							</tr>
							<?php
							if ($plr_agent_wallet_cash_national) {
								foreach ($plr_agent_wallet_cash_national->result() as $row) {
									echo "<tr>
											<td>" . $fmt->format($row->min) . "</td>
											<td>" . $fmt->format($row->max) . "</td>
											  <td>" . $row->taux . "</td>
											  <td>" . (isset($row->plafond) ? floatval($row->plafond) : '') . "</td>" . '
											 </tr>';
								}
							}
							?>
							</tbody>
						</table>
					</div>
					<!-- /.box-body -->
				</div>
			</div>
			<div class="col-lg-6 col-xs-12">
				<div class="box">
					<div class="box-header">
						<h3 class="box-title"><?= $this->lang->line('rate_com_sup_wallet_cash_international'); ?></h3>
					</div>
					<!-- /.box-header -->
					<div class="box-body table-responsive" style="overflow-x:auto;">
						<table class="table table-hover">
							<tbody>
							<tr>
								<th>Min</th>
								<th>Max</th>
								<th>Taux (%)</th>
								<th><?= $this->lang->line('ceiling')?></th>
							</tr>
							<?php
							if ($plr_sup_wallet_cash_international) {
								foreach ($plr_sup_wallet_cash_international->result() as $row) {
									echo "<tr>
											<td>" . $fmt->format($row->min) . "</td>
											<td>" . $fmt->format($row->max) . "</td>
											  <td>" . $row->taux . "</td>
											  <td>" . (isset($row->plafond) ? floatval($row->plafond) : '') . "</td>" . '
											 </tr>';
								}
							}
							?>
							</tbody>
						</table>
					</div>
					<!-- /.box-body -->
				</div>
				<div class="box">
					<div class="box-header">
						<h3 class="box-title"><?= $this->lang->line('rate_com_sup_wallet_cash_national'); ?></h3>
					</div>
					<!-- /.box-header -->
					<div class="box-body table-responsive" style="overflow-x:auto;">
						<table class="table table-hover">
							<tbody>
							<tr>
								<th>Min</th>
								<th>Max</th>
								<th>Taux (%)</th>
								<th><?= $this->lang->line('ceiling')?></th>
							</tr>
							<?php
							if ($plr_sup_wallet_cash_national) {
								foreach ($plr_sup_wallet_cash_national->result() as $row) {
									echo "<tr>
											<td>" . $fmt->format($row->min) . "</td>
											<td>" . $fmt->format($row->max) . "</td>
											  <td>" . $row->taux . "</td>
											  <td>" . (isset($row->plafond) ? floatval($row->plafond) : '') . "</td>" . '
											 </tr>';
								}
							}
							?>
							</tbody>
						</table>
					</div>
					<!-- /.box-body -->
				</div>
			</div>
		</div>
		<div class="modal fade" id="modal-update">
			<div class="modal-dialog" style="max-width: 1300px; width: 100%;">
				<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('Configuration du wallet'); ?></h3>
					</div>
					<div class="modal-body">
						<form id="walletForm">
							<div class="row">
								<div class="col-lg-6">
									<div class="form-group" style="overflow-x:auto;">
										<div class="table-title">
											<div class="row">
												<div class="col-sm-8">
													<label for="nom"
														   class="col-form-label"><?= $this->lang->line('rate_com_agent_wallet_cash_international'); ?></label>
												</div>
												<div class="col-sm-4">
													<button type="button" class="btn btn-info add-new new42"><i
																class="fa fa-plus"></i> <?= $this->lang->line('new_level'); ?>
													</button>
												</div>
											</div>
										</div>
										<table id="agent_wallet_cash_international" class="table table-bordered">
											<thead>
											<tr>
												<th>Min</th>
												<th>Max</th>
												<th><?= $this->lang->line('rate'); ?> (%)</th>
												<th><?= $this->lang->line('ceiling')?></th>
												<th>Actions</th>
											</tr>
											</thead>
											<tbody>
											<?php
											if ($plr_agent_wallet_cash_international) {
												foreach ($plr_agent_wallet_cash_international->result() as $row) {
													echo "<tr>
																			<td>" . $row->min . "</td>
																			<td>" . $row->max . "</td>
																			<td>" . $row->taux . "</td>
																			  <td>" . (isset($row->plafond) ? floatval($row->plafond) : '') . "</td>" . '
																			  <td>
																				<a class="add add42" title="Add" data-toggle="tooltip"><i class="material-icons">&#xE03B;</i></a>
																				<a class="edit edit42" title="Edit" data-toggle="tooltip"><i class="material-icons">&#xE254;</i></a>
																				<a class="delete delete42" title="Delete" data-toggle="tooltip"><i class="material-icons">&#xE872;</i></a>
																			</td>
																			 </tr>';
												}
											}
											?>
											</tbody>
										</table>
									</div>
									<div class="form-group" style="overflow-x:auto;">
										<div class="table-title">
											<div class="row">
												<div class="col-sm-8">
													<label for="nom"
														   class="col-form-label"><?= $this->lang->line('rate_com_agent_wallet_cash_national'); ?></label>
												</div>
												<div class="col-sm-4">
													<button type="button" class="btn btn-info add-new new4"><i
																class="fa fa-plus"></i> <?= $this->lang->line('new_level'); ?>
													</button>
												</div>
											</div>
										</div>
										<table id="agent_wallet_cash_national" class="table table-bordered">
											<thead>
											<tr>
												<th>Min</th>
												<th>Max</th>
												<th><?= $this->lang->line('rate'); ?> (%)</th>
												<th><?= $this->lang->line('ceiling')?></th>
												<th>Actions</th>
											</tr>
											</thead>
											<tbody>
											<?php
											if ($plr_agent_wallet_cash_national) {
												foreach ($plr_agent_wallet_cash_national->result() as $row) {
													echo "<tr>
																			<td>" . $row->min . "</td>
																			<td>" . $row->max . "</td>
																			<td>" . $row->taux . "</td>
																			  <td>" . (isset($row->plafond) ? floatval($row->plafond) : '') . "</td>" . '
																			  <td>
																				<a class="add add4" title="Add" data-toggle="tooltip"><i class="material-icons">&#xE03B;</i></a>
																				<a class="edit edit4" title="Edit" data-toggle="tooltip"><i class="material-icons">&#xE254;</i></a>
																				<a class="delete delete4" title="Delete" data-toggle="tooltip"><i class="material-icons">&#xE872;</i></a>
																			</td>
																			 </tr>';
												}
											}
											?>
											</tbody>
										</table>
									</div>
								</div>
								<div class="col-lg-6">
									<div class="form-group" style="overflow-x:auto;">
										<div class="table-title">
											<div class="row">
												<div class="col-sm-8">
													<label for="nom"
														   class="col-form-label"><?= $this->lang->line('rate_com_sup_wallet_cash_international'); ?></label>
												</div>
												<div class="col-sm-4">
													<button type="button" class="btn btn-info add-new new12"><i
																class="fa fa-plus"></i> <?= $this->lang->line('new_level'); ?>
													</button>
												</div>
											</div>
										</div>
										<table id="sup_wallet_cash_international" class="table table-bordered">
											<thead>
											<tr>
												<th>Min</th>
												<th>Max</th>
												<th><?= $this->lang->line('rate'); ?> (%)</th>
												<th><?= $this->lang->line('ceiling')?></th>
												<th>Actions</th>
											</tr>
											</thead>
											<tbody>
											<?php
											if ($plr_sup_wallet_cash_international) {
												foreach ($plr_sup_wallet_cash_international->result() as $row) {
													echo "<tr>
																	<td>" . $row->min . "</td>
																	<td>" . $row->max . "</td>
																	<td>" . $row->taux . "</td>
																	  <td>" . (isset($row->plafond) ? floatval($row->plafond) : '') . "</td>" . '
																	  <td>
																		<a class="add add12" title="Add" data-toggle="tooltip"><i class="material-icons">&#xE03B;</i></a>
																		<a class="edit edit12" title="Edit" data-toggle="tooltip"><i class="material-icons">&#xE254;</i></a>
																		<a class="delete delete12" title="Delete" data-toggle="tooltip"><i class="material-icons">&#xE872;</i></a>
																	</td>
																	 </tr>';
												}
											}
											?>
											</tbody>
										</table>
									</div>
									<div class="form-group" style="overflow-x:auto;">
										<div class="table-title">
											<div class="row">
												<div class="col-sm-8">
													<label for="nom"
														   class="col-form-label"><?= $this->lang->line('rate_com_sup_wallet_cash_national'); ?></label>
												</div>
												<div class="col-sm-4">
													<button type="button" class="btn btn-info add-new new1"><i
																class="fa fa-plus"></i> <?= $this->lang->line('new_level'); ?>
													</button>
												</div>
											</div>
										</div>
										<table id="sup_wallet_cash_national" class="table table-bordered">
											<thead>
											<tr>
												<th>Min</th>
												<th>Max</th>
												<th><?= $this->lang->line('rate'); ?> (%)</th>
												<th><?= $this->lang->line('ceiling')?></th>
												<th>Actions</th>
											</tr>
											</thead>
											<tbody>
											<?php
											if ($plr_sup_wallet_cash_national) {
												foreach ($plr_sup_wallet_cash_national->result() as $row) {
													echo "<tr>
																	<td>" . $row->min . "</td>
																	<td>" . $row->max . "</td>
																	<td>" . $row->taux . "</td>
																	  <td>" . (isset($row->plafond) ? floatval($row->plafond) : '') . "</td>" . '
																	  <td>
																		<a class="add add1" title="Add" data-toggle="tooltip"><i class="material-icons">&#xE03B;</i></a>
																		<a class="edit edit1" title="Edit" data-toggle="tooltip"><i class="material-icons">&#xE254;</i></a>
																		<a class="delete delete1" title="Delete" data-toggle="tooltip"><i class="material-icons">&#xE872;</i></a>
																	</td>
																	 </tr>';
												}
											}
											?>
											</tbody>
										</table>
									</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="updateWallet" data-network-id="<?= $network_id ?>"
								data-id-config="<?= $idConfig ?>"
								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>

<script src="<?= base_url('bower_components/toastr/toastr.js') ?>"></script>
<script src="<?= base_url('dist/js/sweetalert2.js') ?>"></script>
<script src="<?= base_url('dist/js/helpers.js') ?>"></script>

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

	$('#updateWallet').click(function () {
		const network_id = $(this).data('network-id');
		const id_config = $(this).data('id-config');

		if ($('#walletForm')[0].checkValidity()) {
			// Paliers
			let plr_agent_wallet_cash_national = getPaliersTable('agent_wallet_cash_national');
			let plr_sup_wallet_cash_national = getPaliersTable('sup_wallet_cash_national');

			let plr_agent_wallet_cash_international = getPaliersTable('agent_wallet_cash_international');
			let plr_sup_wallet_cash_international = getPaliersTable('sup_wallet_cash_international');

			$.ajax({
				url: '<?= base_url('Gestion/config_wallet/update')?>',
				type: 'POST',
				dataType: 'json',
				data: {
					"type": "ilink",
					"config": "agent_remove_cash",
					"network_id": network_id, "id_config": id_config,
					"plr_agent_wallet_cash_national": plr_agent_wallet_cash_national,
					"plr_sup_wallet_cash_national": plr_sup_wallet_cash_national,
					"plr_agent_wallet_cash_international": plr_agent_wallet_cash_international,
					"plr_sup_wallet_cash_international": plr_sup_wallet_cash_international,
				},
				success: function (data) {
					if (data == '200') {
						Swal.fire({
							icon: 'success',
							title: "<?= $this->lang->line('wallet_update')?>",
							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 {
			$('#walletForm')[0].reportValidity();
		}

	});
</script>
<script type="text/javascript">
	// Paliers scripts
	$(document).ready(function () {
		$('[data-toggle="tooltip"]').tooltip();

		/**
			NATIONAUX
		 */
			// Table 4
		var actions4 = $("#agent_wallet_cash_national td:last-child").html();
		// Append table with add row form on add new button click
		$(".new4").click(function () {
			if (!actions4) {
				actions4 = '<a class="add add4" title="Add" data-toggle="tooltip"><i class="material-icons">&#xE03B;</i></a>\n' +
					'                            <a class="edit edit4" title="Edit" data-toggle="tooltip"><i class="material-icons">&#xE254;</i></a>\n' +
					'                            <a class="delete delete4" title="Delete" data-toggle="tooltip"><i class="material-icons">&#xE872;</i></a>';
			}
			$(this).attr("disabled", "disabled");
			var index = $("#agent_wallet_cash_national tbody tr:last-child").index();
			var row = '<tr>' +
				'<td><input type="number" required min="0" class="form-control" name="min" id="min"></td>' +
				'<td><input type="number" required class="form-control" name="max" id="max"></td>' +
				'<td><input type="number" required step="any" class="form-control" name="taux" id="taux"></td>' +
				'<td><input type="number" min="1" class="form-control" name="plafond" id="plafond"></td>' +
				'<td> ' + actions4 + '</td>' +
				'</tr>';
			$("#agent_wallet_cash_national").append(row);
			$("#agent_wallet_cash_national tbody tr").eq(index + 1).find(".add4, .edit4").toggle();
			$('[data-toggle="tooltip"]').tooltip();
		});
		// Add row on add button click
		$(document).on("click", ".add4", function () {
			var empty = false;
			var input = $(this).parents("tr").find('input[type="number"]');
			var min = parseFloat($(this).parents("tr").find('input[id="min"]').first().val());
			var max = parseFloat($(this).parents("tr").find('input[id="max"]').first().val());

			input.each(function () {
				if ($(this).attr('id') == 'min') {
					if (min >= max) {
						$(this).addClass("error");
						$(this)[0].reportValidity();
						empty = true;
					} else {
						$(this).removeClass("error");
					}
				} else if ($(this).attr('id') == 'max') {
					if (max <= min) {
						$(this).addClass("error");
						$(this)[0].reportValidity();
						empty = true;
					} else {
						$(this).removeClass("error");
					}
				} else {
					if (!$(this)[0].checkValidity()) {
						$(this).addClass("error");
						$(this)[0].reportValidity();
						empty = true;
					} else {
						$(this).removeClass("error");
					}
				}
			});
			$(this).parents("tr").find(".error").first().focus();
			if (!empty) {
				input.each(function () {
					$(this).parent("td").html($(this).val());
				});
				$(this).parents("tr").find(".add4, .edit4").toggle();
				$(".new4").removeAttr("disabled");
			}
		});
		// Edit row on edit button click
		$(document).on("click", ".edit4", function () {
			$(this).parents("tr").find("td:not(:last-child)").each(function (index) {
				if (index == 0)
					$(this).html('<input type="number" required class="form-control" min="0" id="min" value="' + $(this).text() + '">');
				if (index == 1)
					$(this).html('<input type="number" required class="form-control" id="max" value="' + $(this).text() + '">');
				if (index == 2)
					$(this).html('<input type="number" required class="form-control" step="any" min="0" value="' + $(this).text() + '">');
				if (index == 3)
					$(this).html('<input type="number" class="form-control"  min="1" value="' + $(this).text() + '">');
			});
			$(this).parents("tr").find(".add4, .edit4").toggle();
			$(".new4").attr("disabled", "disabled");
		});
		// Delete row on delete button click
		$(document).on("click", ".delete4", function () {
			$(this).parents("tr").remove();
			$(".new4").removeAttr("disabled");
		});


		//Table 1
		var actions1 = $("#sup_wallet_cash_national td:last-child").html();
		// Append table with add row form on add new button click
		$(".new1").click(function () {
			if (!actions1) {
				actions1 = '<a class="add add1" title="Add" data-toggle="tooltip"><i class="material-icons">&#xE03B;</i></a>\n' +
					'                            <a class="edit edit1" title="Edit" data-toggle="tooltip"><i class="material-icons">&#xE254;</i></a>\n' +
					'                            <a class="delete delete1" title="Delete" data-toggle="tooltip"><i class="material-icons">&#xE872;</i></a>';
			}
			$(this).attr("disabled", "disabled");
			var index = $("#sup_wallet_cash_national tbody tr:last-child").index();
			var row = '<tr>' +
				'<td><input type="number" required min="0" class="form-control" name="min" id="min"></td>' +
				'<td><input type="number"  required class="form-control" name="max" id="max"></td>' +
				'<td><input type="number" required class="form-control" min="0" step="any" name="taux" id="taux"></td>' +
				'<td><input type="number" min="1" class="form-control" name="plafond" id="plafond"></td>' +
				'<td> ' + actions1 + '</td>' +
				'</tr>';
			$("#sup_wallet_cash_national").append(row);
			$("#sup_wallet_cash_national tbody tr").eq(index + 1).find(".add1, .edit1").toggle();
			$('[data-toggle="tooltip"]').tooltip();
		});
		// Add row on add button click
		$(document).on("click", ".add1", function () {
			var empty = false;
			var input = $(this).parents("tr").find('input[type="number"]');
			var min = parseFloat($(this).parents("tr").find('input[id="min"]').first().val());
			var max = parseFloat($(this).parents("tr").find('input[id="max"]').first().val());

			input.each(function () {
				if ($(this).attr('id') == 'min') {
					if (min >= max) {
						$(this).addClass("error");
						$(this)[0].reportValidity();
						empty = true;
					} else {
						$(this).removeClass("error");
					}
				} else if ($(this).attr('id') == 'max') {
					if (max <= min) {
						$(this).addClass("error");
						$(this)[0].reportValidity();
						empty = true;
					} else {
						$(this).removeClass("error");
					}
				} else {
					if (!$(this)[0].checkValidity()) {
						$(this).addClass("error");
						$(this)[0].reportValidity();
						empty = true;
					} else {
						$(this).removeClass("error");
					}
				}
			});
			$(this).parents("tr").find(".error").first().focus();
			if (!empty) {
				input.each(function () {
					$(this).parent("td").html($(this).val());
				});
				$(this).parents("tr").find(".add1, .edit1").toggle();
				$(".new1").removeAttr("disabled");
			}
		});
		// Edit row on edit button click
		$(document).on("click", ".edit1", function () {
			$(this).parents("tr").find("td:not(:last-child)").each(function (index) {
				if (index == 0)
					$(this).html('<input type="number" required class="form-control" min="0" id="min" value="' + $(this).text() + '">');
				if (index == 1)
					$(this).html('<input type="number" required class="form-control" id="max" value="' + $(this).text() + '">');
				if (index == 2)
					$(this).html('<input type="number" required class="form-control" step="any" min="0" value="' + $(this).text() + '">');
				if (index == 3)
					$(this).html('<input type="number" class="form-control"  min="1" value="' + $(this).text() + '">');
			});
			$(this).parents("tr").find(".add1, .edit1").toggle();
			$(".new1").attr("disabled", "disabled");
		});
		// Delete row on delete button click
		$(document).on("click", ".delete1", function () {
			$(this).parents("tr").remove();
			$(".new1").removeAttr("disabled");
		});


		// Paliers nationaux
		/**
		 * INTERNATIONAUX
		 */

			// Table 4
		var actions42 = $("#agent_wallet_cash_international td:last-child").html();
		// Append table with add row form on add new button click
		$(".new42").click(function () {
			if (!actions42) {
				actions42 = '<a class="add add42" title="Add" data-toggle="tooltip"><i class="material-icons">&#xE03B;</i></a>\n' +
					'                            <a class="edit edit42" title="Edit" data-toggle="tooltip"><i class="material-icons">&#xE254;</i></a>\n' +
					'                            <a class="delete delete42" title="Delete" data-toggle="tooltip"><i class="material-icons">&#xE872;</i></a>';
			}
			$(this).attr("disabled", "disabled");
			var index = $("#agent_wallet_cash_international tbody tr:last-child").index();
			var row = '<tr>' +
				'<td><input type="number" required min="0" class="form-control" name="min" id="min"></td>' +
				'<td><input type="number" required class="form-control" name="max" id="max"></td>' +
				'<td><input type="number" required step="any" class="form-control" name="taux" id="taux"></td>' +
				'<td><input type="number" min="1" class="form-control" name="plafond" id="plafond"></td>' +
				'<td> ' + actions42 + '</td>' +
				'</tr>';
			$("#agent_wallet_cash_international").append(row);
			$("#agent_wallet_cash_international tbody tr").eq(index + 1).find(".add42, .edit42").toggle();
			$('[data-toggle="tooltip"]').tooltip();
		});
		// Add row on add button click
		$(document).on("click", ".add42", function () {
			var empty = false;
			var input = $(this).parents("tr").find('input[type="number"]');
			var min = parseFloat($(this).parents("tr").find('input[id="min"]').first().val());
			var max = parseFloat($(this).parents("tr").find('input[id="max"]').first().val());

			input.each(function () {
				if ($(this).attr('id') == 'min') {
					if (min >= max) {
						$(this).addClass("error");
						$(this)[0].reportValidity();
						empty = true;
					} else {
						$(this).removeClass("error");
					}
				} else if ($(this).attr('id') == 'max') {
					if (max <= min) {
						$(this).addClass("error");
						$(this)[0].reportValidity();
						empty = true;
					} else {
						$(this).removeClass("error");
					}
				} else {
					if (!$(this)[0].checkValidity()) {
						$(this).addClass("error");
						$(this)[0].reportValidity();
						empty = true;
					} else {
						$(this).removeClass("error");
					}
				}
			});
			$(this).parents("tr").find(".error").first().focus();
			if (!empty) {
				input.each(function () {
					$(this).parent("td").html($(this).val());
				});
				$(this).parents("tr").find(".add42, .edit42").toggle();
				$(".new42").removeAttr("disabled");
			}
		});
		// Edit row on edit button click
		$(document).on("click", ".edit42", function () {
			$(this).parents("tr").find("td:not(:last-child)").each(function (index) {
				if (index == 0)
					$(this).html('<input type="number" required class="form-control" min="0" id="min" value="' + $(this).text() + '">');
				if (index == 1)
					$(this).html('<input type="number" required class="form-control" id="max" value="' + $(this).text() + '">');
				if (index == 2)
					$(this).html('<input type="number" required class="form-control" step="any" min="0" value="' + $(this).text() + '">');
				if (index == 3)
					$(this).html('<input type="number" class="form-control"  min="1" value="' + $(this).text() + '">');
			});
			$(this).parents("tr").find(".add42, .edit42").toggle();
			$(".new42").attr("disabled", "disabled");
		});
		// Delete row on delete button click
		$(document).on("click", ".delete42", function () {
			$(this).parents("tr").remove();
			$(".new42").removeAttr("disabled");
		});


		//Table 1
		var actions12 = $("#sup_wallet_cash_international td:last-child").html();
		// Append table with add row form on add new button click
		$(".new12").click(function () {
			if (!actions12) {
				actions12 = '<a class="add add12" title="Add" data-toggle="tooltip"><i class="material-icons">&#xE03B;</i></a>\n' +
					'                            <a class="edit edit12" title="Edit" data-toggle="tooltip"><i class="material-icons">&#xE254;</i></a>\n' +
					'                            <a class="delete delete12" title="Delete" data-toggle="tooltip"><i class="material-icons">&#xE872;</i></a>';
			}
			$(this).attr("disabled", "disabled");
			var index = $("#sup_wallet_cash_international tbody tr:last-child").index();
			var row = '<tr>' +
				'<td><input type="number" required min="0" class="form-control" name="min" id="min"></td>' +
				'<td><input type="number"  required class="form-control" name="max" id="max"></td>' +
				'<td><input type="number" required class="form-control" min="0" step="any" name="taux" id="taux"></td>' +
				'<td><input type="number" min="1" class="form-control" name="plafond" id="plafond"></td>' +
				'<td> ' + actions12 + '</td>' +
				'</tr>';
			$("#sup_wallet_cash_international").append(row);
			$("#sup_wallet_cash_international tbody tr").eq(index + 1).find(".add12, .edit12").toggle();
			$('[data-toggle="tooltip"]').tooltip();
		});
		// Add row on add button click
		$(document).on("click", ".add12", function () {
			var empty = false;
			var input = $(this).parents("tr").find('input[type="number"]');
			var min = parseFloat($(this).parents("tr").find('input[id="min"]').first().val());
			var max = parseFloat($(this).parents("tr").find('input[id="max"]').first().val());

			input.each(function () {
				if ($(this).attr('id') == 'min') {
					if (min >= max) {
						$(this).addClass("error");
						$(this)[0].reportValidity();
						empty = true;
					} else {
						$(this).removeClass("error");
					}
				} else if ($(this).attr('id') == 'max') {
					if (max <= min) {
						$(this).addClass("error");
						$(this)[0].reportValidity();
						empty = true;
					} else {
						$(this).removeClass("error");
					}
				} else {
					if (!$(this)[0].checkValidity()) {
						$(this).addClass("error");
						$(this)[0].reportValidity();
						empty = true;
					} else {
						$(this).removeClass("error");
					}
				}
			});
			$(this).parents("tr").find(".error").first().focus();
			if (!empty) {
				input.each(function () {
					$(this).parent("td").html($(this).val());
				});
				$(this).parents("tr").find(".add12, .edit12").toggle();
				$(".new12").removeAttr("disabled");
			}
		});
		// Edit row on edit button click
		$(document).on("click", ".edit12", function () {
			$(this).parents("tr").find("td:not(:last-child)").each(function (index) {
				if (index == 0)
					$(this).html('<input type="number" required class="form-control" min="0" id="min" value="' + $(this).text() + '">');
				if (index == 1)
					$(this).html('<input type="number" required class="form-control" id="max" value="' + $(this).text() + '">');
				if (index == 2)
					$(this).html('<input type="number" required class="form-control" step="any" min="0" value="' + $(this).text() + '">');
				if (index == 3)
					$(this).html('<input type="number" class="form-control"  min="1" value="' + $(this).text() + '">');
			});
			$(this).parents("tr").find(".add12, .edit12").toggle();
			$(".new12").attr("disabled", "disabled");
		});
		// Delete row on delete button click
		$(document).on("click", ".delete12", function () {
			$(this).parents("tr").remove();
			$(".new12").removeAttr("disabled");
		});
	});
</script>
