<?php
/**
 * Super Admin — Edit Branch View
 */
$bp = defined('BASE_PATH') ? BASE_PATH : '/gmind';
$branch = $branch ?? [];
$plans  = $plans  ?? [];
?>
<div class="admin-section">
    <div style="margin-bottom:20px;display:flex;justify-content:space-between;align-items:center;flex-wrap:wrap;gap:12px;">
        <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>
        <a href="<?= $bp ?>/super-admin/branches/<?= (int) ($branch['id'] ?? 0) ?>" style="background:#3498db;color:#fff;text-decoration:none;padding:8px 16px;border-radius:6px;font-size:0.85rem;font-weight:600;">View Branch Details</a>
    </div>

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

        <!-- Branch Details -->
        <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;">Branch Details</h2>
            <p style="margin:0 0 20px;font-size:0.85rem;color:#666;">Code: <code style="background:#6c3fe8;color:#fff;padding:2px 8px;border-radius:4px;"><?= htmlspecialchars($branch['branch_code'] ?? '', ENT_QUOTES, 'UTF-8') ?></code> (cannot be changed)</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;">Branch Name <span style="color:#e84118;">*</span></label>
                    <input type="text" name="name" required value="<?= htmlspecialchars($branch['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;">City <span style="color:#e84118;">*</span></label>
                    <input type="text" name="city" required value="<?= htmlspecialchars($branch['city'] ?? '', 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;">State <span style="color:#e84118;">*</span></label>
                    <input type="text" name="state" required value="<?= htmlspecialchars($branch['state'] ?? '', 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;">Pincode</label>
                    <input type="text" name="pincode" value="<?= htmlspecialchars($branch['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" style="width:100%;padding:10px 12px;border:1px solid #ddd;border-radius:6px;font-size:0.88rem;resize:vertical;"><?= htmlspecialchars($branch['address'] ?? '', ENT_QUOTES, 'UTF-8') ?></textarea>
            </div>

            <div style="display:grid;grid-template-columns:repeat(auto-fit,minmax(220px,1fr));gap:16px;margin-top:14px;">
                <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($branch['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;">Opening Time</label>
                    <input type="text" name="opening_time" value="<?= htmlspecialchars($branch['opening_time'] ?? '', ENT_QUOTES, 'UTF-8') ?>" placeholder="09:00 AM" 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;">Closing Time</label>
                    <input type="text" name="closing_time" value="<?= htmlspecialchars($branch['closing_time'] ?? '', ENT_QUOTES, 'UTF-8') ?>" placeholder="08:00 PM" 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;">Google Map Link</label>
                    <input type="url" name="map_link" value="<?= htmlspecialchars($branch['map_link'] ?? '', ENT_QUOTES, 'UTF-8') ?>" placeholder="https://maps.google.com/?q=..." style="width:100%;padding:10px 12px;border:1px solid #ddd;border-radius:6px;font-size:0.88rem;">
                </div>
            </div>

            <div style="display:grid;grid-template-columns:repeat(auto-fit,minmax(220px,1fr));gap:16px;margin-top:14px;">
                <div>
                    <label style="display:block;font-size:0.85rem;font-weight:600;color:#2c1e4a;margin-bottom:6px;">Latitude</label>
                    <input type="number" step="any" name="latitude" value="<?= htmlspecialchars($branch['latitude'] ?? '', 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;">Longitude</label>
                    <input type="number" step="any" name="longitude" value="<?= htmlspecialchars($branch['longitude'] ?? '', ENT_QUOTES, 'UTF-8') ?>" style="width:100%;padding:10px 12px;border:1px solid #ddd;border-radius:6px;font-size:0.88rem;">
                </div>
            </div>
        </div>

        <!-- Owner & Plan -->
        <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;">Owner & Plan</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;">Owner Name</label>
                    <input type="text" name="owner_name" value="<?= htmlspecialchars($branch['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($branch['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($branch['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;">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="">— None —</option>
                        <?php foreach ($plans as $p): ?>
                            <option value="<?= (int) $p['id'] ?>" <?= ($branch['plan_id'] ?? '') == $p['id'] ? 'selected' : '' ?>>
                                <?= htmlspecialchars($p['name'], ENT_QUOTES, 'UTF-8') ?> — &#8377;<?= number_format((float) $p['price'], 0) ?>
                            </option>
                        <?php endforeach; ?>
                    </select>
                </div>
            </div>
        </div>

        <!-- Wallet & Status -->
        <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 #f39c12;">
            <h2 style="margin:0 0 16px;font-size:1.1rem;color:#2c1e4a;">Wallet & Status</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;">Wallet Balance (&#8377;)</label>
                    <input type="number" step="0.01" min="0" name="wallet_balance" value="<?= htmlspecialchars($branch['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;">
                        <?php foreach (['active','pending','inactive','suspended'] as $s): ?>
                            <option value="<?= $s ?>" <?= ($branch['status'] ?? '') === $s ? 'selected' : '' ?>><?= ucfirst($s) ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div style="display:flex;align-items:center;gap:8px;padding-top:30px;">
                    <input type="checkbox" name="is_verified" value="1" id="is_verified" <?= !empty($branch['is_verified']) ? 'checked' : '' ?> style="width:18px;height:18px;accent-color:#6c3fe8;">
                    <label for="is_verified" style="font-size:0.88rem;color:#2c1e4a;font-weight:600;cursor:pointer;">Verified Branch (KYC verified)</label>
                </div>
                <div style="display:flex;align-items:center;gap:8px;padding-top:12px;">
                    <input type="checkbox" name="is_trusted" value="1" id="is_trusted" <?= !empty($branch['is_trusted']) ? 'checked' : '' ?> style="width:18px;height:18px;accent-color:#f39c12;">
                    <label for="is_trusted" style="font-size:0.88rem;color:#f39c12;font-weight:600;cursor:pointer;">⭐ Trusted Branch (shows TRUSTED badge on listings — Gold plan)</label>
                </div>
            </div>
        </div>

        <!-- Submit Buttons -->
        <div style="display:flex;gap:12px;justify-content:space-between;align-items:center;">
            <form method="POST" action="<?= $bp ?>/super-admin/branches/<?= (int) ($branch['id'] ?? 0) ?>/delete" onsubmit="return confirm('Suspend this branch? It will be inactive but data preserved. You can reactivate later.');" 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 Branch</button>
            </form>
            <div style="display:flex;gap:12px;">
                <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;">Save Changes</button>
            </div>
        </div>
    </form>
</div>
