<?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");

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

if ($id != 0) {
    $sql = "SELECT * FROM ".$s_prefix."_editionstype WHERE id=".$id;
    $val = mysqli_query($link, $sql) or die('Query failed: ' . mysqli_error($link));
    while ($row = mysqli_fetch_array($val, MYSQLI_ASSOC)) {
        $description = $row['description'];
    }
}

$txt = '';

$txt .= '<div class="container-fluid my-1">';

$txt .= '<!-- Form card -->';
$txt .= '<div class="card shadow-sm mb-4">';
$txt .= '<div class="card-header bg-dark text-white d-flex justify-content-between align-items-center">';
$txt .= '<span>TIPOLOGIE EDIZIONI</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_setEditionsType.php">';
$txt .= '<div class="mb-3 row">';
$txt .= '<label for="description" class="col-sm-2 col-form-label text-end">Descrizione <span class="text-danger">*</span></label>';
$txt .= '<div class="col-sm-6">';
$txt .= '<input type="text" id="description" name="description" value="'.htmlspecialchars($description).'" class="form-control" required>';
$txt .= '</div></div>';

$txt .= '<input type="hidden" name="id" id="id" value="'.$id.'">';
$txt .= '<div class="text-end">';
$txt .= '<button type="submit" id="salva" class="btn btn-primary">Salva</button>';
if($id != 0){
    $txt .= '<button type="button" class="btn btn-danger" onclick="deleteEditionsType('.$id.')">Elimina</button>';
}
$txt .= '</div></form></div></div>';

$txt .= '<!-- Lista delle tipologie -->';
$txt .= '<div class="card shadow-sm">';
$txt .= '<div class="card-header bg-dark text-white">DESCRIZIONE</div>';
$txt .= '<div class="card-body p-0">';
$txt .= '<div class="table-responsive">';
$txt .= '<table class="table table-striped table-hover mb-0 align-left">';
$txt .= '<thead class="table-light">';
$txt .= '<tr><th width="10%">ID</th><th width="70%">Descrizione</th><th width="10%">Azioni</th></tr>';
$txt .= '</thead><tbody>';

$sql = "SELECT * FROM ".$s_prefix."_editionstype ORDER BY description";
$val = mysqli_query($link, $sql) or die('Query failed: ' . mysqli_error($link));
while ($row = mysqli_fetch_array($val, MYSQLI_ASSOC)){
    $txt .= '<tr>';
    $txt .= '<td>'.$row['id'].'</td>';
    $txt .= '<td>'.htmlspecialchars($row['description']).'</td>';
    $txt .= '<td><button type="button" class="btn btn-sm btn-primary me-3" title="Modifica" onclick="editEditionsType('.$row['id'].')"><i class="fa fa-pencil-alt"></i></button>';
    $txt .= '<button type="button" class="btn btn-sm btn-danger" title="Elimina" onclick="deleteEditionsType('.$row['id'].')"><i class="fa fa-trash"></i></button></td>';
    $txt .= '</tr>';
}

$txt .= '</tbody></table></div></div></div>'; // chiusura card e container

echo $txt;
?>
