<?php
include 'ominterior_db.php';

// Get Job ID to edit
if (isset($_GET['id'])) {
    $id = (int)$_GET['id'];

    // Fetch existing data
    $sql = "SELECT * FROM  current_openings  WHERE id = $id";
    $result = mysqli_query($conn, $sql);
    $row = mysqli_fetch_assoc($result);

    if (!$row) {
        die('Job opening not found.');
    }
} else {
    die('ID not provided.');
}

// Handle form submission
if (isset($_POST['update'])) {
    $title = mysqli_real_escape_string($conn, $_POST['title']);
    $description = mysqli_real_escape_string($conn, $_POST['description']);
    $location = mysqli_real_escape_string($conn, $_POST['location']);
    $department = mysqli_real_escape_string($conn, $_POST['department']);
    $employment_type = mysqli_real_escape_string($conn, $_POST['employment_type']);
    $vacancy_count = (int)$_POST['vacancy_count'];
    $last_date_to_apply = $_POST['last_date_to_apply'];

    $update_sql = "UPDATE current_openings  
                   SET title='$title', description='$description', location='$location', department='$department',
                       employment_type='$employment_type', vacancy_count='$vacancy_count', last_date_to_apply='$last_date_to_apply' 
                   WHERE id = $id";

    if (mysqli_query($conn, $update_sql)) {
        echo "<script>alert('Job Opening Updated Successfully'); window.location.href='career.php';</script>";
    } else {
        echo "Error updating record: " . mysqli_error($conn);
    }
}
?>

<!doctype html>
<html lang="en" dir="ltr">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">

<link rel="icon" href="assets/images/black-logo.png" type="image/x-icon"/>

<title>OM INTERIOR - Dashborad</title>

<!-- Bootstrap Core and vandor -->
<link rel="stylesheet" href="assets/plugins/bootstrap/css/bootstrap.min.css" />

<!-- Plugins css -->
<link rel="stylesheet" href="assets/plugins/charts-c3/c3.min.css"/>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">

<!-- Core css -->
<link rel="stylesheet" href="assets/css/main2.css"/>
<link rel="stylesheet" href="assets/css/theme1.css"/>
</head>

<body class="font-montserrat">
<!-- Page Loader -->
<div class="page-loader-wrapper">
    <div class="loader">
    </div>
</div>

<div id="main_content">

<?php include 'haderTop_logo.php'?>
    
<?php include 'rightside_menu.php'?>

<?php include 'leftside_menu.php' ?>


    <div class="page">
        <div id="page_top" class="section-body top_dark">
            <div class="container-fluid">
                <div class="page-header">
                    <div class="left">
                        <a href="javascript:void(0)" class="icon menu_toggle mr-3"><i class="fa  fa-align-left"></i></a>
                        <h1 class="page-title">Project</h1>                        
                    </div>
                    <div class="right">
                        <?php include 'dropdown_flex_menu.php' ?>
                    </div>
                </div>
            </div>
        </div>

                <div class="row clearfix">
                    <div class="col-12 col-sm-12">
                        <div class="card">
                            <div class="card-header">
                                <h3 class="card-title">Edit Project Details</h3>
                                <a href="delete-current-opening.php?id=<?= $project["id"] ?>" class="btn btn-danger btn-sm" style="position: relative; left:60vw" onclick="return confirm('Are you sure?')">Delete</a>
                            </div>
                                <!-- Include Bootstrap & jQuery -->
                                <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet" />
                                <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>

                                <div class="container py-5">
                                    <div class="card">
                                        <div class="card-body">
                                             <h2 class="mb-4">Update Job Opening</h2>

                                                    <form method="POST">
                                                        <div class="mb-3">
                                                            <label class="form-label">Job Title</label>
                                                            <input type="text" class="form-control" name="title" value="<?php echo htmlspecialchars($row['title']); ?>" required>
                                                        </div>

                                                        <div class="mb-3">
                                                            <label class="form-label">Job Description</label>
                                                            <textarea class="form-control" name="description" rows="5" required><?php echo htmlspecialchars($row['description']); ?></textarea>
                                                        </div>

                                                        <div class="mb-3">
                                                            <label class="form-label">Job Location</label>
                                                            <input type="text" class="form-control" name="location" value="<?php echo htmlspecialchars($row['location']); ?>">
                                                        </div>

                                                        <div class="mb-3">
                                                            <label class="form-label">Department</label>
                                                            <input type="text" class="form-control" name="department" value="<?php echo htmlspecialchars($row['department']); ?>">
                                                        </div>

                                                        <div class="mb-3">
                                                            <label class="form-label">Employment Type</label>
                                                            <select class="form-select" name="employment_type">
                                                                <option value="">Select Type</option>
                                                                <option value="Full-Time" <?php if ($row['employment_type'] == 'Full-Time') echo 'selected'; ?>>Full-Time</option>
                                                                <option value="Part-Time" <?php if ($row['employment_type'] == 'Part-Time') echo 'selected'; ?>>Part-Time</option>
                                                                <option value="Internship" <?php if ($row['employment_type'] == 'Internship') echo 'selected'; ?>>Internship</option>
                                                                <option value="Contract" <?php if ($row['employment_type'] == 'Contract') echo 'selected'; ?>>Contract</option>
                                                            </select>
                                                        </div>

                                                        <div class="mb-3">
                                                            <label class="form-label">Vacancy Count</label>
                                                            <input type="number" class="form-control" name="vacancy_count" value="<?php echo (int)$row['vacancy_count']; ?>" min="1">
                                                        </div>

                                                        <div class="mb-3">
                                                            <label class="form-label">Last Date to Apply</label>
                                                            <input type="date" class="form-control" name="last_date_to_apply" value="<?php echo $row['last_date_to_apply']; ?>">
                                                        </div>

                                                        <div class="text-center">
                                                            <button type="submit" name="update" class="btn btn-success">Update Opening</button>
                                                        </div>
                                                    </form>
                                        </div>
                                    </div>
                                </div>

                                <script>
                                    const projectTypes = {
                                        architecture: ['Residential', 'Commercial', 'Industrial'],
                                        interior: ['Living Room', 'Kitchen', 'Bedroom'],
                                        '3d_design': ['Walkthrough', 'Rendering', 'Animation']
                                    };

                                    function updateProjectTypeOptions(category, selectedType = '') {
                                        const typeSelect = $('#ptype');
                                        typeSelect.empty();
                                        projectTypes[category].forEach(type => {
                                            const val = type.toLowerCase().replace(/\s+/g, '_');
                                            const isSelected = (val === selectedType) ? 'selected' : '';
                                            typeSelect.append(`<option value="${val}" ${isSelected}>${type}</option>`);
                                        });
                                    }

                                    $(document).ready(function () {
                                        const selectedCategory = $('#Category').val();
                                        const currentType = "<?= $project['project_type'] ?>";
                                        updateProjectTypeOptions(selectedCategory, currentType);

                                        $('#Category').on('change', function () {
                                            updateProjectTypeOptions(this.value);
                                        });
                                    });
                                </script>
                        </div>
                    </div>
                </div>
                
        <div class="section-body">
            <footer class="footer">
                <div class="container-fluid">
                    <div class="row">
                        <div class="col-md-12 col-sm-12 text-md-right">
                            <ul class="list-inline mb-0">
                                <li class="list-inline-item"><a href="doc/index.html">OM INTERIOR</a></li>
                            </ul>
                        </div>
                    </div>
                </div>
            </footer>
        </div>
    </div>    
</div>

<script src="assets/bundles/lib.vendor.bundle.js"></script>

<script src="assets/bundles/apexcharts.bundle.js"></script>
<script src="assets/bundles/counterup.bundle.js"></script>
<script src="assets/bundles/knobjs.bundle.js"></script>
<script src="assets/bundles/c3.bundle.js"></script>

<script src="assets/js/core.js"></script>
<script src="assets/js/page/project-index.js"></script>
</body>
</html>
