<?php
// Include your necessary PHP functions and configuration here, such as the database connection and session start.
include('includes/header.php'); // Include the header

// Check if a project ID is present in the URL
if (!isset($_GET['projectID'])) {
    // Handle the case when projectID is missing in the URL
    echo '<h5>No project ID found!</h5>';
    exit(0);
}

// Retrieve the project ID from the URL
$projectID = $_GET['projectID'];

// Include any necessary validation or data retrieval functions
// For example: include('validation_functions.php');
?>

<div class="row">
    <div class="col-md-12">
        <div class="card">
            <div class="card-header">
                <h3>
                    Project Milestones
                    <a href="milestones.php" class="btn btn-danger float-end">Back</a>
                </h3>
            </div>
            <div class="project-card">
                <?php
                $paramResult = checkParamId('projectID');
                if (!is_numeric($paramResult)) {
                    echo '<h5>' . $paramResult . '</h5>';
                    return false;
                }

                $projects = getBymId('projects', checkParamId('projectID'));
                ?>

                <div class="row">
                    <div class="col-md-12 mb-4">
                        <div class="card card-body p-3">
                            <h3 class="text-lg mb-0 text-capitalize font-weight-bold">Project Name :
                                <?= $projects['data']['projectName']; ?>
                            </h3>
                            <h3 class="text-lg mb-0 text-capitalize font-weight-bold">Contract Value :
                                <?= $projects['data']['contractValue']; ?>
                            </h3>
                        </div>
                    </div>

                </div>
            </div>
            <div class="card-body">
                <?= alertMessage(); ?>
                <?php 
                if (isset($_SESSION['success_message'])) {
                    echo '<h6><div class="alert alert-success">' . $_SESSION['success_message'] . '</h6></div>';
                    // Remove the success message from the session to prevent it from displaying again
                    unset($_SESSION['success_message']);
                }
                ?>
                <table class="table table-bordered table-striped">
                    <thead>
                        <tr class="align-middle text-center">
                            <th>S.No.</th>
                            <th>Milestone</th>
                            <th>Milestone Value</th>
                            <th>Date of Completion</th>
                            <th>% of value</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php

                        $projectID = $_GET['projectID'];

                        // Query the milestones for the specified project
                        $query = "SELECT * FROM milestones WHERE projectID = $projectID";
                        $result = mysqli_query($conn, $query);

                        if (!$result) {
                            die("Query failed: " . mysqli_error($conn));
                        }

                        // Display project name
                        $projectQuery = "SELECT projectName FROM projects WHERE projectID = $projectID";
                        $projectResult = mysqli_query($conn, $projectQuery);
                        $projectRow = mysqli_fetch_assoc($projectResult);
                        $projectName = $projectRow['projectName'];

                        // Close the database connection
                        mysqli_close($conn);
                        ?>
                        <?php
                        $no = 1;
                        while ($row = mysqli_fetch_assoc($result)) { ?>
                            <tr class="align-middle text-center">
                                <td>
                                    <?php echo $no; ?>
                                </td>
                                <td>
                                    <?php echo $row['milestoneName']; ?>
                                </td>
                                <td>
                                    <?php echo $row['milestoneValue']; ?>
                                </td>
                                <td>
                                    <?php echo $row['completionDate']; ?>
                                </td>
                                <td>
                                    <?php
                                    $milestoneValue = $row['milestoneValue'];
                                    $contractValue = $projects['data']['contractValue'];
                                    $percentage = ($milestoneValue / $contractValue) * 100;
                                    echo number_format($percentage, 0); // Display percentage with 2 decimal places
                                    ?>%
                                </td>


                            </tr>
                            <?php $no++;
                        } ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>



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