<?php
/**
 * Branch Profile View — view + edit branch's public profile.
 *
 * Branch admin can update: phone, address, pincode, opening/closing times,
 * running_since, map_link, lat/lng, description, GST/PAN, facilities, photos.
 *
 * Cannot change: name, branch_code, city, state, owner_name, owner_email,
 * wallet_balance, status, plan_id, admin_user_id, referral_code — these
 * are Super Admin-only fields.
 */
$bp = defined('BASE_PATH') ? BASE_PATH : '/gmind';
$branch = $branch ?? [];
$facilities = $facilities ?? [];
$commonFacilities = $commonFacilities ?? [];
$availableFacilities = $availableFacilities ?? [];
?>
<div class="admin-section">
    <div class="admin-card" style="background:linear-gradient(135deg,#16a085,#117a65);color:#fff;border-radius:12px;padding:20px;margin-bottom:20px;">
        <h2 style="margin:0;font-size:1.2rem;"><?= htmlspecialchars($branch['name'] ?? '', ENT_QUOTES, 'UTF-8') ?></h2>
        <p style="margin:4px 0 0;opacity:0.9;font-size:0.88rem;">
            Code: <code style="background:rgba(255,255,255,0.25);padding:2px 8px;border-radius:4px;"><?= htmlspecialchars($branch['branch_code'] ?? '', ENT_QUOTES, 'UTF-8') ?></code>
            · <?= htmlspecialchars($branch['city'] ?? '', ENT_QUOTES, 'UTF-8') ?>, <?= htmlspecialchars($branch['state'] ?? '', ENT_QUOTES, 'UTF-8') ?>
        </p>
        <p style="margin:6px 0 0;font-size:0.78rem;opacity:0.85;">
            Note: Name, branch code, city, state, owner, wallet, status can only be changed by Super Admin.
        </p>
    </div>

    <form method="POST" action="<?= $bp ?>/branch/profile" enctype="multipart/form-data">
        <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($csrfToken ?? '', ENT_QUOTES, 'UTF-8') ?>">

        <!-- Contact & Location -->
        <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);">
            <h3 style="margin:0 0 16px;font-size:1.05rem;color:#0d3b3b;">Contact & Location</h3>
            <div style="display:grid;grid-template-columns:repeat(auto-fit,minmax(220px,1fr));gap:14px;">
                <div>
                    <label style="display:block;font-size:0.85rem;font-weight:600;color:#0d3b3b;margin-bottom:6px;">Branch 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:#0d3b3b;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>
                    <label style="display:block;font-size:0.85rem;font-weight:600;color:#0d3b3b;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:#0d3b3b;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>
            <div style="margin-top:14px;">
                <label style="display:block;font-size:0.85rem;font-weight:600;color:#0d3b3b;margin-bottom:6px;">Full Address</label>
                <textarea name="address" 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:14px;margin-top:14px;">
                <div>
                    <label style="display:block;font-size:0.85rem;font-weight:600;color:#0d3b3b;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>
                    <label style="display:block;font-size:0.85rem;font-weight:600;color:#0d3b3b;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:#0d3b3b;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>
                    <label style="display:block;font-size:0.85rem;font-weight:600;color:#0d3b3b;margin-bottom:6px;">Running Since (Year)</label>
                    <input type="number" min="1990" max="<?= date('Y') ?>" name="running_since" value="<?= htmlspecialchars($branch['running_since'] ?? '', ENT_QUOTES, 'UTF-8') ?>" style="width:100%;padding:10px 12px;border:1px solid #ddd;border-radius:6px;font-size:0.88rem;">
                </div>
            </div>
        </div>

        <!-- About / Description -->
        <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);">
            <h3 style="margin:0 0 16px;font-size:1.05rem;color:#0d3b3b;">About This Branch</h3>
            <textarea name="description" rows="5" placeholder="Tell students about your branch — what makes it special, achievements, teaching methodology, etc. This will be displayed on your public branch profile page." style="width:100%;padding:10px 12px;border:1px solid #ddd;border-radius:6px;font-size:0.88rem;resize:vertical;"><?= htmlspecialchars($branch['description'] ?? '', ENT_QUOTES, 'UTF-8') ?></textarea>
        </div>

        <!-- Facilities -->
        <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);">
            <h3 style="margin:0 0 6px;font-size:1.05rem;color:#0d3b3b;">Facilities Available</h3>
            <p style="margin:0 0 16px;font-size:0.82rem;color:#666;">Tick all the facilities your branch offers. These will be displayed on your public profile.</p>
            <div style="display:grid;grid-template-columns:repeat(auto-fill,minmax(180px,1fr));gap:8px;">
                <?php foreach ($commonFacilities as $f): ?>
                    <label style="display:flex;align-items:center;gap:8px;padding:8px 10px;background:#fafafa;border-radius:6px;cursor:pointer;font-size:0.84rem;">
                        <input type="checkbox" name="facilities[<?= htmlspecialchars($f, ENT_QUOTES, 'UTF-8') ?>]" value="1" <?= in_array($f, $availableFacilities, true) ? 'checked' : '' ?> style="width:16px;height:16px;accent-color:#16a085;">
                        <span><?= htmlspecialchars($f, ENT_QUOTES, 'UTF-8') ?></span>
                    </label>
                <?php endforeach; ?>
            </div>
            <div style="margin-top:14px;">
                <label style="display:block;font-size:0.85rem;font-weight:600;color:#0d3b3b;margin-bottom:6px;">Other Facilities (comma-separated)</label>
                <input type="text" name="custom_facilities" placeholder="e.g. Smart Board, AC Lab, Hostel" style="width:100%;padding:10px 12px;border:1px solid #ddd;border-radius:6px;font-size:0.88rem;">
            </div>
        </div>

        <!-- Tax Info -->
        <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);">
            <h3 style="margin:0 0 16px;font-size:1.05rem;color:#0d3b3b;">Tax Information (for receipts)</h3>
            <div style="display:grid;grid-template-columns:1fr 1fr;gap:14px;">
                <div>
                    <label style="display:block;font-size:0.85rem;font-weight:600;color:#0d3b3b;margin-bottom:6px;">GST Number</label>
                    <input type="text" name="gst_number" value="<?= htmlspecialchars($branch['gst_number'] ?? '', ENT_QUOTES, 'UTF-8') ?>" style="width:100%;padding:10px 12px;border:1px solid #ddd;border-radius:6px;font-size:0.88rem;font-family:monospace;">
                </div>
                <div>
                    <label style="display:block;font-size:0.85rem;font-weight:600;color:#0d3b3b;margin-bottom:6px;">PAN Number</label>
                    <input type="text" name="pan_number" value="<?= htmlspecialchars($branch['pan_number'] ?? '', ENT_QUOTES, 'UTF-8') ?>" style="width:100%;padding:10px 12px;border:1px solid #ddd;border-radius:6px;font-size:0.88rem;font-family:monospace;">
                </div>
            </div>
        </div>

        <!-- Photos -->
        <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);">
            <h3 style="margin:0 0 16px;font-size:1.05rem;color:#0d3b3b;">Branch Photos</h3>
            <p style="margin:0 0 14px;font-size:0.82rem;color:#666;">Upload photos of your branch. These will be displayed on your public profile.</p>
            <div style="display:grid;grid-template-columns:repeat(auto-fit,minmax(180px,1fr));gap:14px;">
                <?php
                $photoFields = [
                    'image'          => 'Branch Logo',
                    'photo_front'    => 'Front View',
                    'photo_back'     => 'Back View',
                    'photo_class'    => 'Classroom',
                    'photo_reception'=> 'Reception',
                ];
                foreach ($photoFields as $field => $label):
                    $current = $branch[$field] ?? '';
                ?>
                <div>
                    <label style="display:block;font-size:0.85rem;font-weight:600;color:#0d3b3b;margin-bottom:6px;"><?= $label ?></label>
                    <?php if (!empty($current)): ?>
                        <img src="<?= $bp ?>/<?= htmlspecialchars($current, ENT_QUOTES, 'UTF-8') ?>" alt="<?= $label ?>" style="width:100%;height:120px;object-fit:cover;border-radius:6px;border:1px solid #ddd;margin-bottom:6px;">
                    <?php else: ?>
                        <div style="width:100%;height:120px;background:#f0f0f0;border-radius:6px;display:flex;align-items:center;justify-content:center;color:#bbb;font-size:0.78rem;margin-bottom:6px;">No photo</div>
                    <?php endif; ?>
                    <input type="file" name="<?= $field ?>" accept="image/jpeg,image/png,image/gif,image/webp" style="width:100%;font-size:0.78rem;">
                </div>
                <?php endforeach; ?>
            </div>
        </div>

        <!-- Submit -->
        <div style="display:flex;gap:12px;justify-content:flex-end;">
            <a href="<?= $bp ?>/branch/dashboard" 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,#16a085,#117a65);color:#fff;border:none;border-radius:8px;font-weight:600;font-size:0.9rem;cursor:pointer;font-family:'Poppins',sans-serif;">Save Profile</button>
        </div>
    </form>
</div>
