<?php
$pageTitle = 'Edit ' . $org['name'];
$breadcrumb = [
    ['label' => 'Organizations', 'url' => APP_URL . '/admin/organizations'],
    ['label' => $org['name'], 'url' => APP_URL . '/admin/organizations/' . $org['id']],
    ['label' => 'Edit', '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">Edit Organization</h1>
        <a href="<?= APP_URL ?>/admin/organizations/<?= $org['id'] ?>" class="text-gray-600 hover:text-gray-900">Back to Details</a>
    </div>

    <form action="<?= APP_URL ?>/admin/organizations/<?= $org['id'] ?>/edit" 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 font-medium text-gray-900">Organization Profile</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">Organization Name *</label>
                    <input type="text" name="name" required value="<?= htmlspecialchars($org['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) ?>" <?= ($org['industry'] ?? '') === $sector ? 'selected' : '' ?>><?= htmlspecialchars($sector) ?></option>
                            <?php else: ?>
                            <optgroup label="<?= htmlspecialchars($sector) ?>">
                                <?php foreach ($subIndustries as $sub): ?>
                                <option value="<?= htmlspecialchars($sub) ?>" <?= ($org['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">Email</label>
                    <input type="email" name="email" value="<?= htmlspecialchars($org['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">Phone</label>
                    <input type="text" name="phone" value="<?= htmlspecialchars($org['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 class="md:col-span-2">
                    <label class="block text-sm font-medium text-gray-700">Address</label>
                    <textarea name="address" rows="3" 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"><?= htmlspecialchars($org['address'] ?? '') ?></textarea>
                </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 font-medium text-gray-900">Subscription</h3>
            </div>
            <div class="p-6 grid grid-cols-1 md:grid-cols-2 gap-6">
                <div>
                    <label class="block text-sm font-medium text-gray-700">Plan</label>
                    <select name="plan_id" 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="">No Plan</option>
                        <?php foreach ($plans as $plan): ?>
                        <option value="<?= $plan['id'] ?>" <?= (int)($org['plan_id'] ?? 0) === (int)$plan['id'] ? 'selected' : '' ?>><?= htmlspecialchars($plan['name']) ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div>
                    <label class="block text-sm font-medium text-gray-700">Subscription Status</label>
                    <select name="subscription_status" 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="trial" <?= ($org['subscription_status'] ?? '') === 'trial' ? 'selected' : '' ?>>Trial</option>
                        <option value="active" <?= ($org['subscription_status'] ?? '') === 'active' ? 'selected' : '' ?>>Active</option>
                        <option value="suspended" <?= ($org['subscription_status'] ?? '') === 'suspended' ? 'selected' : '' ?>>Suspended</option>
                    </select>
                </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">Save Changes</button>
        </div>
    </form>
</div>
<?php
$content = ob_get_clean();
require ROOT_PATH . '/modules/shared/views/layouts/main.php';
?>
