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

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

if ($conn->connect_error) {
    die("Errore di connessione: " . $conn->connect_error);
}

// Recupera l'id immobile (idhome) passato tramite GET
$idhome = isset($_GET['idhome']) ? intval($_GET['idhome']) : 0;

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // Recupera i dati dal form
    $shared_email = $conn->real_escape_string($_POST['shared_email']);
    $sharing_type = $conn->real_escape_string($_POST['sharing_type']);
    $shared_sections = json_encode($_POST['shared_sections']); // Converti le sezioni in JSON
    $expiration_date = !empty($_POST['expiration_date']) ? $_POST['expiration_date'] : null;

    // Inserimento nel database
    $query = $conn->prepare("
        INSERT INTO home_sharing (idhome, shared_email, sharing_type, shared_sections, expiration_date, created_at) 
        VALUES (?, ?, ?, ?, ?, NOW())
    ");
    $query->bind_param('issss', $idhome, $shared_email, $sharing_type, $shared_sections, $expiration_date);

    if ($query->execute()) {
        // Reindirizza a share-home.php con l'idhome
        header("Location: share-home.php?idhome=$idhome&success=1");
        exit;
    } else {
        $error = "Errore nell'inserimento della condivisione. Riprova.";
    }
    $query->close();
}
?>

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

<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0, minimal-ui">
    <title>Aggiungi Condivisione</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">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/flatpickr/dist/flatpickr.min.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">

                    <h4 class="page-title">Aggiungi Condivisione</h4>
                    <?php if (!empty($error)) { ?>
                        <div class="alert alert-danger"><?php echo $error; ?></div>
                    <?php } ?>

                    <form action="add-sharing.php?idhome=<?php echo $idhome; ?>" method="POST">
                        <div class="card">
                            <div class="card-body">
                                <div class="form-group">
                                    <label for="sharedEmail">Email del destinatario</label>
                                    <input type="email" class="form-control" id="sharedEmail" name="shared_email" required>
                                </div>

                                <div class="form-group">
                                    <label for="sharingType">Tipologia di Condivisione</label>
                                    <select class="form-control" id="sharingType" name="sharing_type" required>
                                        <option value="read-only">Sola Lettura</option>
                                        <option value="add-documents">Aggiunta Documenti</option>
                                    </select>
                                </div>

                                <div class="form-group">
                                    <label for="sharedSections">Sezioni Condivise</label>
                                    <select class="form-control" id="sharedSections" name="shared_sections[]" multiple required>
                                        <option value="documents">Documenti</option>
                                        <option value="details">Dettagli</option>
                                        <option value="contracts">Contratti</option>
                                    </select>
                                    <small class="form-text text-muted">Tieni premuto CTRL (o CMD su Mac) per selezionare più opzioni.</small>
                                </div>

                                <div class="form-group">
                                    <label for="expirationDate">Data di Scadenza (Opzionale)</label>
                                    <input type="text" class="form-control flatpickr" id="expirationDate" name="expiration_date">
                                </div>
                            </div>

                            <div class="card-footer text-right">
                                <button type="submit" class="btn btn-success">Salva Condivisione</button>
                                <a href="share-home.php?idhome=<?php echo $idhome; ?>" class="btn btn-secondary">Annulla</a>
                            </div>
                        </div>
                    </form>

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

<script src="assets/js/jquery.min.js"></script>
<script src="assets/js/bootstrap.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/flatpickr"></script>
<script>
    $(document).ready(function () {
        $('.flatpickr').flatpickr({
            enableTime: false,
            dateFormat: "Y-m-d"
        });
    });
</script>
</body>
</html>
