<?php
// mass_dns.php - Com Sidebar e Layout Responsivo
header('Content-Type: text/html; charset=utf-8');
session_start();
require_once 'includes/db.php';
if (!isset($_SESSION['logged_in_fxtream'])) { header('Location: index.php'); exit; }
?>
<!DOCTYPE html>
<html lang="pt-br">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Troca de DNS em Massa</title>
    
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.6.0/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/toastr.min.css">
    
    <link rel="stylesheet" href="https://vendedorp2p.com/views/_css/style.css">
    <link rel="stylesheet" href="https://vendedorp2p.com/views/_css/dark-theme.css">
    
    <style>
        body { background-color: #121212; color: #e0e0e0; font-family: 'Segoe UI', sans-serif; overflow-x: hidden; }
        .card { background-color: #1e1e1e; border: 1px solid #333; }
        .form-control { background-color: #2c2c2c; border: 1px solid #444; color: white; }
        .form-control:focus { background-color: #333; color: white; border-color: #007bff; }
        
        .custom-control-label { cursor: pointer; user-select: none; }

        /* ============================================================
           LAYOUT RESPONSIVO (SIDEBAR + CONTEÚDO)
           ============================================================ */
        
        /* PC (Telas Grandes) */
        @media (min-width: 992px) {
            #sidebar { 
                display: block !important; 
                width: 240px; 
                position: fixed; top: 0; left: 0; height: 100vh; 
                z-index: 1000; overflow-y: auto; 
            }
            .right-column { 
                margin-left: 240px; 
                width: calc(100% - 240px); 
                padding: 0;
            }
            .btn-voltar-mobile { display: none; }
        }

        /* CELULAR (Telas Pequenas) */
        @media (max-width: 991px) {
            #sidebar { display: none !important; }
            .right-column { 
                margin-left: 0 !important; 
                width: 100% !important; 
                padding: 0;
            }
            .btn-voltar-mobile {
                display: inline-block;
                margin-right: 10px;
                color: #fff;
                font-size: 1.2rem;
            }
        }

        /* Cabeçalho Customizado */
        .page-header {
            padding: 20px; 
            border-bottom: 1px solid #333; 
            margin-bottom: 30px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            background-color: #1e1e1e;
        }
    </style>
</head>
<body>

<div class="container-fluid p-0">
    <div class="row no-gutters">
        
        <?php include 'includes/sidebar.php'; ?>

        <div class="right-column">

            <div class="page-header sticky-top">
                <div class="d-flex align-items-center">
                    <a href="dashboard.php" class="btn-voltar-mobile"><i class="fa fa-arrow-left"></i></a>
                    <h3 class="m-0 h4"><i class="fa fa-sync-alt text-info mr-2"></i> Alterar Links em Massa</h3>
                </div>
                <a href="dashboard.php" class="btn btn-secondary btn-sm d-none d-lg-inline-block"><i class="fa fa-arrow-left"></i> Voltar</a>
            </div>

            <div class="container-fluid px-4 pb-5">
                
                <div class="card shadow-lg mx-auto" style="max-width: 900px;">
                    <div class="card-body p-4">
                        
                        <div class="alert alert-info border-info bg-dark">
                            <i class="fa fa-info-circle"></i> Use esta ferramenta para trocar o domínio (DNS) ou porta de seus conteúdos.
                        </div>

                        <form id="replaceForm">
                            <div class="form-group">
                                <label class="font-weight-bold">Texto Antigo (O que procurar):</label>
                                <input type="text" class="form-control form-control-lg" id="findText" placeholder="Ex: http://dns-velho.com:8080">
                            </div>

                            <div class="form-group">
                                <label class="font-weight-bold text-success">Texto Novo (Pelo que substituir):</label>
                                <input type="text" class="form-control form-control-lg border-success" id="replaceText" placeholder="Ex: http://novo-dns.com:80">
                            </div>

                            <div class="form-group mt-4">
                                <label class="d-block font-weight-bold mb-2">Onde aplicar?</label>
                                
                                <div class="custom-control custom-checkbox custom-control-inline">
                                    <input type="checkbox" class="custom-control-input target-chk" id="chkChannels" value="channels" checked>
                                    <label class="custom-control-label" for="chkChannels"><i class="fa fa-tv text-primary"></i> Canais</label>
                                </div>

                                <div class="custom-control custom-checkbox custom-control-inline">
                                    <input type="checkbox" class="custom-control-input target-chk" id="chkMovies" value="movies" checked>
                                    <label class="custom-control-label" for="chkMovies"><i class="fa fa-film text-success"></i> Filmes</label>
                                </div>

                                <div class="custom-control custom-checkbox custom-control-inline">
                                    <input type="checkbox" class="custom-control-input target-chk" id="chkSeries" value="series" checked>
                                    <label class="custom-control-label" for="chkSeries"><i class="fa fa-video text-warning"></i> Episódios (Séries)</label>
                                </div>
                            </div>

                            <hr class="border-secondary my-4">

                            <button type="button" class="btn btn-primary btn-lg btn-block font-weight-bold" onclick="runReplace()">
                                <i class="fa fa-bolt"></i> EXECUTAR TROCA
                            </button>
                        </form>

                    </div>
                </div>

            </div> </div> </div>
</div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.6.0/js/bootstrap.bundle.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/toastr.min.js"></script>
<script src="https://vendedorp2p.com/views/_plugins/bootbox.min.js"></script>

<script>
    function runReplace() {
        let find = $('#findText').val().trim();
        let replace = $('#replaceText').val().trim();
        let targets = [];

        $('.target-chk:checked').each(function() {
            targets.push($(this).val());
        });

        if(!find) { toastr.warning('Digite o texto antigo para procurar.'); return; }
        if(targets.length === 0) { toastr.warning('Selecione pelo menos uma opção (Canais, Filmes ou Séries).'); return; }

        bootbox.confirm({
            title: "Confirmação",
            message: `Você vai trocar <b>"${find}"</b> por <b>"${replace}"</b> em todas as áreas selecionadas.<br><br>Tem certeza?`,
            buttons: {
                confirm: { label: 'Sim, Trocar', className: 'btn-success' },
                cancel: { label: 'Cancelar', className: 'btn-secondary' }
            },
            callback: function (result) {
                if(result) {
                    let loading = bootbox.dialog({ message: '<div class="text-center"><i class="fa fa-spin fa-spinner"></i> Processando...</div>', closeButton: false });
                    
                    $.post('api/tools_api.php', {
                        action: 'mass_replace',
                        find: find,
                        replace: replace,
                        targets: targets
                    }, function(res) {
                        loading.modal('hide');
                        if(res.status) {
                            toastr.success(res.msg);
                        } else {
                            toastr.error(res.msg);
                        }
                    }, 'json');
                }
            }
        });
    }
</script>

</body>
</html>