<?php
// ================= DATABASE CONNECTION =================
$host = "localhost";
$user = "root";
$pass = "";
$db   = "dfc_db";

$conn = new mysqli($host, $user, $pass);

// Create DB if not exists
$conn->query("CREATE DATABASE IF NOT EXISTS $db");
$conn->select_db($db);

// Create table if not exists
$conn->query("CREATE TABLE IF NOT EXISTS contact_requests (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(100),
    email VARCHAR(100),
    phone VARCHAR(20),
    subject VARCHAR(150),
    message TEXT,
    type ENUM('contact','ad_request'),
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)");

$message_status = "";

// ================= FORM SUBMIT =================
if ($_SERVER["REQUEST_METHOD"] == "POST") {

    $name    = $_POST['name'];
    $email   = $_POST['email'];
    $phone   = $_POST['phone'];
    $subject = $_POST['subject'];
    $message = $_POST['message'];
    $type    = $_POST['type'];

    $stmt = $conn->prepare("INSERT INTO contact_requests (name, email, phone, subject, message, type) VALUES (?, ?, ?, ?, ?, ?)");
    $stmt->bind_param("ssssss", $name, $email, $phone, $subject, $message, $type);

    if ($stmt->execute()) {
        $message_status = "✅ Submitted successfully!";
    } else {
        $message_status = "❌ Error: " . $stmt->error;
    }

    $stmt->close();
}
?>

<!DOCTYPE html>
<html>
<head>
    <title>DFC Contact & Cause Submission</title>
    <style>
        body {
            font-family: Arial;
            background: linear-gradient(120deg,#28a745,#20c997);
            margin:0;
        }
        .container {
            width: 40%;
            margin: 60px auto;
            background:#fff;
            padding:25px;
            border-radius:12px;
            box-shadow:0 0 15px rgba(0,0,0,0.2);
        }
        h2 {
            text-align:center;
        }
        input, textarea, select {
            width:100%;
            padding:12px;
            margin:8px 0;
            border-radius:6px;
            border:1px solid #ccc;
        }
        button {
            width:100%;
            padding:12px;
            background:#28a745;
            color:white;
            border:none;
            border-radius:6px;
            font-size:16px;
            cursor:pointer;
        }
        button:hover {
            background:#218838;
        }
        .msg {
            text-align:center;
            font-weight:bold;
            margin-bottom:10px;
        }
    </style>
</head>
<body>

<div class="container">
    <h2>Contact / Submit Your Cause</h2>

    <?php if($message_status != "") { ?>
        <div class="msg"><?php echo $message_status; ?></div>
    <?php } ?>

    <form method="POST">
        <label>Type:</label>
        <select name="type" required>
            <option value="contact">General Contact</option>
            <option value="ad_request">Submit Donation Cause</option>
        </select>

        <input type="text" name="name" placeholder="Your Name" required>
        <input type="email" name="email" placeholder="Your Email" required>
        <input type="text" name="phone" placeholder="Phone Number">

        <input type="text" name="subject" placeholder="Subject / Cause Title" required>

        <textarea name="message" rows="5" placeholder="Describe your message or cause..." required></textarea>

        <button type="submit">Submit</button>
    </form>
</div>

</body>
</html>