<?php
/**
 * Branch Admin — Profile View
 *
 * Branch profile edit form with:
 * - Owner Name, Opening Time, Running Since
 * - Facilities section with checkboxes
 * - Photo upload section (Front, Back, Reception)
 * - Current wallet balance display
 */
$bp = defined('BASE_PATH') ? BASE_PATH : '/gmind';
$branchData = $branch ?? [];
$walletBalance = (float) ($branchData['wallet_balance'] ?? 0);
?>

<!-- Wallet Balance Card -->
<div class="admin-card" style="margin-bottom:20px;">
    <div class="admin-card-body" style="display:flex;align-items:center;gap:16px;">
        <div style="background:linear-gradient(135deg,#27ae60,#2ecc71);border-radius:12px;padding:16px;color:#fff;">
            <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" width="32" height="32"><rect x="1" y="4" width="22" height="16" rx="2" ry="2"/><line x1="1" y1="10" x2="23" y2="10"/></svg>
        </div>
        <div>
            <span style="color:#888;font-size:14px;">Current Wallet Balance</span>
            <div style="font-size:28px;font-weight:700;color:#27ae60;">&#8377;<?= number_format($walletBalance, 2) ?></div>
        </div>
    </div>
</div>

<!-- Branch Profile Form -->
<div class="admin-card">
    <div class="admin-card-header">
        <h2 class="admin-card-title">Branch Profile</h2>
    </div>
    <div class="admin-card-body">
        <form method="POST" action="<?= $bp ?>/branch/profile" class="admin-form">
            <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($csrfToken ?? '', ENT_QUOTES, 'UTF-8') ?>">

            <div class="admin-form-row" style="display:grid;grid-template-columns:1fr 1fr;gap:16px;">
                <div class="admin-form-group">
                    <label class="admin-form-label" for="name">Branch Name</label>
                    <input type="text" name="name" id="name" class="admin-form-input"
                           value="<?= htmlspecialchars($branchData['name'] ?? '', ENT_QUOTES, 'UTF-8') ?>"
                           placeholder="Branch name">
                </div>
                <div class="admin-form-group">
                    <label class="admin-form-label" for="owner_name">Owner Name</label>
                    <input type="text" name="owner_name" id="owner_name" class="admin-form-input"
                           value="<?= htmlspecialchars($branchData['owner_name'] ?? '', ENT_QUOTES, 'UTF-8') ?>"
                           placeholder="Enter owner name">
                </div>
            </div>

            <div class="admin-form-row" style="display:grid;grid-template-columns:1fr 1fr;gap:16px;">
                <div class="admin-form-group">
                    <label class="admin-form-label" for="opening_time">Opening Time</label>
                    <input type="time" name="opening_time" id="opening_time" class="admin-form-input"
                           value="<?= htmlspecialchars($branchData['opening_time'] ?? '', ENT_QUOTES, 'UTF-8') ?>">
                </div>
                <div class="admin-form-group">
                    <label class="admin-form-label" for="running_since">Running Since</label>
                    <input type="text" name="running_since" id="running_since" class="admin-form-input"
                           value="<?= htmlspecialchars($branchData['running_since'] ?? '', ENT_QUOTES, 'UTF-8') ?>"
                           placeholder="e.g., 2018 or 5 years">
                </div>
            </div>

            <div class="admin-form-row" style="display:grid;grid-template-columns:1fr 1fr;gap:16px;">
                <div class="admin-form-group">
                    <label class="admin-form-label" for="phone">Phone</label>
                    <input type="tel" name="phone" id="phone" class="admin-form-input"
                           value="<?= htmlspecialchars($branchData['phone'] ?? '', ENT_QUOTES, 'UTF-8') ?>"
                           placeholder="Branch phone number">
                </div>
                <div class="admin-form-group">
                    <label class="admin-form-label" for="map_link">Google Map Link</label>
                    <input type="url" name="map_link" id="map_link" class="admin-form-input"
                           value="<?= htmlspecialchars($branchData['map_link'] ?? '', ENT_QUOTES, 'UTF-8') ?>"
                           placeholder="https://maps.google.com/...">
                </div>
            </div>

            <div class="admin-form-group">
                <label class="admin-form-label" for="address">Address</label>
                <textarea name="address" id="address" class="admin-form-input" rows="2"
                          placeholder="Branch address"><?= htmlspecialchars($branchData['address'] ?? '', ENT_QUOTES, 'UTF-8') ?></textarea>
            </div>

            <div class="admin-form-row" style="display:grid;grid-template-columns:1fr 1fr;gap:16px;">
                <div class="admin-form-group">
                    <label class="admin-form-label" for="city">City</label>
                    <input type="text" name="city" id="city" class="admin-form-input"
                           value="<?= htmlspecialchars($branchData['city'] ?? '', ENT_QUOTES, 'UTF-8') ?>"
                           placeholder="City">
                </div>
                <div class="admin-form-group">
                    <label class="admin-form-label" for="state">State</label>
                    <input type="text" name="state" id="state" class="admin-form-input"
                           value="<?= htmlspecialchars($branchData['state'] ?? '', ENT_QUOTES, 'UTF-8') ?>"
                           placeholder="State">
                </div>
            </div>

            <div style="margin-top:20px;">
                <button type="submit" class="admin-btn admin-btn-primary">Update Profile</button>
            </div>
        </form>
    </div>
</div>

<!-- Facilities Section -->
<div class="admin-card" style="margin-top:20px;">
    <div class="admin-card-header">
        <h2 class="admin-card-title">Facilities</h2>
    </div>
    <div class="admin-card-body">
        <form method="POST" action="<?= $bp ?>/branch/profile/facilities" class="admin-form">
            <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($csrfToken ?? '', ENT_QUOTES, 'UTF-8') ?>">

            <div style="display:grid;grid-template-columns:repeat(auto-fill,minmax(180px,1fr));gap:12px;margin-bottom:16px;">
                <?php
                // Facility options as specified in the requirements
                $facilityOptions = [
                    'WiFi', 'Parking', 'CCTV Surveillance', 'Generator Backup',
                    'Books', 'Drinking Water', 'AC Classrooms', 'Computer Lab',
                    'Library', 'Canteen', 'Projector', 'Whiteboard',
                    'Audio System', 'First Aid', 'Separate Seating', 'Washroom',
                ];

                // Merge with commonFacilities from controller if available
                if (!empty($commonFacilities) && is_array($commonFacilities)) {
                    $facilityOptions = array_unique(array_merge($facilityOptions, $commonFacilities));
                }

                foreach ($facilityOptions as $facilityName):
                    $isChecked = in_array($facilityName, $availableFacilities ?? []);
                ?>
                <label style="display:flex;align-items:center;gap:8px;cursor:pointer;padding:8px 12px;border-radius:6px;border:1px solid #e0e0e0;transition:all 0.2s;"
                       onmouseover="this.style.background='#f0f7ff'" onmouseout="this.style.background='<?= $isChecked ? '#e8f5e9' : 'transparent' ?>' ?>"
                       <?= $isChecked ? 'class="checked-facility" style="background:#e8f5e9;border-color:#4caf50;"' : '' ?>>
                    <input type="checkbox" name="facilities[<?= htmlspecialchars($facilityName, ENT_QUOTES, 'UTF-8') ?>]" value="1"
                           <?= $isChecked ? 'checked' : '' ?>
                           style="width:16px;height:16px;">
                    <span style="font-size:14px;"><?= htmlspecialchars($facilityName, ENT_QUOTES, 'UTF-8') ?></span>
                </label>
                <?php endforeach; ?>
            </div>

            <div class="admin-form-group">
                <label class="admin-form-label" for="custom_facilities">Other Facilities (comma-separated)</label>
                <input type="text" name="custom_facilities" id="custom_facilities" class="admin-form-input"
                       placeholder="e.g., Smart Board, Rooftop Garden, Gym">
            </div>

            <div style="margin-top:16px;">
                <button type="submit" class="admin-btn admin-btn-primary">Update Facilities</button>
            </div>
        </form>
    </div>
</div>

<!-- Photo Upload Section -->
<div class="admin-card" style="margin-top:20px;">
    <div class="admin-card-header">
        <h2 class="admin-card-title">Branch Photos</h2>
    </div>
    <div class="admin-card-body">
        <div style="display:grid;grid-template-columns:repeat(auto-fill,minmax(280px,1fr));gap:20px;">
            <?php
            $photoTypes = [
                'front_photo'    => 'Front Photo',
                'back_photo'     => 'Back Photo',
                'reception_photo' => 'Reception Photo',
            ];

            foreach ($photoTypes as $photoType => $label):
                $currentPhoto = $branchData[$photoType] ?? '';
                $photoUrl = '';
                if (!empty($currentPhoto)) {
                    $photoUrl = $bp . '/' . $currentPhoto;
                }
            ?>
            <div style="border:2px dashed #ddd;border-radius:10px;padding:20px;text-align:center;">
                <h4 style="margin:0 0 12px 0;font-size:15px;"><?= htmlspecialchars($label, ENT_QUOTES, 'UTF-8') ?></h4>

                <?php if (!empty($photoUrl)): ?>
                <div style="margin-bottom:12px;">
                    <img src="<?= htmlspecialchars($photoUrl, ENT_QUOTES, 'UTF-8') ?>"
                         alt="<?= htmlspecialchars($label, ENT_QUOTES, 'UTF-8') ?>"
                         style="max-width:100%;max-height:200px;border-radius:8px;object-fit:cover;">
                </div>
                <?php else: ?>
                <div style="margin-bottom:12px;padding:40px;background:#f5f5f5;border-radius:8px;">
                    <svg viewBox="0 0 24 24" fill="none" stroke="#ccc" stroke-width="1.5" width="48" height="48">
                        <rect x="3" y="3" width="18" height="18" rx="2" ry="2"/>
                        <circle cx="8.5" cy="8.5" r="1.5"/>
                        <polyline points="21 15 16 10 5 21"/>
                    </svg>
                    <p style="color:#aaa;margin:8px 0 0;font-size:13px;">No photo uploaded</p>
                </div>
                <?php endif; ?>

                <form method="POST" action="<?= $bp ?>/branch/profile/photo" enctype="multipart/form-data">
                    <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($csrfToken ?? '', ENT_QUOTES, 'UTF-8') ?>">
                    <input type="hidden" name="photo_type" value="<?= htmlspecialchars($photoType, ENT_QUOTES, 'UTF-8') ?>">
                    <div class="admin-form-group" style="margin-bottom:8px;">
                        <input type="file" name="photo" accept="image/jpeg,image/png,image/gif,image/webp"
                               class="admin-form-input" style="font-size:13px;">
                    </div>
                    <small style="color:#888;display:block;margin-bottom:8px;">JPG, PNG, GIF, or WebP. Max 5MB.</small>
                    <button type="submit" class="admin-btn admin-btn-sm admin-btn-primary">Upload <?= htmlspecialchars($label, ENT_QUOTES, 'UTF-8') ?></button>
                </form>
            </div>
            <?php endforeach; ?>
        </div>
    </div>
</div>
