<?php
// Start the session
session_start();
ob_start();

// If the user is not logged in
if (!isset($_SESSION['user'])) {
    header('Location: index.php');
    exit;
}

// Include the database connection
include_once('../inc/db.php');
$db = db_connect();
if (@$_GET["delete"]) {
    $id = $_GET["delete"];
    $sql = "DELETE FROM products WHERE id = :id";
    $stmt = $db->prepare($sql);
    $stmt->bindParam(':id', $id);
    $stmt->execute();
    header("Location: urun.php");
}
if (@$_GET["updateprice"]) {
    $id = $_GET["updateprice"];
    $sql = "UPDATE products SET ProductPrice = :price WHERE id = :id";
    $stmt = $db->prepare($sql);
    $stmt->bindParam(':price', $_POST["price"]);
    $stmt->bindParam(':id', $id);
    $stmt->execute();
    header("Location: urun.php");
}
if (@$_GET["updatecategory"]) {
    $id = $_GET["updatecategory"];
    $sql = "UPDATE products SET ProductCategory = :category WHERE id = :id";
    $stmt = $db->prepare($sql);
    $stmt->bindParam(':category', $_POST["product-category"]);
    $stmt->bindParam(':id', $id);
    $stmt->execute();
    header("Location: urun.php");
}
?>
<?php

$sql = "SELECT * FROM products";
$stmt = $db->prepare($sql);
$stmt->execute();

$products = $stmt->fetchAll(PDO::FETCH_ASSOC);

// Sonuçları ekrana yazdırma

?>
<!DOCTYPE html>
<html lang="tr">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>yeni ürün ekle • 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"/>
    <!-- CKEditor -->
    <script src="https://cdn.ckeditor.com/4.20.2/standard/ckeditor.js"></script>
    <!-- Favicon -->
    <link rel="icon" href="src/icon/favicon.ico" type="image/x-icon"/>
</head>
<body>
<header class="bg-white shadow">
    <div class="flex justify-between items-center h-24 px-8">
        <div class="flex items-center">
            <img src="src/img/logo.png" class="w-full h-12" alt="Logo">
        </div>
        <div class="flex items-center">
            <span class="text-gray-500">
                <?= $_SESSION['user']->username ?>
                <small class="text-gray-400">olarak giriş yaptın.</small>
            </span>
        </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">
            <a href="dashboard.php" class="block px-4 py-2 text-gray-400 hover:bg-gray-700 hover:text-white">
                Ana Sayfa
            </a>
            <a href="logs.php" class="block px-4 py-2 text-gray-400 hover:bg-gray-700 hover:text-white">
                Loglar
            </a>
            <a href="yeniurun.php" class="block px-4 py-2 text-gray-400 hover:bg-gray-700 hover:text-white">
                Yeni Ürün Ekle
            </a>
            <a href="urun.php" class="block px-4 py-2 text-gray-400 hover:bg-gray-700 hover:text-white">
                Ürün Düzenle
            </a>
            <a href="logout.php" class="block px-4 py-2 text-gray-400 hover:bg-gray-700 hover:text-white">
                Çıkış Yap
            </a>
        </nav>
    </div>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
    <div class="w-full md:flex-1">
        <main class="p-4">
            <h1 class="text-2xl font-bold mb-4">Yeni Ürün Ekle</h1>
           <table class="table">
    <thead>
        <tr>
            <th scope="col">ID</th>
            <th scope="col">RESİM</th>
            <th scope="col">KATEGORİ</th>
            <th scope="col">İSİM</th>
            <th scope="col">TUTAR</th>
            <th scope="col">Düzenle</th>
            <th scope="col">Sil</th>
        </tr>
    </thead>
    <tbody>
        <?php foreach ($products as $product): ?>
            <tr>
                <td><?php echo $product['id']; ?></td>
                <td>
                    <img style="width: 50px;" src="/sadece-online-ozel/assets/img/products/<?php echo $product['ImageURL']; ?>" alt="">
                </td>
                <td>
                    <form action="urun.php?updatecategory=<?php echo $product['id']; ?>" method="post">
                        <?php $selectedCategory = $product['ProductCategory'] ?? null; ?>
                        <select class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline" id="product-category" name="product-category" required>
            <option value="" <?= $selectedCategory === null ? 'selected' : ''; ?>>Seçiniz</option>
            <option value="elektronik" <?= $selectedCategory === 'elektronik' ? 'selected' : ''; ?>>Elektronik</option>
            <option value="ev" <?= $selectedCategory === 'ev' ? 'selected' : ''; ?>>EV YAŞAM</option>
            <option value="giyim" <?= $selectedCategory === 'giyim' ? 'selected' : ''; ?>>GİYİM AKSESUAR</option>
            <option value="anne" <?= $selectedCategory === 'anne' ? 'selected' : ''; ?>>ANNE BEBEK OYUNCAK</option>
            <option value="oto" <?= $selectedCategory === 'oto' ? 'selected' : ''; ?>>OTO BAHÇE YAPI</option>
            <option value="kozmetik" <?= $selectedCategory === 'kozmetik' ? 'selected' : ''; ?>>KOZMETİK KİŞİSEL BAKIM</option>
            <option value="kitap" <?= $selectedCategory === 'kitap' ? 'selected' : ''; ?>>KİTAP KIRTASİYE</option>
        </select>
						<button class="btn btn-primary">Düzenle</button>
                    </form>
                </td>
                <td><?php echo $product['ProductName']; ?></td>
                <td>
                    <form action="urun.php?updateprice=<?php echo $product['id']; ?>" method="post">
                        <input type="text" name="price" value="<?php echo $product['ProductPrice']; ?>" class="form-control">
                        <button class="btn btn-primary">Düzenle</button>
                    </form>
                </td>
                <td>
                    <form action="urun.php?delete=<?php echo $product['id']; ?>" method="post">
                        <button class="btn btn-danger">Sil</button>
                    </form>
                </td>
            </tr>
        <?php endforeach; ?>
    </tbody>
</table>

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

</body>
</html>