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

    $file_path = 'a/qrcode.json';
    $file_content = file_get_contents($file_path);
    $data_array = json_decode($file_content, true);
    $flash_message = '<div class="alert alert-primary" id="flash-msg"><h4><i class="icon fa fa-check"></i>Atualizado com Sucesso!</h4></div>';

    if (isset($_POST['texto_superior'])) {
        $new_data = ['texto_inferior' => $_POST['texto_inferior'], 'texto_superior' => $_POST['texto_superior'], 'imagem' => $_POST['imagem']];
        $merged_data = array_replace_recursive($data_array, $new_data);
        $updated_content = json_encode($merged_data, JSON_UNESCAPED_UNICODE);
        file_put_contents($file_path, $updated_content);
       echo "<script>window.location.href='qrcode.php?m=$flash_message'</script>";
    }
    
    $texto_inferior = $data_array['texto_inferior'];
    $texto_superior = $data_array['texto_superior'];
    $imagem = $data_array['imagem'];
  
?>
    <div class="container-fluid">
<?php
    if (isset($_GET['m'])) {
        echo $_GET['m'];
    }
?>
        <h1 class="h3 mb-1 text-gray-800">QR Code e Texto</h1>
        <!-- Content Row -->
        <div class="row">
            <!-- First Column -->
            <div class="col-lg-6">
                <!-- Custom codes -->
                <div class="card border-left-primary shadow h-100 card shadow mb-4">
                    <div class="card-header py-3">
                        <h6 class="m-0 font-weight-bold text-primary2"><i class="fa fa-bullhorn"></i> QR Code e Texto</h6>
                    </div>
                    <div class="card-body">
                        <form method="post">
                            <div class="form-group ">
                                <label class="control-label" for="title"><strong>Texto Superior:</strong></label>
                                <div class="input-group">
                                    <input type="text" class="form-control" placeholder="Texto Supeior" name="texto_superior" value="<?php echo $texto_superior ?>">
                                </div>
                            </div>
                            <div class="form-group ">
                                <label class="control-label" for="title"><strong>Texto Inferior:</strong></label>
                                <div class="input-group">
                                    <input type="text" class="form-control" placeholder="Texto Inferior" name="texto_inferior" value="<?php echo $texto_inferior ?>">
                                </div>
                            </div>
                            <div class="form-group ">
                                <label class="control-label " for="content"><strong>QR Code:</strong></label>
                                <div class="input-group">
                                    <input type="text" class="form-control" placeholder="QR Code URL" name="imagem" value="<?php echo $imagem ?>">
                                </div>
                            </div>
                            <button type="submit" name="text" class="btn btn-primary">Atualizar</button>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>

<?php
    include 'includes/footer.php';
?>
</body>
</html>
