<?php
// Database connection (update your credentials)
$host = 'localhost';
$db = 'bookstore_dashboard';
$user = 'bookstore_dashboard';
$pass = '9(AhSb@uHXMUk0wK';

try {
    $pdo = new PDO("mysql:host=$host;dbname=$db", $user, $pass);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
    die("Connection failed: " . $e->getMessage());
}

// Handle form submission (insert new product record)
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['add_product'])) {
    $product_name = $_POST['product_name'];
    $category_id = $_POST['category_id'];
    $author = $_POST['author'];
    $year_of_publication = $_POST['year_of_publication'];
    $mrp = $_POST['mrp'];

    $sql = "INSERT INTO product_master (product_name, category_id, author, year_of_publication, mrp) 
            VALUES (:product_name, :category_id, :author, :year_of_publication, :mrp)";
    $stmt = $pdo->prepare($sql);

    $stmt->execute([
        'product_name' => $product_name,
        'category_id' => $category_id,
        'author' => $author,
        'year_of_publication' => $year_of_publication,
        'mrp' => $mrp,
    ]);

    echo "<p>New product added successfully!</p>";
}

// Fetch all product records
$sql = "SELECT * FROM product_master ORDER BY product_id ASC";
$stmt = $pdo->query($sql);
$products = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Product Master</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 20px;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 20px;
        }
        table, th, td {
            border: 1px solid #ddd;
        }
        th, td {
            padding: 10px;
            text-align: left;
        }
        th {
            background-color: #f4f4f4;
        }
        .form-container {
            display: none;
            margin-bottom: 20px;
            padding: 10px;
            border: 1px solid #ccc;
            border-radius: 5px;
            background-color: #f9f9f9;
        }
        .form-container.active {
            display: block;
        }
        .form-control {
            margin-bottom: 10px;
        }
        .form-control label {
            display: block;
            margin-bottom: 5px;
        }
        .form-control input, .form-control select {
            width: 100%;
            padding: 8px;
            box-sizing: border-box;
        }
        .form-control button {
            padding: 10px 15px;
            background-color: #28a745;
            color: #fff;
            border: none;
            cursor: pointer;
        }
        .form-control button:hover {
            background-color: #218838;
        }
        .toggle-button {
            margin-bottom: 20px;
            padding: 10px 15px;
            background-color: #007bff;
            color: #fff;
            border: none;
            cursor: pointer;
            border-radius: 5px;
        }
        .toggle-button:hover {
            background-color: #0056b3;
        }
    </style>
    <script>
        function toggleForm() {
            const formContainer = document.querySelector('.form-container');
            formContainer.classList.toggle('active');
        }
    </script>
</head>
<body>
    <h1>Product Master</h1>

    <!-- Button to Toggle the Add Product Form -->
    <button class="toggle-button" onclick="toggleForm()">Add New Product</button>

    <!-- Product Entry Form -->
    <div class="form-container">
        <form method="POST">
            <div class="form-control">
                <label for="product_name">Product Name</label>
                <input type="text" id="product_name" name="product_name" required>
            </div>
            <div class="form-control">
                <label for="category_id">Category ID</label>
                <input type="number" id="category_id" name="category_id" required>
            </div>
            <div class="form-control">
                <label for="author">Author</label>
                <input type="text" id="author" name="author" required>
            </div>
            <div class="form-control">
                <label for="year_of_publication">Year of Publication</label>
                <input type="number" id="year_of_publication" name="year_of_publication" required>
            </div>
            <div class="form-control">
                <label for="mrp">MRP</label>
                <input type="number" step="0.01" id="mrp" name="mrp" required>
            </div>
            <button type="submit" name="add_product">Add Product</button>
        </form>
    </div>

    <!-- Product Table -->
    <?php if ($products && count($products) > 0): ?>
        <table>
            <thead>
                <tr>
                    <th>Product ID</th>
                    <th>Product Name</th>
                    <th>Category ID</th>
                    <th>Author</th>
                    <th>Year of Publication</th>
                    <th>MRP</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($products as $product): ?>
                    <tr>
                        <td><?= $product['product_id'] ?></td>
                        <td><?= $product['product_name'] ?></td>
                        <td><?= $product['category_id'] ?></td>
                        <td><?= $product['author'] ?></td>
                        <td><?= $product['year_of_publication'] ?></td>
                        <td><?= $product['mrp'] ?></td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    <?php else: ?>
        <p>No products found.</p>
    <?php endif; ?>
</body>
</html>