<?php
session_start();
ini_set('max_execution_time', 300000);
include("include/setsession.php");
include("include/functions.php");
require_once("include/connect.inc");
mysqli_query($link, "SET character_set_results=utf8");

$txt = "";
$id = 1;
$check = "";

// Inizio card form
$txt .= '<div class="card shadow-sm my-4">';
$txt .= '<div class="card-header bg-dark text-white d-flex justify-content-between align-items-center">';
$txt .= '<span>SETUP ALLARMI</span>';
$txt .= '<button type="button" class="btn-close btn-close-white" aria-label="Close" onclick="closeDet();"></button>';
$txt .= '</div>';

$txt .= '<div class="card-body">';
$txt .= '<form name="insert" id="insert" method="POST" enctype="multipart/form-data" action="sql_setSetupAlarm.php">';

// Recupero valori dal DB
$query = "SELECT * FROM ".$s_prefix."_config WHERE id=".$id;
$values = mysqli_query($link, $query) or die('Query failed: ' . mysqli_error($link));
while($result = mysqli_fetch_array($values, MYSQLI_ASSOC)) {
    if($result['enabled']==1) $check=' checked="checked"';

    // E-mail mittente
    $txt .= '<div class="mb-3 row">';
    $txt .= '<label for="email_1" class="col-sm-2 col-form-label fw-bold">E-mail Mittente</label>';
    $txt .= '<div class="col-sm-10">';
    $txt .= '<input type="email" id="email_1" name="email_1" value="'.htmlspecialchars($result['email_1']).'" class="form-control" required>';
    $txt .= '</div></div>';

    // E-mail destinatari
    $txt .= '<div class="mb-3 row">';
    $txt .= '<label for="email_2" class="col-sm-2 col-form-label fw-bold">E-mail Destinatari</label>';
    $txt .= '<div class="col-sm-10">';
    $txt .= '<input type="email" id="email_2" name="email_2" value="'.htmlspecialchars($result['email_2']).'" class="form-control" required>';
    $txt .= '</div></div>';

    // Allarme attivo
    $txt .= '<div class="form-check mb-3">';
    $txt .= '<input type="checkbox" class="form-check-input" id="active" name="active" value="1" '.$check.'>';
    $txt .= '<label class="form-check-label fw-bold" for="active">Allarme Attivo</label>';
    $txt .= '</div>';
}

// Footer con pulsante
$txt .= '<div class="text-end">';
$txt .= '<input type="hidden" name="id" value="'.$id.'">';
$txt .= '<button type="submit" class="btn btn-primary">Salva</button>';
$txt .= '</div>';

// Fine form e card
$txt .= '</form></div></div>';

unset($result);
mysqli_free_result($values);

echo $txt;
?>
