<?php
include('include/headscript.php');

// Connessione al database
$conn = new mysqli($servername, $username, $password, $database);

// Verifica se l'ID della classe è passato tramite GET
if (isset($_GET['idyogaclass']) && is_numeric($_GET['idyogaclass'])) {
    $idyogaclass = intval($_GET['idyogaclass']);
} else {
    // Se non c'è un ID valido, reindirizza a yogaclass.php
    header("Location: yogaclass.php");
    exit;
}

// Recupera i dati della classe dal database
$query = $conn->prepare("SELECT * FROM yogaclass WHERE idyogaclass = ?");
$query->bind_param("i", $idyogaclass);
$query->execute();
$result = $query->get_result();
$classData = $result->fetch_assoc();

// Se non esiste la classe, reindirizza
if (!$classData) {
    header("Location: yogaclass.php");
    exit;
}

// Variabili per popolare il form
$nameyogaclass = $classData['nameyogaclass'];
$descriptionyogaclass = $classData['descriptionyogaclass'];
$idclasscategory = $classData['idclasscategory'];
$imgyogaclass = $classData['imgyogaclass'];
$productcode = $classData['productcode'];

?>

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0, minimal-ui">
    <?php include('include/seo.php'); ?>
    <title>Edit Class</title>
    <link href="assets/css/bootstrap.min.css" rel="stylesheet" type="text/css">
    <link href="assets/css/icons.css" rel="stylesheet" type="text/css">
    <link href="assets/css/style.css" rel="stylesheet" type="text/css">
</head>

<body class="fixed-left">

    <div id="wrapper">

        <?php include('include/navigationbar.php'); ?>

        <div class="content-page">
            <div class="content">
                <?php include('include/topbar.php'); ?>

                <div class="page-content-wrapper ">
                    <div class="container-fluid">
                        <div class="row">
                            <div class="col-sm-12">
                                <div class="page-title-box">
                                    <h4 class="page-title">Modifica Classe</h4>
                                </div>
                            </div>
                        </div>

                        <!-- Form di modifica della classe -->
                        <div class="row">
                            <div class="col-lg-12">
                                <div class="card card-body">
                                    <form method="POST" action="update_class.php" enctype="multipart/form-data">
                                        <input type="hidden" name="idyogaclass" value="<?php echo $idyogaclass; ?>">

                                        <div class="form-group">
                                            <label for="nameyogaclass">Nome Classe</label>
                                            <input type="text" class="form-control" id="nameyogaclass" name="nameyogaclass" value="<?php echo htmlspecialchars($nameyogaclass); ?>" required>
                                        </div>

                                        <div class="form-group">
                                            <label for="idclasscategory">Categoria Classe</label>
                                            <select class="form-control" id="idclasscategory" name="idclasscategory" required>
                                                <?php
                                                $query_categories = $conn->query("SELECT * FROM classcategory");
                                                while ($row = $query_categories->fetch_assoc()) {
                                                    $selected = $row['idclasscategory'] == $idclasscategory ? 'selected' : '';
                                                    echo '<option value="' . $row['idclasscategory'] . '" ' . $selected . '>' . $row['namecategory'] . '</option>';
                                                }
                                                ?>
                                            </select>
                                        </div>

                                        <div class="form-group">
                                            <label for="descriptionyogaclass">Descrizione Classe</label>
                                            <textarea class="form-control" id="descriptionyogaclass" name="descriptionyogaclass" rows="10"><?php echo htmlspecialchars($descriptionyogaclass); ?></textarea>
                                        </div>

                                        <div class="form-group">
                                            <label for="imgyogaclass">Immagine Classe</label>
                                            <?php if (!empty($imgyogaclass)): ?>
                                                <img src="uploadphotoclass/<?php echo $imgyogaclass; ?>" alt="Immagine Classe" width="100" class="mb-3">
                                            <?php endif; ?>
                                            <input type="file" class="form-control" id="imgyogaclass" name="imgyogaclass">
                                        </div>

                                        <div class="form-group">
                                            <label for="productcode">Codice Prodotto (Opzionale)</label>
                                            <input type="text" class="form-control" id="productcode" name="productcode" value="<?php echo htmlspecialchars($productcode); ?>">
                                        </div>

                                        <button type="submit" class="btn btn-primary">Salva Modifiche</button>
                                        <a href="yogaclass.php" class="btn btn-secondary">Annulla</a>
                                    </form>
                                </div>
                            </div>
                        </div>

                    </div><!-- container -->
                </div><!-- page-content-wrapper -->
            </div><!-- content -->
        </div><!-- content-page -->
    </div><!-- wrapper -->

    <script src="assets/js/jquery.min.js"></script>
    <script src="assets/js/bootstrap.min.js"></script>
    <script src="assets/js/app.js"></script>
</body>

</html>