<?php
/**
 * Public Branch Profile Page — like a hotel/business listing
 *
 * Shows: hero photo, about, facilities, faculty, courses, gallery, map,
 * reviews, contact, social links, opening hours.
 */
$bp = defined('BASE_PATH') ? BASE_PATH : '/gmind';
$b = $branch ?? [];
$facilities = $facilities ?? [];
$faculty = $faculty ?? [];
$socialLinks = $socialLinks ?? [];
$courses = $courses ?? [];
$reviews = $reviews ?? [];
$avgRating = $avgRating ?? 0;
$totalReviews = $totalReviews ?? 0;

$facilityIcons = [
    'AC Classrooms' => '❄️', 'WiFi' => '📶', 'Computer Lab' => '💻', 'Library' => '📚',
    'Parking' => '🅿️', 'CCTV Surveillance' => '📹', 'Drinking Water' => '💧',
    'Washroom' => '🚻', 'Projector' => '📽️', 'Generator Backup' => '⚡',
    'Canteen' => '🍽️', 'Whiteboard' => '📋', 'Audio System' => '🔊',
    'Study Room' => '📖', 'Reception Area' => '🏢',
];

// Build gallery photos
$galleryPhotos = array_filter([
    $b['photo_front'] ?? null,
    $b['photo_back'] ?? null,
    $b['photo_class'] ?? null,
    $b['photo_reception'] ?? null,
]);
?>
<style>
.bi-hero { position:relative; height:360px; border-radius:0 0 20px 20px; overflow:hidden; background:#0d2b6e; }
.bi-hero img { width:100%; height:100%; object-fit:cover; opacity:0.7; }
.bi-hero-overlay { position:absolute; bottom:0; left:0; right:0; background:linear-gradient(transparent, rgba(0,0,0,0.8)); padding:40px 20px 24px; }
.bi-hero h1 { color:#fff; font-family:'Poppins',sans-serif; font-size:2rem; margin:0; }
.bi-hero p { color:rgba(255,255,255,0.85); margin:4px 0 0; font-size:0.95rem; }
.bi-trusted-badge { display:inline-block; background:#f39c12; color:#fff; padding:4px 14px; border-radius:20px; font-size:0.78rem; font-weight:700; margin-bottom:8px; }
.bi-container { max-width:960px; margin:0 auto; padding:24px 16px; }
.bi-grid { display:grid; grid-template-columns:2fr 1fr; gap:24px; margin-top:20px; }
@media (max-width:768px) { .bi-grid { grid-template-columns:1fr; } }
.bi-card { background:#fff; border-radius:12px; padding:20px; box-shadow:0 2px 12px rgba(0,0,0,0.06); margin-bottom:16px; }
.bi-card h2 { font-family:'Poppins',sans-serif; font-size:1.15rem; color:#0d2b6e; margin:0 0 12px; padding-bottom:8px; border-bottom:2px solid #eaf0fb; }
.bi-facility { display:inline-flex; align-items:center; gap:6px; background:#eaf0fb; color:#1a4fa0; padding:6px 12px; border-radius:8px; font-size:0.82rem; font-weight:500; margin:4px; }
.bi-faculty-card { display:flex; gap:12px; align-items:center; padding:12px; background:#f9f9f9; border-radius:8px; margin-bottom:8px; }
.bi-faculty-card img { width:50px; height:50px; border-radius:50%; object-fit:cover; }
.bi-course-card { padding:12px; background:#f9f9f9; border-radius:8px; margin-bottom:8px; display:flex; justify-content:space-between; align-items:center; }
.bi-contact-item { display:flex; align-items:center; gap:8px; padding:6px 0; font-size:0.86rem; color:#666; }
.bi-contact-item svg { flex-shrink:0; color:#1a4fa0; }
.bi-social { display:flex; gap:8px; margin-top:10px; }
.bi-social a { width:36px; height:36px; border-radius:50%; background:#eaf0fb; display:flex; align-items:center; justify-content:center; text-decoration:none; color:#1a4fa0; font-size:1.1rem; transition:background 0.2s; }
.bi-social a:hover { background:#1a4fa0; color:#fff; }
.bi-gallery { display:grid; grid-template-columns:repeat(2,1fr); gap:8px; }
.bi-gallery img { width:100%; height:120px; object-fit:cover; border-radius:8px; }
.bi-review { padding:12px; background:#f9f9f9; border-radius:8px; margin-bottom:8px; }
.bi-stars { color:#f39c12; font-size:0.85rem; }
</style>

<!-- Hero -->
<div class="bi-hero">
    <?php if (!empty($b['photo_front'])): ?>
    <img src="<?= $bp ?>/<?= htmlspecialchars($b['photo_front'], ENT_QUOTES, 'UTF-8') ?>" alt="<?= htmlspecialchars($b['name'] ?? '', ENT_QUOTES, 'UTF-8') ?>">
    <?php endif; ?>
    <div class="bi-hero-overlay">
        <?php if (!empty($b['is_trusted'])): ?>
        <div class="bi-trusted-badge">⭐ TRUSTED BRANCH</div>
        <?php endif; ?>
        <h1><?= htmlspecialchars($b['name'] ?? 'Branch', ENT_QUOTES, 'UTF-8') ?></h1>
        <p>
            <?php if (!empty($b['branch_code'])): ?><code style="background:rgba(255,255,255,0.2);padding:2px 8px;border-radius:4px;color:#fff;"><?= htmlspecialchars($b['branch_code'], ENT_QUOTES, 'UTF-8') ?></code> · <?php endif; ?>
            <?= htmlspecialchars($b['address'] ?? '', ENT_QUOTES, 'UTF-8') ?>, <?= htmlspecialchars($b['city'] ?? '', ENT_QUOTES, 'UTF-8') ?>, <?= htmlspecialchars($b['state'] ?? '', ENT_QUOTES, 'UTF-8') ?>
            <?php if (!empty($b['pincode'])): ?> - <?= htmlspecialchars($b['pincode'], ENT_QUOTES, 'UTF-8') ?><?php endif; ?>
        </p>
    </div>
</div>

<div class="bi-container">
<div class="bi-grid">

<!-- LEFT: Main Content -->
<div>
    <!-- About -->
    <?php if (!empty($b['description'])): ?>
    <div class="bi-card">
        <h2>📍 About This Center</h2>
        <p style="font-size:0.9rem; line-height:1.7; color:#555; margin:0;"><?= nl2br(htmlspecialchars($b['description'], ENT_QUOTES, 'UTF-8')) ?></p>
    </div>
    <?php endif; ?>

    <!-- Facilities -->
    <?php if (!empty($facilities)): ?>
    <div class="bi-card">
        <h2>🏫 Facilities Available</h2>
        <div>
            <?php foreach ($facilities as $f): ?>
            <span class="bi-facility"><?= $facilityIcons[$f['facility_name']] ?? '✓' ?> <?= htmlspecialchars($f['facility_name'], ENT_QUOTES, 'UTF-8') ?></span>
            <?php endforeach; ?>
        </div>
    </div>
    <?php endif; ?>

    <!-- Courses Offered -->
    <?php if (!empty($courses)): ?>
    <div class="bi-card">
        <h2>📚 Courses Offered</h2>
        <?php foreach ($courses as $c):
            $fee = (float) ($c['fee_amount'] ?? 0);
            $discount = (float) ($c['discount_percentage'] ?? 0);
            $finalFee = $fee - ($fee * $discount / 100);
        ?>
        <div class="bi-course-card">
            <div>
                <div style="font-weight:600; color:#0d2b6e; font-size:0.9rem;"><?= htmlspecialchars($c['name'], ENT_QUOTES, 'UTF-8') ?></div>
                <div style="font-size:0.76rem; color:#999; margin-top:2px;"><?= htmlspecialchars($c['duration'] ?? '', ENT_QUOTES, 'UTF-8') ?> · <?= ucfirst($c['category'] ?? 'computer') ?></div>
            </div>
            <div style="text-align:right;">
                <?php if ($fee > 0): ?>
                    <?php if ($discount > 0): ?><span style="font-size:0.74rem; color:#999; text-decoration:line-through;">₹<?= number_format($fee, 0) ?></span><br><?php endif; ?>
                    <span style="font-weight:700; color:#1a4fa0; font-size:0.92rem;">₹<?= number_format($finalFee, 0) ?></span>
                <?php else: ?>
                    <span style="color:#999; font-size:0.82rem;">Contact for fee</span>
                <?php endif; ?>
            </div>
        </div>
        <?php endforeach; ?>
    </div>
    <?php endif; ?>

    <!-- Faculty -->
    <?php if (!empty($faculty)): ?>
    <div class="bi-card">
        <h2>👨‍🏫 Our Faculty</h2>
        <?php foreach ($faculty as $f): ?>
        <div class="bi-faculty-card">
            <?php if (!empty($f['photo'])): ?>
            <img src="<?= $bp ?>/<?= htmlspecialchars($f['photo'], ENT_QUOTES, 'UTF-8') ?>" alt="<?= htmlspecialchars($f['name'], ENT_QUOTES, 'UTF-8') ?>">
            <?php else: ?>
            <div style="width:50px; height:50px; border-radius:50%; background:#eaf0fb; display:flex; align-items:center; justify-content:center; font-size:1.2rem; color:#1a4fa0;"><?= strtoupper(substr($f['name'] ?? '?', 0, 1)) ?></div>
            <?php endif; ?>
            <div>
                <div style="font-weight:600; color:#0d2b6e; font-size:0.88rem;"><?= htmlspecialchars($f['name'], ENT_QUOTES, 'UTF-8') ?></div>
                <div style="font-size:0.76rem; color:#999;"><?= htmlspecialchars($f['designation'] ?? '', ENT_QUOTES, 'UTF-8') ?><?= !empty($f['qualification']) ? ' · ' . htmlspecialchars($f['qualification'], ENT_QUOTES, 'UTF-8') : '' ?></div>
                <?php if (!empty($f['specialization'])): ?><div style="font-size:0.72rem; color:#1a4fa0; margin-top:2px;"><?= htmlspecialchars($f['specialization'], ENT_QUOTES, 'UTF-8') ?></div><?php endif; ?>
            </div>
        </div>
        <?php endforeach; ?>
    </div>
    <?php endif; ?>

    <!-- Gallery -->
    <?php if (!empty($galleryPhotos)): ?>
    <div class="bi-card">
        <h2>📷 Center Gallery</h2>
        <div class="bi-gallery">
            <?php foreach ($galleryPhotos as $photo): ?>
            <img src="<?= $bp ?>/<?= htmlspecialchars($photo, ENT_QUOTES, 'UTF-8') ?>" alt="Gallery" style="cursor:pointer;" onclick="window.open(this.src, '_blank')">
            <?php endforeach; ?>
        </div>
    </div>
    <?php endif; ?>

    <!-- Reviews -->
    <?php if (!empty($reviews)): ?>
    <div class="bi-card">
        <h2>⭐ Reviews (<?= $totalReviews ?>) · Rating: <?= $avgRating ?>/5</h2>
        <?php foreach ($reviews as $r): ?>
        <div class="bi-review">
            <div style="display:flex; justify-content:space-between; align-items:center; margin-bottom:4px;">
                <strong style="color:#0d2b6e; font-size:0.86rem;"><?= htmlspecialchars($r['student_name'] ?? $r['user_name'] ?? 'Anonymous', ENT_QUOTES, 'UTF-8') ?></strong>
                <span class="bi-stars"><?= str_repeat('★', (int) $r['rating']) ?><?= str_repeat('☆', 5 - (int) $r['rating']) ?></span>
            </div>
            <p style="font-size:0.82rem; color:#666; margin:0; line-height:1.5;"><?= htmlspecialchars($r['review'] ?? '', ENT_QUOTES, 'UTF-8') ?></p>
        </div>
        <?php endforeach; ?>
    </div>
    <?php endif; ?>
</div>

<!-- RIGHT: Sidebar -->
<div>
    <!-- Contact -->
    <div class="bi-card">
        <h2>📞 Contact</h2>
        <?php if (!empty($b['phone'])): ?>
        <div class="bi-contact-item">
            <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M22 16.92v3a2 2 0 01-2.18 2 19.79 19.79 0 01-8.63-3.07 19.5 19.5 0 01-6-6 19.79 19.79 0 01-3.07-8.67A2 2 0 014.11 2h3a2 2 0 012 1.72c.127.96.361 1.903.7 2.81a2 2 0 01-.45 2.11L8.09 9.91a16 16 0 006 6l1.27-1.27a2 2 0 012.11-.45c.907.339 1.85.573 2.81.7A2 2 0 0122 16.92z"/></svg>
            <a href="tel:<?= htmlspecialchars($b['phone'], ENT_QUOTES, 'UTF-8') ?>" style="color:#1a4fa0; text-decoration:none;"><?= htmlspecialchars($b['phone'], ENT_QUOTES, 'UTF-8') ?></a>
        </div>
        <?php endif; ?>
        <?php if (!empty($b['owner_email'])): ?>
        <div class="bi-contact-item">
            <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M4 4h16c1.1 0 2 .9 2 2v12c0 1.1-.9 2-2 2H4c-1.1 0-2-.9-2-2V6c0-1.1.9-2 2-2z"/><polyline points="22,6 12,13 2,6"/></svg>
            <a href="mailto:<?= htmlspecialchars($b['owner_email'], ENT_QUOTES, 'UTF-8') ?>" style="color:#1a4fa0; text-decoration:none; font-size:0.82rem;"><?= htmlspecialchars($b['owner_email'], ENT_QUOTES, 'UTF-8') ?></a>
        </div>
        <?php endif; ?>
        <div class="bi-contact-item">
            <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M21 10c0 7-9 13-9 13s-9-6-9-13a9 9 0 0118 0z"/><circle cx="12" cy="10" r="3"/></svg>
            <span style="font-size:0.82rem;"><?= htmlspecialchars($b['address'] ?? '', ENT_QUOTES, 'UTF-8') ?></span>
        </div>

        <!-- Opening Hours -->
        <?php if (!empty($b['opening_time'])): ?>
        <div class="bi-contact-item">
            <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><circle cx="12" cy="12" r="10"/><polyline points="12 6 12 12 16 14"/></svg>
            <span style="font-size:0.82rem;"><strong>Hours:</strong> <?= htmlspecialchars($b['opening_time'], ENT_QUOTES, 'UTF-8') ?> - <?= htmlspecialchars($b['closing_time'] ?? '', ENT_QUOTES, 'UTF-8') ?></span>
        </div>
        <?php endif; ?>

        <!-- Running Since -->
        <?php if (!empty($b['running_since'])): ?>
        <div class="bi-contact-item">
            <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M12 2L2 7l10 5 10-5-10-5z"/><path d="M2 17l10 5 10-5"/></svg>
            <span style="font-size:0.82rem;"><strong>Since:</strong> <?= htmlspecialchars($b['running_since'], ENT_QUOTES, 'UTF-8') ?></span>
        </div>
        <?php endif; ?>

        <!-- Map -->
        <?php if (!empty($b['map_link'])): ?>
        <a href="<?= htmlspecialchars($b['map_link'], ENT_QUOTES, 'UTF-8') ?>" target="_blank" style="display:block; margin-top:10px; padding:8px; background:#eaf0fb; color:#1a4fa0; text-align:center; border-radius:8px; text-decoration:none; font-size:0.82rem; font-weight:600;">
            📍 View on Google Maps
        </a>
        <?php endif; ?>

        <!-- Social Links -->
        <?php if (!empty($socialLinks)): ?>
        <div class="bi-social">
            <?php foreach ($socialLinks as $sl):
                $platformIcons = ['facebook'=>'📘','twitter'=>'🐦','instagram'=>'📷','youtube'=>'▶️','linkedin'=>'💼','whatsapp'=>'💬','website'=>'🌐'];
            ?>
            <a href="<?= htmlspecialchars($sl['url'], ENT_QUOTES, 'UTF-8') ?>" target="_blank" title="<?= htmlspecialchars($sl['platform'], ENT_QUOTES, 'UTF-8') ?>"><?= $platformIcons[$sl['platform']] ?? '🔗' ?></a>
            <?php endforeach; ?>
        </div>
        <?php endif; ?>
    </div>

    <!-- Quick Stats -->
    <div class="bi-card" style="text-align:center;">
        <h2>📊 Quick Stats</h2>
        <div style="display:grid; grid-template-columns:1fr 1fr; gap:12px;">
            <div><div style="font-size:1.5rem; font-weight:700; color:#1a4fa0;"><?= (int)($b['total_students'] ?? 0) ?></div><div style="font-size:0.72rem; color:#999;">Students</div></div>
            <div><div style="font-size:1.5rem; font-weight:700; color:#27ae60;"><?= (int)($b['total_certificates'] ?? 0) ?></div><div style="font-size:0.72rem; color:#999;">Certificates</div></div>
        </div>
    </div>

    <!-- Apply Now -->
    <div class="bi-card" style="text-align:center; background:linear-gradient(135deg,#1a4fa0,#0d2b6e); color:#fff;">
        <h3 style="margin:0 0 8px; color:#fff; font-family:'Poppins',sans-serif;">Ready to Join?</h3>
        <p style="font-size:0.82rem; opacity:0.85; margin:0 0 12px;">Start your career with <?= htmlspecialchars($b['name'] ?? 'Gmind Institute', ENT_QUOTES, 'UTF-8') ?></p>
        <a href="<?= $bp ?>/admission" style="display:inline-block; padding:10px 28px; background:#fff; color:#1a4fa0; border-radius:8px; text-decoration:none; font-weight:700; font-size:0.9rem;">Apply Now</a>
    </div>
</div>

</div>
</div>
