<?php

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

require_once("header.php");
require_once("left_sidebar.php");

?>
<link rel="stylesheet" href="vendors/datatables.net-bs4/dataTables.bootstrap4.css">
<link rel="stylesheet" href="vendors/jquery-toast-plugin/jquery.toast.min.css">

<style>/* Stili di base per la pagina */
.content-wrapper {
    background-color: #000000 !important;
    color: #ffffff !important;
}

/* Stili per la card principale */
.card {
    background: #0c161c !important;
    border: 1px solid rgba(255, 255, 255, 0.1) !important;
}

.card-title {
    color: #00ffff !important;
    border-bottom: 1px solid rgba(255, 255, 255, 0.1);
    padding: 15px !important;
}

/* Stili per la tabella */
.table {
    color: #ffffff !important;
    background: #0c161c !important;
}

.table thead th {
    background: #000000 !important;
    color: #00ffff !important;
    border-color: rgba(255, 255, 255, 0.1) !important;
}

.table td, .table th {
    border-color: rgba(255, 255, 255, 0.1) !important;
    padding: 12px !important;
}

/* Stili per i badge di stato */
.badge-outline-success {
    color: #00ffaa !important;
    border: 1px solid #00ffaa !important;
}

.badge-outline-warning {
    color: #ffaa00 !important;
    border: 1px solid #ffaa00 !important;
}

.badge-outline-danger {
    color: #ff3366 !important;
    border: 1px solid #ff3366 !important;
}

/* Stili per i modali */
.modal-content {
    background: #0c161c !important;
    color: #ffffff !important;
    border: 1px solid rgba(255, 255, 255, 0.1) !important;
}

.modal-header {
    border-bottom: 1px solid rgba(255, 255, 255, 0.1) !important;
}

.modal-footer {
    border-top: 1px solid rgba(255, 255, 255, 0.1) !important;
}

/* Stili per i form */
.form-control {
    background: rgba(255, 255, 255, 0.1) !important;
    border: 1px solid rgba(255, 255, 255, 0.2) !important;
    color: #ffffff !important;
}

.form-control:disabled {
    background: rgba(255, 255, 255, 0.05) !important;
    color: rgba(255, 255, 255, 0.5) !important;
}

/* Stili per i bottoni */
.btn-danger, .btn-success, .btn-primary, .btn-warning, .btn-info {
    background: linear-gradient(90deg, #00ffff, #ff00ff) !important;
    background-size: 200% 100% !important;
    border: none !important;
    color: white !important;
    font-weight: bold !important;
    animation: gradientAnimation 3s ease infinite !important;
    transition: all 0.3s ease !important;
}

.btn:hover {
    transform: translateY(-2px) !important;
    box-shadow: 0 0 10px rgba(0, 255, 255, 0.5) !important;
}

/* Stili per DataTables */
.dataTables_wrapper .dataTables_length,
.dataTables_wrapper .dataTables_filter,
.dataTables_wrapper .dataTables_info,
.dataTables_wrapper .dataTables_processing,
.dataTables_wrapper .dataTables_paginate {
    color: #ffffff !important;
}

.dataTables_wrapper .dataTables_paginate .paginate_button {
    color: #ffffff !important;
}

.dataTables_wrapper .dataTables_paginate .paginate_button.current {
    background: linear-gradient(90deg, #00ffff, #ff00ff) !important;
    border: none !important;
    color: white !important;
}

/* Stili per le notifiche toast */
.jq-toast-wrap {
    background: #0c161c !important;
    color: #ffffff !important;
}

/* Animazione gradiente */
@keyframes gradientAnimation {
    0% {
        background-position: 0% 50%;
    }
    50% {
        background-position: 100% 50%;
    }
    100% {
        background-position: 0% 50%;
    }
}
/* Stile per la sidebar sinistra */
.sidebar {
    background: black !important;
    border-right: 1px solid rgba(255, 255, 255, 0.1) !important;
}

/* Stile per i link della sidebar */
.sidebar .nav-item .nav-link {
    color: #ffffff !important;
    transition: all 0.3s ease !important;
}

.sidebar .nav-item .nav-link:hover {
    background: rgba(0, 255, 255, 0.1) !important;
    color: #00ffff !important;
}

/* Stile per l'icona e il testo del logo */
.sidebar .navbar-brand {
    background: #0c161c !important;
    border-bottom: 1px solid rgba(255, 255, 255, 0.1) !important;
}

/* Stile per le divisioni tra i menu */
.sidebar .nav-item {
    border-bottom: 1px solid rgba(255, 255, 255, 0.05) !important;
}

/* Stile per le icone nella sidebar */
.sidebar .nav-link i {
    color: #00ffff !important;
}

/* Stile per l'item attivo */
.sidebar .nav-item.active .nav-link {
    background: linear-gradient(90deg, #00ffff20, transparent) !important;
    color: #00ffff !important;
}

/* Stile per il testo del menu */
.nav-link span {
    color: #ffffff !important;
}

/* Hover effect per i link */
.nav-link:hover span {
    color: #00ffff !important;
}

/* Stile per il separatore */
.sidebar-divider {
    border-color: rgba(255, 255, 255, 0.1) !important;
}
/* Stile per i select dropdown */
select.form-control {
    background-color: #0c161c !important;
    color: #ffffff !important;
    border-color: rgba(255, 255, 255, 0.2) !important;
}

select.form-control option {
    background-color: #0c161c !important;
    color: #ffffff !important;
    padding: 10px !important;
}

select.form-control option:hover,
select.form-control option:focus,
select.form-control option:active,
select.form-control option:checked {
    background: linear-gradient(90deg, #00ffff20, transparent) !important;
    color: #00ffff !important;
}

/* Stile per il contenitore del select quando è aperto */
select.form-control:focus {
    background-color: #0c161c !important;
    border-color: #00ffff !important;
    box-shadow: 0 0 0 0.2rem rgba(0, 255, 255, 0.25) !important;
}

/* Stile per le opzioni selezionate */
select.form-control option:checked {
    background: linear-gradient(90deg, #00ffff40, transparent) !important;
    color: #00ffff !important;
}
.navbar .navbar-brand-wrapper {
    border-right: 0px solid #000000;
}
.bg-danger {
    background-color: #4a00ff !important;
}
.bg-warning {
    background-color: #4a00ff !important;
}
.bg-primary, .settings-panel .color-tiles .tiles.primary {
    background-color: #4a00ff !important;
}
.modal .modal-dialog .modal-content .modal-header .close span {
    font-size: 34px;
    font-weight: 400;
    color: #02feff;
}
</style>
<div class="content-wrapper">

	<div class="card">
		<div class="card-body">
			<h4 class="card-title">Gestione Rivenditori</h4>
			<!-------- INIZIO MODAL AGGIUNGI ------------->
			<div class="modal fade" id="aggiungi_rivenditore" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
				style="display: none;" aria-hidden="true">
				<div class="modal-dialog" role="document">
					<form class="forms-sample " name="aggiungi_rivenditore_f" id="aggiungi_rivenditore_f">
						<div class="modal-content">
							<div class="modal-header bg-danger text-white">
								<h5 class="modal-title" id="ModalLabel">Aggiungi Nuovo Rivenditore</h5>
								<button type="button" id="chiudi_modal_aggiungi" class="close" data-dismiss="modal"
									aria-label="Close"><span aria-hidden="true">×</span></button>
							</div>
							<input type="hidden" name="azione" value="aggiungi_rivenditore">
							<div class="modal-body">
								<div class="form-group">
									<label for="username" class="col-form-label">Username:</label>
									<input type="text" class="form-control" id="username_r" name="username_r">
								</div>
								<div class="form-group">
									<label for="password" class="col-form-label">Password:</label>
									<input type="password" class="form-control" id="password_r" name="password_r">
								</div>
								<div class="form-group">
									<label for="crediti" class="col-form-label">Crediti:</label>
									<input type="number" class="form-control" id="crediti" name="crediti" min="30"
										value="30">
								</div>
								<div class="form-group">
									<label for="exampleFormControlSelect2">Tipo</label>
									<select class="form-control" id="tipo_r" name="tipo_r">
										<option value="RESELLER">RESELLER</option>
										<?php
										switch ($permessi_user_logged) {
											case "6923e6c01ef0afda823f8a4085890079": //admin
												echo '
													<option value="SUBRESELLER">SUBRESELLER</option>
													<option value="MASTER">MASTER</option>
												';
												break;
											case "9d444c8a0c6f4935583d6bed6f44eabb"://MASTER
												$permessi = "9d444c8a0c6f4935583d6bed6f44eabb";
												echo '
													<option value="SUBRESELLER">SUBRESELLER</option>
												';
												break;
										}

										?>

									</select>
								</div>
								<div class="form-group">
									<label for="note">Note:</label>
									<textarea class="form-control" id="note" name="note" rows="5"></textarea>
								</div>
							</div>
							<div class="modal-footer">
								<button type="submit" class="btn btn-success">CONFERMA</button>
								<button type="button" class="btn btn-light" data-dismiss="modal">Annulla</button>
							</div>
						</div>
					</form>
				</div>
			</div>
			<!-------- FINE MODAL AGGIUNGI ------------->
			<!-------- INIZIO MODAL MODIFICA ------------->
			<div class="modal fade" id="modifica_rivenditore" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
				style="display: none;" aria-hidden="true">
				<div class="modal-dialog" role="document">
					<form class="forms-sample " name="modifica_rivenditore_f" id="modifica_rivenditore_f">
						<div class="modal-content">
							<div class="modal-header bg-danger text-white">
								<h5 class="modal-title" id="ModalLabel">Modifica Rivenditore</h5>
								<button type="button" id="chiudi_modal_modifica" class="close" data-dismiss="modal"
									aria-label="Close"><span aria-hidden="true">×</span></button>
							</div>
							<input type="hidden" name="azione" value="modifica_rivenditore">
							<input type="hidden" name="id_rivenditore_e" id="id_rivenditore_e">
							<input type="hidden" name="username_r_e2" id="username_r_e2">
							<input type="hidden" name="password_r_e_old" id="password_r_e_old">
							<input type="hidden" name="crediti_r_e_old" id="crediti_r_e_old">
							<div class="modal-body">
								<div class="form-group">
									<label for="username" class="col-form-label">Username:</label>
									<input type="text" class="form-control" id="username_r_e" name="username_r_e"
										disabled>
								</div>
								<div class="form-group">
									<label for="password" class="col-form-label">Password:</label>
									<input type="password" class="form-control" id="password_r_e" name="password_r_e">
								</div>
								<?php
								if (($permessi_user_logged == "6923e6c01ef0afda823f8a4085890079") or ($permessi_user_logged == "7f1ceb7c8aedd46408b3ea1796aa44a6")) {
									echo "
										<div class=\"form-group\" id=\"div_crediti\">
											<label for=\"crediti\" class=\"col-form-label\">Crediti:</label>
											<input type=\"number\" class=\"form-control\" id=\"crediti_e\" name=\"crediti_e\" min=\"0\">
										</div>
									";
								}
								?>
								<div class="form-group">
									<label for="note">Note:</label>
									<textarea class="form-control" id="note_e" name="note_e" rows="5"></textarea>
								</div>
							</div>
							<div class="modal-footer">
								<button type="submit" class="btn btn-success">CONFERMA</button>
								<button type="button" class="btn btn-light" data-dismiss="modal">Annulla</button>
							</div>
						</div>
					</form>
				</div>
			</div>
			<!-------- FINE MODAL MODIFICA ------------->
			<!-------- INIZIO MODAL ELIMINA ------------->
			<div class="modal fade" id="elimina_rivenditore" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
				style="display: none;" aria-hidden="true">
				<div class="modal-dialog" role="document">
					<form class="forms-sample " name="elimina_rivenditore_f" id="elimina_rivenditore_f">
						<div class="modal-content">
							<div class="modal-header bg-info text-white">
								<h5 class="modal-title" id="ModalLabel">Elimina Rivenditore</h5>
								<button type="button" id="chiudi_modal_elimina" class="close" data-dismiss="modal"
									aria-label="Close"><span aria-hidden="true">×</span></button>
							</div>
							<input type="hidden" name="azione" value="elimina_rivenditore">
							<input type="hidden" id="id_rivenditore_r_d" name="id_rivenditore_r_d">
							<input type="hidden" id="username_r_d" name="username_r_d">
							<div class="modal-body">
								<p style="text-align:justify">Sei sicuro di voler eliminare il rivenditore [<strong
										id="nome_rivenditore" name="nome_rivenditore"> </strong>]</p>
								<div class="modal-footer">
									<button type="submit" class="btn btn-success">CONFERMA</button>
									<button type="button" class="btn btn-light" data-dismiss="modal">Annulla</button>
								</div>
							</div>
					</form>
				</div>
			</div>
		</div>
		<!-------- FINE MODAL ELIMINA ------------->
		<!-------- INIZIO MODAL AGGIUNGI CREDITI ------------->
		<div class="modal fade" id="aggiungi_crediti" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
			style="display: none;" aria-hidden="true">
			<div class="modal-dialog" role="document">
				<form class="forms-sample " name="aggiungi_crediti_f" id="aggiungi_crediti_f">
					<div class="modal-content">
						<div class="modal-header bg-primary text-white">
							<h5 class="modal-title" id="ModalLabel">Aggiungi Crediti [<strong id="nome_rivenditore_ac"
									name="nome_rivenditore_ac"> </strong>]</h5>
							<button type="button" id="chiudi_modal_aggiungi_crediti" class="close" data-dismiss="modal"
								aria-label="Close"><span aria-hidden="true">×</span></button>
						</div>
						<input type="hidden" name="azione" value="aggiungi_crediti">
						<input type="hidden" id="id_rivenditore_ac" name="id_rivenditore_ac">
						<input type="hidden" id="username_r_ac" name="username_r_ac">
						<div class="modal-body">
							<div class="form-group">
								<label for="crediti" class="col-form-label">Crediti:</label>
								<input type="number" class="form-control" id="crediti_ac" name="crediti_ac" min="10"
									value="10">
							</div>
							<div class="modal-footer">
								<button type="submit" class="btn btn-success">CONFERMA</button>
								<button type="button" class="btn btn-light" data-dismiss="modal">Annulla</button>
							</div>
						</div>
				</form>
			</div>
		</div>
	</div>
	<!-------- FINE MODAL AGGIUNGI CREDITI ------------->
	<button type="button" onclick="$('#aggiungi_rivenditore_f').trigger('reset');" class="btn btn-danger btn-icon-text"
		data-toggle="modal" data-target="#aggiungi_rivenditore" data-whatever="aggiungi_rivenditore"><i
			class="mdi mdi-account-multiple-plus btn-icon-prepend"></i>Aggiungi Rivenditore</button><br><br>
	<div class="row">
		<div class="col-12">
			<div class="table-responsive">
				<table id="reseller_list" class="table table-bordered">
					<thead>
						<tr>
							<th style="width:6%">#</th>
							<th style="width:4%">Stato</th>
							<th style="width:20%">Creatore</th>
							<th style="width:20%">Username</th>
							<th style="width:5%">N° Clienti</th>
							<th style="width:5%">Crediti</th>
							<th style="width:10%">Tipo</th>
							<th style="width:10%">Ultima Ricarica</th>
							<th style="width:20%">Note</th>
							<th style="width:5%">Azioni</th>
						</tr>
					</thead>
					<tbody>
					</tbody>
				</table>
			</div>
		</div>
	</div>
</div>
</div>
</div>

<script>
	function elimina_rivenditore(id, username) {
		$('#id_rivenditore_r_d').attr('value', id);
		$('#username_r_d').attr('value', username);
		$('#nome_rivenditore').html(username);
	}
	function aggiungi_crediti(id, username) {
		$('#id_rivenditore_ac').attr('value', id);
		$('#username_r_ac').attr('value', username);
		$('#nome_rivenditore_ac').html(username);
	}
	function modifica_rivenditore(id, username, note, password, crediti, v_cr) {
		$('#id_rivenditore_e').attr('value', id);
		$('#username_r_e').attr('value', username);
		$('#username_r_e2').attr('value', username);
		$('#note_e').val(note);
		$('#password_r_e').attr('value', password);
		if (crediti == "∞") {
			$("#div_crediti").hide();
		} else {
			$("#div_crediti").show();
			$('#crediti_e').val(crediti);
			$('#crediti_r_e_old').attr('value', crediti);
		}

		$('#password_r_e_old').attr('value', password);

	}

	$(document).ready(function () {

		$("#aggiungi_rivenditore_f").submit(function (e) {
			var crediti_s = $('#crediti').val();
			if ((crediti_s == "test1h") || (crediti_s == "test12h")) {
				crediti_s = 0;
			}
			var crediti_i = $('#crediti_top').html();
			e.preventDefault();
			$.ajax({
				url: "operazioni_aj.php",
				type: "POST",
				data: $("#aggiungi_rivenditore_f").serialize(),
				success: function (data) {
					if (data == "OK") {
						$("#chiudi_modal_aggiungi").click()
						$("#client_list").dataTable().fnClearTable();
						client_list.ajax.reload();
						NotificaSuccesso("Successo", "Rivenditore aggiunto correttamente");
						if (crediti_i != "∞") {
							var crediti_n = crediti_i - crediti_s;
							$('#crediti_top').html(crediti_n);
						}
					} else {
						NotificaErrore("Attenzione Errori", data);
					}
				},
				error: function (data) {
					NotificaScriptNonDisponibile();
				}
			});
		});

		$("#modifica_rivenditore_f").submit(function (e) {
			e.preventDefault();
			$.ajax({
				url: "operazioni_aj.php",
				type: "POST",
				data: $("#modifica_rivenditore_f").serialize(),
				success: function (data) {
					if (data == "OK") {
						$("#chiudi_modal_modifica").click()
						$("#client_list").dataTable().fnClearTable();
						client_list.ajax.reload();
						NotificaSuccesso("Successo", "Rivenditore modificato correttamente");
					} else {
						NotificaErrore("Attenzione Errori", data);
					}
				},
				error: function (data) {
					NotificaScriptNonDisponibile();
				}
			});
		});

		$("#aggiungi_crediti_f").submit(function (e) {
			var crediti_s = $('#crediti_ac').val();
			var crediti_i = $('#crediti_top').html();
			e.preventDefault();
			$.ajax({
				url: "operazioni_aj.php",
				type: "POST",
				data: $("#aggiungi_crediti_f").serialize(),
				success: function (data) {
					if (data == "OK") {
						$("#chiudi_modal_aggiungi_crediti").click()
						$("#client_list").dataTable().fnClearTable();
						client_list.ajax.reload();
						NotificaSuccesso("Successo", "Crediti aggiunti correttamente");
						if (crediti_i != "∞") {
							var crediti_n = crediti_i - crediti_s;
							$('#crediti_top').html(crediti_n);
						}
					} else {
						NotificaErrore("Attenzione Errori", data);
					}
				},
				error: function (data) {
					NotificaScriptNonDisponibile();
				}
			});
		});

		$("#elimina_rivenditore_f").submit(function (e) {
			e.preventDefault();
			$.ajax({
				url: "operazioni_aj.php",
				type: "POST",
				data: $("#elimina_rivenditore_f").serialize(),
				success: function (data) {
					if (data == "OK") {
						$("#chiudi_modal_elimina").click()
						$("#client_list").dataTable().fnClearTable();
						client_list.ajax.reload();
						NotificaSuccesso("Successo", "Rivenditore eliminato correttamente");
					} else {
						NotificaErrore("Attenzione Errori", data);
					}
				},
				error: function (data) {
					NotificaScriptNonDisponibile();
				}
			});
		});

		var client_list = $('#reseller_list').DataTable({
			"order": [[0, "desc"]],
			"lengthMenu": [[15, 30, 50, 100], [15, 30, 50, 100]],
			"pageLength": 15,
			"responsive": true,
			"autoWidth": false,
			"language": {
				"sEmptyTable": "Nessun dato presente nella tabella",
				"sInfo": "Vista da _START_ a _END_ di _TOTAL_ elementi",
				"sInfoEmpty": "Vista da 0 a 0 di 0 elementi",
				"sInfoFiltered": "(filtrati da _MAX_ elementi totali)",
				"sLengthMenu": "Visualizza _MENU_ elementi",
				"sLoadingRecords": "Caricamento...",
				"sProcessing": "Elaborazione...",
				"sSearch": "Cerca:",
				"sZeroRecords": "La ricerca non ha portato alcun risultato.",
				"oPaginate": {
					"sFirst": "Inizio",
					"sPrevious": "Precedente",
					"sNext": "Successivo",
					"sLast": "Fine"
				},
				"oAria": {
					"sSortAscending": ": attiva per ordinare la colonna in ordine crescente",
					"sSortDescending": ": attiva per ordinare la colonna in ordine decrescente"
				}
			},
			"ajax": {
				"url": "lista_rivenditori_json.php",
				"dataSrc": "data"
			},
			"columns": [
				{ data: 0 }, // ID
				{
					data: 1,
					"render": function (data) {
						return data === '0'
							? "<label class='badge badge-outline-danger' title='BLOCCATO'>Bloccato</label>"
							: "<label class='badge badge-outline-success' title='ATTIVO'>Attivo</label>";
					}
				}, // Stato
				{ data: 2 }, // Superiore
				{ data: 3 }, // Username
				{ data: 4 }, // Crediti
				{ data: 5 }, // Tipo
				{ data: 6 }, // Ultima ricarica
				{ data: 7 }, // Note
				{ data: 8 }, // Azioni (menu dropdown)
				{ data: 9 }  // Altri dati se necessari
			]
		});


	});

</script>
<script src="vendors/jquery-toast-plugin/jquery.toast.min.js"></script>
<script src="js/notifiche.js"></script>
<script src="vendors/datatables.net/jquery.dataTables.js"></script>
<script src="vendors/datatables.net-bs4/dataTables.bootstrap4.js"></script>

<?php

require_once("footer.php");
?>