<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('edit_tax'); ?>
		</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-tax"
							style="width: 100%"><?= $this->lang->line('Modifier la configuration') ?></button>
				</div>
			</div>
		</div>
		<div class="row centered">
			<div class="col-lg-6 col-lg-offset-3 col-xs-12">
				<div class="box">
					<div class="box-header">
						<h3 class="box-title"><?= $this->lang->line('transaction_taxes'); ?></h3>
					</div>
					<div class="box-body table-responsive" style="overflow-x:auto;">
						<table class="table table-hover" >
							<thead>
							<tr>
								<th><?= $this->lang->line('Nom') ; ?> </th>
								<th>Type</th>
								<th>Valeur</th>
								<th>Destination</th>
							</tr>
							</thead>
							<tbody>
							<?php
							if($taxes!=false){
								foreach($taxes->result() as $row) {
									echo "<tr>
																	<td>".$row->nom."</td>
																	<td>".$row->type."</td>
																	<td>".$row->valeur."</td>
																	  <td>".$row->destination."</td>".'
																	 </tr>';
								}
							}
							?>
							</tbody>
						</table>
					</div>
				</div>
			</div>
		</div>

		<div class="modal fade" id="modal-tax">
			<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('configuration_of_tax'); ?></h3>
					</div>
					<div class="modal-body">
						<form id="taxForm">
							<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('transaction_taxes') ; ?></label>
										</div>
										<div class="col-sm-4">
											<button type="button" class="btn btn-info add-new new5"><i class="fa fa-plus"></i> <?= $this->lang->line('new_tax') ; ?></button>
										</div>
									</div>
								</div>
								<table id="transactions_taxes" class="table table-bordered">
									<thead>
									<tr>
										<th><?= $this->lang->line('Nom') ; ?> </th>
										<th>Type</th>
										<th>Valeur</th>
										<th>Destination</th>
										<th>Actions</th>
									</tr>
									</thead>
									<tbody>
									<?php
									if($taxes!=false){
										foreach($taxes->result() as $row) {
											echo "<tr>
																	<td>".$row->nom."</td>
																	<td>".$row->type."</td>
																	<td>".$row->valeur."</td>
																	  <td>".$row->destination."</td>".'
																	  <td>
																		<a class="add add5" title="Add" data-toggle="tooltip"><i class="material-icons">&#xE03B;</i></a>
																		<a class="edit edit5" title="Edit" data-toggle="tooltip"><i class="material-icons">&#xE254;</i></a>
																		<a class="delete delete5" title="Delete" data-toggle="tooltip"><i class="material-icons">&#xE872;</i></a>
																	</td>
																	 </tr>';
										}
									}
									//										?>
									</tbody>
								</table>
							</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="saveTaxes" 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>
<!-- 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>
	toastr.options.closeButton = true;
	toastr.options.closeMethod = 'fadeOut';
	toastr.options.closeDuration = 5000;
	toastr.options.closeEasing = 'swing';

	$('#saveTaxes').click(function () {
		const network_id = $(this).data('network-id');
		const id_config = $(this).data('id-config');
		// Paliers
		var taxes = [];

		$('#transactions_taxes tr').has('td').each(function() {
			var arrayItem = {};
			$('td', $(this)).each(function(index, item) {
				if(index < 4){
					if(index == 2)
						arrayItem[index] = parseFloat($(item).html());
					arrayItem[index] = $(item).html();
				}
			});
			taxes.push(arrayItem);
		});

		if ($('#taxForm')[0].checkValidity()) {

			$.ajax({
				url: '<?= base_url('Gestion/saveTaxes')?>',
				type: 'POST',
				dataType: 'json',
				data: {
					"network_id": network_id,"id_config" : id_config,
					"taxes" : taxes ,
				},
				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();
						});

					}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 {
			$('#taxForm')[0].reportValidity();
		}

	});
</script>
<script>
	// Taxes
	$(document).ready(function() {
		$('[data-toggle="tooltip"]').tooltip();
		var actions5 = $("#transactions_taxes td:last-child").html();
		// Append table with add row form on add new button click
		$(".new5").click(function(){
			if(!actions5){
				actions5 = '<a class="add add5" title="Add" data-toggle="tooltip"><i class="material-icons">&#xE03B;</i></a>\n' +
						'                            <a class="edit edit5" title="Edit" data-toggle="tooltip"><i class="material-icons">&#xE254;</i></a>\n' +
						'                            <a class="delete delete5" title="Delete" data-toggle="tooltip"><i class="material-icons">&#xE872;</i></a>';
			}
			$(this).attr("disabled", "disabled");
			var index = $("#transactions_taxes tbody tr:last-child").index();
			var row = "<tr> <td><input type='text' required class='form-control' name='nom' id='nom'></td> " +
					"<td> <select class='form-control' id='type' required> <option value='%'> % </option> <option value='fixe'> fixe </option> </select> </td> "+
					"<td><input type='number' required class='form-control' min='0' step='0.01' name='valeur' id='valeur'></td>" +
					"<td> <select class='form-control' id='destination' required> <option value='national'> national </option> <option value='international'> international </option> </select> </td>" +
					"<td>" + actions5 + "</td>" +
					"</tr>";
			$("#transactions_taxes").append(row);
			$("#transactions_taxes tbody tr").eq(index + 1).find(".add, .edit").toggle();
			$('[data-toggle="tooltip"]').tooltip();
		});
		// Add row on add button click
		$(document).on("click", ".add5", function(){
			var empty = false;
			var input = $(this).parents("tr").find('input');
			var select = $(this).parents("tr").find('select');
			input.each(function(){
				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){
				select.each(function(){
					$(this).parent("td").html($(this).val());
				});
				input.each(function(){
					$(this).parent("td").html($(this).val());
				});
				$(this).parents("tr").find(".add, .edit").toggle();
				$(".new5").removeAttr("disabled");
			}
		});
		// Edit row on edit button click
		$(document).on("click", ".edit5", function(){
			$(this).parents("tr").find("td:not(:last-child)").each(function(index){
				if(index == 0)
					$(this).html('<input type="text" required class="form-control" value="' + $(this).text() + '">');
				if(index == 1)
					$(this).html("<select class='form-control' id='type' required> <option value='%' > % </option> <option value='fixe'> fixe </option> </select>");
				if(index == 2)
					$(this).html('<input type="number" required class="form-control"  min="0" step="0.01" value="' + $(this).text() + '">');
				if(index == 3)
					$(this).html("<select class='form-control' id='destination' required> <option value='national'> national </option> <option value='international'> international </option> </select>");
			});
			$(this).parents("tr").find(".add, .edit").toggle();
			$(".new5").attr("disabled", "disabled");
		});
		// Delete row on delete button click
		$(document).on("click", ".delete5", function(){
			$(this).parents("tr").remove();
			$(".new5").removeAttr("disabled");
		});
	});
</script>
