<?php
/**
 * Super Admin — Create Branch View
 *
 * Same fields as the public registration form but:
 *   - No captcha (admin is trusted)
 *   - Branch code is editable (auto-suggested but changeable)
 *   - Can set wallet_balance, is_trusted, status directly
 */
$bp = defined('BASE_PATH') ? BASE_PATH : '/gmind';
$plans    = $plans    ?? [];
$nextCode = $nextCode ?? 'GMBR02';

// Load Indian states + districts
// ✅ FIX: Path was dirname(__DIR__, 2) = /gmind/super-admin (WRONG)
// Correct path: dirname(__DIR__, 3) = /gmind (where config/ lives)
$indianStates = [];
$candidates = [
    dirname(__DIR__, 3) . '/config/indian_states.php',  // /gmind/config/indian_states.php
    dirname(__DIR__, 2) . '/config/indian_states.php',  // fallback (old path)
    (defined('APP_ROOT') ? APP_ROOT : dirname(__DIR__, 3)) . '/config/indian_states.php',
];
foreach ($candidates as $statesFile) {
    if (file_exists($statesFile)) {
        $indianStates = require $statesFile;
        break;
    }
}
?>
<div class="admin-section">
    <div style="margin-bottom:20px;">
        <a href="<?= $bp ?>/super-admin/branches" style="color:#6c3fe8;text-decoration:none;font-size:0.85rem;display:inline-flex;align-items:center;gap:4px;">
            <svg xmlns="http://www.w3.org/2000/svg" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><polyline points="15 18 9 12 15 6"/></svg>
            Back to Branches
        </a>
    </div>

    <form method="POST" action="<?= $bp ?>/super-admin/branches" style="max-width:900px;">
        <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($csrfToken ?? '', ENT_QUOTES, 'UTF-8') ?>">

        <!-- Center Information -->
        <div class="admin-card" style="background:#fff;border-radius:12px;padding:24px;margin-bottom:20px;box-shadow:0 2px 8px rgba(0,0,0,0.06);">
            <h2 style="margin:0 0 6px;font-size:1.1rem;color:#2c1e4a;">Center Information</h2>
            <p style="margin:0 0 20px;font-size:0.85rem;color:#666;">Enter the institute's basic information.</p>

            <div style="display:grid;grid-template-columns:repeat(auto-fit,minmax(220px,1fr));gap:16px;">
                <div>
                    <label style="display:block;font-size:0.85rem;font-weight:600;color:#2c1e4a;margin-bottom:6px;">Center / Institute Name <span style="color:#e84118;">*</span></label>
                    <input type="text" name="name" required value="<?= htmlspecialchars($_POST['name'] ?? '', ENT_QUOTES, 'UTF-8') ?>" placeholder="e.g. ABC Computer Education" style="width:100%;padding:10px 12px;border:1px solid #ddd;border-radius:6px;font-size:0.88rem;">
                </div>
                <div>
                    <label style="display:block;font-size:0.85rem;font-weight:600;color:#2c1e4a;margin-bottom:6px;">Branch Code <span style="color:#27ae60;font-weight:normal;">(editable)</span></label>
                    <input type="text" name="branch_code" value="<?= htmlspecialchars($_POST['branch_code'] ?? $nextCode, ENT_QUOTES, 'UTF-8') ?>" placeholder="GMBR02" style="width:100%;padding:10px 12px;border:1px solid #ddd;border-radius:6px;font-size:0.88rem;font-family:monospace;text-transform:uppercase;">
                    <small style="display:block;margin-top:3px;color:#999;font-size:0.74rem;">Auto-suggested: <?= htmlspecialchars($nextCode, ENT_QUOTES, 'UTF-8') ?>. You can change it.</small>
                </div>
                <div>
                    <label style="display:block;font-size:0.85rem;font-weight:600;color:#2c1e4a;margin-bottom:6px;">Owner Name</label>
                    <input type="text" name="owner_name" value="<?= htmlspecialchars($_POST['owner_name'] ?? '', ENT_QUOTES, 'UTF-8') ?>" style="width:100%;padding:10px 12px;border:1px solid #ddd;border-radius:6px;font-size:0.88rem;">
                </div>
                <div>
                    <label style="display:block;font-size:0.85rem;font-weight:600;color:#2c1e4a;margin-bottom:6px;">Owner Phone</label>
                    <input type="text" name="owner_phone" value="<?= htmlspecialchars($_POST['owner_phone'] ?? '', ENT_QUOTES, 'UTF-8') ?>" style="width:100%;padding:10px 12px;border:1px solid #ddd;border-radius:6px;font-size:0.88rem;">
                </div>
                <div>
                    <label style="display:block;font-size:0.85rem;font-weight:600;color:#2c1e4a;margin-bottom:6px;">Owner Email</label>
                    <input type="email" name="owner_email" value="<?= htmlspecialchars($_POST['owner_email'] ?? '', ENT_QUOTES, 'UTF-8') ?>" style="width:100%;padding:10px 12px;border:1px solid #ddd;border-radius:6px;font-size:0.88rem;">
                </div>
                <div>
                    <label style="display:block;font-size:0.85rem;font-weight:600;color:#2c1e4a;margin-bottom:6px;">Branch Phone</label>
                    <input type="text" name="phone" value="<?= htmlspecialchars($_POST['phone'] ?? '', ENT_QUOTES, 'UTF-8') ?>" placeholder="9305640290" style="width:100%;padding:10px 12px;border:1px solid #ddd;border-radius:6px;font-size:0.88rem;">
                </div>
            </div>
        </div>

        <!-- Location -->
        <div class="admin-card" style="background:#fff;border-radius:12px;padding:24px;margin-bottom:20px;box-shadow:0 2px 8px rgba(0,0,0,0.06);">
            <h2 style="margin:0 0 16px;font-size:1.1rem;color:#2c1e4a;">Location</h2>
            <div style="display:grid;grid-template-columns:repeat(auto-fit,minmax(220px,1fr));gap:16px;">
                <div>
                    <label style="display:block;font-size:0.85rem;font-weight:600;color:#2c1e4a;margin-bottom:6px;">State <span style="color:#e84118;">*</span></label>
                    <select name="state" id="stateSelect" required style="width:100%;padding:10px 12px;border:1px solid #ddd;border-radius:6px;font-size:0.88rem;">
                        <option value="">— Select State —</option>
                        <?php foreach (array_keys($indianStates) as $state): ?>
                            <option value="<?= htmlspecialchars($state, ENT_QUOTES, 'UTF-8') ?>" <?= ($_POST['state'] ?? '') === $state ? 'selected' : '' ?>><?= htmlspecialchars($state, ENT_QUOTES, 'UTF-8') ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div>
                    <label style="display:block;font-size:0.85rem;font-weight:600;color:#2c1e4a;margin-bottom:6px;">District <span style="color:#e84118;">*</span></label>
                    <select name="city" id="districtSelect" required style="width:100%;padding:10px 12px;border:1px solid #ddd;border-radius:6px;font-size:0.88rem;" disabled>
                        <option value="">— Select District —</option>
                    </select>
                </div>
                <div>
                    <label style="display:block;font-size:0.85rem;font-weight:600;color:#2c1e4a;margin-bottom:6px;">Pincode</label>
                    <input type="text" name="pincode" value="<?= htmlspecialchars($_POST['pincode'] ?? '', ENT_QUOTES, 'UTF-8') ?>" style="width:100%;padding:10px 12px;border:1px solid #ddd;border-radius:6px;font-size:0.88rem;">
                </div>
            </div>
            <div style="margin-top:14px;">
                <label style="display:block;font-size:0.85rem;font-weight:600;color:#2c1e4a;margin-bottom:6px;">Full Address <span style="color:#e84118;">*</span></label>
                <textarea name="address" required rows="2" placeholder="Complete address with landmark" style="width:100%;padding:10px 12px;border:1px solid #ddd;border-radius:6px;font-size:0.88rem;resize:vertical;"><?= htmlspecialchars($_POST['address'] ?? '', ENT_QUOTES, 'UTF-8') ?></textarea>
            </div>
        </div>

        <!-- Plan & Settings -->
        <div class="admin-card" style="background:#fff;border-radius:12px;padding:24px;margin-bottom:20px;box-shadow:0 2px 8px rgba(0,0,0,0.06);">
            <h2 style="margin:0 0 16px;font-size:1.1rem;color:#2c1e4a;">Plan & Settings</h2>
            <div style="display:grid;grid-template-columns:repeat(auto-fit,minmax(220px,1fr));gap:16px;">
                <div>
                    <label style="display:block;font-size:0.85rem;font-weight:600;color:#2c1e4a;margin-bottom:6px;">Franchise Plan</label>
                    <select name="plan_id" style="width:100%;padding:10px 12px;border:1px solid #ddd;border-radius:6px;font-size:0.88rem;">
                        <option value="">— Select Plan —</option>
                        <?php foreach ($plans as $p): ?>
                            <option value="<?= (int) $p['id'] ?>" <?= ($_POST['plan_id'] ?? '') == $p['id'] ? 'selected' : '' ?>>
                                <?= htmlspecialchars($p['name'], ENT_QUOTES, 'UTF-8') ?> — &#8377;<?= number_format((float) $p['price'], 0) ?> <?= !empty($p['is_trusted']) ? '⭐' : '' ?>
                            </option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div>
                    <label style="display:block;font-size:0.85rem;font-weight:600;color:#2c1e4a;margin-bottom:6px;">Wallet Balance (Initial)</label>
                    <input type="number" name="wallet_balance" step="0.01" min="0" value="<?= htmlspecialchars($_POST['wallet_balance'] ?? '0', ENT_QUOTES, 'UTF-8') ?>" style="width:100%;padding:10px 12px;border:1px solid #ddd;border-radius:6px;font-size:0.88rem;">
                </div>
                <div>
                    <label style="display:block;font-size:0.85rem;font-weight:600;color:#2c1e4a;margin-bottom:6px;">Status</label>
                    <select name="status" style="width:100%;padding:10px 12px;border:1px solid #ddd;border-radius:6px;font-size:0.88rem;">
                        <option value="active" <?= ($_POST['status'] ?? 'active') === 'active' ? 'selected' : '' ?>>Active</option>
                        <option value="pending" <?= ($_POST['status'] ?? '') === 'pending' ? 'selected' : '' ?>>Pending</option>
                        <option value="inactive" <?= ($_POST['status'] ?? '') === 'inactive' ? 'selected' : '' ?>>Inactive</option>
                        <option value="suspended" <?= ($_POST['status'] ?? '') === 'suspended' ? 'selected' : '' ?>>Suspended</option>
                    </select>
                </div>
            </div>
            <div style="display:flex;align-items:center;gap:16px;margin-top:14px;">
                <label style="display:flex;align-items:center;gap:6px;cursor:pointer;">
                    <input type="checkbox" name="is_verified" value="1" checked style="width:16px;height:16px;accent-color:#6c3fe8;">
                    <span style="font-size:0.85rem;font-weight:600;color:#2c1e4a;">Verified</span>
                </label>
                <label style="display:flex;align-items:center;gap:6px;cursor:pointer;">
                    <input type="checkbox" name="is_trusted" value="1" style="width:16px;height:16px;accent-color:#f39c12;">
                    <span style="font-size:0.85rem;font-weight:600;color:#f39c12;">⭐ Trusted (Gold plan)</span>
                </label>
            </div>
        </div>

        <!-- Branch Admin Login -->
        <div class="admin-card" style="background:#fff;border-radius:12px;padding:24px;margin-bottom:20px;box-shadow:0 2px 8px rgba(0,0,0,0.06);border-left:4px solid #6c3fe8;">
            <h2 style="margin:0 0 6px;font-size:1.1rem;color:#2c1e4a;">Branch Admin Login Account</h2>
            <p style="margin:0 0 20px;font-size:0.85rem;color:#666;">Create the branch's login account. They'll log in at <code><?= $bp ?>/branch/login</code>.</p>
            <div style="display:grid;grid-template-columns:repeat(auto-fit,minmax(220px,1fr));gap:16px;">
                <div>
                    <label style="display:block;font-size:0.85rem;font-weight:600;color:#2c1e4a;margin-bottom:6px;">Admin Name</label>
                    <input type="text" name="admin_name" value="<?= htmlspecialchars($_POST['admin_name'] ?? '', ENT_QUOTES, 'UTF-8') ?>" style="width:100%;padding:10px 12px;border:1px solid #ddd;border-radius:6px;font-size:0.88rem;">
                </div>
                <div>
                    <label style="display:block;font-size:0.85rem;font-weight:600;color:#2c1e4a;margin-bottom:6px;">Admin Email</label>
                    <input type="email" name="admin_email" value="<?= htmlspecialchars($_POST['admin_email'] ?? '', ENT_QUOTES, 'UTF-8') ?>" style="width:100%;padding:10px 12px;border:1px solid #ddd;border-radius:6px;font-size:0.88rem;">
                </div>
                <div>
                    <label style="display:block;font-size:0.85rem;font-weight:600;color:#2c1e4a;margin-bottom:6px;">Admin Phone</label>
                    <input type="text" name="admin_phone" value="<?= htmlspecialchars($_POST['admin_phone'] ?? '', ENT_QUOTES, 'UTF-8') ?>" style="width:100%;padding:10px 12px;border:1px solid #ddd;border-radius:6px;font-size:0.88rem;">
                </div>
                <div>
                    <label style="display:block;font-size:0.85rem;font-weight:600;color:#2c1e4a;margin-bottom:6px;">Password</label>
                    <input type="text" name="admin_password" value="<?= htmlspecialchars($_POST['admin_password'] ?? '123456', ENT_QUOTES, 'UTF-8') ?>" style="width:100%;padding:10px 12px;border:1px solid #ddd;border-radius:6px;font-size:0.88rem;">
                </div>
            </div>
        </div>

        <!-- Submit -->
        <div style="display:flex;gap:12px;justify-content:flex-end;">
            <a href="<?= $bp ?>/super-admin/branches" style="padding:12px 24px;border:1px solid #ddd;border-radius:8px;text-decoration:none;color:#666;font-weight:600;font-size:0.9rem;">Cancel</a>
            <button type="submit" style="padding:12px 28px;background:linear-gradient(135deg,#6c3fe8,#4a2db8);color:#fff;border:none;border-radius:8px;font-weight:600;font-size:0.9rem;cursor:pointer;font-family:'Poppins',sans-serif;">Create Branch</button>
        </div>
    </form>
</div>

<script>
var indianStates = <?= json_encode($indianStates, JSON_UNESCAPED_UNICODE) ?>;
document.getElementById('stateSelect').addEventListener('change', function() {
    var state = this.value;
    var ds = document.getElementById('districtSelect');
    ds.innerHTML = '<option value="">— Select District —</option>';
    if (state && indianStates[state]) {
        indianStates[state].forEach(function(d) {
            var o = document.createElement('option');
            o.value = d; o.textContent = d;
            ds.appendChild(o);
        });
        ds.disabled = false;
    } else { ds.disabled = true; }
});
</script>
