<?php 
include ('includes/header.php');

$table_name = 'devop';
$page_name = 'dev_option';
$data = ['eduuid' => 'disable','maxpg' => '8' , 'macmod' => '1'];

$db->insertIfEmpty($table_name, $data);

$res = $db->select($table_name, '*', '', '');

// --- LÓGICA PARA LEER LA LLAVE ACTUAL ---
// Ruta apuntando a la carpeta /api/ que está al mismo nivel que este archivo
$ruta_llave = 'api/llave.txt'; 
$idioma_actual = 'es'; // Español por defecto
if (file_exists($ruta_llave)) {
    $idioma_actual = trim(file_get_contents($ruta_llave));
}
// ----------------------------------------

if(isset($_POST['submit'])){
    unset($_POST['submit']);
    
    // --- LÓGICA PARA GUARDAR EL IDIOMA ---
    if(isset($_POST['ad_language'])){
        $nuevo_idioma = $_POST['ad_language'];
        // ¡Se agregó 'fr' a la validación permitida!
        if ($nuevo_idioma === 'en' || $nuevo_idioma === 'es' || $nuevo_idioma === 'fr') {
            
            // Intentamos guardar y verificamos si hay error de permisos
            $exito = file_put_contents($ruta_llave, $nuevo_idioma);
            
            if ($exito === false) {
                echo "<script>alert('Error: El servidor no permitió guardar el idioma. Por favor, asegúrate de crear el archivo api/llave.txt y darle permisos 777.');</script>";
            }
        }
        // Lo eliminamos del $_POST para que no cause error al actualizar la base de datos devop
        unset($_POST['ad_language']); 
    }
    // -------------------------------------

    $updateData = $_POST;
    // Solo actualizamos la base de datos si quedaron datos en $_POST
    if(!empty($updateData)) {
        $db->update($table_name, $updateData, 'id = :id',[':id' => 1]);
    }
    
    // Redirigir para refrescar la página
    echo "<script>window.location.href='". $page_name.".php'</script>";
}
?>
<div class="container-fluid pt-4 px-4">
    <div class="row g-4">
        <div class="col-12">
            <div class="bg-secondary rounded h-100 p-4">
                <h2 class="mb-4">Advance Options</h2>
                    <form method="post">
                        <label for="exampleInputEmail1" class="form-label">Edit DNS UUID</label>
                        <select class="form-select mb-3" id="eduuid" name="eduuid">
                            <option value="enable" <?=$res[0]['eduuid']=='enable'?'selected':'' ?>>Enable</option>
                            <option value="disable" <?=$res[0]['eduuid']=='disable'?'selected':'' ?>>Disable</option>
                        </select>
                        
                        <label for="exampleInputEmail1" class="form-label">Mac User Results per page</label>
                        <select class="form-select mb-3" id="maxpg" name="maxpg">
                            <option value="2" <?=$res[0]['maxpg']=='2'?'selected':'' ?>>2 pages</option>
                            <option value="5" <?=$res[0]['maxpg']=='5'?'selected':'' ?>>5 pages</option>
                            <option value="8" <?=$res[0]['maxpg']=='8'?'selected':'' ?>>8 pages</option>
                            <option value="10" <?=$res[0]['maxpg']=='10'?'selected':'' ?>>10 pages</option>
                            <option value="15" <?=$res[0]['maxpg']=='15'?'selected':'' ?>>15 pages</option>
                            <option value="20" <?=$res[0]['maxpg']=='20'?'selected':'' ?>>20 pages</option>
                            <option value="25" <?=$res[0]['maxpg']=='25'?'selected':'' ?>>25 pages</option>
                            <option value="30" <?=$res[0]['maxpg']=='30'?'selected':'' ?>>30 pages</option>
                            <option value="40" <?=$res[0]['maxpg']=='40'?'selected':'' ?>>40 pages</option>
                            <option value="50" <?=$res[0]['maxpg']=='50'?'selected':'' ?>>50 pages</option>
                        </select>
                        
                        <label for="exampleInputEmail1" class="form-label">Mac generate method</label>
                        <select class="form-select mb-3" id="macmod" name="macmod">
                            <option value="1" <?=$res[0]['macmod']=='1'?'selected':'' ?>>Basic method</option>
                            <option value="2" <?=$res[0]['macmod']=='2'?'selected':'' ?>>Advance method</option>
                            <option value="3" <?=$res[0]['macmod']=='3'?'selected':'' ?>>Numbers Only [Ex : 12:15:16:18:11:25]</option>
                            <option value="4" <?=$res[0]['macmod']=='4'?'selected':'' ?>>Text Only [Ex : bc:cc:fc:ec:ff:de]</option>
                        </select>

                        <label for="ad_language" class="form-label" style="color: #f64e60;">AdView Movie Language (TMDB)</label>
                        <select class="form-select mb-4" id="ad_language" name="ad_language">
                            <option value="es" <?=$idioma_actual=='es'?'selected':'' ?>>Español (Latino)</option>
                            <option value="en" <?=$idioma_actual=='en'?'selected':'' ?>>Inglés (English)</option>
                            <option value="fr" <?=$idioma_actual=='fr'?'selected':'' ?>>Francés (Français)</option> </select>

                        <button type="submit" name="submit" class="btn btn-primary">Update Advance Options</button>
                    </form>
            </div>
        </div>
    </div>
</div>
<?php include ('includes/footer.php');?>
</body>
</html>