<?php
session_start();
include 'php/db_connection.php';

// শুধুমাত্র লগইন করা ইউজার পেজ দেখতে পারবে
if (!isset($_SESSION['user_id'])) {
    header("Location: login.php");
    exit();
}

$userName = $_SESSION['user_name'];
$role = $_SESSION['role']; // ✅ এখানে role সেভ করা হচ্ছে


$isAdmin = ($_SESSION['role'] == 'admin');

?>

<?php include 'heade.php';?>
<body>
<?php include 'sidebar.php';?>
<?php
// Category variables
                  $Id = $_GET ['category'];
                  $read = "SELECT * FROM  product_cat  Where Product_id = $Id ";
                  $query = mysqli_query ($con,$read);
                  $row = mysqli_fetch_array($query);
?>
    
<h1> <?php echo $row["Product_name"] ; ?> == Adds</h1>
<button><a href="add_product_name.php">Back</a></button>
    
<form id="productForm" method="POST">
                        <?php
                            if(isset($_POST['send'])){
                                $productName = $_POST['productName'];
                                $productId = $_POST['productId'];
                                $quantity = $_POST['quantity'];
                                $Expiration_date = $_POST['Expiration_date'];
                                $price = $_POST['price'];
                                $totalPrice = $_POST['totalPrice'];
                  
                                $insert = "INSERT INTO product ( name, product_id, quantity, Expiration_date, price, totalPrice ) VALUES ( '$productName', $productId, '$quantity','$Expiration_date', '$price', '$totalPrice')";
                                $query = mysqli_query($con , $insert );
                                if ($query){

                                    echo "<script>alert('Add successfully')</script>";
                                    
                                }
                                else "<script>alert('Add Not successfully') </script>";
                            }
                            ?>


        <div class="form-group">
            <label for="productId">Product ID:</label>
            <input type="text" value="<?php echo $row["Product_id"] ; ?>" id="productId" name="productId"  placeholder="Enter ID" class="readonly" readonly>
        </div>

        <div class="form-group">
            <label for="productName">Product Name:</label>
           
            
            <input type="tex" value="<?php echo $row["Product_name"] ; ?>" id="productName" name="productName" placeholder="Enter Name" class="readonly" readonly>                                
          
        </div>

        <div class="form-group">
            <label for="quantity">Quantity:</label>
            <input type="number" id="quantity" name="quantity" min="1" value="0" oninput="calculateTotal()" required>
        </div>
        <div class="form-group">
            <label for=" Expiration_date">Expiration_date:</label>
            <input type="date" id="Expiration_date" name="Expiration_date" min="1" value="0" oninput="calculateTotal()" required>
        </div>

        <div class="form-group">
            <label for="price">Price per Unit:</label>
            <input type="number" value="<?php echo $row["price"] ; ?>" id="price" name="price"  step="0.01" min="0" placeholder="Enter Price" oninput="calculateTotal()"  class="readonly" readonly>
        </div>

        <div class="form-group">
            <label for="totalPrice">Total Price:</label>
            <input type="number" id="totalPrice" name="totalPrice" class="readonly" readonly>
        </div>

        <button name="send" >Add Product</button>
    </form>

<table>

<!--------------------------------------------selected category ‍Show------------------------------->
          
    <?php
    if (isset($_GET['category']) && !empty($_GET['category'])) {
        $selectedCategory = $_GET['category'];

        // Fetch product history for the selected category
        $historyQuery = "
            SELECT p.id, p.name, p.product_id, p.quantity, p.price, p.totalPrice, p.date, p.Expiration_date
            FROM product p
            JOIN product_cat pc ON p.product_id = pc.Product_id
            WHERE pc.Product_id = '$selectedCategory'
        ";
        $historyResult = mysqli_query($con, $historyQuery);

        // Check if there are results
        if (mysqli_num_rows($historyResult) > 0) {
            ?>
            <table>
                <thead>
                    <tr>
                        <th>Sl No</th>
                        <th>Product Name</th>
                        <th>Product ID</th>
                        <th>Quantity</th>
                        <th>Price</th>
                        <th>Total Price</th>
                        <th>Date</th>
                        <th>Expiration</th>
                        <th>Action</th>
                    </tr>
                </thead>
                <tbody>
                    <?php
                    $sl_no = 1;
                    while ($row = mysqli_fetch_assoc($historyResult)) {
                        echo "<tr>";
                        echo "<td>{$sl_no}</td>";
                        echo "<td>{$row['name']}</td>";
                        echo "<td>{$row['product_id']}</td>";
                        echo "<td>{$row['quantity']}</td>";
                        echo "<td>{$row['price']}</td>";
                        echo "<td>{$row['totalPrice']}</td>";
                        echo "<td>{$row['date']}</td>";
                        echo "<td>{$row['Expiration_date']}</td>";
                        echo "<td>
                            <a href='edit_product.php?id={$row['id']}'>Edit</a>
                          <!----- <a href='delete_product.php?id={$row['id']}'>Delete</a>----->
                        </td>";
                        echo "</tr>";
                        $sl_no++;
                    }
                    ?>
                </tbody>
            </table>
            <?php
        } else {
            // Display message if no products found
            echo "<p>No products found for the selected category.</p>";
        }
    } else {
        echo "<p>Please select a category to view the product history.</p>";
    }
    ?>
</body>
</html>
<?php
                 
// Pagination variables
$limit = 5;
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$offset = ($page - 1) * $limit;
?>
<?php
// Set the number of records per page
$records_per_page = 5;

// Find out the total number of records
$total_sql = "SELECT COUNT(*) FROM product";
$total_result = $con->query($total_sql);
$total_row = $total_result->fetch_row();
$total_records = $total_row[0];

// Calculate the total number of pages
$total_pages = ceil($total_records / $records_per_page);

// Get the current page number from URL, default to 1 if not set
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;

// Calculate the starting record for the current page
$start_from = ($page - 1) * $records_per_page;

// Fetch the sales records for the current page
$sql = "SELECT * 
        FROM product
        ORDER BY product_id DESC 
        LIMIT $start_from, $records_per_page";
$result = $con->query($sql);
?>

    <?php
    $con->close();
    ?>
