<?php include('include/headscript.php'); ?>
<?php include('../../languages/it/generaluserdashboard.php'); ?>
<!doctype html>
<html lang="en">

<head>
    <!-- Required meta tags -->
    <?php include('include/seodashboard.php'); ?>
    <!-- App favicon -->
    <link rel="shortcut icon" href="assets/dist/img/favicon.png">
    <!-- Global Styles(used by all pages) -->
    <link href="assets/plugins/bootstrap/css/bootstrap.min.css" rel="stylesheet">
    <link href="assets/plugins/metisMenu/metisMenu.min.css" rel="stylesheet">
    <link href="assets/plugins/fontawesome/css/all.min.css" rel="stylesheet">
    <!-- Third party Styles(used by this page) -->
    <link rel="stylesheet" href="assets/plugins/jquery-fancyfileuploader/fancy-file-uploader/fancy_fileupload.css">
    <!-- App css -->
    <link href="assets/dist/css/app.min.css" rel="stylesheet">
    <!-- Start Your Custom Style Now -->
    <link href="assets/dist/css/style.css" rel="stylesheet">
</head>
<style>
    /* Classe per evidenziare i campi non validi */
    .input-error {
        border: 2px solid red !important;
    }
</style>

<body class="fixed sidebar-mini">
    <div class="wrapper">
        <!-- Sidebar  -->
        <?php include('include/leftnavbar.php'); ?>
        <!-- Page Content  -->
        <div class="content-wrapper">
            <div class="main-content">
                <!-- Star navbar -->

                <!-- End /. search -->
                <?php include('include/topbar.php'); ?>


                <!-- End /. navbar -->
                <div class="body-content">
                    <div class="decoration blur-2"></div>
                    <div class="decoration blur-3"></div>
                    <div class="container-xxl">
                        <!-- Start Page Header -->
                        <div class="card mb-3 p-3">
                            <!-- Profile Cover -->
                            <div class="profile-cover">
                                <div class="profile-cover-img-wrapper">
                                    <img class="profile-cover-img" src="assets/dist/img/profile-bg.jpg" alt="Image Description">
                                    <div class="end-0 me-3 mt-3 position-absolute top-0">

                                    </div>
                                </div>
                            </div>
                            <!-- /. End Profile Cover -->
                            <div class="text-center">
                                <!-- Start Image -->
                                <img class="bg-white p-1 position-relative profile-cover-avatar rounded-circle shadow" src="../../upload/users/<?php echo $avatarName; ?>" alt="">
                                <!-- /. End Image -->
                                <!-- Start Title -->
                                <h4 class="align-items-center justify-content-center d-flex gap-2 mt-3 mb-2 fw-semibold">
                                    <?php echo $name; ?> <?php echo $surname; ?>
                                    <svg xmlns="http://www.w3.org/2000/svg" width="18" height="18" fill="currentColor" class="bi bi-patch-check-fill text-success" viewBox="0 0 16 16">
                                        <path d="M10.067.87a2.89 2.89 0 0 0-4.134 0l-.622.638-.89-.011a2.89 2.89 0 0 0-2.924 2.924l.01.89-.636.622a2.89 2.89 0 0 0 0 4.134l.637.622-.011.89a2.89 2.89 0 0 0 2.924 2.924l.89-.01.622.636a2.89 2.89 0 0 0 4.134 0l.622-.637.89.011a2.89 2.89 0 0 0 2.924-2.924l-.01-.89.636-.622a2.89 2.89 0 0 0 0-4.134l-.637-.622.011-.89a2.89 2.89 0 0 0-2.924-2.924l-.89.01-.622-.636zm.287 5.984-3 3a.5.5 0 0 1-.708 0l-1.5-1.5a.5.5 0 1 1 .708-.708L7 8.793l2.646-2.647a.5.5 0 0 1 .708.708z"></path>
                                    </svg>
                                </h4>
                                <!-- /. End Title -->
                                <ul class="fs-14 fw-medium list-inline list-separator mb-0 mb-2 text-muted justify-content-center">

                                    <li class="list-inline-item">
                                        <!-- <i class="fa-solid fa-user fs-14 me-1"></i> -->
                                        <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-geo-alt me-1" viewBox="0 0 16 16">
                                            <path d="M12.166 8.94c-.524 1.062-1.234 2.12-1.96 3.07A31.493 31.493 0 0 1 8 14.58a31.481 31.481 0 0 1-2.206-2.57c-.726-.95-1.436-2.008-1.96-3.07C3.304 7.867 3 6.862 3 6a5 5 0 0 1 10 0c0 .862-.305 1.867-.834 2.94M8 16s6-5.686 6-10A6 6 0 0 0 2 6c0 4.314 6 10 6 10" />
                                            <path d="M8 8a2 2 0 1 1 0-4 2 2 0 0 1 0 4m0 1a3 3 0 1 0 0-6 3 3 0 0 0 0 6" />
                                        </svg>
                                        <a href="#"> <?php echo $city; ?></a>
                                    </li>
                                    <li class="list-inline-item">
                                        <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-calendar2-plus me-1" viewBox="0 0 16 16">
                                            <path d="M3.5 0a.5.5 0 0 1 .5.5V1h8V.5a.5.5 0 0 1 1 0V1h1a2 2 0 0 1 2 2v11a2 2 0 0 1-2 2H2a2 2 0 0 1-2-2V3a2 2 0 0 1 2-2h1V.5a.5.5 0 0 1 .5-.5M2 2a1 1 0 0 0-1 1v11a1 1 0 0 0 1 1h12a1 1 0 0 0 1-1V3a1 1 0 0 0-1-1z" />
                                            <path d="M2.5 4a.5.5 0 0 1 .5-.5h10a.5.5 0 0 1 .5.5v1a.5.5 0 0 1-.5.5H3a.5.5 0 0 1-.5-.5zM8 8a.5.5 0 0 1 .5.5V10H10a.5.5 0 0 1 0 1H8.5v1.5a.5.5 0 0 1-1 0V11H6a.5.5 0 0 1 0-1h1.5V8.5A.5.5 0 0 1 8 8" />
                                        </svg>
                                        <span><?php echo date('d - F Y', strtotime($createDate)); ?></span>

                                    </li>
                                </ul>
                            </div>
                        </div>
                        <!-- /. End Page Header -->

                        <div class="card mb-4">
                            <div class="card-header position-relative">
                                <h6 class="fs-17 fw-semi-bold mb-0"><?php echo $details; ?></h6>
                            </div>
                            <div class="card-body">
                                <div class="row g-4">
                                    <div class="col-sm-6 col-lg-4">
                                        <!-- start form group -->
                                        <div class="">
                                            <label class="required fw-medium mb-2"><?php echo $nametitle; ?></label>
                                            <input type="text" class="form-control" id="first_name" name="first_name" placeholder="<?php echo $nametitle; ?>" value="<?php echo $name; ?>" required="">
                                        </div>
                                        <!-- end /. form group -->
                                    </div>
                                    <div class="col-sm-6 col-lg-4">
                                        <!-- start form group -->
                                        <div class="">
                                            <label class="required fw-medium mb-2"><?php echo $surnametitle; ?></label>
                                            <input type="text" class="form-control" id="last_name" name="last_name" placeholder="<?php echo $surnametitle; ?>" value="<?php echo $surname; ?>" required="">
                                        </div>
                                        <!-- end /. form group -->
                                    </div>
                                    <div class="col-lg-4">
                                        <!-- start form group -->
                                        <div class="">
                                            <label class="required fw-medium mb-2"><?php echo $emailtitle; ?></label>
                                            <input type="email" class="form-control" id="email" name="email" placeholder="<?php echo $emailtitle; ?>" value="<?php echo $emailuser; ?>" required="" readonly>
                                        </div>
                                        <!-- end /. form group -->
                                    </div>
                                    <div class="card-header position-relative">
                                        <h6 class="fs-17 fw-semi-bold mb-0"><?php echo $invoicedetails; ?></h6>
                                    </div>
                                    <div class="col-sm-12">
                                        <hr>
                                    </div>
                                    <div class="col-sm-6 col-lg-4">
                                        <!-- start form group -->
                                        <div class="">
                                            <label class="required fw-medium mb-2"><?php echo $addresstitle; ?></label>
                                            <input type="text" class="form-control" id="address" name="address" placeholder="<?php echo $addresstitle; ?>" value="<?php echo $address; ?>" required="">
                                        </div>
                                        <!-- end /. form group -->
                                    </div>
                                    <div class="col-sm-6 col-lg-4">
                                        <!-- start form group -->
                                        <div class="">
                                            <label class="required fw-medium mb-2"><?php echo $ziptitle; ?></label>
                                            <input type="text" class="form-control" id="zip" name="zip" value="<?php echo $zip; ?>" placeholder="<?php echo $ziptitle; ?>" required="">
                                        </div>
                                        <!-- end /. form group -->
                                    </div>
                                    <div class="col-lg-4">
                                        <!-- start form group -->
                                        <div class="">
                                            <label class="required fw-medium mb-2"><?php echo $citytitle; ?></label>
                                            <input type="text" class="form-control" id="city" name="city" value="<?php echo $city; ?>" placeholder="<?php echo $citytitle; ?>" required="">
                                        </div>
                                        <!-- end /. form group -->
                                    </div>
                                    <div class="col-sm-6 col-lg-4">
                                        <!-- start form group -->
                                        <div class="">
                                            <label class="required fw-medium mb-2"><?php echo $countrytitle; ?></label>
                                            <select class="form-control" id="country_id" name="country_id" required="">
                                                <option value=""><?php echo $selecttitle; ?></option> <!-- Opzione "Seleziona" -->
                                                <?php
                                                // Query per recuperare i paesi dalla tabella auth_countries
                                                $conn = new mysqli($servername, $username, $password, $database);
                                                $query = "SELECT id, name FROM auth_countries";
                                                $result = $conn->query($query);

                                                // Recupera il country_id attuale dell'utente dal database
                                                $selected_country_id = $user_country_id; // Supponendo che tu abbia già il valore di $user_country_id
                                                echo $user_country_id;
                                                echo "ciao";
                                                // Cicla tra i paesi e seleziona quello corrente, se esiste
                                                while ($row = $result->fetch_assoc()) {
                                                    $selected = ($row['id'] == $selected_country_id) ? 'selected' : ''; // Verifica se l'id corrisponde a quello selezionato
                                                    echo '<option value="' . $row['id'] . '" ' . $selected . '>' . $row['name'] . '</option>';
                                                }
                                                ?>
                                            </select>
                                        </div>
                                        <!-- end /. form group -->
                                    </div>

                                    <div class="col-sm-6 col-lg-4">
                                        <!-- start form group -->
                                        <div class="">
                                            <label class="fw-medium mb-2"><?php echo $phonetitle; ?></label>
                                            <input type="text" class="form-control" id="phone" name="phone" value="<?php echo $phone; ?>" placeholder="<?php echo $phonetitle; ?>">
                                        </div>
                                        <!-- end /. form group -->
                                    </div>
                                    <div class="col-lg-4">
                                        <!-- start form group -->
                                        <div class="">
                                            <label class="required fw-medium mb-2"><?php echo $fiscalcodetitle; ?></label>
                                            <input type="text" class="form-control" id="fiscalcode" name="fiscalcode" value="<?php echo $fiscalcode; ?>" placeholder="<?php echo $fiscalcodetitle; ?>" required="">
                                        </div>
                                        <!-- end /. form group -->
                                    </div>
                                    <div class="col-12">
                                        <button id="saveButton" class="btn btn-success">
                                            <i class="fas fa-save"></i>
                                        </button>
                                    </div>
                                </div>
                            </div>
                        </div>


                        <div class="card mb-4">
                            <div class="card-header position-relative">
                                <h6 class="fs-17 fw-semi-bold mb-0"><?php echo $changepassword; ?></h6>
                            </div>
                            <div class="card-body">
                                <div class="row g-4">
                                    <div class="col-sm-6">
                                        <!-- start form group -->
                                        <div class="">
                                            <label class="required fw-medium mb-2"><?php echo $newpassword; ?></label>
                                            <div class="input-group">
                                                <input type="password" class="form-control" id="new_password" name="password" required="">
                                                <button class="btn btn-outline-secondary" type="button" id="toggleNewPassword">
                                                    <i class="fas fa-eye"></i>
                                                </button>
                                            </div>
                                        </div>
                                        <!-- end /. form group -->
                                    </div>
                                    <div class="col-sm-6">
                                        <!-- start form group -->
                                        <div class="">
                                            <label class="required fw-medium mb-2"><?php echo $confirmpassword; ?></label>
                                            <div class="input-group">
                                                <input type="password" class="form-control" id="confirm_password" name="password" required="">
                                                <button class="btn btn-outline-secondary" type="button" id="toggleConfirmPassword">
                                                    <i class="fas fa-eye"></i>
                                                </button>
                                            </div>
                                        </div>
                                        <!-- end /. form group -->
                                    </div>
                                </div>
                                <div class="col-12">
                                    <button id="savePasswordButton" class="btn btn-success">
                                        <i class="fas fa-save"></i>
                                    </button>
                                </div>
                            </div>
                        </div>


                    </div>
                </div>
                <!--/.body content-->
            </div>
            <!--/.main content-->
            <footer class="footer-content">
                <div class="align-items-center d-flex footer-text gap-3 justify-content-between">
                    <div class="copy">© 2022 ListOn - All Rights Reserved</div>
                    <div class="credit">Developed by: <a href="#">ListOn</a> 🌺💚</div>
                </div>
            </footer>
            <!--/.footer content-->
            <div class="overlay"></div>
        </div>
        <!--/.wrapper-->
    </div>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>

    <script>
        $(document).ready(function() {
            $('#saveButton').click(function(e) {
                e.preventDefault();

                // Recupera i valori dai campi input
                var first_name = $('#first_name').val();
                var last_name = $('#last_name').val();
                var address = $('#address').val();
                var zip = $('#zip').val();
                var city = $('#city').val();
                var country_id = $('#country_id').val();
                var phone = $('#phone').val();
                var fiscalcode = $('#fiscalcode').val();

                // Verifica che tutti i campi richiesti abbiano dei valori
                if (!first_name || !last_name || !address || !zip || !city || !country_id || !fiscalcode) {
                    Swal.fire({
                        icon: 'error',
                        title: 'Errore',
                        text: 'Tutti i campi richiesti devono essere compilati!',
                    });
                    return;
                }

                // Effettua la richiesta AJAX
                $.ajax({
                    url: 'update_user.php', // File PHP per l'aggiornamento
                    type: 'POST',
                    data: {
                        first_name: first_name,
                        last_name: last_name,
                        address: address,
                        zip: zip,
                        city: city,
                        country_id: country_id,
                        phone: phone,
                        fiscalcode: fiscalcode,
                        user_id: <?php echo $iduserlogin; ?> // Passa l'ID dell'utente loggato
                    },
                    success: function(response) {
                        // Mostra un SweetAlert di successo
                        Swal.fire({
                            icon: 'success',
                            title: 'Dati aggiornati con successo!',
                            showConfirmButton: false,
                            timer: 2000
                        });

                        // Cambia il colore degli input a verde per qualche secondo
                        $('input').addClass('input-success'); // Aggiungi la classe 'input-success' a tutti gli input

                        setTimeout(function() {
                            $('input').removeClass('input-success'); // Rimuovi la classe dopo 2 secondi
                        }, 2000);
                    },
                    error: function(xhr, status, error) {
                        Swal.fire({
                            icon: 'error',
                            title: 'Errore durante l\'aggiornamento dei dati',
                            text: error,
                        });
                    }
                });
            });
        });
    </script>
    <script>
        $(document).ready(function() {
            // Mostra/nascondi password
            $('#toggleNewPassword').click(function() {
                const input = $('#new_password');
                const icon = $(this).find('i');
                if (input.attr('type') === 'password') {
                    input.attr('type', 'text');
                    icon.removeClass('fa-eye').addClass('fa-eye-slash');
                } else {
                    input.attr('type', 'password');
                    icon.removeClass('fa-eye-slash').addClass('fa-eye');
                }
            });

            $('#toggleConfirmPassword').click(function() {
                const input = $('#confirm_password');
                const icon = $(this).find('i');
                if (input.attr('type') === 'password') {
                    input.attr('type', 'text');
                    icon.removeClass('fa-eye').addClass('fa-eye-slash');
                } else {
                    input.attr('type', 'password');
                    icon.removeClass('fa-eye-slash').addClass('fa-eye');
                }
            });

            // Gestione del salvataggio della password con AJAX
            $('#savePasswordButton').click(function(e) {
                e.preventDefault();

                var new_password = $('#new_password').val();
                var confirm_password = $('#confirm_password').val();

                // Verifica che le password coincidano
                if (new_password !== confirm_password) {
                    Swal.fire({
                        icon: 'error',
                        title: 'Errore',
                        text: 'Le password non coincidono!',
                    });
                    return;
                }

                // Effettua la richiesta AJAX
                $.ajax({
                    url: 'update_password.php', // File PHP per l'aggiornamento della password
                    type: 'POST',
                    data: {
                        new_password: new_password,
                        user_id: <?php echo $iduserlogin; ?> // Passa l'ID dell'utente loggato
                    },
                    success: function(response) {
                        Swal.fire({
                            icon: 'success',
                            title: 'Password aggiornata con successo!',
                            showConfirmButton: false,
                            timer: 2000
                        });
                    },
                    error: function(xhr, status, error) {
                        Swal.fire({
                            icon: 'error',
                            title: 'Errore durante l\'aggiornamento della password',
                            text: error,
                        });
                    }
                });
            });
        });
    </script>

    <style>
        /* Aggiungi una classe per l'effetto verde degli input */
        .input-success {
            border-color: #28a745 !important;
            background-color: #d4edda !important;
        }
    </style>


    <!-- Global script(used by all pages) -->
    <script src="assets/plugins/jQuery/jquery.min.js"></script>
    <script src="assets/plugins/bootstrap/js/bootstrap.bundle.min.js"></script>
    <script src="assets/plugins/metisMenu/metisMenu.min.js"></script>
    <script src="assets/plugins/perfect-scrollbar/perfect-scrollbar.min.js"></script>
    <!-- Third Party Scripts(used by this page) -->

    <!-- Page Scripts(used by all page) -->
    <script src="assets/dist/js/app.min.js"></script>
    <!-- Page Active Scripts(used by this page) -->
    <script src="assets/dist/js/dashboard.js"></script>
</body>

</html>