<?php
/**
 * =========================================================
 * JHARKHAND DIRECTORY v1.0.0
 * District Detail Page
 * © 2026 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()->getConnection();

$district_slug = sanitize($_GET['district'] ?? '');

if (!$district_slug) {
    header('Location: ../index.php');
    exit();
}

// Get district
$stmt = $db->prepare("SELECT * FROM " . TBL_DISTRICTS . " WHERE slug = ?");
$stmt->execute([$district_slug]);
$district = $stmt->fetch();

if (!$district) {
    header('Location: ../index.php');
    exit();
}

$page_title = ($district['meta_title'] ?? escape($district['name']) . ' | ' . SITE_NAME);
$page_subtitle = $district['meta_description'] ?? 'Find businesses and services in ' . escape($district['name']);

// Get listings
$stmt = $db->query("
    SELECT l.*, c.name as category_name 
    FROM " . TBL_LISTINGS . " l 
    JOIN " . TBL_CATEGORIES . " c ON l.category_id = c.id 
    WHERE l.district_id = " . $district['id'] . " AND l.status = 'approved' 
    ORDER BY l.is_featured DESC, l.created_at DESC
");
$listings = $stmt->fetchAll();

// Get categories
$stmt = $db->query("
    SELECT DISTINCT c.* 
    FROM " . TBL_CATEGORIES . " c 
    JOIN " . TBL_LISTINGS . " l ON c.id = l.category_id 
    WHERE l.district_id = " . $district['id'] . " AND l.status = 'approved'
    ORDER BY c.name
");
$categories = $stmt->fetchAll();

?>
<?php require_once '../../includes/header.php'; ?>

<style>
    .district-header {
        background: linear-gradient(135deg, var(--primary-color) 0%, var(--primary-dark) 100%);
        color: #fff;
        padding: 40px 0;
        margin-bottom: 40px;
    }
    
    .district-header h1 {
        font-size: 36px;
        margin-bottom: 10px;
    }
    
    .breadcrumb {
        background-color: transparent;
        padding: 0;
        margin-bottom: 20px;
    }
    
    .breadcrumb a {
        color: rgba(255,255,255,0.9);
    }
    
    .listings-grid {
        display: grid;
        grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
        gap: 20px;
        margin: 30px 0;
    }
    
    .listing-card {
        background-color: #fff;
        border: 1px solid #e0e0e0;
        border-radius: 8px;
        overflow: hidden;
        transition: all 0.3s;
        height: 100%;
    }
    
    .listing-card:hover {
        box-shadow: 0 10px 30px rgba(0,0,0,0.1);
        transform: translateY(-5px);
    }
    
    .listing-card .header {
        background: linear-gradient(135deg, var(--primary-color), var(--accent-color));
        padding: 20px;
        color: #fff;
    }
    
    .listing-card .name {
        font-weight: 600;
        font-size: 16px;
        margin-bottom: 5px;
    }
    
    .listing-card .category {
        font-size: 12px;
        opacity: 0.9;
    }
    
    .listing-card .content {
        padding: 20px;
    }
    
    .listing-card .details {
        font-size: 13px;
        color: #666;
        margin-bottom: 15px;
    }
    
    .listing-card .details > div {
        margin-bottom: 8px;
    }
    
    .listing-card .action {
        text-align: center;
    }
    
    .listing-card .action a {
        background-color: var(--primary-color);
        color: #fff;
        padding: 8px 15px;
        border-radius: 4px;
        text-decoration: none;
        display: inline-block;
        transition: all 0.3s;
    }
    
    .listing-card .action a:hover {
        background-color: var(--primary-dark);
    }
</style>

<div class="district-header">
    <div class="container">
        <nav class="breadcrumb" aria-label="breadcrumb">
            <ol class="breadcrumb" style="margin-bottom: 0; background: transparent; padding: 0;">
                <li class="breadcrumb-item"><a href="<?php echo SITE_URL; ?>" style="color: rgba(255,255,255,0.8);">Home</a></li>
                <li class="breadcrumb-item active" style="color: #fff;"><?php echo escape($district['name']); ?></li>
            </ol>
        </nav>
        <h1><?php echo escape($district['name']); ?></h1>
        <p><?php echo escape($page_subtitle); ?></p>
    </div>
</div>

<div class="container">
    
    <?php if (count($listings) > 0): ?>
        <h2 style="color: var(--text-color); margin-bottom: 10px;">
            Listings in <?php echo escape($district['name']); ?> (<?php echo count($listings); ?>)
        </h2>
        <p style="color: #666; margin-bottom: 30px;">
            Browse all available businesses and services
        </p>
        
        <div class="listings-grid">
            <?php foreach ($listings as $listing): ?>
                <div class="listing-card">
                    <div class="header">
                        <div class="name"><?php echo escape($listing['name']); ?></div>
                        <div class="category"><?php echo escape($listing['category_name']); ?></div>
                    </div>
                    <div class="content">
                        <div class="details">
                            <?php if ($listing['phone']): ?>
                                <div><i class="fas fa-phone" style="color: var(--primary-color); width: 20px;"></i> <?php echo escape($listing['phone']); ?></div>
                            <?php endif; ?>
                            
                            <?php if ($listing['address']): ?>
                                <div><i class="fas fa-map-marker-alt" style="color: var(--primary-color); width: 20px;"></i> <?php echo escape(truncate($listing['address'], 40)); ?></div>
                            <?php endif; ?>
                            
                            <?php if ($listing['email']): ?>
                                <div><i class="fas fa-envelope" style="color: var(--primary-color); width: 20px;"></i> <?php echo escape(truncate($listing['email'], 35)); ?></div>
                            <?php endif; ?>
                        </div>
                        
                        <div class="action">
                            <a href="../listing/view.php?id=<?php echo $listing['id']; ?>">View Details</a>
                        </div>
                    </div>
                </div>
            <?php endforeach; ?>
        </div>
    <?php else: ?>
        <div class="alert alert-info" style="margin-top: 40px;">
            <i class="fas fa-info-circle"></i> No listings found in <?php echo escape($district['name']); ?>.
        </div>
    <?php endif; ?>
    
</div>

<?php require_once '../../includes/footer.php'; ?>