<?php
$isEdit = isset($client) && !empty($client);
$pageTitle = $isEdit ? 'Edit Client' : 'Add Client';
$breadcrumb = [
    ['label' => 'Clients', 'url' => APP_URL . '/clients'],
    ['label' => $pageTitle, 'url' => null]
];
ob_start();
?>
<div class="max-w-3xl mx-auto">
    <div class="flex justify-between items-center mb-6">
        <h1 class="text-2xl font-bold text-gray-800"><?= $pageTitle ?></h1>
        <a href="<?= APP_URL ?>/clients" class="text-gray-600 hover:text-gray-900">Back to List</a>
    </div>

    <form action="<?= APP_URL ?>/clients<?= $isEdit ? '/' . $client['id'] . '/edit' : '/create' ?>" method="POST" class="space-y-6">
        <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($_SESSION[CSRF_TOKEN_NAME] ?? '') ?>">

        <div class="bg-white rounded-lg shadow overflow-hidden">
            <div class="bg-gray-50 px-6 py-4 border-b border-gray-200">
                <h3 class="text-lg leading-6 font-medium text-gray-900">Company Information</h3>
            </div>
            <div class="p-6 grid grid-cols-1 md:grid-cols-2 gap-6">
                <div class="md:col-span-2">
                    <label class="block text-sm font-medium text-gray-700">Company Name *</label>
                    <input type="text" name="company_name" required value="<?= htmlspecialchars($client['company_name'] ?? '') ?>" class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-[#0A3A6B] focus:ring-[#0A3A6B] sm:text-sm border p-2">
                </div>
                <div>
                    <label class="block text-sm font-medium text-gray-700">Industry</label>
                    <select name="industry" class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-[#0A3A6B] focus:ring-[#0A3A6B] sm:text-sm border p-2">
                        <option value="">Select Industry</option>
                        <?php foreach (getIndustryGroups() as $sector => $subIndustries): ?>
                            <?php if (empty($subIndustries)): ?>
                            <option value="<?= htmlspecialchars($sector) ?>" <?= ($client['industry'] ?? '') === $sector ? 'selected' : '' ?>><?= htmlspecialchars($sector) ?></option>
                            <?php else: ?>
                            <optgroup label="<?= htmlspecialchars($sector) ?>">
                                <?php foreach ($subIndustries as $sub): ?>
                                <option value="<?= htmlspecialchars($sub) ?>" <?= ($client['industry'] ?? '') === $sub ? 'selected' : '' ?>><?= htmlspecialchars($sub) ?></option>
                                <?php endforeach; ?>
                            </optgroup>
                            <?php endif; ?>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div>
                    <label class="block text-sm font-medium text-gray-700">TIN</label>
                    <input type="text" name="tin" value="<?= htmlspecialchars($client['tin'] ?? '') ?>" class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-[#0A3A6B] focus:ring-[#0A3A6B] sm:text-sm border p-2">
                </div>
                <div class="md:col-span-2">
                    <label class="block text-sm font-medium text-gray-700">Address</label>
                    <input type="text" name="address" value="<?= htmlspecialchars($client['address'] ?? '') ?>" class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-[#0A3A6B] focus:ring-[#0A3A6B] sm:text-sm border p-2">
                </div>
                <div>
                    <label class="block text-sm font-medium text-gray-700">Phone</label>
                    <input type="text" name="phone" value="<?= htmlspecialchars($client['phone'] ?? '') ?>" class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-[#0A3A6B] focus:ring-[#0A3A6B] sm:text-sm border p-2">
                </div>
                <div>
                    <label class="block text-sm font-medium text-gray-700">Email</label>
                    <input type="email" name="email" value="<?= htmlspecialchars($client['email'] ?? '') ?>" class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-[#0A3A6B] focus:ring-[#0A3A6B] sm:text-sm border p-2">
                </div>
                <div>
                    <label class="block text-sm font-medium text-gray-700">Website</label>
                    <input type="text" name="website" placeholder="www.example.com" value="<?= htmlspecialchars($client['website'] ?? '') ?>" class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-[#0A3A6B] focus:ring-[#0A3A6B] sm:text-sm border p-2">
                </div>
            </div>
        </div>

        <div class="bg-white rounded-lg shadow overflow-hidden">
            <div class="bg-gray-50 px-6 py-4 border-b border-gray-200">
                <h3 class="text-lg leading-6 font-medium text-gray-900">Contact Person</h3>
            </div>
            <div class="p-6 grid grid-cols-1 md:grid-cols-2 gap-6">
                <div class="md:col-span-2">
                    <label class="block text-sm font-medium text-gray-700">Name</label>
                    <input type="text" name="contact_name" value="<?= htmlspecialchars($client['contact_name'] ?? '') ?>" class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-[#0A3A6B] focus:ring-[#0A3A6B] sm:text-sm border p-2">
                </div>
                <div>
                    <label class="block text-sm font-medium text-gray-700">Phone</label>
                    <input type="text" name="contact_phone" value="<?= htmlspecialchars($client['contact_phone'] ?? '') ?>" class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-[#0A3A6B] focus:ring-[#0A3A6B] sm:text-sm border p-2">
                </div>
                <div>
                    <label class="block text-sm font-medium text-gray-700">Email</label>
                    <input type="email" name="contact_email" value="<?= htmlspecialchars($client['contact_email'] ?? '') ?>" class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-[#0A3A6B] focus:ring-[#0A3A6B] sm:text-sm border p-2">
                </div>
            </div>
        </div>

        <div class="flex justify-end pt-4">
            <button type="submit" class="bg-[#0A3A6B] hover:bg-blue-700 text-white font-bold py-3 px-8 rounded shadow">
                <?= $isEdit ? 'Update Client' : 'Add Client' ?>
            </button>
        </div>
    </form>
</div>
<?php
$content = ob_get_clean();
require ROOT_PATH . '/modules/shared/views/layouts/main.php';
?>
