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

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

$txt .= '<div class="container-fluid py-3">
    <div class="card shadow-sm">
        <div class="card-header bg-danger text-white d-flex justify-content-between align-items-center">
            <span>EDIZIONI</span>
            <button type="button" class="btn-close btn-close-white" aria-label="Close" onclick="closeDet();"></button>
        </div>
        <div class="card-body p-0">
            <div class="table-responsive">
                <table class="table table-striped table-hover mb-0 align-middle text-nowrap">';

if ($id == 0) {
    $txt .= '<tr class="table-secondary">
                <th>ID</th>
                <th>TIPO</th>
                <th>ORA</th>
                <th>LIMITE UPLOAD</th>
                <th>GIORNI</th>
                <th>PATH</th>
                <th>FILENAME</th>
                <th>Azioni</th>
             </tr>
             <tr>
             <form name="insert" id="insert" method="POST" enctype="multipart/form-data" action="sql_setEditions.php">';

    // Tipo
    $txt .= '<td></td>
             <td>
                <select name="idtype" id="idtype" class="form-select form-select-sm">
                    <option value="0"></option>';
    $sql = "SELECT * FROM ".$s_prefix."_editionstype ORDER BY description";
    $valb = mysqli_query($link, $sql) or die('Query failed: ' . mysqli_error($link));
    while ($rowb = mysqli_fetch_array($valb, MYSQLI_ASSOC)) {
        $txt .= '<option value="'.$rowb['id'].'">'.$rowb['description'].'</option>';
    }
    $txt .= '</select>
            </td>';

    // Ora e Limite Upload
    $txt .= '<td><input type="text" name="timeedition" class="form-control form-control-sm" style="width:80px;" required /></td>
             <td><input type="text" name="maxtimeup" class="form-control form-control-sm" style="width:80px;" required /></td>';

    // Giorni
    $txt .= '<td>';
    $days = ['Mon','Tue','Wed','Thu','Fri','Sat','Sun'];
    foreach ($days as $index => $day) {
        $txt .= '<div class="form-check form-check-inline">
                    <input class="form-check-input" type="checkbox" id="day_'.$index.'" name="day_'.$index.'" value="1">
                    <label class="form-check-label" for="day_'.$index.'">'.$day.'</label>
                 </div>';
    }
    $txt .= '</td>';

    // Path e Filename
    $txt .= '<td><input type="text" name="path" class="form-control form-control-sm"></td>
             <td><input type="text" name="name" class="form-control form-control-sm"></td>';

    // Azioni
    $txt .= '<td>
                <input type="hidden" name="id" value="0">
                <button type="submit" class="btn btn-sm btn-success">Save</button>
             </td>';

    $txt .= '</form></tr>';
}

// Lista delle edizioni
$txt .= '<tr class="table-secondary">
            <th>ID</th>
            <th>TIPO</th>
            <th>ORA</th>
            <th>LIMITE UPLOAD</th>
            <th>GIORNI</th>
            <th>PATH</th>
            <th>FILENAME</th>
            <th>Azioni</th>
         </tr>';

$sql = "SELECT a.*, b.description FROM ".$s_prefix."_editions a 
        LEFT JOIN ".$s_prefix."_editionstype b ON a.idtype=b.id 
        ORDER BY b.description, a.timeedition";
$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>'.$row['description'].'</td>';
    $txt .= '<td>'.formatHHMM($row['timeedition']/1000).'</td>';
    $txt .= '<td>'.formatHHMM($row['maxtimeup']/1000).'</td>';

    // Giorni
    $txt .= '<td>';
    for ($i=0;$i<7;$i++) {
        $checked = (substr($row['days'], ($i==0?0:$i), 1)==1) ? '<span class="text-success">&#10004;</span>' : '<span class="text-muted">&#10006;</span>';
        $txt .= '<div class="d-inline-block me-1 text-center" style="width:30px;">'.$checked.'<br>'.substr('SunMonTueWedThuFriSat', $i*3, 3).'</div>';
    }
    $txt .= '</td>';

    $txt .= '<td>'.$row['path'].'</td>';
    $txt .= '<td>'.$row['name'].'</td>';

    // Azioni
    $txt .= '<td>
                <button class="btn btn-sm btn-primary me-1" onclick="editEditions('.$row['id'].')">Edit</button>
                <button class="btn btn-sm btn-danger" onclick="deleteEditions('.$row['id'].')">Delete</button>
             </td>';

    $txt .= '</tr>';
}

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

echo $txt;
?>
