<?php
/**
 * Super Admin — Edit Branch Admin View
 */
$bp = defined('BASE_PATH') ? BASE_PATH : '/gmind';
$admin    = $admin ?? [];
$branches = $branches ?? [];
?>
<div class="admin-section">
    <div style="margin-bottom:20px;">
        <a href="<?= $bp ?>/super-admin/branch-admins" 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 Branch Admins
        </a>
    </div>

    <form method="POST" action="<?= $bp ?>/super-admin/branch-admins/<?= (int) ($admin['id'] ?? 0) ?>" style="max-width:640px;">
        <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($csrfToken ?? '', ENT_QUOTES, 'UTF-8') ?>">

        <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;">Edit Branch Admin</h2>
            <p style="margin:0 0 20px;font-size:0.85rem;color:#666;">Currently assigned to: <code style="background:#6c3fe8;color:#fff;padding:2px 8px;border-radius:4px;"><?= htmlspecialchars($admin['current_branch_code'] ?? 'N/A', ENT_QUOTES, 'UTF-8') ?></code> <?= htmlspecialchars($admin['branch_name'] ?? '', ENT_QUOTES, 'UTF-8') ?></p>

            <div style="display:grid;grid-template-columns:1fr 1fr;gap:16px;">
                <div>
                    <label style="display:block;font-size:0.85rem;font-weight:600;color:#2c1e4a;margin-bottom:6px;">Full Name <span style="color:#e84118;">*</span></label>
                    <input type="text" name="name" required value="<?= htmlspecialchars($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;">Phone</label>
                    <input type="text" name="phone" value="<?= htmlspecialchars($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>

            <div style="margin-top:14px;">
                <label style="display:block;font-size:0.85rem;font-weight:600;color:#2c1e4a;margin-bottom:6px;">Email <span style="color:#e84118;">*</span></label>
                <input type="email" name="email" required value="<?= htmlspecialchars($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 style="margin-top:14px;">
                <label style="display:block;font-size:0.85rem;font-weight:600;color:#2c1e4a;margin-bottom:6px;">New Password <span style="color:#999;font-weight:normal;">(leave blank to keep current)</span></label>
                <input type="text" name="password" value="" placeholder="Enter new password to reset" style="width:100%;padding:10px 12px;border:1px solid #ddd;border-radius:6px;font-size:0.88rem;">
                <small style="display:block;margin-top:4px;color:#999;font-size:0.75rem;">Min 6 characters. Leave blank to keep existing password.</small>
            </div>

            <div style="margin-top:14px;">
                <label style="display:block;font-size:0.85rem;font-weight:600;color:#2c1e4a;margin-bottom:6px;">Reassign to Branch <span style="color:#e84118;">*</span></label>
                <select name="branch_id" required style="width:100%;padding:10px 12px;border:1px solid #ddd;border-radius:6px;font-size:0.88rem;">
                    <option value="">— Select Branch —</option>
                    <?php foreach ($branches as $b): ?>
                        <option value="<?= (int) $b['id'] ?>" <?= ($admin['branch_id'] ?? '') == $b['id'] ? 'selected' : '' ?>>
                            <?= htmlspecialchars($b['branch_code'] ?? '', ENT_QUOTES, 'UTF-8') ?> — <?= htmlspecialchars($b['name'] ?? '', ENT_QUOTES, 'UTF-8') ?> (<?= htmlspecialchars($b['city'] ?? '', ENT_QUOTES, 'UTF-8') ?>)
                        </option>
                    <?php endforeach; ?>
                </select>
            </div>

            <div style="margin-top:14px;">
                <label style="display:block;font-size:0.85rem;font-weight:600;color:#2c1e4a;margin-bottom:6px;">Account Status</label>
                <select name="status" style="width:100%;padding:10px 12px;border:1px solid #ddd;border-radius:6px;font-size:0.88rem;">
                    <?php foreach (['active','inactive','suspended'] as $s): ?>
                        <option value="<?= $s ?>" <?= ($admin['status'] ?? '') === $s ? 'selected' : '' ?>><?= ucfirst($s) ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
        </div>

        <div style="display:flex;gap:12px;justify-content:space-between;align-items:center;">
            <form method="POST" action="<?= $bp ?>/super-admin/branch-admins/<?= (int) ($admin['id'] ?? 0) ?>/delete" onsubmit="return confirm('Suspend this branch admin? They will not be able to login. Data preserved.');" style="display:inline;">
                <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($csrfToken ?? '', ENT_QUOTES, 'UTF-8') ?>">
                <button type="submit" style="padding:12px 24px;background:#e84118;color:#fff;border:none;border-radius:8px;font-weight:600;font-size:0.9rem;cursor:pointer;">Suspend Account</button>
            </form>
            <div style="display:flex;gap:12px;">
                <a href="<?= $bp ?>/super-admin/branch-admins" 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;">Save Changes</button>
            </div>
        </div>
    </form>
</div>
