<?php
require_once 'config.php';

function userHasRole($required_roles) {
    if (!isset($_SESSION['user_roles'])) return false;
    return count(array_intersect($required_roles, $_SESSION['user_roles'])) > 0;
}

if (!isset($_SESSION['user_id']) || !userHasRole(['Admin', 'Manager'])) {
    header("Location: dashboard.php");
    exit();
}

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

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)) {
        $message = "Please fill all mandatory fields: Business Name, Contact Person, Phone, and Activation Date.";
        $message_type = 'error';
    } else {
        $stmt = $conn->prepare("INSERT INTO clients (name, contact_person, contact_phone, activation_date, notes, contact_email) VALUES (?, ?, ?, ?, ?, ?)");
        $stmt->bind_param("ssssss", $name, $contact_person, $contact_phone, $activation_date, $notes, $contact_email);

        if ($stmt->execute()) {
            $message = "Client added successfully!";
            $message_type = 'success';
        } else {
            $message = "Error adding client: " . $stmt->error;
            $message_type = 'error';
        }
        $stmt->close();
    }
    $conn->close();
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    </head>
<body class="bg-gray-100">
    <div class="flex flex-col md:flex-row min-h-screen" x-data="{ sidebarOpen: false }">
        <div :class="{'translate-x-0': sidebarOpen, '-translate-x-full': !sidebarOpen}" class="w-64 bg-gray-800 text-white p-4 ...">
            </div>

        <div class="flex-1">
            <header class="bg-white shadow-sm p-4 ...">
                </header>
            <main class="p-6 md:p-10">
                <div class="max-w-2xl mx-auto bg-white p-8 rounded-lg shadow">
                    <?php if (!empty($message)): ?>
                        <div class="mb-4 p-4 rounded <?php echo ($message_type == 'success') ? 'bg-green-100 text-green-800' : 'bg-red-100 text-red-800'; ?>">
                            <?php echo htmlspecialchars($message); ?>
                        </div>
                    <?php endif; ?>

                    <form method="POST" action="add-client.php">
                        <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                            <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="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>
                                <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 class="mt-6">
                            <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 class="mt-6">
                            <label for="notes" class="block text-sm font-medium text-gray-700">Notes</label>
                            <textarea name="notes" id="notes" rows="4" class="mt-1 block w-full border-gray-300 rounded-md shadow-sm"></textarea>
                        </div>
                        <div class="flex justify-end mt-8">
                            <a href="clients.php" class="px-4 py-2 border rounded-md">Cancel</a>
                            <button type="submit" class="ml-3 px-4 py-2 bg-blue-600 text-white rounded-md">
                                Save Client
                            </button>
                        </div>
                    </form>
                </div>
            </main>
        </div>
    </div>
</body>
</html>