<?php

ini_set("display_errors", 0);
ini_set("display_startup_errors", 0);
session_start();

// Check authentication
if (!isset($_SESSION['id'])) {
    header("Location: login.php");
    exit();
}

$id_store = $_SESSION['id'];

if ($_SESSION['store_type'] == 2) {
    header("Location: users_mac.php");
    exit();
}

ini_set("display_errors", 1);
ini_set("display_startup_errors", 1);
error_reporting(E_ALL);

// DB connection
$db = new SQLite3("./api/.ansdb.db");

// Handle domain update form
if (isset($_POST["update_domain"])) {
    $dominio_antigo = trim($_POST['old_domain']);
    $dominio_novo   = trim($_POST['new_domain']);

    $id_user = $_SESSION['id'];
    $result  = $db->query("SELECT COUNT(*) as count FROM ibo WHERE dns LIKE '%$dominio_antigo%' AND id_user = '$id_user'");
    $row     = $result->fetchArray();

    if ($row['count'] > 0) {
        $db->exec("UPDATE ibo SET
            dns = REPLACE(dns, '$dominio_antigo', '$dominio_novo'),
            url = REPLACE(url, '$dominio_antigo', '$dominio_novo')
            WHERE id_user = '$id_user'");

        $_SESSION['success_message'] = "DOMAIN UPDATED SUCCESSFULLY!";
    } else {
        $_SESSION['error_message'] = "THE OLD DOMAIN YOU ENTERED DOES NOT EXIST IN THE DATABASE.";
    }

    header("Location: " . $_SERVER['PHP_SELF']);
    exit();
}

include "includes/header.php";

// Success message
if (isset($_SESSION['success_message'])) {
    echo '<div class="alert alert-success alert-dismissible fade show" role="alert" id="success-alert">
            ' . $_SESSION['success_message'] . '
            <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                <span aria-hidden="true">&times;</span>
            </button>
          </div>';
    unset($_SESSION['success_message']);
}

// Error message
if (isset($_SESSION['error_message'])) {
    echo '<div class="alert alert-danger alert-dismissible fade show" role="alert" id="error-alert">
            ' . $_SESSION['error_message'] . '
            <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                <span aria-hidden="true">&times;</span>
            </button>
          </div>';
    unset($_SESSION['error_message']);
}
?>

<div class="domain-wrapper">
    <div class="domain-card">
        <div class="domain-header">
            <h2>Bulk Domain Update</h2>
        </div>

        <div class="domain-body">
            <form method="post" action="">
                <div class="form-group">
                    <label for="old_domain"><strong>Old Domain</strong></label>
                    <input type="text"
                           class="form-control domain-input"
                           name="old_domain"
                           placeholder="Enter the old domain (ex: http://old-domain.com)"
                           required>
                </div>

                <div class="form-group">
                    <label for="new_domain"><strong>New Domain</strong></label>
                    <input type="text"
                           class="form-control domain-input"
                           name="new_domain"
                           placeholder="Enter the new domain (ex: http://new-domain.com)"
                           required>
                </div>

                <div class="domain-footer">
                    <button type="submit" name="update_domain" class="btn btn-warning domain-btn">
                        Update Domain
                    </button>
                </div>
            </form>
        </div>
    </div>
</div>

<style>
/* Wrapper to center the card similar to ALL USERS page */
.domain-wrapper{
    max-width: 780px;
    margin: 40px auto 60px;
}

/* Card style (same dark / neon style as all_users.php) */
.domain-card{
    background: radial-gradient(circle at top, #0f172a 0%, #020617 55%, #000 100%);
    border-radius: 16px;
    padding: 20px 24px 22px;
    box-shadow: 0 32px 60px rgba(0,0,0,0.65);
    border: 1px solid rgba(148,163,184,0.35);
    color: #e5e7eb;
}

/* Header */
.domain-header{
    padding-bottom: 10px;
    border-bottom: 1px solid rgba(51,65,85,0.9);
    margin-bottom: 18px;
}
.domain-header h2{
    font-size: 1.45rem;
    font-weight: 700;
    color: #f9fafb;
    margin: 0;
}

/* Body */
.domain-body{
    margin-top: 6px;
}

/* Inputs with dark style */
.domain-input{
    background: #020617;
    border-radius: 8px;
    border: 1px solid rgba(148,163,184,0.5);
    color: #e5e7eb;
    font-size: 0.95rem;
    padding: 8px 10px;
}
.domain-input::placeholder{
    color: #6b7280;
    font-size: 0.85rem;
}
.domain-input:focus{
    outline: none;
    box-shadow: 0 0 0 1px #22d3ee;
    border-color: #22d3ee;
}

/* Footer button area */
.domain-footer{
    text-align: right;
    margin-top: 18px;
}

/* Button style (yellow/orange but with neon glow) */
.domain-btn{
    padding: 8px 22px;
    border-radius: 999px;
    font-weight: 600;
    border: none;
    background: linear-gradient(135deg,#facc15,#f97316);
    color:#111827;
    box-shadow: 0 10px 22px rgba(248, 181, 0, 0.45);
}
.domain-btn:hover{
    filter: brightness(1.05);
    box-shadow: 0 14px 26px rgba(248, 181, 0, 0.55);
}

/* Adjust default bootstrap alert look on dark bg */
#success-alert, #error-alert{
    max-width: 780px;
    margin: 15px auto 0;
}
</style>

<?php
$db->close();
include "includes/footer.php";
?>

<script>
$(document).ready(function() {
    $("#success-alert").fadeTo(2000, 500).slideUp(500, function(){
        $(this).alert('close');
    });

    $("#error-alert").fadeTo(2000, 500).slideUp(500, function(){
        $(this).alert('close');
    });
});
</script>
