<?php
include "ominterior_db.php";

$slider_query = "SELECT id, slider_name, slider_images FROM slider_images WHERE slider_name = 'Career'";
$slider_result = $conn->query($slider_query);

$sql = "SELECT * FROM  current_openings  ORDER BY id DESC";
$curent_opening = mysqli_query($conn, $sql);


$menu_query = "SELECT name, link FROM menu ORDER BY id ASC";
$menu_result = $conn->query($menu_query);

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $name = $conn->real_escape_string($_POST['name']);
    $email = $conn->real_escape_string($_POST['email']);
    $phone = $conn->real_escape_string($_POST['phone']);
    $message = $conn->real_escape_string($_POST['message']);

    $file = $_FILES['pdf'];
    $ext = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION)); 
    
    if ($file['error'] == 0 && $ext == 'pdf') {
        $filename = uniqid('career_', true) . '.pdf';
        $path = "admin/uploads/career/$name/";
        $upload = $path.$filename;

        if (!is_dir($path)) { mkdir($path, 0777, true); }

        if (move_uploaded_file($file['tmp_name'], $upload)) {
            $conn->query("INSERT INTO career_uploads (name, email, phone, message, pdf_file) 
                          VALUES ('$name', '$email', '$phone', '$message', '$filename')");
              header("Location: " . $_SERVER['PHP_SELF']);
        } else {
            echo "Upload Failed!";
        }
    } else {
        echo "Only PDF allowed.";
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>OM INTERIOR | Career</title>

    <link rel="icon" href="assets/images/favicon.png" type="image/x-icon">

    <!-- Bootstrap & Icons -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.5/font/bootstrap-icons.css">

    <!-- Swiper -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/swiper@11/swiper-bundle.min.css" />
    <script src="https://cdn.jsdelivr.net/npm/swiper@11/swiper-bundle.min.js"></script>

    <!-- Font Awesome -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css">

    <!-- Custom CSS -->
    <link rel="stylesheet" href="assets/css/footer.css">
    <link rel="stylesheet" href="assets/css/career.css">
</head>
<body>
    <!-- Sliders -->
<div class="container-fluid px-0">
    <?php if ($slider_result->num_rows > 0): ?>
        <?php while($row = $slider_result->fetch_assoc()):
            $sliderName = $row["slider_name"];
            $images = array_filter(array_map('trim', explode(',', $row["slider_images"])));
            $totalSlides = count($images);
            $carouselId = 'carousel_' . $row["id"];
        ?>
        <div id="<?= $carouselId ?>" class="carousel slide position-relative" data-bs-ride="carousel">
            <?php if($totalSlides > 1): ?>
                <div class="carousel-indicators">
                    <?php foreach ($images as $i => $_): ?>
                        <button type="button" data-bs-target="#<?= $carouselId ?>" data-bs-slide-to="<?= $i ?>" class="<?= $i === 0 ? 'active' : '' ?>"></button>
                    <?php endforeach; ?>
                </div>
            <?php endif; ?>

            <div class="carousel-inner">
                <?php foreach ($images as $index => $img):
                    $safeImg = basename($img);
                    $path = 'admin/uploads/sliders/' . $sliderName . '/' . $safeImg;
                    if (file_exists($path)): ?>
                        <div class="carousel-item <?= $index === 0 ? 'active' : '' ?>">
                            <img src="<?= htmlspecialchars($path) ?>" class="d-block w-100 img-fluid object-fit-cover" alt="<?= htmlspecialchars($sliderName) ?>">
                        </div>
                    <?php endif;
                endforeach; ?>
            </div>

            <?php if($totalSlides > 1): ?>
                <div class="slide-counter" id="counter_<?= $carouselId ?>">
                    <span class="active-count">1</span>/<span class="total-count"><?= $totalSlides ?></span>
                </div>
            <?php endif; ?>
        </div>
        <?php endwhile; ?>
    <?php else: ?>
        <p class="text-center">No sliders found.</p>
    <?php endif; ?>

    <!-- Static Lines -->
    <div class="carousel-lines">
        <div class="line-left"></div>
        <div class="line-right"></div>
        <div class="line-top"></div>
        <div class="line-bottom"></div>
    </div>

    <!-- Logo -->
    <div class="logo d-flex align-items-center p-2">
        <img src="images/icons/logo.jpg" alt="Logo" class="img-fluid me-2" style="height: 75px;">
    </div>

    <!-- Centered Slider Text -->
    <div class="slider-text-overlay">
        <h2>Career @OM INTERIOR</h2>
        <h2 class="startproject">Start project</h2>
    </div>

    <!-- Hamburger Icon -->
    <div class="top-right-icons d-flex align-items-center p-2">
        <button class="btn text-white" data-bs-toggle="offcanvas" data-bs-target="#offcanvasMenu">
            <i class="bi bi-list fs-2 icon-opacity-low"></i>
        </button>
    </div>

    <!-- Offcanvas Menu -->
    <div class="offcanvas offcanvas-end" id="offcanvasMenu">
        <div class="offcanvas-header">
            <h5>Main Menu</h5>
            <img src="images/icons/Back Arrow.png" data-bs-dismiss="offcanvas" aria-label="Close">
        </div>
        <div class="offcanvas-body">
            <ul class="nav flex-column">
                <?php if ($menu_result->num_rows > 0): ?>
                    <?php while ($row = $menu_result->fetch_assoc()): ?>
                        <li class="nav-item">
                            <a class="nav-link text-white" href="<?= htmlspecialchars($row['link']) ?>">
                                <?= htmlspecialchars($row['name']) ?>
                            </a>
                        </li>
                    <?php endwhile; ?>
                <?php else: ?>
                    <li class="nav-item"><a class="nav-link text-white">No menu items found</a></li>
                <?php endif; ?>
            </ul>
        </div>
    </div>

    <!-- Social Icons -->
    <div class="social-media-icons">
        <a href="https://facebook.com" target="_blank"><i class="fab fa-facebook-f"></i></a>
        <a href="https://instagram.com" target="_blank"><i class="fab fa-instagram"></i></a>
        <a href="https://linkedin.com" target="_blank"><i class="fab fa-linkedin-in"></i></a>
        <a href="https://pinterest.com" target="_blank"><i class="fab fa-pinterest-p"></i></a>
    </div>
</div>
<?php $conn->close(); ?>


<div class="container-fluid career">
        <h2 class="how-text">How to Apply?</h2>
        <div class="g-rounded-30">
            <div class="row mb-5 p-1">
                <div class="col-md-3 g-my-5 g-my-0--md g-ml-5 g-ml-0--md">
                    <span class="rounded-span"><b>1</b></span><span class="g-font-size-18"> Candidate Registration</span>
                </div>
                <div class="col-md-3 g-my-5 g-my-0--md g-ml-5 g-ml-0--md">
                    <span class="rounded-span"><b>2</b></span><span class="g-font-size-18"> Email Verification</span>
                </div>
                <div class="col-md-3 g-my-5 g-my-0--md g-ml-5 g-ml-0--md">
                    <span class="rounded-span"><b>3</b></span><span class="g-font-size-18"> Fill Details</span>
                </div>
                <div class="col-md-3 g-my-5 g-my-0--md g-ml-5 g-ml-0--md">
                    <span class="rounded-span"><b>4</b></span><span class="g-font-size-18"> Select Post</span>
                </div>
            </div>
        </div>

        <div class="row current-text">
            <div class="col-md-6 col-sm-12">
                <div class="border border-opacity-25 rounded-2 p-4">
                    <h4>Current Openings</h4>
                    <hr style="border-style: dotted" />
                        <table class="table table-bordered">
                            <?php if ($curent_opening->num_rows > 0): ?>
                                <thead>
                                    <tr class="text-center">
                                        <th>Job Title</th>
                                        <th>Job Description</th>
                                        <th>Location</th>
                                        <th>Employment Type</th>
                                        <th>Vacancy Count</th>
                                        <th>Last Date to Apply</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php while($row = $curent_opening->fetch_assoc()): ?>
                                        <tr>
                                            <td><?= $row["title"] ?></td>
                                            <td><?= substr($row["description"], 0, 20) ?>...</td>
                                            <td><?= $row["location"] ?></td>
                                            <td><?= $row["employment_type"] ?></td>
                                            <td><?= $row["vacancy_count"] ?></td>
                                            <td><?= $row["last_date_to_apply"] ?></td>
                                        </tr>
                                    <?php endwhile; ?>
                                </tbody>
                            <?php else: ?>
                                <div class="alert alert-danger p-2 mt-3" role="alert">
                                    Currently, there are no openings available.
                                </div>
                            <?php endif; ?>
                        </table>
                </div>
            </div>

            <div class="col-md-6 col-sm-12">
                 <div class="border border-opacity-25 rounded-2 p-4">
                    <h4 class="text-danger">Register Online</h4>
                    <hr style="border-style: dotted" />
                <div class="form-section">
                    <form method="POST" enctype="multipart/form-data">
                        <div class="mb-3">
                            <label for="name" class="form-label">Full Name:</label>
                            <input type="text" name="name" id="name" class="form-control" required>
                        </div>

                        <div class="mb-3">
                            <label for="email" class="form-label">Email Address:</label>
                            <input type="email" name="email" id="email" class="form-control" required>
                        </div>

                        <div class="mb-3">
                            <label for="phone" class="form-label">Phone Number:</label>
                            <input type="text" name="phone" id="phone" class="form-control" required>
                        </div>

                        <div class="mb-3">
                            <label for="message" class="form-label">Your Message:</label>
                            <textarea name="message" id="message" class="form-control" rows="5" required></textarea>
                        </div>

                        <div class="mb-3">
                            <label for="pdf" class="form-label">Select PDF File:</label>
                            <input type="file" name="pdf" id="pdf" class="form-control" accept="application/pdf" required>
                        </div>

                        <button type="submit" name="submit" class="btn btn-primary">Upload Application</button>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

<?php include"footer_section.php"?>

<!-- Scripts -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
<script src="https://cdn.lordicon.com/lordicon.js"></script>
<script src="assets/js/script.js"></script>
</body>
</html>
