<?php $bp = defined('BASE_PATH') ? BASE_PATH : '/gmind'; ?>
<div class="admin-page-header">
    <h2>Branch Facility</h2>
</div>

<div class="admin-card">
    <div class="admin-card-body">
        <form method="POST" action="<?= $bp ?>/branch/facilities/update" class="admin-form" enctype="multipart/form-data" 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="owner_name" class="admin-form-label">Owner Name</label>
                    <input type="text" id="owner_name" name="owner_name" class="admin-form-input"
                           placeholder="Enter owner name"
                           value="<?= htmlspecialchars($branch['owner_name'] ?? '', ENT_QUOTES, 'UTF-8') ?>">
                </div>
                <div class="admin-form-group">
                    <label for="running_since" class="admin-form-label">Running Since (Year)</label>
                    <input type="number" id="running_since" name="running_since" class="admin-form-input"
                           placeholder="e.g. 2015" min="1990" max="<?= date('Y') ?>"
                           value="<?= htmlspecialchars($branch['running_since'] ?? '', ENT_QUOTES, 'UTF-8') ?>">
                </div>
            </div>

            <div class="admin-form-row">
                <div class="admin-form-group">
                    <label for="opening_time" class="admin-form-label">Opening Time</label>
                    <input type="time" id="opening_time" name="opening_time" class="admin-form-input"
                           value="<?= htmlspecialchars($branch['opening_time'] ?? '', ENT_QUOTES, 'UTF-8') ?>">
                </div>
                <div class="admin-form-group">
                    <label for="closing_time" class="admin-form-label">Closing Time</label>
                    <input type="time" id="closing_time" name="closing_time" class="admin-form-input"
                           value="<?= htmlspecialchars($branch['closing_time'] ?? '', ENT_QUOTES, 'UTF-8') ?>">
                </div>
            </div>

            <!-- Facilities Checkboxes -->
            <div class="admin-form-group">
                <label class="admin-form-label">Facilities Available</label>
                <div class="admin-checkbox-grid">
                    <?php foreach ($facilityOptions ?? [] as $opt): ?>
                    <div class="admin-checkbox-item">
                        <input type="checkbox" id="fac_<?= htmlspecialchars(md5($opt), ENT_QUOTES, 'UTF-8') ?>"
                               name="facilities[]" value="<?= htmlspecialchars($opt, ENT_QUOTES, 'UTF-8') ?>"
                               <?= in_array($opt, $branch['facilities_list'] ?? []) ? 'checked' : '' ?>>
                        <label for="fac_<?= htmlspecialchars(md5($opt), ENT_QUOTES, 'UTF-8') ?>"><?= htmlspecialchars($opt, ENT_QUOTES, 'UTF-8') ?></label>
                    </div>
                    <?php endforeach; ?>
                </div>
            </div>

            <!-- Description -->
            <div class="admin-form-group">
                <label for="description" class="admin-form-label">Description</label>
                <textarea id="description" name="description" class="admin-form-textarea" rows="4"
                          placeholder="Describe your branch, special features, etc."><?= htmlspecialchars($branch['description'] ?? '', ENT_QUOTES, 'UTF-8') ?></textarea>
            </div>

            <!-- Photo Uploads -->
            <div class="admin-form-group">
                <label class="admin-form-label">Branch Photos</label>
                <div class="admin-photo-grid">
                    <!-- Front Photo -->
                    <div class="admin-photo-upload">
                        <label for="photo_front" class="admin-photo-label">Front Photo</label>
                        <?php if (!empty($branch['photo_front'])): ?>
                        <div class="admin-photo-preview">
                            <img src="<?= $bp ?>/<?= htmlspecialchars($branch['photo_front'], ENT_QUOTES, 'UTF-8') ?>" alt="Front Photo">
                        </div>
                        <?php endif; ?>
                        <input type="file" id="photo_front" name="photo_front" class="admin-form-file"
                               accept="image/jpeg,image/png,image/gif,image/webp">
                        <small class="admin-form-hint">JPG, PNG, GIF, or WebP. Max 5MB.</small>
                    </div>

                    <!-- Back Photo -->
                    <div class="admin-photo-upload">
                        <label for="photo_back" class="admin-photo-label">Back Photo</label>
                        <?php if (!empty($branch['photo_back'])): ?>
                        <div class="admin-photo-preview">
                            <img src="<?= $bp ?>/<?= htmlspecialchars($branch['photo_back'], ENT_QUOTES, 'UTF-8') ?>" alt="Back Photo">
                        </div>
                        <?php endif; ?>
                        <input type="file" id="photo_back" name="photo_back" class="admin-form-file"
                               accept="image/jpeg,image/png,image/gif,image/webp">
                        <small class="admin-form-hint">JPG, PNG, GIF, or WebP. Max 5MB.</small>
                    </div>

                    <!-- Class Photo -->
                    <div class="admin-photo-upload">
                        <label for="photo_class" class="admin-photo-label">Class Photo</label>
                        <?php if (!empty($branch['photo_class'])): ?>
                        <div class="admin-photo-preview">
                            <img src="<?= $bp ?>/<?= htmlspecialchars($branch['photo_class'], ENT_QUOTES, 'UTF-8') ?>" alt="Class Photo">
                        </div>
                        <?php endif; ?>
                        <input type="file" id="photo_class" name="photo_class" class="admin-form-file"
                               accept="image/jpeg,image/png,image/gif,image/webp">
                        <small class="admin-form-hint">JPG, PNG, GIF, or WebP. Max 5MB.</small>
                    </div>

                    <!-- Reception Photo -->
                    <div class="admin-photo-upload">
                        <label for="photo_reception" class="admin-photo-label">Reception Photo</label>
                        <?php if (!empty($branch['photo_reception'])): ?>
                        <div class="admin-photo-preview">
                            <img src="<?= $bp ?>/<?= htmlspecialchars($branch['photo_reception'], ENT_QUOTES, 'UTF-8') ?>" alt="Reception Photo">
                        </div>
                        <?php endif; ?>
                        <input type="file" id="photo_reception" name="photo_reception" class="admin-form-file"
                               accept="image/jpeg,image/png,image/gif,image/webp">
                        <small class="admin-form-hint">JPG, PNG, GIF, or WebP. Max 5MB.</small>
                    </div>
                </div>
            </div>

            <div class="admin-form-actions">
                <button type="submit" class="admin-btn admin-btn-primary">Save Facility Details</button>
            </div>
        </form>
    </div>
</div>
