<?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">
    <!-- App css -->
    <link href="assets/dist/css/app.min.css" rel="stylesheet">
    <link href="assets/dist/css/style.css" rel="stylesheet">
</head>

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

                <div class="body-content">
                    <div class="container-xxl">
                        <!-- Tabs navigation -->
                        <ul class="nav nav-tabs" id="bookmarkTabs" role="tablist">
                            <li class="nav-item" role="presentation">
                                <button class="nav-link active" id="school-tab" data-bs-toggle="tab" data-bs-target="#school-tab-pane" type="button" role="tab" aria-controls="school-tab-pane" aria-selected="true">
                                    Scuole Preferite
                                </button>
                            </li>
                            <li class="nav-item" role="presentation">
                                <button class="nav-link" id="class-tab" data-bs-toggle="tab" data-bs-target="#class-tab-pane" type="button" role="tab" aria-controls="class-tab-pane" aria-selected="false">
                                    Lezioni Preferite
                                </button>
                            </li>
                        </ul>

                        <!-- Tab content -->
                        <div class="tab-content mt-4" id="bookmarkTabsContent">
                            <!-- Scuole Preferite Tab -->
                            <div class="tab-pane fade show active" id="school-tab-pane" role="tabpanel" aria-labelledby="school-tab">
                                <div class="row">
                                    <?php
                                    // Connessione al database
                                    $conn = new mysqli($servername, $username, $password, $database);

                                    // Controlla la connessione
                                    if ($conn->connect_error) {
                                        die("Connessione fallita: " . $conn->connect_error);
                                    }

                                    // Prepara la query per recuperare le scuole preferite
                                    $query = "
                                    SELECT yogaschool.idyogaschool, yogaschool.nameschool, yogaschool.address, yogaschool.city, yogaschool.zip, yogaschool.country, yogaschool.phoneschool, yogaschool.emailschool, yogaschool.schooldescription, photoyogaschool.photofilename
                                    FROM bookmarks
                                    LEFT JOIN yogaschool ON bookmarks.idyogaschool = yogaschool.idyogaschool
                                    LEFT JOIN photoyogaschool ON photoyogaschool.idyogaschool = yogaschool.idyogaschool
                                    WHERE bookmarks.iduser = ? AND bookmarks.product_id = 0 AND photoyogaschool.priority = 1
                                ";


                                    // Prepara lo statement
                                    if ($stmt = $conn->prepare($query)) {
                                        $stmt->bind_param("i", $iduserlogin); // Associa l'ID utente
                                        $stmt->execute(); // Esegui la query
                                        $result = $stmt->get_result(); // Ottieni il risultato

                                        // Controlla se ci sono scuole preferite
                                        if ($result->num_rows > 0) {
                                            // Cicla tra i risultati e genera le card
                                            while ($row = $result->fetch_assoc()) {
                                                $idyogaschool = $row['idyogaschool'];
                                                $nameschool = $row['nameschool'];
                                                $address = $row['address'];
                                                $city = $row['city'];
                                                $zip = $row['zip'];
                                                $country = $row['country'];
                                                $phoneschool = $row['phoneschool'];
                                                $emailschool = $row['emailschool'];
                                                $schooldescription = $row['schooldescription'];
                                                $photofile = $row['photofilename'];

                                                // Genera la card dinamica
                                                echo '
                                                    <div class="border-0 card card-hover flex-fill overflow-hidden rounded-3 shadow-sm w-100 card-hover-bg mb-3">
                                                        <a href="../listing-details.php?idschool=' . $idyogaschool . '" class="stretched-link"></a>
                                                        <div class="card-body p-0">
                                                            <div class="g-0 h-100 row">
                                                                <div class="col-lg-3 col-md-5 col-sm-4 col-xxl-2 position-relative">
                                                                    <div class="card-image-hover dark-overlay h-100 overflow-hidden position-relative">
                                                                        <!-- start image -->
                                                                        <img src="../../schoolarea/uploadphotoschool/' . $photofile . '" alt="" class="h-100 w-100 object-fit-cover">
                                                                        <!-- end /. image -->
                                                                    </div>
                                                                </div>
                                                                <div class="col-lg-9 col-md-7 col-sm-8 col-xxl-10 p-3 p-lg-4 p-md-3 p-sm-4">
                                                                    <div class="d-flex flex-column h-100">
                                                                        <div class="d-flex end-0 gap-2 me-3 mt-3 position-absolute top-0 z-1">
                                                                            <a href="#" class="align-items-center bg-light btn-icon d-flex justify-content-center rounded-circle text-primary" data-bs-toggle="tooltip" data-bs-placement="top" data-bs-title="Rimuovi Preferito" onclick="removeBookmark(' . $idyogaschool . ', 0)">
                                                                                <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-trash3" viewBox="0 0 16 16">
                                                                                    <path d="M6.5 1h3a.5.5 0 0 1 .5.5v1H6v-1a.5.5 0 0 1 .5-.5M11 2.5v-1A1.5 1.5 0 0 0 9.5 0h-3A1.5 1.5 0 0 0 5 1.5v1H2.506a.58.58 0 0 0-.01 0H1.5a.5.5 0 0 0 0 1h.538l.853 10.66A2 2 0 0 0 4.885 16h6.23a2 2 0 0 0 1.994-1.84l.853-10.66h.538a.5.5 0 0 0 0-1h-.995a.59.59 0 0 0-.01 0zm1.958 1-.846 10.58a1 1 0 0 1-.997.92h-6.23a1 1 0 0 1-.997-.92L3.042 3.5zm-7.487 1a.5.5 0 0 1 .528.47l.5 8.5a.5.5 0 0 1-.998.06L5 5.03a.5.5 0 0 1 .47-.53Zm5.058 0a.5.5 0 0 1 .47.53l-.5 8.5a.5.5 0 1 1-.998-.06l.5-8.5a.5.5 0 0 1 .528-.47ZM8 4.5a.5.5 0 0 1 .5.5v8.5a.5.5 0 0 1-1 0V5a.5.5 0 0 1 .5-.5" />
                                                                                </svg>
                                                                            </a>
                                                                        </div>
                                                                        <div class="align-items-center d-flex flex-wrap gap-1 text-primary  card-start mb-2">
                                                                            <!-- start ratings -->
                                                                            <i class="fa-solid fa-star"></i>
                                                                            <!-- end /. ratings -->
                                                                            <!-- start rating counter text -->
                                                                            <span class="fw-medium text-primary"><span class="fs-6 fw-semibold me-1">(4.5)</span>2,391 reviews</span>
                                                                            <!-- end /. rating counter text -->
                                                                        </div>
                                                                        <!-- start card title -->
                                                                        <h4 class="fs-18 fw-semibold mb-0">' . $nameschool . '</h4>
                                                                        <!-- end /. card title -->
                                                                        <!-- start card description -->
                                                                        <p class="mt-1 fs-14 text-muted">' . $address . ', ' . $zip . ' ' . $city . ' - ' . $country . '</p>
                                                                        <p class="fs-14 text-muted">Telefono: ' . $phoneschool . '</p>
                                                                        <p class="fs-14 text-muted">Email: ' . $emailschool . '</p>
                                                                    </div>
                                                                </div>
                                                            </div>
                                                        </div>
                                                    </div>';
                                            }
                                        } else {
                                            echo '<p>Non hai scuole preferite.</p>';
                                        }

                                        // Chiudi lo statement
                                        $stmt->close();
                                    } else {
                                        echo "Errore nella preparazione della query.";
                                    }

                                    // Chiudi la connessione
                                    $conn->close();
                                    ?>
                                </div>
                            </div>

                            <!-- Lezioni Preferite Tab -->
                            <div class="tab-pane fade" id="class-tab-pane" role="tabpanel" aria-labelledby="class-tab">
                                <div class="row">
                                    <?php
                                    // Connessione al database per recuperare le lezioni preferite
                                    $conn = new mysqli($servername, $username, $password, $database);

                                    // Controlla la connessione
                                    if ($conn->connect_error) {
                                        die("Connessione fallita: " . $conn->connect_error);
                                    }

                                    // Prepara la query per recuperare le lezioni preferite
                                    $query = "
                                        SELECT bookmarks.product_id, yogaschool.nameschool, yogaclass.nameyogaclass, yogaclass.descriptionyogaclass, yogaclass.idyogaclass
                                        FROM bookmarks
                                        LEFT JOIN yogaschool ON bookmarks.idyogaschool = yogaschool.idyogaschool
                                        LEFT JOIN yogaclass ON bookmarks.product_id = yogaclass.idyogaclass
                                        LEFT JOIN photoyogaschool ON photoyogaschool.idyogaschool = yogaschool.idyogaschool
                                        WHERE bookmarks.iduser = ? AND bookmarks.product_id != 0
                                    ";

                                    // Prepara lo statement
                                    if ($stmt = $conn->prepare($query)) {
                                        $stmt->bind_param("i", $iduserlogin); // Associa l'ID utente
                                        $stmt->execute(); // Esegui la query
                                        $result = $stmt->get_result(); // Ottieni il risultato

                                        // Controlla se ci sono lezioni preferite
                                        if ($result->num_rows > 0) {
                                            // Cicla tra i risultati e genera le card
                                            while ($row = $result->fetch_assoc()) {
                                                $idyogaclass = $row['idyogaclass'];
                                                $nameschool = $row['nameschool'];
                                                $nameyogaclass = $row['nameyogaclass'];
                                                $descriptionyogaclass = $row['descriptionyogaclass'];

                                                // Genera la card dinamica per le lezioni
                                                echo '
                                                    <div class="border-0 card card-hover flex-fill overflow-hidden rounded-3 shadow-sm w-100 card-hover-bg mb-3">
                                                        <a href="class-details.php?idclass=' . $idyogaclass . '" class="stretched-link"></a>
                                                        <div class="card-body p-0">
                                                            <div class="g-0 h-100 row">
                                                                <div class="col-lg-3 col-md-5 col-sm-4 col-xxl-2 position-relative">
                                                                    <div class="card-image-hover dark-overlay h-100 overflow-hidden position-relative">
                                                                        <!-- start image -->
                                                                        <img src="assets/dist/img/class_placeholder.jpg" alt="" class="h-100 w-100 object-fit-cover">
                                                                        <!-- end /. image -->
                                                                    </div>
                                                                </div>
                                                                <div class="col-lg-9 col-md-7 col-sm-8 col-xxl-10 p-3 p-lg-4 p-md-3 p-sm-4">
                                                                    <div class="d-flex flex-column h-100">
                                                                        <div class="d-flex end-0 gap-2 me-3 mt-3 position-absolute top-0 z-1">
                                                                            <a href="#" class="align-items-center bg-light btn-icon d-flex justify-content-center rounded-circle text-primary" data-bs-toggle="tooltip" data-bs-placement="top" data-bs-title="Rimuovi Preferito" onclick="removeBookmark(' . $idyogaschool . ', ' . $idyogaclass . ')">
                                                                                <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-trash3" viewBox="0 0 16 16">
                                                                                    <path d="M6.5 1h3a.5.5 0 0 1 .5.5v1H6v-1a.5.5 0 0 1 .5-.5M11 2.5v-1A1.5 1.5 0 0 0 9.5 0h-3A1.5 1.5 0 0 0 5 1.5v1H2.506a.58.58 0 0 0-.01 0H1.5a.5.5 0 0 0 0 1h.538l.853 10.66A2 2 0 0 0 4.885 16h6.23a2 2 0 0 0 1.994-1.84l.853-10.66h.538a.5.5 0 0 0 0-1h-.995a.59.59 0 0 0-.01 0zm1.958 1-.846 10.58a1 1 0 0 1-.997.92h-6.23a1 1 0 0 1-.997-.92L3.042 3.5zm-7.487 1a.5.5 0 0 1 .528.47l.5 8.5a.5.5 0 0 1-.998.06L5 5.03a.5.5 0 0 1 .47-.53Zm5.058 0a.5.5 0 0 1 .47.53l-.5 8.5a.5.5 0 1 1-.998-.06l.5-8.5a.5.5 0 0 1 .528-.47ZM8 4.5a.5.5 0 0 1 .5.5v8.5a.5.5 0 0 1-1 0V5a.5.5 0 0 1 .5-.5" />
                                                                                </svg>
                                                                            </a>
                                                                        </div>
                                                                        <h4 class="fs-18 fw-semibold mb-0">' . $nameyogaclass . '</h4>
                                                                        <p class="fs-14 text-muted">Scuola: ' . $nameschool . '</p>
                                                                        <p class="fs-14 text-muted">' . $descriptionyogaclass . '</p>
                                                                    </div>
                                                                </div>
                                                            </div>
                                                        </div>
                                                    </div>';
                                            }
                                        } else {
                                            echo '<p>Non hai lezioni preferite.</p>';
                                        }

                                        // Chiudi lo statement
                                        $stmt->close();
                                    } else {
                                        echo "Errore nella preparazione della query.";
                                    }

                                    // Chiudi la connessione
                                    $conn->close();
                                    ?>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <?php include('include/footer.php'); ?>
        </div>
    </div>

    <!-- Scripts -->
    <script src="assets/plugins/bootstrap/js/bootstrap.bundle.min.js"></script>
    <script src="assets/dist/js/app.min.js"></script>
</body>

</html>