<?php
// Database connection
include_once("header.php");

// Define tables and respective date columns
$tableDateColumns = [
    "card_log" => "created_at",
    "history_work" => "order_time",
    "logsn" => "created_at",
    "order_list" => "order_time",
    "tin_data" => "created_at",
    "server_copy" => "created_at"
];

// Bengali table names
$tableNamesBn = [
    "card_log" => "📌 āφāχāĻĄāĻŋ āĻ•āĻžāĻ°ā§āĻĄ āϞāĻŋāĻˇā§āϟ",
    "history_work" => "📜 āĻ•āĻžāĻœā§‡āϰ āĻšāĻŋāĻ¸ā§āĻŸā§‹āϰāĻŋ āϞāĻŋāĻˇā§āϟ",
    "logsn" => "📂 āύāĻŋāĻŦāĻ¨ā§āϧāύ āϞāĻŋāĻˇā§āϟ",
    "order_list" => "đŸ“Ļ āĻ…āĻ°ā§āĻĄāĻžāϰ āϞāĻŋāĻˇā§āϟ",
    "tin_data" => "đŸ—ƒī¸ āϟāĻŋāύ āĻĄāĻžāϟāĻž āϞāĻŋāĻˇā§āϟ",
    "server_copy" => "💾 āϏāĻžāĻ°ā§āĻ­āĻžāϰ āĻ•āĻĒāĻŋ āϞāĻŋāĻˇā§āϟ"
];

// Allowed days in Bengali
$validDays = [1, 3, 5, 7, 15];
$validDaysBn = [
    1 => "ā§§ āĻĻāĻŋāύ",
    3 => "ā§Š āĻĻāĻŋāύ",
    5 => "ā§Ģ āĻĻāĻŋāύ",
    7 => "ā§­ āĻĻāĻŋāύ",
    15 => "ā§§ā§Ģ āĻĻāĻŋāύ"
];

// Initialize response variables
$message = "";
$alertClass = "";

// Handle Form Submission
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $days = isset($_POST['days']) ? intval($_POST['days']) : 7;
    $selectedTable = isset($_POST['table']) ? $_POST['table'] : "";

    if (!in_array($days, $validDays)) {
        $message = "âš ī¸ āĻ…āĻŦ⧈āϧ āĻĻāĻŋāύ⧇āϰ āϏāĻ‚āĻ–ā§āϝāĻž āύāĻŋāĻ°ā§āĻŦāĻžāϚāύ āĻ•āϰāĻž āĻšāϝāĻŧ⧇āϛ⧇!";
        $alertClass = "alert-danger";
    } elseif (!array_key_exists($selectedTable, $tableDateColumns)) {
        $message = "âš ī¸ āĻ…āĻŦ⧈āϧ āĻŸā§‡āĻŦāĻŋāϞ āύāĻŋāĻ°ā§āĻŦāĻžāϚāύ āĻ•āϰāĻž āĻšāϝāĻŧ⧇āϛ⧇!";
        $alertClass = "alert-danger";
    } else {
        // Get correct date column
        $dateColumn = $tableDateColumns[$selectedTable];

        // Prepare & Execute SQL Query
        $sql = "DELETE FROM `$selectedTable` WHERE `$dateColumn` < NOW() - INTERVAL ? DAY";
        $stmt = $conn->prepare($sql);
        $stmt->bind_param("i", $days);

        if ($stmt->execute()) {
            $message = "✅ <b>{$tableNamesBn[$selectedTable]}</b> āĻĨ⧇āϕ⧇ <b>$validDaysBn[$days]</b> āĻĻāĻŋāύ⧇āϰ āĻŦ⧇āĻļāĻŋ āĻĒ⧁āϰāύ⧋ āϰ⧇āĻ•āĻ°ā§āĻĄ āϏāĻĢāϞāĻ­āĻžāĻŦ⧇ āĻŽā§āϛ⧇ āĻĢ⧇āϞāĻž āĻšāϝāĻŧ⧇āϛ⧇!";
            $alertClass = "alert-success";
        } else {
            $message = "❌ āĻ¤ā§āϰ⧁āϟāĻŋ: " . $stmt->error;
            $alertClass = "alert-danger";
        }
        $stmt->close();
    }
}

$conn->close();
?>

<!DOCTYPE html>
<html lang="bn">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>āĻĄāĻžāϟāĻžāĻŦ⧇āϏ āĻ•ā§āϞāĻŋāύāĻžāϰ</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <style>
        body {
            background-color: #f4f6f9;
        }
        .container {
            max-width: 480px;
            margin-top: 50px;
            background: white;
            padding: 30px;
            border-radius: 12px;
            box-shadow: 0px 0px 15px rgba(0, 0, 0, 0.1);
        }
        h2 {
            font-weight: bold;
            color: #343a40;
            text-align: center;
            margin-bottom: 20px;
        }
        .btn-delete {
            background-color: #d9534f;
            color: white;
            font-weight: bold;
        }
        .btn-delete:hover {
            background-color: #c9302c;
        }
    </style>
</head>
<body>

<div class="container">
    <h2>đŸ—‘ī¸ āĻĒ⧁āϰāύ⧋ āϰ⧇āĻ•āĻ°ā§āĻĄ āĻŽā§āϛ⧁āύ</h2>

    <?php if (!empty($message)): ?>
        <div class="alert <?php echo $alertClass; ?>" role="alert">
            <?php echo $message; ?>
        </div>
    <?php endif; ?>

    <form id="deleteForm" method="POST">
        <div class="mb-3">
            <label for="table" class="form-label">📌 āĻŸā§‡āĻŦāĻŋāϞ āύāĻŋāĻ°ā§āĻŦāĻžāϚāύ āĻ•āϰ⧁āύ:</label>
            <select class="form-control" id="table" name="table" required>
                <option value="" disabled selected>đŸ”Ŋ āĻŸā§‡āĻŦāĻŋāϞ āύāĻŋāĻ°ā§āĻŦāĻžāϚāύ āĻ•āϰ⧁āύ</option>
                <?php foreach ($tableNamesBn as $table => $label): ?>
                    <option value="<?php echo $table; ?>"><?php echo $label; ?></option>
                <?php endforeach; ?>
            </select>
        </div>

        <div class="mb-3">
            <label for="days" class="form-label">📆 āĻĻāĻŋāύ āύāĻŋāĻ°ā§āĻŦāĻžāϚāύ āĻ•āϰ⧁āύ:</label>
            <select class="form-control" id="days" name="days" required>
                <option value="" disabled selected>đŸ”Ŋ āĻĻāĻŋāύ āύāĻŋāĻ°ā§āĻŦāĻžāϚāύ āĻ•āϰ⧁āύ</option>
                <?php foreach ($validDaysBn as $day => $label): ?>
                    <option value="<?php echo $day; ?>"><?php echo $label; ?></option>
                <?php endforeach; ?>
            </select>
        </div>

        <button type="submit" class="btn btn-delete w-100">đŸ—‘ī¸ āϰ⧇āĻ•āĻ°ā§āĻĄ āĻŽā§āϛ⧁āύ</button>
    </form>
</div>

<script>
document.addEventListener("DOMContentLoaded", function() {
    document.getElementById("deleteForm").addEventListener("submit", function(event) {
        event.preventDefault(); // Prevent form submission

        // Get selected values
        let tableElement = document.getElementById("table");
        let daysElement = document.getElementById("days");

        let selectedTable = tableElement.options[tableElement.selectedIndex].text;
        let selectedDays = daysElement.options[daysElement.selectedIndex].text;

        // Show confirmation alert with selected table and days
        Swal.fire({
            title: "āφāĻĒāύāĻŋ āĻ•āĻŋ āύāĻŋāĻļā§āϚāĻŋāϤ?",
            html: `<b>📂 āĻŸā§‡āĻŦāĻŋāϞ:</b> ${selectedTable} <br> <b>📆 āĻĻāĻŋāύ:</b> ${selectedDays}`,
            icon: "warning",
            showCancelButton: true,
            confirmButtonColor: "#d33",
            cancelButtonColor: "#3085d6",
            confirmButtonText: "✅ āĻšā§āϝāĻžāρ, āĻŽā§āϛ⧇ āĻĢ⧇āϞ⧁āύ!",
            cancelButtonText: "❌ āĻŦāĻžāϤāĻŋāϞ āĻ•āϰ⧁āύ"
        }).then((result) => {
            if (result.isConfirmed) {
                this.submit(); // Submit form if confirmed
            }
        });
    });
});

</script>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>