<?php include('include/headscript.php'); ?>
<?php
$conn = new mysqli($servername, $username, $password, $database);

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

// Recupera idhome
$idhome = isset($_GET['idhome']) ? (int)$_GET['idhome'] : 0;

// Recupera le sezioni dalla tabella 'sections'
$querySections = "SELECT idsections, section_name FROM sections ORDER BY section_name ASC";
$resultSections = $conn->query($querySections);
$sections = [];
if ($resultSections->num_rows > 0) {
    while ($row = $resultSections->fetch_assoc()) {
        $sections[] = $row;
    }
}

// Recupera tutti i ruoli per la dropdown
$queryRoles = "SELECT idrole, role_name FROM sharing_roles";
$resultRoles = $conn->query($queryRoles);
$roles = [];
if ($resultRoles->num_rows > 0) {
    while ($row = $resultRoles->fetch_assoc()) {
        $roles[] = $row;
    }
}
?>
<!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">
    <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 href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
    <style>
        body {
            background-color: #f8f9fa;
        }

        .card {
            border-radius: 10px;
            box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
        }

        .form-control {
            border-radius: 5px;
        }

        .form-check-label {
            font-weight: 600;
        }

        .btn-primary {
            background-color: #007bff;
            border-color: #007bff;
        }

        .btn-primary:hover {
            background-color: #0056b3;
            border-color: #004085;
        }

        .btn-secondary {
            background-color: #6c757d;
            border-color: #6c757d;
        }

        .btn-secondary:hover {
            background-color: #5a6268;
            border-color: #545b62;
        }
    </style>
</head>

<body>
    <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-md-12">
                                <div class="card mt-4">
                                    <div class="card-header bg-warning text-white">
                                        <h4 class="mb-0">
                                            <i class="fas fa-share-alt"></i> Aggiungi Condivisione
                                        </h4>
                                    </div>
                                    <div class="card-body">
                                        <form action="save-sharing.php" method="POST">
                                            <input type="hidden" name="idhome" value="<?php echo $idhome; ?>">

                                            <!-- Email destinatario -->
                                            <div class="form-group">
                                                <label for="shared_email" class="form-label">
                                                    <i class="fas fa-envelope"></i> Email destinatario
                                                </label>
                                                <input type="email" class="form-control" id="shared_email" name="shared_email" placeholder="Inserisci email del destinatario" required>
                                            </div>

                                            <!-- Ruolo destinatario -->
                                            <div class="form-group">
                                                <label for="role_id" class="form-label">
                                                    <i class="fas fa-user-tag"></i> Tipologia destinatario
                                                </label>
                                                <select class="form-control" id="role_id" name="role_id" required>
                                                    <option value="">Seleziona un ruolo</option>
                                                    <?php foreach ($roles as $role) { ?>
                                                        <option value="<?php echo $role['idrole']; ?>"><?php echo htmlspecialchars($role['role_name']); ?></option>
                                                    <?php } ?>
                                                </select>
                                            </div>

                                            <!-- Tipo di condivisione -->
                                            <div class="form-group">
                                                <label for="sharing_type" class="form-label">
                                                    <i class="fas fa-lock"></i> Tipo di condivisione
                                                </label>
                                                <select class="form-control" id="sharing_type" name="sharing_type" required>
                                                    <option value="read-only">Solo lettura</option>
                                                    <option value="add-documents">Aggiunta documenti</option>
                                                </select>
                                            </div>

                                            <!-- Sezioni condivise -->
                                            <div class="form-group">
                                                <label class="form-label">
                                                    <i class="fas fa-folder"></i> Sezioni condivise
                                                </label>
                                                <div>
                                                    <?php foreach ($sections as $section) { ?>
                                                        <div class="form-check">
                                                            <input type="checkbox" class="form-check-input" id="section_<?php echo $section['idsections']; ?>" name="shared_sections[]" value="<?php echo $section['idsections']; ?>">
                                                            <label class="form-check-label" for="section_<?php echo $section['idsections']; ?>">
                                                                <?php echo htmlspecialchars($section['section_name']); ?>
                                                            </label>
                                                        </div>
                                                    <?php } ?>
                                                </div>
                                            </div>

                                            <!-- Data di scadenza -->
                                            <div class="form-group">
                                                <label for="expiration_date" class="form-label">
                                                    <i class="fas fa-calendar-alt"></i> Data di scadenza
                                                </label>
                                                <input type="date" class="form-control" id="expiration_date" name="expiration_date">
                                            </div>

                                            <!-- Pulsanti -->
                                            <div class="mt-4">
                                                <button type="submit" class="btn btn-primary btn-lg">
                                                    <i class="fas fa-save"></i> Salva Condivisione
                                                </button>
                                                <a href="share-home.php?idhome=<?php echo $idhome; ?>" class="btn btn-secondary btn-lg">
                                                    <i class="fas fa-arrow-left"></i> Annulla
                                                </a>
                                            </div>
                                        </form>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div> <!-- Page content Wrapper -->
            </div> <!-- content -->
            <?php include('include/footer.php'); ?>
        </div>
    </div>

    <!-- Scripts -->
    <script src="assets/js/jquery.min.js"></script>
    <script src="assets/js/bootstrap.min.js"></script>
</body>

</html>