<?php
$success = false;
$error = false;

// Handle form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $name    = trim($_POST['name']);
    $email   = trim($_POST['email']);
    $subject = trim($_POST['subject']);
    $message = trim($_POST['message']);

    if ($name && $email && $subject && $message) {
        // You can also send this as email using mail() function
        $data = [
            'name' => htmlspecialchars($name),
            'email' => htmlspecialchars($email),
            'subject' => htmlspecialchars($subject),
            'message' => htmlspecialchars($message),
            'date' => date("Y-m-d H:i:s")
        ];

        // Save messages to a JSON file
        $file = './data/messages.json';
        $messages = file_exists($file) ? json_decode(file_get_contents($file), true) : [];
        $messages[] = $data;
        file_put_contents($file, json_encode($messages, JSON_PRETTY_PRINT));

        $success = true;
    } else {
        $error = true;
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Contact Us | CryptoBitSense</title>
    <link rel="stylesheet" href="style.css">
    <style>
        :root {
        --Primary-color: #59981a;
        --Secondary-color: #81b622;
        --Tetiary-color: #ecf87f;
        --Primary-font: Open Sans;
        --Secondary-font: Montserrat;
        --Tetiary-font: Mitr;
        --Extra-font: Courgette;
        --Extrastyle-font: ;
        }
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        .contact-form {
            max-width: 600px;
            margin: 50px auto;
            background: #fff;
            padding: 30px;
            border-radius: 8px;
            box-shadow: 0 0 20px rgba(0,0,0,0.1);
            font-family: 'Segoe UI', sans-serif;
        }

        .contact-form h2 {
            margin-bottom: 20px;
            color: var(--Primary-color);
            text-align: center;
        }

        .contact-form label {
            display: block;
            margin-bottom: 8px;
            font-weight: 600;
            color: #444;
        }

        .contact-form input,
        .contact-form textarea {
            width: 100%;
            padding: 10px 15px;
            margin-bottom: 20px;
            border: 1px solid #ddd;
            border-radius: 6px;
            font-size: 15px;
        }

        .contact-form button {
            background-color: var(--Primary-color);
            color: var(--Tetiary-color);
            border: none;
            padding: 17px;
            font-size: 16px;
            border-radius: 6px;
            cursor: pointer;
            transition: 0.3s;
        }

        .contact-form button:hover {
            background-color: #00c9aa;
        }

        .message {
            padding: 10px 15px;
            border-radius: 6px;
            margin-bottom: 20px;
            text-align: center;
        }

        .success {
            background-color: #d4edda;
            color: #155724;
        }

        .error {
            background-color: #f8d7da;
            color: #721c24;
        }
    </style>
</head>
<body>

<?php include("header.php"); ?>

<div class="contact-form">
    <h2>Contact Us</h2>

    <?php if ($success): ?>
        <div class="message success">Thank you! Your message has been sent.</div>
    <?php elseif ($error): ?>
        <div class="message error">All fields are required. Please fill out the form completely.</div>
    <?php endif; ?>

    <form method="post" action="contact.php">
        <label for="name">Your Name</label>
        <input type="text" name="name" id="name" required>

        <label for="email">Your Email</label>
        <input type="email" name="email" id="email" required>

        <label for="subject">Subject</label>
        <input type="text" name="subject" id="subject" required>

        <label for="message">Your Message</label>
        <textarea name="message" id="message" rows="6" required></textarea>

        <button type="submit">Send Message</button>
    </form>
</div>

<?
