<?php
echo "<!-- inc.editcat.php -->\n";
?>
<!-- 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

// Includi il file delle azioni (salva)
include_once('inc.editcat.actions.php');

if($isubsezione == 0)
{
    // Gestione ricerca
    $search = isset($_POST['search']) ? trim($_POST['search']) : (isset($_GET['search']) ? trim($_GET['search']) : '');
    
   
    // Condizione di ricerca
    $search_cond = '';
    if($search != '') {
        $search_escaped = $mydb->Escape('%'.$search.'%');
        $search_cond = " AND (l.titolo LIKE $search_escaped )";
    }
    
    // Lista delle labels - solo quelle associate a categorie visibili di livello 1
    $sql = "SELECT l.*
            FROM t_categorie l
            WHERE l.visibile = '1' AND level = 1
            $search_cond
            AND l.visibile = 1
            ORDER BY l.titolo ASC";
    $res = $mydbG->DoSelect($sql);
    $total_found = count($res);
?>

<div style="margin-bottom:20px;">
    <h2>Gestione Categorie Prodotti</h2>
</div>

<!-- Campo di ricerca -->
<div style="margin-bottom:20px; padding:15px; background:#fff; border-radius:8px; border:1px solid #ddd;">
    <form name="frmsearch" method="post" action="<?=$action?>" style="display:flex; gap:10px; align-items:flex-end;">
        <input type="hidden" name="MSID" value="<?=$MSID?>" />
        <input type="hidden" name="sezione" value="cms" />
        <input type="hidden" name="tab" value="<?=$tab?>" />
        
        <div style="flex:1; max-width:400px;">
            <label style="display:block; margin-bottom:5px; font-weight:bold; font-size:13px;">
                <i class="bi bi-search" style="margin-right:5px;"></i> Cerca
            </label>
            <input type="text" name="search" value="<?=htmlspecialchars($search)?>" 
                   style="width:100%; padding:8px 12px; border:1px solid #ccc; border-radius:4px; font-size:14px; box-sizing:border-box;"
                   placeholder="Nome, descrizione o testo italiano..." />
        </div>
        
        <button type="submit" class="button" style="width:120px;">
            <div><i class="bi bi-search" style="margin-right:5px;"></i> Cerca</div>
        </button>
        
        <?php if($search != '') { ?>
        <button type="button" onclick="document.frmsearch.search.value=''; document.frmsearch.submit();" class="button buttonrev" style="width:120px;">
            <div><i class="bi bi-x-circle" style="margin-right:5px;"></i> Reset</div>
        </button>
        <?php } ?>
    </form>
    
    <?php if($search != '') { ?>
    <div style="margin-top:10px; font-size:13px; color:#666;">
        <strong>Risultati:</strong> <?=$total_found?> etichett<?=$total_found==1?'a':'e'?> trovata/e per "<strong><?=htmlspecialchars($search)?></strong>"
    </div>
    <?php } ?>
</div>


<form name="frmins" method="post" action="<?=$action?>">

<div class="labels-table-container">
<table width="100%" border="0" cellspacing="0" cellpadding="0" class="labels-table">
  <thead>
  <tr>
    <th width="5%">ID</th>
    <th width="">Categoria</th>
    <th width="15%">Azioni</th>
  </tr>
  </thead>
  <tbody>

<?php
if(count($res) == 0) {
    echo '<tr><td colspan="3" align="center" style="padding:40px;">📝 Nessuna categoria presente.</td></tr>';
}

foreach($res as $row)
{
    $id_label = $row['id'];
    $valore_it = $row['titolo'];
    if($valore_it != "") $valore_it = mydecodeTxt($valore_it);
   

?>
  <tr class="label-row">
    <td class="label-id"><?=$id_label?></td>
    <td class="label-value"><strong><?=$valore_it?></strong></td>
    <td class="label-actions">
        <button type="button" class="btn-translate" onclick="document.frmins.sezione.value='cms,1'; document.frmins.id_label.value='<?=$id_label?>'; document.frmins.submit();" title="Modifica traduzioni">
            <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">Traduci</span>
        </button>
    </td>
  </tr>

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

        <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_label" value="" />
        
        <input type="hidden" name="tab" value="<?=$tab?>" />
        <input type="hidden" name="tab2" value="<?=$tab2 ?? ''?>" />  
        
        <input type="hidden" name="ord" value="<?=$ord?>" />
</form>

<?php
}
else if($isubsezione == 1)
{
    // Form di modifica traduzioni
    $id_label = isset($_POST['id_label']) ? (int)$_POST['id_label'] : 0;
    
    if($id_label == 0) {
        echo "<div class='boxerr'>ID label non valido!</div>";
        return;
    }
    
    // Carica info label
    $sql = "SELECT * FROM t_categorie WHERE id=".$id_label;
    $mydbG->DoSelect($sql);
    $label_info = $mydbG->GetRow();
    
    if(!$label_info) {
        echo "<div class='boxerr'>Etichetta non trovata!</div>";
        return;
    }
    
    $valore_default = $label_info['titolo'];
    
    // Carica traduzioni esistenti
    $testi_lingue = array();
    $sql = "SELECT lang, titolo FROM t_testi_categorie WHERE id_cat=".$id_label;
    $res_lingue = $mydbG->DoSelect($sql);
    if(is_array($res_lingue)) {
        foreach($res_lingue as $row_lang) {
            $testi_lingue[$row_lang['lang']] = $row_lang['titolo'];
        }
    }
?>

<div style="margin-bottom:20px;">
    <h2>Traduzioni per: <code><?=mydecodeTxt($valore_default)?></code></h2>
</div>

<form name="frmlabel" method="post" action="<?=$action?>">
    <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_label" value="<?=$id_label?>" />
    <input type="hidden" name="azione" value="salva" />
    
    <table width="100%" border="0" cellspacing="5" cellpadding="5" class="tbladmin">
        
        
        <tr>
            <td width="20%" class="tbladmin_label">Valore Default:</td>
            <td>
                <strong><?=htmlspecialchars($valore_default)?></strong>
                <br><small>Valore originale nel database (campo "valore" in t_labels)</small>
            </td>
        </tr>
        
       
        
        <tr>
            <td class="tbladmin_label" style="vertical-align:top; padding-top:15px;">Traduzioni:</td>
            <td>
                <!-- Lingue disponibili -->
                <div style="display:flex; flex-wrap:wrap; gap:20px; max-width:100%;">
                    <?php 
                    // Mappa codici lingua a flag-icons (SVG)
                    $flags = array(
                        'it' => 'it',
                        'en' => 'gb',
                        'fr' => 'fr',
                        'es' => 'es',
                        'de' => 'de'
                    );
                    
                    foreach($_vlanguage as $idx => $lang_code) {
                        if($_vlanguage_enabled[$idx] == '1') {
                            $valore_lang = isset($testi_lingue[$lang_code]) ? $testi_lingue[$lang_code] : '';
                            
                            // Per l'italiano, usa il valore di default da t_labels se non c'è traduzione
                            if($lang_code == 'it' && $valore_lang == '') {
                                $valore_lang = $valore_default;
                            }
                            
                            $flag_code = isset($flags[$lang_code]) ? $flags[$lang_code] : 'un';
                            $lang_name = $_vdeslanguage[$idx];
                    ?>
                    <div style="flex:1 1 calc(60% - 10px); min-width:280px; max-width:100%; margin-bottom:15px; box-sizing:border-box;">
                        <label style="display:block; margin-bottom:8px; font-weight:bold; font-size:14px;">
                            <span class="fi fi-<?=$flag_code?>" style="margin-right:8px; width:24px; height:18px; display:inline-block; vertical-align:middle; border-radius:2px;"></span>
                            Testo (<?=strtoupper($lang_code)?>)
                        </label>
                        <textarea name="valore_<?=$lang_code?>" rows="1" style="width:100%; max-width:100%; padding:8px; border:1px solid #ccc; border-radius:4px; font-size:13px; box-sizing:border-box; resize:vertical;"><?=$valore_lang?></textarea>
                        <small style="color:#666; font-size:11px;">Traduzione in <?=ucfirst($lang_name)?></small>
                    </div>
                    <?php 
                        }
                    }
                    ?>
                </div>
            </td>
        </tr>
        
        <tr>
            <td></td>
            <td style="padding-top:20px;">
                <div class="button" onclick="waitpage();document.frmlabel.azione.value='salva'; document.frmlabel.submit();"><div>Salva Traduzioni</div></div>
                
                &nbsp;&nbsp;
                <div class="button buttonrev" onclick="document.frmlabel.azione.value=''; document.frmlabel.sezione.value='cms'; document.frmlabel.submit();"><div>Annulla</div></div>
            </td>
        </tr>
    </table>
</form>

<form name="frmins" method="post" action="<?=$action?>">
        <input type="hidden" name="MSID" value="<?=$MSID?>" />
        <input type="hidden" name="sezione" value="cms" />
        <input type="hidden" name="azione" value="" />
        <input type="hidden" name="tab" value="<?=$tab?>" />
        <input type="hidden" name="tab2" value="<?=$tab2 ?? ''?>" />  

</form>

<?php
}
?>

