<?php
// config.php को शामिल करें ताकि हम डेटाबेस से कनेक्ट कर सकें।
require_once 'config.php';

$message = '';
$message_type = '';

// --- FORM SUBMISSION LOGIC ---
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $name = $_POST['name'];
    $contact_person = $_POST['contact_person'];
    $contact_phone = $_POST['contact_phone'];
    $activation_date = $_POST['activation_date'];
    $notes = $_POST['notes'];
    $contact_email = $_POST['contact_email'];

    if (empty($name) || empty($contact_person) || empty($contact_phone) || empty($activation_date) || empty($notes)) {
        $message = "Please fill all mandatory fields marked with *.";
        $message_type = 'error';
    } else {
        $stmt = $conn->prepare("INSERT INTO clients (name, contact_person, contact_phone, activation_date, notes, contact_email, is_active) VALUES (?, ?, ?, ?, ?, ?, TRUE)");
        $stmt->bind_param("ssssss", $name, $contact_person, $contact_phone, $activation_date, $notes, $contact_email);

        if ($stmt->execute()) {
            $message = "Onboarding successful! The client has been added to the system.";
            $message_type = 'success';
        } else {
            $message = "Error: Could not save the client details. " . $stmt->error;
            $message_type = 'error';
        }
        $stmt->close();
    }
    $conn->close();
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Client Onboarding - Digify</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <style>
        body {
            background-image: url('https://images.unsplash.com/photo-1522071820081-009f0129c71c?q=80&w=2070&auto=format&fit=crop');
            background-size: cover;
            background-position: center;
            background-attachment: fixed;
        }
    </style>
</head>
<body class="bg-gray-200 flex items-center justify-center min-h-screen p-4">
    <div class="w-full max-w-4xl mx-auto">
        <div class="text-center mb-10 text-white">
            <h1 class="text-5xl font-extrabold drop-shadow-xl">Digify Onboarding Form</h1>
            <p class="text-xl mt-3 drop-shadow-lg">Welcome! Let's get started by filling out the details below.</p>
        </div>
        
        <div class="bg-white/95 backdrop-blur-sm p-8 md:p-10 rounded-2xl shadow-2xl border border-gray-200">
            <?php if (!empty($message)): ?>
                <div class="mb-6 p-4 rounded-lg <?php echo ($message_type == 'success') ? 'bg-green-100 text-green-800' : 'bg-red-100 text-red-800'; ?>">
                    <?php echo htmlspecialchars($message); ?>
                </div>
            <?php endif; ?>
            
            <?php if ($message_type !== 'success'): ?>
            <form method="POST" action="onboarding.php" class="space-y-6">
                
                <div class="border-b border-gray-900/10 pb-6">
                    <h2 class="text-xl font-semibold leading-7 text-gray-900">Business Information</h2>
                    <p class="mt-1 text-sm leading-6 text-gray-600">Please provide the primary details of the business.</p>
                    <div class="mt-6 grid grid-cols-1 gap-x-6 gap-y-6 sm:grid-cols-2">
                        <div>
                            <label for="name" class="block text-sm font-medium text-gray-700">Client Business Name <span class="text-red-500">*</span></label>
                            <input type="text" name="name" id="name" required class="mt-1 block w-full border-gray-300 rounded-md shadow-sm">
                        </div>
                        <div>
                            <label for="activation_date" class="block text-sm font-medium text-gray-700">Activation Date <span class="text-red-500">*</span></label>
                            <input type="date" name="activation_date" id="activation_date" required class="mt-1 block w-full border-gray-300 rounded-md shadow-sm">
                        </div>
                    </div>
                </div>

                <div class="border-b border-gray-900/10 pb-6">
                     <h2 class="text-xl font-semibold leading-7 text-gray-900">Contact Information</h2>
                     <p class="mt-1 text-sm leading-6 text-gray-600">Who is the main point of contact for this account?</p>
                     <div class="mt-6 grid grid-cols-1 gap-x-6 gap-y-6 sm:grid-cols-2">
                        <div>
                            <label for="contact_person" class="block text-sm font-medium text-gray-700">Contact Person Name <span class="text-red-500">*</span></label>
                            <input type="text" name="contact_person" id="contact_person" required class="mt-1 block w-full border-gray-300 rounded-md shadow-sm">
                        </div>
                        <div>
                            <label for="contact_phone" class="block text-sm font-medium text-gray-700">Phone Number <span class="text-red-500">*</span></label>
                            <input type="text" name="contact_phone" id="contact_phone" required class="mt-1 block w-full border-gray-300 rounded-md shadow-sm">
                        </div>
                        <div class="sm:col-span-2">
                            <label for="contact_email" class="block text-sm font-medium text-gray-700">Contact Email</label>
                            <input type="email" name="contact_email" id="contact_email" class="mt-1 block w-full border-gray-300 rounded-md shadow-sm">
                        </div>
                     </div>
                </div>

                <div>
                    <label for="notes" class="block text-sm font-medium text-gray-700">Additional Details <span class="text-red-500">*</span></label>
                    <textarea name="notes" id="notes" rows="4" required class="mt-1 block w-full border-gray-300 rounded-md shadow-sm" placeholder="Please provide any additional details that will help us serve you better.&#10;Examples: Full business address, WhatsApp number (if different), links to existing social media profiles (Facebook, Instagram, etc.)."></textarea>
                </div>

                <div class="flex justify-end pt-6 border-t">
                    <button type="submit" class="w-full md:w-auto inline-flex items-center justify-center px-8 py-3 bg-blue-600 border border-transparent rounded-md font-semibold text-white hover:bg-blue-700">
                        Submit Onboarding Form
                    </button>
                </div>
            </form>
            <?php endif; ?>
        </div>
    </div>
</body>
</html>