<?php
/**
 * Página de detalle de vacante - Frontend público
 * Turpial Airlines, C.A.
 */

require_once '../includes/functions.php';
require_once '../includes/JobVacancy.php';
require_once '../includes/Application.php';

// Verificar que se proporcione ID de vacante
if (!isset($_GET['id']) || !is_numeric($_GET['id'])) {
    header('Location: jobs.php');
    exit;
}

$job_id = (int)$_GET['id'];

// Obtener detalles de la vacante
$jobVacancy = new JobVacancy();
$job = $jobVacancy->getById($job_id);

if (!$job || !$job['is_active']) {
    header('Location: jobs.php?error=not_found');
    exit;
}

// Verificar si la vacante ha expirado
$is_expired = false;
if ($job['deadline'] && strtotime($job['deadline']) < time()) {
    $is_expired = true;
}

$page_title = $job['title'];

// Obtener aplicaciones para mostrar el conteo
$application = new Application();
$applications = $application->getByJobVacancy($job_id);
$application_count = count($applications);

// Vacantes relacionadas (misma categoría)
$related_jobs = $jobVacancy->getAll([
    'category_id' => $job['category_id'],
    'is_active' => 1
]);

// Filtrar la vacante actual de las relacionadas
$related_jobs = array_filter($related_jobs, function($j) use ($job_id) {
    return $j['id'] != $job_id;
});
$related_jobs = array_slice($related_jobs, 0, 3);

include 'includes/header.php';
?>

<!-- Breadcrumb -->
<nav aria-label="breadcrumb" class="bg-light py-3">
    <div class="container">
        <ol class="breadcrumb mb-0">
            <li class="breadcrumb-item"><a href="index.php">Inicio</a></li>
            <li class="breadcrumb-item"><a href="jobs.php">Vacantes</a></li>
            <li class="breadcrumb-item"><a href="jobs.php?division=<?php echo $job['division_id']; ?>"><?php echo escape($job['division_name']); ?></a></li>
            <li class="breadcrumb-item active"><?php echo escape($job['title']); ?></li>
        </ol>
    </div>
</nav>

<div class="container py-5">
    <div class="row">
        <!-- Detalles de la Vacante -->
        <div class="col-lg-8">
            <div class="card">
                <div class="card-header bg-turpial-gradient">
                    <div class="d-flex justify-content-between align-items-start">
                        <div>
                            <h1 class="h3 mb-2"><?php echo escape($job['title']); ?></h1>
                            <p class="mb-0">
                                <i class="fas fa-building me-2"></i><?php echo escape($job['division_name']); ?> - 
                                <i class="fas fa-tag me-2"></i><?php echo escape($job['category_name']); ?>
                            </p>
                        </div>
                        <?php if ($job['is_featured']): ?>
                            <span class="badge bg-warning">
                                <i class="fas fa-star"></i> Destacada
                            </span>
                        <?php endif; ?>
                    </div>
                </div>
                
                <div class="card-body">
                    <!-- Información rápida -->
                    <div class="row mb-4">
                        <div class="col-md-6">
                            <div class="d-flex align-items-center mb-2">
                                <i class="fas fa-map-marker-alt text-primary me-2"></i>
                                <strong>Ubicación:</strong>&nbsp;<?php echo escape($job['location']); ?>
                            </div>
                            <div class="d-flex align-items-center mb-2">
                                <i class="fas fa-clock text-primary me-2"></i>
                                <strong>Tipo:</strong>&nbsp;<?php echo escape($job['employment_type']); ?>
                            </div>
                            <?php if ($job['experience_required']): ?>
                                <div class="d-flex align-items-center mb-2">
                                    <i class="fas fa-briefcase text-primary me-2"></i>
                                    <strong>Experiencia:</strong>&nbsp;<?php echo escape($job['experience_required']); ?>
                                </div>
                            <?php endif; ?>
                        </div>
                        <div class="col-md-6">
                            <?php if ($job['salary_range']): ?>
                                <div class="d-flex align-items-center mb-2">
                                    <i class="fas fa-dollar-sign text-primary me-2"></i>
                                    <strong>Salario:</strong>&nbsp;<?php echo escape($job['salary_range']); ?>
                                </div>
                            <?php endif; ?>
                            <?php if ($job['education_required']): ?>
                                <div class="d-flex align-items-center mb-2">
                                    <i class="fas fa-graduation-cap text-primary me-2"></i>
                                    <strong>Educación:</strong>&nbsp;<?php echo escape($job['education_required']); ?>
                                </div>
                            <?php endif; ?>
                            <div class="d-flex align-items-center mb-2">
                                <i class="fas fa-calendar text-primary me-2"></i>
                                <strong>Publicada:</strong>&nbsp;<?php echo formatDate($job['created_at']); ?>
                            </div>
                        </div>
                    </div>
                    
                    <!-- Descripción -->
                    <div class="mb-4">
                        <h4><i class="fas fa-info-circle me-2"></i>Descripción del Puesto</h4>
                        <div class="text-muted">
                            <?php echo nl2br(escape($job['description'])); ?>
                        </div>
                    </div>
                    
                    <!-- Responsabilidades -->
                    <?php if ($job['responsibilities']): ?>
                        <div class="mb-4">
                            <h4><i class="fas fa-tasks me-2"></i>Responsabilidades</h4>
                            <div class="text-muted">
                                <?php echo nl2br(escape($job['responsibilities'])); ?>
                            </div>
                        </div>
                    <?php endif; ?>
                    
                    <!-- Requisitos -->
                    <?php if ($job['requirements']): ?>
                        <div class="mb-4">
                            <h4><i class="fas fa-list-check me-2"></i>Requisitos</h4>
                            <div class="text-muted">
                                <?php echo nl2br(escape($job['requirements'])); ?>
                            </div>
                        </div>
                    <?php endif; ?>
                    
                    <!-- Fecha límite -->
                    <?php if ($job['deadline']): ?>
                        <div class="alert <?php echo $is_expired ? 'alert-danger' : 'alert-warning'; ?>">
                            <i class="fas fa-hourglass-end me-2"></i>
                            <strong>Fecha límite de aplicación:</strong> <?php echo formatDate($job['deadline']); ?>
                            <?php if ($is_expired): ?>
                                <br><small>Esta vacante ha expirado.</small>
                            <?php endif; ?>
                        </div>
                    <?php endif; ?>
                </div>
            </div>
            
            <!-- Vacantes Relacionadas -->
            <?php if (!empty($related_jobs)): ?>
                <div class="mt-5">
                    <h3><i class="fas fa-sitemap me-2"></i>Vacantes Relacionadas</h3>
                    <div class="row">
                        <?php foreach ($related_jobs as $related): ?>
                            <div class="col-md-4 mb-3">
                                <div class="card h-100">
                                    <div class="card-body">
                                        <h6 class="card-title"><?php echo escape($related['title']); ?></h6>
                                        <p class="card-text small text-muted"><?php echo truncateText(strip_tags($related['description']), 80); ?></p>
                                        <a href="job-detail.php?id=<?php echo $related['id']; ?>" class="btn btn-outline-primary btn-sm">Ver Detalles</a>
                                    </div>
                                </div>
                            </div>
                        <?php endforeach; ?>
                    </div>
                </div>
            <?php endif; ?>
        </div>
        
        <!-- Sidebar -->
        <div class="col-lg-4">
            <!-- Acción Principal -->
            <div class="card mb-4">
                <div class="card-body text-center">
                    <?php if ($is_expired): ?>
                        <div class="alert alert-danger">
                            <i class="fas fa-exclamation-triangle"></i>
                            Esta vacante ha expirado
                        </div>
                    <?php else: ?>
                        <h5 class="card-title">Aplicar a esta Vacante</h5>
                        <p class="card-text">Envía tu currículum y carta de presentación para esta oportunidad.</p>
                        <a href="apply.php?job=<?php echo $job['id']; ?>" class="btn btn-turpial btn-lg w-100">
                            <i class="fas fa-paper-plane me-2"></i>Aplicar Ahora
                        </a>
                    <?php endif; ?>
                </div>
            </div>
            
            <!-- Estadísticas -->
            <div class="card mb-4">
                <div class="card-header">
                    <h6 class="mb-0"><i class="fas fa-chart-bar me-2"></i>Estadísticas</h6>
                </div>
                <div class="card-body">
                    <div class="d-flex justify-content-between mb-2">
                        <span>Candidatos:</span>
                        <strong><?php echo $application_count; ?></strong>
                    </div>
                    <div class="d-flex justify-content-between mb-2">
                        <span>Publicada:</span>
                        <strong><?php echo formatDate($job['created_at']); ?></strong>
                    </div>
                    <?php if ($job['deadline']): ?>
                        <div class="d-flex justify-content-between">
                            <span>Cierra:</span>
                            <strong><?php echo formatDate($job['deadline']); ?></strong>
                        </div>
                    <?php endif; ?>
                </div>
            </div>
            
            <!-- Información de Contacto -->
            <div class="card">
                <div class="card-header">
                    <h6 class="mb-0"><i class="fas fa-envelope me-2"></i>Contacto</h6>
                </div>
                <div class="card-body">
                    <p class="mb-2">
                        <i class="fas fa-envelope me-2"></i>
                        <?php echo getSetting('company_email', 'rrhh@turpialairlines.com'); ?>
                    </p>
                    <p class="mb-2">
                        <i class="fas fa-phone me-2"></i>
                        <?php echo getSetting('company_phone', '+58-212-555-0123'); ?>
                    </p>
                    <small class="text-muted">
                        Si tienes preguntas sobre esta vacante, no dudes en contactarnos.
                    </small>
                </div>
            </div>
        </div>
    </div>
</div>

<?php include 'includes/footer.php'; ?>