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

$txt = "";
$id = 0;
if (isset($_GET['id']) && $_GET['id'] != '') $id = $_GET['id'];

$lastname = "";
$name = "";
$email = "";
$pwd = "";
$idlevel = 0;
$enabled = 0;
$checked = '';

if ($id != 0) {
    $sql = "SELECT * FROM ".$s_prefix."_tb_users WHERE id=".$id;
    $val = mysqli_query($link, $sql) or die('Query failed: ' . mysqli_error($link));
    while ($row = mysqli_fetch_array($val, MYSQLI_ASSOC)) {
        $lastname = $row['lastname'];
        $name = $row['name'];
        $email = $row['usr'];
        $pwd = $row['pwd'];
        $idlevel = $row['userlevel'];
        $enabled = 1;
        $checked = 'checked="checked"';
    }
}

// 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>USER - '.htmlspecialchars($lastname).'</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_setAnag.php">';

// Cognome, Nome, Email
$txt .= '<div class="row g-3 mb-3">';
$txt .= '<div class="col-md-4"><label class="form-label">Cognome <span class="text-danger">*</span></label>';
$txt .= '<input type="text" name="lastname" value="'.htmlspecialchars($lastname).'" class="form-control" required></div>';
$txt .= '<div class="col-md-4"><label class="form-label">Nome <span class="text-danger">*</span></label>';
$txt .= '<input type="text" name="name" value="'.htmlspecialchars($name).'" class="form-control" required></div>';
$txt .= '<div class="col-md-4"><label class="form-label">E-mail <span class="text-danger">*</span></label>';
$txt .= '<input type="email" name="email" value="'.htmlspecialchars($email).'" class="form-control" required></div>';
$txt .= '</div>';

// Password e Livello
$txt .= '<div class="row g-3 mb-3">';
$txt .= '<div class="col-md-4"><label class="form-label">Password</label>';
$txt .= '<input type="password" name="pwd" value="'.htmlspecialchars($pwd).'" class="form-control" onblur="checkPwd(1)"></div>';
$txt .= '<div class="col-md-4"><label class="form-label">Conferma Password</label>';
$txt .= '<input type="password" name="pwdcheck" class="form-control" onblur="checkPwd(2)"></div>';
$txt .= '<div class="col-md-4"><label class="form-label">Livello <span class="text-danger">*</span></label>';
$txt .= '<select name="idlevel" class="form-select">';
$txt .= '<option value="0"></option>';
$txt .= '<option value="1" '.($idlevel==1?'selected':'').'>ADMINISTRATOR</option>';
$txt .= '<option value="2" '.($idlevel==2?'selected':'').'>USER</option>';
$txt .= '</select></div></div>';

// Attivo
$txt .= '<div class="form-check mb-3">';
$txt .= '<input type="checkbox" class="form-check-input" id="enabled" name="enabled" value="1" '.$checked.'>';
$txt .= '<label class="form-check-label" for="enabled">Attivo</label>';
$txt .= '</div>';

// Tipi (in griglia)
$txt .= '<div class="mb-3"><label class="form-label">Tipi</label><div class="row">';
$sql = "SELECT * FROM ".$s_prefix."_editionstype ORDER BY description";
$val = mysqli_query($link, $sql) or die('Query failed: ' . mysqli_error($link));
$col = 0;
while ($row = mysqli_fetch_array($val, MYSQLI_ASSOC)) {
    $checkedtype = '';
    $sql1 = "SELECT count(id) AS tot FROM ".$s_prefix."_editionsuser WHERE iduser=".$id." AND idedition=".$row['id'];
    $val1 = mysqli_query($link, $sql1) or die('Query failed: ' . mysqli_error($link));
    if ($row1 = mysqli_fetch_array($val1, MYSQLI_ASSOC)) {
        if ($row1['tot']>0) $checkedtype = 'checked="checked"';
    }
    $txt .= '<div class="col-md-3 form-check">';
    $txt .= '<input class="form-check-input" type="checkbox" name="type[]" value="'.$row['id'].'" '.$checkedtype.' id="type'.$row['id'].'">';
    $txt .= '<label class="form-check-label" for="type'.$row['id'].'">'.htmlspecialchars($row['description']).'</label>';
    $txt .= '</div>';
}
$txt .= '</div></div>';

// Footer
$txt .= '<div class="text-end">';
$txt .= '<input type="hidden" name="id" value="'.$id.'">';
$txt .= '<input type="hidden" name="pwdold" value="'.htmlspecialchars($pwd).'">';
$txt .= '<button type="submit" class="btn btn-primary">Salva</button>';
$txt .= '<button type="button" class="btn btn-danger" onclick="deleteDetAnag('.$id.')">Elimina</button>';
$txt .= '</div>';

$txt .= '</form></div></div>';

echo $txt;
?>
