<?php
session_start();
if(!isset($_SESSION['user'])){
    header("location:login.php");
    exit;
}
$db_server = "localhost";
$db_user = "root";
$db_pass = "";
$db_name = "foodshare";


$conn = mysqli_connect($db_server, $db_user, $db_pass, $db_name);


if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {

    $donor_name = $_POST['donorName'];
    $food_type = $_POST['foodType'];
    $quantity = $_POST['quantity'];
    $location = $_POST['location'];
    $expiration_date = $_POST['expirationDate'];


    $query = "INSERT INTO donations (donor_name, food_type, quantity, location, expiration_date) VALUES (?, ?, ?, ?, ?)";
    $stmt = mysqli_prepare($conn, $query);

    if ($stmt) {
        mysqli_stmt_bind_param($stmt, "sssss", $donor_name, $food_type, $quantity, $location, $expiration_date);
        if (mysqli_stmt_execute($stmt)) {
            echo "Donation submitted successfully!";
        } else {
            echo "Error executing query: " . mysqli_error($conn);
        }
        mysqli_stmt_close($stmt);
    } else {
        echo "Error preparing statement: " . mysqli_error($conn);
    }

    mysqli_close($conn);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Donate Food</title>
    <link rel="stylesheet" href="donor.css">
</head>
<body>
    <div class="hero">
        <nav>
            <img src="logo.png" alt="" class="logo">
            <h2 class="ffg">FoodShare</h2>
            <!-- <ul>
                <li><a href="#">Contribution</a></li>
                <li><a href="#">Donate</a></li>
            </ul> -->
            <img src="anshuman.jpg" alt="" class="user-pic" onclick="toggleMenu()">
            
            <div class="sub-menu-wrap hidden" id="subMenu">
                <div class="sub-menu">
                    <div class="user-info">
                        <img src="anshuman.jpg" alt="user" class="bigboy">
                        <!-- extract name for donorhome -->
                        <h3>Anshuman Bist</h3>
                    </div>
                    <hr>
                    <a href="donorhome.php" class="sub-menu-link">
                        <img src="chat-history-line.png" alt="">
                        <p>Contribution History</p>
                        <span>></span>
                    </a>
                    <a href="donor.php" class="sub-menu-link">
                        <img src="hand-heart-line.png" alt="">
                        <p>Donate</p>
                        <span>></span>
                    </a>
                    <a href="home.html" class="sub-menu-link">
                        <img src="logout-box-r-line.png" alt="">
                        <p>Log Out</p>
                        <span>></span>
                    </a>
                </div>
            </div>

        </nav>
    </div>
    

    <div class="container">
    <div class="header">
        <h1><img src="logo.png" alt="logo" width="100" height="100" style="margin-right: 10px; border-radius: 100%;"></h1>
    </div>
    <h2>Food Donation Form</h2>
        <p>Please provide correct information.</p>
        <form action="donor.php" method="post">
            <label for="donorName">Donor Name</label>
            <input type="text" id="donorName" name="donorName" placeholder="Enter your name" required>

            <label for="foodType">Food Type</label>
            <input type="text" id="foodType" name="foodType" placeholder="e.g., Bread, Vegetables" required>

            <label for="quantity">Quantity</label>
            <input type="text" id="quantity" name="quantity" placeholder="e.g., 10 kg" required>

            <label for="location">Location</label>
            <input type="text" id="location" name="location" placeholder="e.g., Downtown" required>

            <label for="expirationDate">Expiration Date</label>
            <input type="date" id="expirationDate" name="expirationDate" required>


            <button type="submit">Submit Donation</button>
        </form>
    </div>

    <script>
        let subMenu = document.getElementById("subMenu");

        const toggleMenu = () =>{
            subMenu.classList.toggle("hidden")
        }
    </script>



</body>
</html>