<?php
    include "ominterior_db.php";

        if (isset($_POST['submit'])) {
            $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'];

            $sql = "INSERT INTO current_openings (title, description, location, department, employment_type, vacancy_count, last_date_to_apply)
                    VALUES ('$title', '$description', '$location', '$department', '$employment_type', '$vacancy_count', '$last_date_to_apply')";

            if (mysqli_query($conn, $sql)) {
                echo "<script>alert('New Job Opening Added Successfully'); window.location.href='career.php';</script>";
            } else {
                echo "Error: " . 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">Current Opening</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">Add New Opening</h3>
                    </div>
                </div>
                <div class="card">
                    <div class="card-body">
                        <form method="POST">
                            <div class="mb-3">
                                <label for="title" class="form-label">Job Title</label>
                                <input type="text" class="form-control" id="title" name="title" placeholder="e.g. PHP Developer" required>
                            </div>

                            <div class="mb-3">
                                <label for="description" class="form-label">Job Description</label>
                                <textarea class="form-control" id="description" name="description" rows="5" placeholder="Write job details here..." required></textarea>
                            </div>

                            <div class="mb-3">
                                <label for="location" class="form-label">Job Location</label>
                                <input type="text" class="form-control" id="location" name="location" placeholder="e.g. Rajkot, Gujarat">
                            </div>

                            <div class="mb-3">
                                <label for="department" class="form-label">Department</label>
                                <input type="text" class="form-control" id="department" name="department" placeholder="e.g. IT, HR, Sales">
                            </div>

                            <div class="mb-3">
                                <label for="employment_type" class="form-label">Employment Type</label>
                                <select class="form-select w-100 form-control" id="employment_type" name="employment_type" required>
                                    <option value="">Select Type</option>
                                    <option value="Full-Time">Full-Time</option>
                                    <option value="Part-Time">Part-Time</option>
                                    <option value="Internship">Internship</option>
                                    <option value="Contract">Contract</option>
                                </select>
                            </div>


                            <div class="mb-3">
                                <label for="vacancy_count" class="form-label">Vacancy Count</label>
                                <input type="number" class="form-control" id="vacancy_count" name="vacancy_count" value="1" min="1">
                            </div>

                            <div class="mb-3">
                                <label for="last_date_to_apply" class="form-label">Last Date to Apply</label>
                                <input type="date" class="form-control" id="last_date_to_apply" name="last_date_to_apply">
                            </div>

                            <div class="text-center">
                                <button type="submit" name="submit" class="btn btn-primary">Add Opening</button>
                            </div>
                        </form>
                    </div>
                </div>
            </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>

<?php

include 'ominterior_db.php'; // Ensure connection is established

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $client = $_POST['cname'];
    $category = $_POST['Category'];
    $ptype = $_POST['ptype'];
    $pname = $_POST['pname'];
    $pdes = $_POST['pdes'];
    $values = [];
   

    $uploadDir = "uploads/projects/$pname/";
    if (!is_dir($uploadDir)) {
        mkdir($uploadDir, 0777, true);
    }
    
    $values = [];
    
    foreach ($_FILES['images']['tmp_name'] as $key => $tmp_name) {
        $originalName = basename($_FILES['images']['name'][$key]);
        $fileType = pathinfo($originalName, PATHINFO_EXTENSION);
    
        // Allow certain file formats
        $allowTypes = array('jpg', 'png', 'jpeg', 'gif', 'webp');
        if (in_array(strtolower($fileType), $allowTypes)) {
            // Generate unique name using timestamp + random ID
            $uniqueName = uniqid('img_') . "_" . time() . "." . $fileType;
            $targetFilePath = $uploadDir . $uniqueName;
    
            if (move_uploaded_file($tmp_name, $targetFilePath)) {
                $values[] = "'$uniqueName'";
            }
        }
    }
    
    if (!empty($values)) {
        echo "Images uploaded successfully!";
    }
    
    if (!empty($category) && !empty($ptype) && !empty($pname) && !empty($pdes) &&!empty($values)) {
        $stmt = $conn->prepare("INSERT INTO project_list (client_name, project_category, project_type, project_title, project_description, project_images) VALUES (?, ?, ?, ?, ?, ?)");
        
        if (!$stmt) {
            die("Statement preparation failed: " . $conn->error);
        }

        $stmt->bind_param("ssssss",$client, $category, $ptype, $pname, $pdes, implode(",",$values));

        if ($stmt->execute()) {
            echo "Project added successfully!";
        } else {
            echo "Error executing statement: " . $stmt->error;
        }
        $stmt->close();
    } 
}
?>

?>