<?php include('includes/header.php'); ?>

<div class="row">
    <div class="col-md-12">
        <div class="card">
            <div class="card-header">
                <h4>
                    Edit Project
                    <a href="projects.php" class="btn btn-danger float-end">Back</a>
                </h4>
            </div>
            <div class="card-body">
                <form action="code.php" method="POST">
                    <?php
                    // Retrieve the projectID from the URL
                    $projectID = $_GET['projectID'];


                    // Fetch project details based on projectID
                    $projectQuery = "SELECT * FROM projects WHERE projectID = '$projectID'";
                    $projectResult = mysqli_query($conn, $projectQuery);
                    $projectData = mysqli_fetch_assoc($projectResult);

                    if ($projectData) {
                        ?>
                        <input type="hidden" name="projectID" value="<?= $projectID; ?>">
                        <div class="row">
                            <div class="col-md-6">
                                <div class="mb-3">
                                    <label>Project Name</label>
                                    <input type="text" name="projectName" value="<?= $projectData['projectName']; ?>" class="form-control">
                                </div>
                            </div>

                            <div class="col-md-6">
                                <div class="mb-3">
                                    <label>Client Name</label>
                                    <select name="clientID" class="form-control">
                                        <option value="">Select Client</option>
                                        <?php
                                        // Fetch clients from the database
                                        $sqlFetchClients = "SELECT clientID, clientName FROM clients";
                                        $result = mysqli_query($conn, $sqlFetchClients);
                                        while ($row = mysqli_fetch_assoc($result)) {
                                            $clientID = $row['clientID'];
                                            $clientName = $row['clientName'];
                                            $selected = ($clientID == $projectData['clientID']) ? 'selected' : '';
                                            echo "<option value='$clientID' $selected>$clientName</option>";
                                        }
                                        ?>
                                    </select>
                                </div>
                            </div>

                            <div class="col-md-6">
                                <div class="mb-3">
                                    <label>Contract Value</label>
                                    <input type="text" name="contractValue" value="<?= $projectData['contractValue']; ?>" class="form-control">
                                </div>
                            </div>

                            <div class="col-md-6">
                                <div class="mb-3">
                                    <br>
                                    <button type="submit" name="updateProject" class="btn btn-primary">Update</button>
                                </div>
                            </div>
                        </div>
                        <?php
                    } else {
                        echo '<h5>Project not found!</h5>';
                    }
                    ?>

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

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