<?php
echo "<!-- inc.editslider.php -->\n";

// Gestione ID gallery: se arriva da inc.sliderlist.php usa quello, altrimenti usa il tab
if(!isset($id_gallery) || $id_gallery == 0) {
    $id_gallery = ($tab==1) ? 1 : 2; //1=Slider Home Page, 2=Slider Catalogo (legacy)
}

// Carica informazioni dello slider
$sql = "SELECT * FROM t_gallery WHERE id=$id_gallery AND tipo='2'";
$mydb->DoSelect($sql);
$slider_info = $mydb->GetRow();

if(!$slider_info) {
    echo "<div class='boxerr'>Slider non trovato!</div>";
    return;
}

$slider_title = htmlspecialchars($slider_info['titolo']);

// Includi il file delle azioni (salva, elimina, ecc.)
include_once('inc.editslider.actions.php');

?>
<!-- Flag Icons CSS per le bandiere SVG -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/lipis/flag-icons@7.2.3/css/flag-icons.min.css" />

<?php
if($isubsezione == 0)
{
    $sql = "SELECT * FROM t_img_gallery WHERE id_gal=".$id_gallery." ORDER BY ordine ASC";
    $res = $mydb->DoSelect($sql);
?>
<!-- Pulsante torna alla lista slider buttonrev-->
<div style="margin-bottom:20px;">
    <button type="button buttonrev" onclick="tornaAllaLista();" class="button buttonrev" style="width:200px">
        <div><i class="bi bi-arrow-left" style="margin-right:5px;"></i> Torna alla Lista Slider</div>
    </button>
</div>

<div style="margin-bottom:20px;">
    <h2><?=$slider_title?></h2>
    <p><strong>Nota:</strong> Per lo slider catalogo, il campo URL contiene il link della slide.</p>
</div>

<form name="frmEditSlider" method="post" action="<? echo $action ?>" >

<div style="margin-bottom:15px;">
    <button type="button" onclick="document.frmEditSlider.sezione.value='cms,1';waitpage();document.frmEditSlider.submit();" class="button" style="width:200px">
        <div><i class="bi bi-plus-circle" style="margin-right:5px;"></i> Aggiungi Nuova Slide</div>
    </button>
</div>

<table width="100%" border="0" cellspacing="0" cellpadding="0" class="labels-table">
  <thead>
  <tr>
    <th width="5%">Ordine</th>
    <th width="12%">Immagine</th>
    <th >Titolo</th>
    <th width="8%">Visibile</th>
    <th width="10%">Azioni</th>
  </tr>
  </thead>
  <tbody>

<?php
if(count($res) == 0) {
    echo '<tr><td colspan="7" align="center" style="padding:40px;">📝 Nessuna slide presente. Clicca su "Aggiungi Nuova Slide" per iniziare.</td></tr>';
}

foreach($res as $row)
{
    $id_img = $row['id'];
    $img = $row['img'];
    $titolo = $row['titolo'];
    $des = $row['des'];
    $ordine = $row['ordine'];
    $visibile = $row['visibile'];
    $tipo = $row['tipo'];
    $url = $row['url'];

?>
  <tr class="label-row">
    <td class="label-id" style="text-align:center;"><?=$ordine?></td>
    <td style="text-align:center; padding:10px;">
        <? if($img!=""){ ?>
            <img src="<?=$path_gallery.$img?>" style="max-width:100px; max-height:60px; border:1px solid #ccc; border-radius:4px;" />
        <? } else { ?>
            <span style="color:#999;">Nessuna immagine</span>
        <? } ?>
    </td>
    <td class="label-value"><?=strip_tags(str_replace(["<br>","\n"], " ", $titolo))?></td>
    <td style="text-align:center;">
        <span style="color:<?=($visibile=='1')?'green':'red'?>;">
            <?=($visibile=='1')?'✓ Sì':'✗ No'?>
        </span>
    </td>
    <td class="label-actions" style="text-align:center;">
        <button type="button" class="btn-translate" onclick="document.frmEditSlider.sezione.value='cms,1'; document.frmEditSlider.id.value='<?=$id_img?>'; document.frmEditSlider.submit();" title="Modifica slide">
            <svg class="btn-icon" width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg">
                <path d="M4.5 11H5.914L12.5 4.414L11.086 3L4.5 9.586V11ZM13.207 3.707L12.293 2.793C11.902 2.402 11.269 2.402 10.878 2.793L10.086 3.586L11.5 5L12.293 4.207C12.684 3.816 12.684 3.183 12.293 2.793L13.207 3.707Z" fill="currentColor"/>
                <path d="M2 13.5C2 13.776 2.224 14 2.5 14H13.5C13.776 14 14 13.776 14 13.5V7.5C14 7.224 13.776 7 13.5 7H11V8H13V13H3V8H5V7H2.5C2.224 7 2 7.224 2 7.5V13.5Z" fill="currentColor"/>
            </svg>
            <span class="translate-text">Modifica</span>
        </button>
        <button type="button" class="btn-delete" onclick="if(confirm('Sei sicuro di voler eliminare questa slide?')){ document.frmEditSlider.azione.value='elimina'; document.frmEditSlider.id.value='<?=$id_img?>'; document.frmEditSlider.submit(); }" title="Elimina slide" style="margin-left:5px;">
            <svg class="btn-icon" width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg">
                <path d="M6 2V3H2V4H3V13C3 13.55 3.45 14 4 14H12C12.55 14 13 13.55 13 13V4H14V3H10V2H6ZM4 4H12V13H4V4ZM6 6V11H7V6H6ZM9 6V11H10V6H9Z" fill="currentColor"/>
            </svg>
            <span class="delete-text">Elimina</span>
        </button>
    </td>
  </tr>

<?php
}
?>
  </tbody>
</table>

        <input type="hidden" name="MSID" value="<?=$MSID?>" />
        <input type="hidden" name="sezione" value="cms" />
        <input type="hidden" name="azione" value="" />
        <input type="hidden" name="id" value="" />
        <input type="hidden" name="id_gallery" value="<?=$id_gallery ?? 0?>" />
        
        <input type="hidden" name="tab" value="<?=$tab?>" />
        <input type="hidden" name="tab2" value="<?=$tab2 ?? ''?>" />  
        
        <input type="hidden" name="ord" value="<?=$ord?>" />
</form>


<script>
function tornaAllaLista() {
    document.frmEditSlider.id_gallery.value = '0';
    document.frmEditSlider.sezione.value = '<?=$sezione?>';
    document.frmEditSlider.submit();
}
</script>

<?php
}
else if($isubsezione == 1)
{
    // Form di modifica/inserimento
    $id_img = isset($_POST['id']) ? (int)$_POST['id'] : 0;
    $img = '';
    $titolo = '';
    $des = '';
    $ordine = 1;
    $visibile = '1';
    $url = '';
    
    // Array per i testi multilingua
    $testi_lingue = array();
    $testolink = '';
    
    if($id_img > 0) {
        $sql = "SELECT * FROM t_img_gallery WHERE id=".$id_img;
        $mydb->DoSelect($sql);
        if($row = $mydb->GetRow()) {
            $img = $row['img'];
            $titolo = $row['titolo'];
            $des = $row['des'];
            $ordine = $row['ordine'];
            $visibile = $row['visibile'];
            $url = $row['url'];
            $testolink = isset($row['testolink']) ? $row['testolink'] : '';
        } else {
            echo "<div class='boxerr'>Slide non trovata!</div>";
            exit;
        }
        
        // Carica traduzioni esistenti
        $sql = "SELECT lang, titolo, testo, testolink FROM t_testi_cnt_gallery WHERE id_img=".$id_img;
        $res_lingue = $mydb->DoSelect($sql);
        if(is_array($res_lingue)) {
            foreach($res_lingue as $row_lang) {
                $testi_lingue[$row_lang['lang']] = array(
                    'titolo' => $row_lang['titolo'],
                    'testo' => $row_lang['testo'],
                    'testolink' => isset($row_lang['testolink']) ? $row_lang['testolink'] : ''
                );
            }
        }
    } else {
        // Calcola il prossimo ordine
        $sql = "SELECT MAX(ordine) as max_ord FROM t_img_gallery WHERE id_gal=".$id_gallery;
        $mydb->DoSelect($sql);
        if($row = $mydb->GetRow()) {
            $ordine = $row['max_ord'] + 1;
        }
    }
    
    $slider_title = mydecodeTxt($slider_info['titolo']);
    $form_title = ($id_img > 0) ? 'Modifica Slide' : 'Nuova Slide';
?>

<!-- Pulsante torna indietro -->
<div style="margin-bottom:20px;">
    <button type="button" onclick="document.frmslide.azione.value=''; document.frmslide.sezione.value='cms,0'; document.frmslide.submit();" class="button buttonrev" style="width:200px">
        <div><i class="bi bi-arrow-left" style="margin-right:5px;"></i> Torna alle Slide</div>
    </button>
</div>

<div style="margin-bottom:20px;">
    <h2><?=$slider_title?> - <?=$form_title?></h2>
</div>

<form name="frmslide" method="post" action="<?=$action?>" enctype="multipart/form-data">
    <input type="hidden" name="MSID" value="<?=$MSID?>" />
    <input type="hidden" name="sezione" value="cms,1" />
    <input type="hidden" name="tab" value="<?=$tab?>" />
    <input type="hidden" name="tab2" value="<?=$tab2 ?? ''?>" />
    <input type="hidden" name="ord" value="<?=$ord?>" />
    <input type="hidden" name="id" value="<?=$id_img?>" />
    <input type="hidden" name="id_gallery" value="<?=$id_gallery?>" />
    <input type="hidden" name="azione" value="salva" />
    
    <!-- Dati Generali -->
    <div style="border:1px solid #ddd; border-radius:8px; padding:20px; background:#f8f9fa; margin-bottom:20px;">
        <h3 style="margin-top:0; margin-bottom:20px; color:#224C8C;">Dati Generali</h3>
        
        <div style="display:grid; grid-template-columns: 150px 1fr; gap:15px; align-items:start;">
            <label style="font-weight:bold; padding-top:8px;">Ordine:</label>
            <div>
                <input type="number" name="ordine" value="<?=$ordine?>" min="1" style="width:80px;" />
                <br><small style="color:#666;">(definisce la posizione della slide)</small>
            </div>
            
            <label style="font-weight:bold; padding-top:8px;">Immagine:</label>
            <div>
                <?php if($img != '') { ?>
                    <div style="margin-bottom:10px;">
                        <img src="<?=$path_gallery.$img?>" alt="" style="max-width:400px; max-height:250px; border:1px solid #ccc; border-radius:4px;" />
                        <br><small style="color:#666;">Immagine attuale: <?=$img?></small>
                    </div>
                <?php } ?>
                <input type="file" name="img_upload" accept="image/*" />
                <br><small style="color:#666;">Formato consigliato: JPG o PNG, dimensioni minime 1200x400px</small>
            </div>
            
            <label style="font-weight:bold; padding-top:8px;">URL Link:</label>
            <div>
                <input type="text" name="url" value="<?=htmlspecialchars($url)?>" style="width:100%; max-width:600px;" placeholder="es: /catalogo/?offerte o https://..." />
                <br><small style="color:#666;">Link al quale punta la slide quando cliccata</small>
            </div>
            
            <label style="font-weight:bold; padding-top:8px;">Visibile:</label>
            <div>
                <label style="font-weight:normal;"><input type="radio" name="visibile" value="1" <?=($visibile=='1')?'checked':''?> /> Sì</label>
                &nbsp;&nbsp;
                <label style="font-weight:normal;"><input type="radio" name="visibile" value="0" <?=($visibile=='0')?'checked':''?> /> No</label>
            </div>
        </div>
    </div>
    
    <!-- Testi Multilingua -->
    <h3 style="margin-bottom:15px; color:#224C8C;">Testi nelle diverse lingue</h3>
    
    <div style="display:flex; flex-direction:column; gap:20px; max-width:100%;">
        <?php 
        // Mappa codici lingua a flag-icons
        $flags = array(
            'it' => 'it',
            'en' => 'gb',
            'fr' => 'fr',
            'es' => 'es',
            'de' => 'de'
        );
        
        foreach($_vlanguage as $idx => $lang_code) {
            if($_vlanguage_enabled[$idx] == '1') {
                $flag = isset($flags[$lang_code]) ? $flags[$lang_code] : $lang_code;
                $titolo_lang = isset($testi_lingue[$lang_code]) ? htmlspecialchars($testi_lingue[$lang_code]['titolo']) : '';
                $des_lang = isset($testi_lingue[$lang_code]) ? htmlspecialchars($testi_lingue[$lang_code]['testo']) : '';
                $testolink_lang = isset($testi_lingue[$lang_code]) ? htmlspecialchars($testi_lingue[$lang_code]['testolink']) : '';
                
                // Per l'italiano, usa i valori di default da t_img_gallery
                if($lang_code == 'it' && $titolo_lang == '') $titolo_lang = htmlspecialchars($titolo);
                if($lang_code == 'it' && $des_lang == '') $des_lang = htmlspecialchars($des);
                if($lang_code == 'it' && $testolink_lang == '') $testolink_lang = htmlspecialchars($testolink);
                
                $lang_name = $_vdeslanguage[$idx];
        ?>
            <div style="border:1px solid #ddd; border-radius:8px; padding:20px; background:#f8f9fa;">
                <label style="display:flex; align-items:center; gap:10px; margin-bottom:15px; font-weight:bold; font-size:16px;">
                    <span class="fi fi-<?=$flag?>" style="font-size:24px;"></span>
                    <?=htmlspecialchars($lang_name)?> (<?=strtoupper($lang_code)?>)
                </label>
                
                <div style="margin-bottom:15px;">
                    <label style="display:block; margin-bottom:5px; font-weight:500;">Titolo:</label>
                    <input type="text" name="titolo_<?=$lang_code?>" value="<?=$titolo_lang?>" 
                           style="width:100%; padding:8px; border:1px solid #ccc; border-radius:4px; font-size:14px;" 
                           placeholder="Testo principale della slide" />
                </div>
                
                <div style="margin-bottom:15px;">
                    <label style="display:block; margin-bottom:5px; font-weight:500;">Descrizione:</label>
                    <textarea name="des_<?=$lang_code?>" rows="3" 
                              style="width:100%; padding:8px; border:1px solid #ccc; border-radius:4px; font-size:14px; resize:vertical;"
                              placeholder="Testo descrittivo/sottotitolo"><?=$des_lang?></textarea>
                </div>
                
                <div>
                    <label style="display:block; margin-bottom:5px; font-weight:500;">Testo Pulsante:</label>
                    <input type="text" name="testolink_<?=$lang_code?>" value="<?=$testolink_lang?>" 
                           style="width:100%; padding:8px; border:1px solid #ccc; border-radius:4px; font-size:14px;" 
                           placeholder="es: Scopri di più, Vai al sito, Leggi tutto..." />
                    <small style="color:#666;">Testo visualizzato nel pulsante se è impostato un URL</small>
                </div>
            </div>
        <?php 
            }
        }
        ?>
    </div>
    
    <!-- Pulsanti Azione -->
    <div style="margin-top:30px; padding:20px; background:#f8f9fa; border-radius:8px; display:flex; gap:10px; align-items:center; justify-content:center;">
        <button type="submit" class="button">
            <div>
                <svg class="btn-icon" width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg" style="vertical-align:middle; margin-right:5px;">
                    <path d="M13.854 3.646a.5.5 0 0 1 0 .708l-7 7a.5.5 0 0 1-.708 0l-3.5-3.5a.5.5 0 1 1 .708-.708L6.5 10.293l6.646-6.647a.5.5 0 0 1 .708 0z" fill="currentColor"/>
                </svg>
                Salva Slide
            </div>
        </button>
        <button type="button" onclick="document.frmslide.azione.value=''; document.frmslide.sezione.value='cms,0'; document.frmslide.submit();" class="button buttonrev">
            <div>
                <svg class="btn-icon" width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg" style="vertical-align:middle; margin-right:5px;">
                    <path d="M4.646 4.646a.5.5 0 0 1 .708 0L8 7.293l2.646-2.647a.5.5 0 0 1 .708.708L8.707 8l2.647 2.646a.5.5 0 0 1-.708.708L8 8.707l-2.646 2.647a.5.5 0 0 1-.708-.708L7.293 8 4.646 5.354a.5.5 0 0 1 0-.708z" fill="currentColor"/>
                </svg>
                Annulla
            </div>
        </button>
    </div>
</form>

<?php
}
?>


