<?php
/**
 * Emergency & Essential Services Page
 * Displays quick-access emergency contact information and essential services
 * Version: 1.0.0
 * Developer: SolutionJunction.in
 */

require_once '../../includes/config.php';
require_once '../../includes/db.php';
require_once '../../includes/session.php';
require_once '../../includes/functions.php';

Session::start();

$db = Database::getInstance();

// Fetch emergency services
$services = $db->fetchAll(
    "SELECT * FROM " . TBL_ESSENTIAL_SERVICES . "
     ORDER BY is_emergency DESC, name ASC"
);

// Define emergency hotline data with priorities
$emergency_hotlines = [
    ['name' => 'Police', 'number' => '100', 'description' => 'Crime Prevention & Law Enforcement', 'icon' => 'shield-alt', 'color' => '#dc3545', 'priority' => 1],
    ['name' => 'Ambulance', 'number' => '102', 'description' => 'Medical Emergency & First Aid', 'icon' => 'ambulance', 'color' => '#dc3545', 'priority' => 1],
    ['name' => 'Fire Brigade', 'number' => '101', 'description' => 'Fire & Rescue Services', 'icon' => 'fire', 'color' => '#ff6b35', 'priority' => 1],
    ['name' => 'Women Helpline', 'number' => '1091', 'description' => 'Women Safety & Domestic Violence', 'icon' => 'people-carry', 'color' => '#dc3545', 'priority' => 2],
    ['name' => 'Child Helpline', 'number' => '1098', 'description' => 'Child Care & Abuse Prevention', 'icon' => 'child', 'color' => '#007bff', 'priority' => 2],
    ['name' => 'Railway Police', 'number' => '139', 'description' => 'Railway Crime & Security', 'icon' => 'train', 'color' => '#0B6B57', 'priority' => 2],
    ['name' => 'Disaster Management', 'number' => '1077', 'description' => 'Natural Disaster Response', 'icon' => 'exclamation-triangle', 'color' => '#ff9800', 'priority' => 3],
    ['name' => 'Electricity', 'number' => '1912', 'description' => 'Power Supply Issues & Faults', 'icon' => 'bolt', 'color' => '#ffc107', 'priority' => 3],
    ['name' => 'Water Supply', 'number' => '1916', 'description' => 'Water & Sanitation Services', 'icon' => 'droplet', 'color' => '#17a2b8', 'priority' => 3],
];

include '../../includes/header.php';
?>

<div class="container-fluid py-5" style="background: linear-gradient(135deg, #dc3545 0%, #c82333 100%);">
    <div class="container text-center text-white">
        <h1 class="mb-3" style="font-size: 3rem; font-weight: 700;">
            <i class="fas fa-siren me-3"></i> Emergency & Essential Services
        </h1>
        <p class="lead mb-0" style="font-size: 1.2rem; opacity: 0.95;">
            Important contact numbers and emergency services available 24/7 in Jharkhand
        </p>
        <p class="small mt-2" style="opacity: 0.8;">
            <i class="fas fa-clock me-2"></i> All services available round-the-clock
        </p>
    </div>
</div>

<div class="container-fluid py-5" style="background: #f8f9fa;">
    <div class="container">
        <!-- Critical Emergency Services -->
        <div class="mb-5">
            <h3 class="mb-4" style="color: #18352E; font-weight: 700;">
                <i class="fas fa-exclamation-circle me-2" style="color: #dc3545;"></i> Critical Emergency Services
            </h3>
            <div class="row g-3 mb-5">
                <?php
                $critical = array_filter($emergency_hotlines, function($s) { return $s['priority'] === 1; });
                foreach ($critical as $service):
                ?>
                    <div class="col-md-6 col-lg-4">
                        <div class="card h-100 shadow-sm border-0 hover-emergency" style="transition: all 0.3s ease; background: white;">
                            <div style="height: 120px; background: linear-gradient(135deg, <?php echo $service['color']; ?> 0%, <?php echo adjustColor($service['color'], -20); ?> 100%); display: flex; align-items: center; justify-content: center;">
                                <i class="fas fa-<?php echo $service['icon']; ?>" style="font-size: 3rem; color: white;"></i>
                            </div>
                            <div class="card-body text-center">
                                <h5 class="card-title mb-2" style="color: #18352E; font-weight: 700;">
                                    <?php echo escape($service['name']); ?>
                                </h5>
                                <p class="card-text text-muted small mb-3">
                                    <?php echo escape($service['description']); ?>
                                </p>
                                <a href="tel:<?php echo escape($service['number']); ?>" 
                                   class="btn btn-lg w-100 mb-2"
                                   style="background: <?php echo $service['color']; ?>; color: white; border: none; font-weight: 700; font-size: 1.5rem; padding: 15px;">
                                    <i class="fas fa-phone me-2"></i> <?php echo escape($service['number']); ?>
                                </a>
                                <small class="text-muted d-block">
                                    <i class="fas fa-info-circle me-1"></i> Dial & Press 1 to connect
                                </small>
                            </div>
                        </div>
                    </div>
                <?php endforeach; ?>
            </div>
        </div>

        <!-- Other Important Services -->
        <div class="mb-5">
            <h3 class="mb-4" style="color: #18352E; font-weight: 700;">
                <i class="fas fa-phone-alt me-2" style="color: #0B6B57;"></i> Important Services
            </h3>
            <div class="row g-3 mb-5">
                <?php
                $important = array_filter($emergency_hotlines, function($s) { return $s['priority'] === 2; });
                foreach ($important as $service):
                ?>
                    <div class="col-md-6 col-lg-4">
                        <div class="card h-100 shadow-sm border-0 hover-service" style="transition: all 0.3s ease; background: white;">
                            <div style="height: 100px; background: linear-gradient(135deg, <?php echo $service['color']; ?> 0%, <?php echo adjustColor($service['color'], -15); ?> 100%); display: flex; align-items: center; justify-content: center;">
                                <i class="fas fa-<?php echo $service['icon']; ?>" style="font-size: 2.5rem; color: white;"></i>
                            </div>
                            <div class="card-body text-center">
                                <h6 class="card-title mb-2" style="color: #18352E; font-weight: 600;">
                                    <?php echo escape($service['name']); ?>
                                </h6>
                                <p class="card-text text-muted small mb-3">
                                    <?php echo escape($service['description']); ?>
                                </p>
                                <a href="tel:<?php echo escape($service['number']); ?>" 
                                   class="btn w-100"
                                   style="background: <?php echo $service['color']; ?>; color: white; border: none; font-weight: 600;">
                                    <i class="fas fa-phone me-1"></i> <?php echo escape($service['number']); ?>
                                </a>
                            </div>
                        </div>
                    </div>
                <?php endforeach; ?>
            </div>
        </div>

        <!-- Utility Services -->
        <div class="mb-5">
            <h3 class="mb-4" style="color: #18352E; font-weight: 700;">
                <i class="fas fa-wrench me-2" style="color: #0B6B57;"></i> Utility & Support Services
            </h3>
            <div class="row g-3">
                <?php
                $utility = array_filter($emergency_hotlines, function($s) { return $s['priority'] === 3; });
                foreach ($utility as $service):
                ?>
                    <div class="col-md-6 col-lg-4">
                        <div class="card h-100 shadow-sm border-0 hover-service" style="transition: all 0.3s ease; background: white;">
                            <div style="height: 100px; background: linear-gradient(135deg, <?php echo $service['color']; ?> 0%, <?php echo adjustColor($service['color'], -15); ?> 100%); display: flex; align-items: center; justify-content: center;">
                                <i class="fas fa-<?php echo $service['icon']; ?>" style="font-size: 2.5rem; color: white;"></i>
                            </div>
                            <div class="card-body text-center">
                                <h6 class="card-title mb-2" style="color: #18352E; font-weight: 600;">
                                    <?php echo escape($service['name']); ?>
                                </h6>
                                <p class="card-text text-muted small mb-3">
                                    <?php echo escape($service['description']); ?>
                                </p>
                                <a href="tel:<?php echo escape($service['number']); ?>" 
                                   class="btn w-100"
                                   style="background: <?php echo $service['color']; ?>; color: white; border: none; font-weight: 600;">
                                    <i class="fas fa-phone me-1"></i> <?php echo escape($service['number']); ?>
                                </a>
                            </div>
                        </div>
                    </div>
                <?php endforeach; ?>
            </div>
        </div>

        <!-- Database Services (if any exist) -->
        <?php if (!empty($services)): ?>
            <div class="mt-5 pt-5 border-top">
                <h3 class="mb-4" style="color: #18352E; font-weight: 700;">
                    <i class="fas fa-briefcase me-2" style="color: #0B6B57;"></i> Additional Services
                </h3>
                <div class="row g-3">
                    <?php foreach ($services as $service): ?>
                        <div class="col-md-6 col-lg-4">
                            <div class="card h-100 shadow-sm border-0" style="background: white;">
                                <div class="card-body">
                                    <h6 class="card-title" style="color: #18352E; font-weight: 600;">
                                        <?php echo escape($service['name']); ?>
                                    </h6>
                                    <?php if ($service['description']): ?>
                                        <p class="card-text text-muted small mb-3">
                                            <?php echo escape($service['description']); ?>
                                        </p>
                                    <?php endif; ?>
                                    <div class="mb-2">
                                        <a href="tel:<?php echo escape($service['phone']); ?>" 
                                           class="btn btn-sm w-100 mb-2"
                                           style="background: #0B6B57; color: white; border: none;">
                                            <i class="fas fa-phone me-1"></i> <?php echo escape($service['phone']); ?>
                                        </a>
                                        <?php if ($service['email']): ?>
                                            <a href="mailto:<?php echo escape($service['email']); ?>" 
                                               class="btn btn-sm w-100 mb-2"
                                               style="background: #074a3d; color: white; border: none;">
                                                <i class="fas fa-envelope me-1"></i> Email
                                            </a>
                                        <?php endif; ?>
                                    </div>
                                    <?php if ($service['address']): ?>
                                        <small class="text-muted d-block">
                                            <i class="fas fa-map-pin me-1"></i> <?php echo escape($service['address']); ?>
                                        </small>
                                    <?php endif; ?>
                                </div>
                            </div>
                        </div>
                    <?php endforeach; ?>
                </div>
            </div>
        <?php endif; ?>

        <!-- Information Section -->
        <div class="row mt-5 pt-5 border-top">
            <div class="col-lg-6 mb-4">
                <div class="card shadow-sm" style="border: none; border-left: 4px solid #0B6B57;">
                    <div class="card-body">
                        <h6 class="card-title mb-3" style="color: #18352E; font-weight: 700;">
                            <i class="fas fa-lightbulb me-2" style="color: #0B6B57;"></i> Emergency Tips
                        </h6>
                        <ul class="list-unstyled small text-muted" style="line-height: 2;">
                            <li><i class="fas fa-check me-2" style="color: #0B6B57;"></i> <strong>Stay Calm</strong> - Speak clearly to the operator</li>
                            <li><i class="fas fa-check me-2" style="color: #0B6B57;"></i> <strong>Provide Location</strong> - Tell your exact address or landmarks</li>
                            <li><i class="fas fa-check me-2" style="color: #0B6B57;"></i> <strong>Describe Situation</strong> - Give brief details about the emergency</li>
                            <li><i class="fas fa-check me-2" style="color: #0B6B57;"></i> <strong>Follow Instructions</strong> - Listen and obey the operator's guidance</li>
                            <li><i class="fas fa-check me-2" style="color: #0B6B57;"></i> <strong>Keep Phone Available</strong> - Don't hang up until told to do so</li>
                        </ul>
                    </div>
                </div>
            </div>

            <div class="col-lg-6 mb-4">
                <div class="card shadow-sm" style="border: none; border-left: 4px solid #D4941F;">
                    <div class="card-body">
                        <h6 class="card-title mb-3" style="color: #18352E; font-weight: 700;">
                            <i class="fas fa-info-circle me-2" style="color: #D4941F;"></i> About This Service
                        </h6>
                        <p class="card-text text-muted small" style="line-height: 1.7;">
                            This page provides quick access to essential and emergency services available in Jharkhand. All numbers listed are official government hotlines and should be used only for genuine emergencies and service requests. For non-emergency matters, please contact local authorities or service providers directly.
                        </p>
                    </div>
                </div>
            </div>
        </div>

        <!-- CTA Section -->
        <div class="alert alert-info mt-5" style="background: linear-gradient(135deg, #0B6B57 0%, #074a3d 100%); border: none; color: white;">
            <div class="row align-items-center">
                <div class="col-md-8">
                    <h5 class="mb-2">
                        <i class="fas fa-search me-2"></i> Looking for a Business?
                    </h5>
                    <p class="mb-0 small">
                        Browse our directory to find hospitals, doctors, plumbers, electricians, and many more services in your area.
                    </p>
                </div>
                <div class="col-md-4 text-end mt-3 mt-md-0">
                    <a href="/" class="btn btn-light btn-sm">
                        <i class="fas fa-home me-1"></i> Back to Directory
                    </a>
                </div>
            </div>
        </div>
    </div>
</div>

<?php include '../../includes/footer.php'; ?>

<style>
    .hover-emergency:hover,
    .hover-service:hover {
        transform: translateY(-8px);
        box-shadow: 0 1rem 2rem rgba(220, 53, 69, 0.15) !important;
    }

    .card {
        border-radius: 0.5rem;
    }

    .btn {
        border-radius: 0.35rem;
        font-size: 0.95rem;
        transition: all 0.2s ease;
    }

    .btn:hover {
        transform: scale(1.02);
    }

    a[href^="tel:"] {
        -webkit-user-select: none;
        user-select: none;
    }
</style>

<?php
/**
 * Helper function to adjust color brightness
 */
function adjustColor($color, $percent) {
    $color = str_replace('#', '', $color);
    if (strlen($color) == 6) {
        list($r, $g, $b) = array_map('hexdec', str_split($color, 2));
    } else {
        return $color;
    }

    $r = intval($r * (100 + $percent) / 100);
    $g = intval($g * (100 + $percent) / 100);
    $b = intval($b * (100 + $percent) / 100);

    $r = ($r > 255) ? 255 : (($r < 0) ? 0 : $r);
    $g = ($g > 255) ? 255 : (($g < 0) ? 0 : $g);
    $b = ($b > 255) ? 255 : (($b < 0) ? 0 : $b);

    return '#' . str_pad(dechex($r), 2, '0', STR_PAD_LEFT) .
           str_pad(dechex($g), 2, '0', STR_PAD_LEFT) .
           str_pad(dechex($b), 2, '0', STR_PAD_LEFT);
}
?>
