<?php include 'dbcon.php'; ?>
<!DOCTYPE html>
<html>
<head>
    <title>Apply for Feasibility Study</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="bg-light">

<div class="container mt-5">
    <div class="row justify-content-center">
        <div class="col-md-8">

            <div class="card shadow">
                <div class="card-header bg-primary text-white">
                    <h4 class="mb-0">Feasibility Study Application</h4>
                </div>

                <div class="card-body">

                    <form method="post">

                        <div class="mb-3">
                            <label class="form-label">Name</label>
                            <input type="text" name="name" class="form-control" required>
                        </div>

                        <div class="mb-3">
                            <label class="form-label">Company Name</label>
                            <input type="text" name="company_name" class="form-control" required>
                        </div>

                        <div class="mb-3">
                            <label class="form-label">Mobile Number</label>
                            <input type="text" name="mobile" class="form-control" required>
                        </div>

                        <div class="mb-3">
                            <label class="form-label">Email</label>
                            <input type="email" name="email" class="form-control" required>
                        </div>

                        <div class="mb-3">
                            <label class="form-label">Address</label>
                            <textarea name="address" class="form-control" rows="3" required></textarea>
                        </div>

                        <div class="mb-3">
                            <label class="form-label">Project Location</label>
                            <input type="text" name="project_location" class="form-control" required>
                        </div>

                        <div class="mb-3">
                            <label class="form-label">Any Initial Notes</label>
                            <textarea name="initial_notes" class="form-control" rows="3"></textarea>
                        </div>

                        <div class="d-grid">
                            <button type="submit" name="submit" class="btn btn-primary">
                                Submit Application
                            </button>
                        </div>

                    </form>

                    <?php
                    if (isset($_POST['submit'])) {

                        $name = $_POST['name'];
                        $company = $_POST['company_name'];
                        $mobile = $_POST['mobile'];
                        $email = $_POST['email'];
                        $address = $_POST['address'];
                        $location = $_POST['project_location'];
                        $notes = $_POST['initial_notes'];

                            $sql = "INSERT INTO projects 
                                    (name, company_name, mobile, email, address, project_location, initial_notes,lead_status)
                                    VALUES 
                                    ('$name','$company','$mobile','$email','$address','$location','$notes','active')";



                        if (mysqli_query($conn, $sql)) {
                            echo "<div class='alert alert-success mt-3'>
                                    Application submitted successfully!
                                  </div>";
                                  
$project_id = mysqli_insert_id($conn);
                   $title = "New Enquiry";
$message = "New feasibility enquiry received from $name ($company)";
$link = "enquiry_view.php?project_id=".$project_id;

$sqlNotify = "INSERT INTO notifications 
(role, type, title, message, link) 
VALUES 
('admin', 'enquiry', '$title', '$message', '$link')";

mysqli_query($conn, $sqlNotify);
                        } else {
                            echo "<div class='alert alert-danger mt-3'>
                                    Error: " . mysqli_error($conn) . "
                                  </div>";
                        }
                    }
                    ?>

                </div>
            </div>

        </div>
    </div>
</div>

</body>
</html>
