<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('management_operators'); ?>
			<input type="button" class="btn btn-primary pull-right" id="Bactiver" value="Activer/Désactiver le(s) banque(s)" style="display: none;" />
		</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-lg-8">
				<div class="box">
					<div class="box-header">
						<h3 class="box-title"><?= $this->lang->line('operators'); ?></h3>
					</div>
					<div class="box-body" style="overflow-x:auto;">

						<?php

						$sql2 = $operators;
						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>Type</th>
										<th><?= $this->lang->line('Nom'); ?></th>
										<th>Code</th>
										<th><?= $this->lang->line('Adresse'); ?></th>
										<th align='center'><?= $this->lang->line('Statut'); ?></th>
										<th align='center'><?= $this->lang->line('Activer/Désactiver'); ?></th>
										<th align='center'>Action</th>
									</tr>
									</thead>
									<tbody>
									<?php
									foreach($sql2->result() as $row) {
										$num ++;
										$libelle_status = $this->lang->line('Actif');
										if($row->status==0){
											$libelle_status = $this->lang->line('Inactif');
										}
										$type = $this->session->userdata('site_lang') === 'french' ? $row->type_fr : $row->type_en;
									 ?>
										<tr>
                                                  <td align='center'><?= $num?></td>
                                                  <td><?= $row->country?></td>
                                                  <td><?=$type ?></td>
                                                   <td ><?=$row->operator_name ?></td>
                                                    <td ><?=$row->operator_code?></td>
                                                   <td ><?=$row->operator_address?></td>
                                                  <td align='center'><?=$libelle_status?></td>
										<?php
										if($row->status==0){
											echo "<td align='center'><label>
                                                  <input type='checkbox' name='activer' class='minimal' data-id-operator='".$row->id."' data-id-country='".$row->country_id."' >
                                                </label>
                                                <label>
                                                  <input type='checkbox' name='desactiver' class='minimal-red' disabled data-id-operator='".$row->id."' data-id-country='".$row->country_id."'>
                                                </label></td>";
										}else{
											echo "<td align='center'><label>
                                                  <input type='checkbox' name='activer' class='minimal' disabled data-id-operator='".$row->id."' data-id-country='".$row->country_id."'>
                                                </label>
                                                <label>
                                                  <input type='checkbox' name='desactiver' class='minimal-red' data-id-operator='".$row->id."' data-id-country='".$row->country_id."'>
                                                </label></td>";
										}
										?>
										<td align='center'>
											<button data-toggle='modal' data-id-operator='<?=$row->id?>' data-name-operator='<?=$row->operator_name?>' data-type-operator="<?=$type?>"
                                                             data-address-operator='<?=$row->operator_address?>' data-code-operator='<?=$row->operator_code?>' data-country='<?=$row->country?>' data-id-country='<?=$row->country_id?>' class='open-modal btn btn-success' data-target='#editModal'><i class='fa fa-edit'></i></button>
                                                      
											<button data-id-operator='<?=$row->id?>' data-id-country='<?=$row->country_id?>' class='delete-operator btn btn-danger' ><i class='fa fa-trash'></i></button>
                                                </td>
										</tr>
									<?php } ?>
									</tbody>
								</table>

								<?php

							}

							else {
								echo $this->lang->line('no_operator');
							}
						}else {
							echo $this->lang->line('no_operator');
						}
						?>
					</div>
				</div>
			</div>
			<div class="col-lg-4">
				<div class="box">
						<div class="box-header">
							<h3 class="box-title"><?= $this->lang->line('add_operator'); ?></h3>
						</div>
						<div class="box-body">
							<form id="add-operator" method="post" class="bottom-75 center-block">
								<div class="form-group">
									<label for="name"><?= $this->lang->line('Nom'); ?></label>
									<input type="text" class="form-control input-lg" name="name" required id="name">
								</div>
								<div class="form-group">
									<label for="type"><?= $this->lang->line('operator_type'); ?></label>
									<?php
									$result=$operators_types;
									$numrows=$result->num_rows();
									if ($numrows > 0) { ?>
										<select class="form-control input-lg" name="type" required id="type" >
											<?php foreach($result->result() as $row) {
												echo "<option value='".$row->code."' id='".$row->id."'>".($this->session->userdata('site_lang') === 'french' ? $row->description_fr : $row->description_en)."</option>";
											} ?>
										</select>
									<?php }else{
										echo $this->lang->line("no_type");
									}
									?>
								</div>
								<div class="form-group">
									<button type="submit" class="btn btn-primary"><?= $this->lang->line('Ajouter'); ?></button>
									<button type="button" class="btn btn-success" data-toggle='modal' data-target='#addOperatorType' > <?= $this->lang->line('add_operator_type'); ?></button>
								</div>
							</form>
						</div>
					</div>
				<div class="box">
						<div class="box-header">
							<h3 class="box-title"><?= $this->lang->line('assign_operator_to_country'); ?></h3>
						</div>
						<div class="box-body">
							<form id="assign-form" method="post" class="bottom-75 center-block">
								<div class="form-group">
									<label for="pays"><?= $this->lang->line('Pays'); ?></label>
									<?php
									$result=$pays;
									$numrows=$result->num_rows();
									if ($numrows > 0) { ?>
										<select class="form-control input-lg" name="pays" required id="pays" multiple="multiple" >
											<?php foreach($result->result() as $row) {
												echo "<option value='".$row->id."' id='".$row->code_country."'>".$row->name."</option>";
											} ?>
										</select>
									<?php }else{
										echo $this->lang->line("Aucun pays");
									}
									?>
								</div>
								<div class="form-group">
									<label for="ville"><?= $this->lang->line('operators'); ?></label>
									<?php
									$result=$distinct_operators;
									if($result!=false) {
										$numrows = $result->num_rows();
										if ($numrows > 0) { ?>
											<select class="form-control input-lg" name="operator" required id="operator">
												<?php foreach ($result->result() as $row) {
													echo "<option value='" . $row->id. "'>" . $row->nom." - ".($this->session->userdata('site_lang') === 'french' ? $row->description_fr : $row->description_en). "</option>";
												} ?>
											</select>
										<?php } else {
											echo $this->lang->line("no_operator");
										}
									}else{
										echo $this->lang->line("no_operator");
									}
									?>
								</div>
								<div class="form-group">
									<input  type="submit" value="<?= $this->lang->line('Ajouter'); ?>" class="btn btn-primary">
								</div>
							</form>
						</div>
					</div>
			</div>

		</div>
		<div class="modal fade" id="editModal">
			<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>
						<h4 class="modal-title"><?= $this->lang->line('modify_operator'); ?></h4>
					</div>
					<div class="modal-body">
						<form id="update-form" method="post" class="bottom-75 center-block">
							<div class="form-group">
								<label for="modal-pays"><?= $this->lang->line('Pays'); ?></label>
								<input type="text" class="form-control input-lg" name="pays" required id="modal-pays" readonly="readonly">
							</div>
							<div class="form-group">
								<label for="type">Type</label>
								<input type="text" class="form-control input-lg" name="pays" required id="type" readonly="readonly">
							</div>
							<div class="form-group">
								<label for="name"><?= $this->lang->line('Nom'); ?></label>
								<input type="text" class="form-control input-lg" name="name" required id="name">
							</div>
							<div class="form-group">
								<label for=code">Code</label>
								<input type="text" pattern=".{4,}" class="form-control input-lg" name="code"  id="code">
							</div>
							<div class="form-group">
								<label for="address"><?= $this->lang->line('Adresse'); ?></label>
								<input type="text" class="form-control input-lg" name="address" id="address">
							</div>
							<div class="form-group">
								<input  type="submit" value="Confirmer" class="btn btn-primary">
								<button type="button" class="btn btn-default pull-right" data-dismiss="modal"><?= $this->lang->line('Fermer'); ?></button>
							</div>
						</form>
					</div>
				</div>
			</div>
		</div>

		<div class="modal fade" id="addOperatorType">
			<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>
						<h4 class="modal-title"><?= $this->lang->line('new_operator_type'); ?></h4>
					</div>
					<div class="modal-body">
						<form id="add-operator-type-form" method="post" class="bottom-75 center-block">
							<div class="form-group">
								<label>Code</label>
								<input type="text" class="form-control input-lg" name="code" required id="code">
							</div>
							<div class="form-group">
								<label>Description</label>
								<input type="text" class="form-control input-lg" name="description_fr" required id="description_fr">
							</div>
							<div class="form-group">
								<button type="submit" class="btn btn-primary"><?= $this->lang->line('Confirmer'); ?></button>
								<button type="button" class="btn btn-default pull-right" data-dismiss="modal"><?= $this->lang->line('Fermer'); ?></button>
							</div>
						</form>
					</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="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-multiselect/0.9.15/js/bootstrap-multiselect.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';

	$(document).ready(function() {
		var checkboxes = $("td input[type='checkbox']");
		checkboxes.click(function() {
			if ($("td input:checkbox:checked").length > 0)
			{
				$("#Bactiver").css("display", "block");
			}else{
				$("#Bactiver").css("display", "none");
			}

		});


		var Bactiver = $("#Bactiver");
		Bactiver.click(function(){
			var activation = [];
			var desactivation = [];

			$.each($("input[name='activer']:checked"), function(){
				activation.push({"id_operator" : $(this).data('id-operator'),"id_country": $(this).data('id-country')});
			});
			$.each($("input[name='desactiver']:checked"), function(){
				desactivation.push({"id_operator" : $(this).data('id-operator'),"id_country": $(this).data('id-country')});
			});

			// console.log('act' , activation)
			// console.log('desact' , desactivation)

			$.ajax({
				url:'<?= base_url('Gestion/change_operator_status')?>',
				type: 'post',
				data: {activations: activation,desactivations:desactivation},
				dataType: 'json',
				success: function(data){
					if(data==='completed'){
						Swal.fire({
							icon: 'success',
							title: "<?= $this->lang->line('operators_enabled_disabled')?>",
							text: "<?= $this->lang->line('informations_updated')?>",
							timer: 3000
						}).then(()=>{
							location.reload();
						});
					}else{
						console.log('Error' , data);
						toastr.error("<?= $this->lang->line('error_message')?>" , "<?= $this->lang->line('request_error')?>");
					}
				}
			});
		});
	});
</script>
<script>
	$(function() {
		$('#unvalidated').DataTable();
		$('#validated').DataTable();
	})

</script>

<script>
	$(function() {
		$('#pays').multiselect({
			includeSelectAllOption: true
		});
	});

</script>

<script>

	var id_operator = null;
	var name_network = null;
	var id_country = null;
	var country = null;

	$(document).on("click", ".open-modal", function () {
		id_operator = $(this).data('id-operator');
		const operator_name = $(this).data('name-operator');
		const operator_type = $(this).data('type-operator');
		console.log('type',operator_type)
		const operator_address = $(this).data('address-operator');
		const operator_code = $(this).data('code-operator');
		id_country = $(this).data('id-country');
		country = $(this).data('country');

		$(".modal-body #modal-pays").val(country);
		$(".modal-body #type").val(operator_type);
		$(".modal-body #name").val( operator_name );
		$(".modal-body #address").val( operator_address );
		$(".modal-body #code").val( operator_code );

	});

	$(document).on("click", ".delete-operator", function (event) {
		id_operator = $(this).data('id-operator');
		id_country = $(this).data('id-country');

		$.ajax({
			url:'<?= base_url('Gestion/delete_operator')?>',
			type: 'post',
			data: {id_operator: id_operator , id_country : id_country},
			dataType: 'json',
			success: function(data){
				if(data==='completed'){
					Swal.fire({
						icon: 'success',
						title: "<?= $this->lang->line('operator_deleted')?>",
						text: "<?= $this->lang->line('informations_updated')?>",
						timer: 3000
					}).then(()=>{
						location.reload();
					});
				}else{
					console.log("Error",data);
					toastr.error("<?= $this->lang->line('unexpected_error')?>" , "<?= $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')?>");
			}
		});
		event.preventDefault();

	});

	$("#assign-form").submit(function(event){
		var id_operator = $("#assign-form #operator").val();
		var selectedValues = $('#assign-form #pays').val();

		$.ajax({
			url:'<?= base_url('Gestion/assign_operator')?>',
			type: 'post',
			data: {id_country: selectedValues,id_operator:id_operator},
			dataType: 'json',
			success: function(data){
				if(data==='completed'){
					Swal.fire({
						icon: 'success',
						title: "<?= $this->lang->line('operator_assigned')?>",
						text: "<?= $this->lang->line('informations_updated')?>",
						timer: 3000
					}).then(()=>{
						location.reload();
					});
				}else if(data==='existe'){
					alert('Ce réseau a déjà été assigné à certains des pays sélectionnés');
					location.reload();
				}else{
					console.log("Error",data);
					toastr.error("<?= $this->lang->line('unexpected_error')?>" , "<?= $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')?>");
			}
		});

		event.preventDefault();
	});

	$("#add-operator").submit(function(event){
		var name = $("#add-operator #name").val();
		var type = $("#add-operator #type").val();
		$.ajax({
			url:'<?= base_url('Gestion/add_operator')?>',
			type: 'post',
			data: {name: name , type :type},
			dataType: 'json',
			success: function(data){
				if(data==='completed'){
					Swal.fire({
						icon: 'success',
						title: "<?= $this->lang->line('operator_added')?>",
						text: "<?= $this->lang->line('informations_updated')?>",
						timer: 3000
					}).then(()=>{
						location.reload();
					});
				}else{
					console.log("Error",data);
					toastr.error("<?= $this->lang->line('unexpected_error')?>" , "<?= $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')?>");
			}
		});
		event.preventDefault();
	});

	$("#update-form").submit(function(event){
		const name = $("#update-form #name").val();
		const address = $("#update-form #address").val();
		const code = $("#update-form #code").val();
		$.ajax({
			url:'<?= base_url('Gestion/update_operator')?>',
			type: 'post',
			data: {id_operator: id_operator ,name:name , address :address , id_country : id_country , code : code},
			dataType: 'json',
			success: function(data){
				if(data==='completed'){
					Swal.fire({
						icon: 'success',
						title: "<?= $this->lang->line('operator_modified')?>",
						text: "<?= $this->lang->line('informations_updated')?>",
						timer: 3000
					}).then(()=>{
						location.reload();
					});
				}else{
					console.log("Error",data);
					toastr.error("<?= $this->lang->line('unexpected_error')?>" , "<?= $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')?>");
			}
		});

		event.preventDefault();
	});

	$("#add-operator-type-form").submit(function(event){
		var code = $("#add-operator-type-form #code").val();
		// var description_en = $("#add-operator-type-form #description_en").val();
		var description_fr = $("#add-operator-type-form #description_fr").val();
		$.ajax({
			url:'<?= base_url('Gestion/add_operator_type')?>',
			type: 'post',
			data: {code: code , description_fr :description_fr},
			dataType: 'json',
			success: function(data){
				if(data===200){
					Swal.fire({
						icon: 'success',
						title: "<?= $this->lang->line('new_operator_added')?>",
						text: "<?= $this->lang->line('informations_updated')?>",
						timer: 3000
					}).then(()=>{
						location.reload();
					});
				}else if(data === 422){
					toastr.error("<?= $this->lang->line('operator_type_code_exists')?>" , "<?= $this->lang->line('request_error')?>");
				}else if(data === 500){
					toastr.error("<?= $this->lang->line('unexpected_error')?>" , "<?= $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')?>");
			}
		});
		event.preventDefault();
	});
</script>
