<!-- Branches Management -->
<div class="admin-section">
    <?php if (!($showForm ?? false)): ?>
    <div class="admin-section-header">
        <a href="<?= url('/admin/branches/create') ?>" class="admin-btn admin-btn-primary">
            <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" width="18" height="18"><line x1="12" y1="5" x2="12" y2="19"/><line x1="5" y1="12" x2="19" y2="12"/></svg>
            Add New Branch
        </a>
    </div>
    <?php endif; ?>

    <?php if ($showForm ?? false): ?>
    <!-- Add/Edit Branch Form -->
    <div class="admin-card" style="margin-bottom: 24px;">
        <div class="admin-card-header">
            <h2 class="admin-card-title"><?= $editBranch ? 'Edit Branch' : 'Add New Branch' ?></h2>
        </div>
        <div class="admin-card-body">
            <form method="POST" action="<?= $editBranch ? '/admin/branches/' . (int)$editBranch['id'] : '/admin/branches' ?>" enctype="multipart/form-data" class="admin-form" novalidate>
                <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($csrfToken ?? '', ENT_QUOTES, 'UTF-8') ?>">

                <div class="admin-form-row">
                    <div class="admin-form-group">
                        <label for="name" class="admin-form-label">Branch Name <span class="admin-required">*</span></label>
                        <input type="text" id="name" name="name" class="admin-form-input" required
                               value="<?= htmlspecialchars($editBranch['name'] ?? '', ENT_QUOTES, 'UTF-8') ?>">
                    </div>
                    <div class="admin-form-group">
                        <label for="phone" class="admin-form-label">Phone</label>
                        <input type="tel" id="phone" name="phone" class="admin-form-input"
                               value="<?= htmlspecialchars($editBranch['phone'] ?? '', ENT_QUOTES, 'UTF-8') ?>">
                    </div>
                </div>

                <div class="admin-form-row">
                    <div class="admin-form-group">
                        <label for="city" class="admin-form-label">City <span class="admin-required">*</span></label>
                        <input type="text" id="city" name="city" class="admin-form-input" required
                               value="<?= htmlspecialchars($editBranch['city'] ?? '', ENT_QUOTES, 'UTF-8') ?>">
                    </div>
                    <div class="admin-form-group">
                        <label for="state" class="admin-form-label">State <span class="admin-required">*</span></label>
                        <input type="text" id="state" name="state" class="admin-form-input" required
                               value="<?= htmlspecialchars($editBranch['state'] ?? '', ENT_QUOTES, 'UTF-8') ?>">
                    </div>
                </div>

                <div class="admin-form-group">
                    <label for="address" class="admin-form-label">Full Address</label>
                    <textarea id="address" name="address" class="admin-form-textarea" rows="3"><?= htmlspecialchars($editBranch['address'] ?? '', ENT_QUOTES, 'UTF-8') ?></textarea>
                </div>

                <div class="admin-form-group">
                    <label for="map_link" class="admin-form-label">Google Maps Link</label>
                    <input type="url" id="map_link" name="map_link" class="admin-form-input"
                           value="<?= htmlspecialchars($editBranch['map_link'] ?? '', ENT_QUOTES, 'UTF-8') ?>">
                </div>

                <div class="admin-form-row">
                    <div class="admin-form-group">
                        <label for="image" class="admin-form-label">Branch Image</label>
                        <input type="file" id="image" name="image" class="admin-form-input" accept="image/jpeg,image/png,image/gif,image/webp"
                               data-preview="imagePreview">
                        <?php if ($editBranch && $editBranch['image']): ?>
                        <div class="admin-current-image">
                            <span>Current:</span>
                            <img src="<?= asset('/' . $editBranch['image']) ?>" alt="Current branch" height="60">
                        </div>
                        <?php endif; ?>
                        <div id="imagePreview" class="admin-image-preview"></div>
                    </div>
                    <div class="admin-form-group">
                        <label class="admin-form-label">Active Status</label>
                        <label class="admin-toggle">
                            <input type="checkbox" name="active" value="1" <?= ($editBranch['active'] ?? 1) ? 'checked' : '' ?>>
                            <span class="admin-toggle-slider"></span>
                            <span class="admin-toggle-label">Active</span>
                        </label>
                    </div>
                </div>

                <div class="admin-form-actions">
                    <button type="submit" class="admin-btn admin-btn-primary"><?= $editBranch ? 'Update Branch' : 'Create Branch' ?></button>
                    <a href="<?= url('/admin/branches') ?>" class="admin-btn admin-btn-secondary">Cancel</a>
                </div>
            </form>
        </div>
    </div>
    <?php endif; ?>

    <!-- Branches Table -->
    <?php if (!empty($branches)): ?>
    <div class="admin-card">
        <div class="admin-card-body">
            <div class="admin-table-responsive">
                <table class="admin-table" data-search="true">
                    <thead>
                        <tr>
                            <th>Name</th>
                            <th>City</th>
                            <th>State</th>
                            <th>Phone</th>
                            <th>Active</th>
                            <th>Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($branches as $branch): ?>
                        <tr>
                            <td><strong><?= htmlspecialchars($branch['name'], ENT_QUOTES, 'UTF-8') ?></strong></td>
                            <td><?= htmlspecialchars($branch['city'], ENT_QUOTES, 'UTF-8') ?></td>
                            <td><?= htmlspecialchars($branch['state'], ENT_QUOTES, 'UTF-8') ?></td>
                            <td><?= htmlspecialchars($branch['phone'] ?? '—', ENT_QUOTES, 'UTF-8') ?></td>
                            <td>
                                <label class="admin-toggle admin-toggle-sm">
                                    <input type="checkbox" <?= $branch['active'] ? 'checked' : '' ?>>
                                    <span class="admin-toggle-slider"></span>
                                </label>
                            </td>
                            <td>
                                <div class="admin-actions">
                                    <a href="<?= url('/admin/branches/' . (int) $branch['id'] . '/edit') ?>" class="admin-btn-icon" title="Edit">
                                        <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" width="16" height="16"><path d="M11 4H4a2 2 0 00-2 2v14a2 2 0 002 2h14a2 2 0 002-2v-7"/><path d="M18.5 2.5a2.121 2.121 0 013 3L12 15l-4 1 1-4 9.5-9.5z"/></svg>
                                    </a>
                                    <button type="button" class="admin-btn-icon admin-btn-delete" title="Delete"
                                            data-delete-url="<?= url('/admin/branches/' . (int) $branch['id'] . '/delete') ?>">
                                        <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" width="16" height="16"><polyline points="3 6 5 6 21 6"/><path d="M19 6v14a2 2 0 01-2 2H7a2 2 0 01-2-2V6m3 0V4a2 2 0 012-2h4a2 2 0 012 2v2"/></svg>
                                    </button>
                                </div>
                            </td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
    <?php elseif (!($showForm ?? false)): ?>
    <div class="admin-card">
        <div class="admin-card-body">
            <p class="admin-empty-text">No branches found. <a href="<?= url('/admin/branches/create') ?>">Add your first branch</a>.</p>
        </div>
    </div>
    <?php endif; ?>
</div>
