<?php include 'header.php'; 
$msg="";
if (isset($_GET['success'])) {
         $msg = '<div class="alert alert-success alert-top-border alert-dismissible fade show" role="alert">
                        <i class="ri-check-double-line me-3 align-middle fs-16 text-success"></i><strong>Success</strong> - Period updated
                        <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                    </div>';
}

if (isset($_GET['d-success'])) {
         $msg = '<div class="alert alert-success alert-top-border alert-dismissible fade show" role="alert">
                        <i class="ri-check-double-line me-3 align-middle fs-16 text-success"></i><strong>Success</strong> - Period deleted
                        <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                    </div>';
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$lower = $_POST['lower'];
$higher = $_POST['higher'];
$period = $_POST['period'];
                      $insert_query = "INSERT INTO project_period (user_id, lower_bid, higher_bid, period) VALUES ('$userId', '$lower', '$higher', '$period')";
if ($conn->query($insert_query) === TRUE) {
     $msg = '<div class="alert alert-success alert-top-border alert-dismissible fade show" role="alert">
                        <i class="ri-check-double-line me-3 align-middle fs-16 text-success"></i><strong>Success</strong> - Period added
                        <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                    </div>';
} else {
                    $msg = '<div class="alert alert-warning alert-top-border alert-dismissible fade show" role="alert">
                        <i class="ri-alert-line me-3 align-middle fs-16 text-warning"></i><strong>Warning</strong> - Period not added
                        <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                    </div>';
                }
}
?>

<div class="page-content">
    <div class="container-fluid">

        <!-- start page title -->
        <div class="row">
            <div class="col-12">
                <div class="page-title-box d-sm-flex align-items-center justify-content-between bg-transparent">
                    <h4 class="mb-sm-0">Project Period</h4>

                    <div class="page-title-right">
                        <ol class="breadcrumb m-0">
                            <li class="breadcrumb-item"><a href="javascript: void(0);">Dashboard</a></li>
                            <li class="breadcrumb-item active">Project Period</li>
                        </ol>
                    </div>

                </div>
            </div>
        </div>
        <!-- end page title -->

        <center>

            <div class="col-lg-6">
                <?php echo $msg; ?>
                <div class="card card-body text-center">

                    <h2>Project Period</h2>


                    <form method="POST" class="p-4">

                        <div class="row row-cols-lg-auto align-items-center g-3 justify-content-between">


                            <div class="mb-2 w-50">
                                <label class="float-start" for="lower">Lower bid price</label>
                                <input type="number" class="form-control" id="lower" name="lower" placeholder="0" required>
                            </div>


                            <div class="mb-2 w-50">
                                <label class="float-start" for="higher">Higher bid price</label>
                                <input type="number" class="form-control" id="higher" name="higher" placeholder="0" required>
                            </div>
                        </div>
                            <div class="mb-2">
                                <label class="float-start" for="period">Project Period (in days)</label>
                                <input type="number" class="form-control" id="period" name="period" placeholder="0" required>
                            </div>


                        <!--end row-->
                        <button type="submit" class="btn btn-success mt-4 w-100">Submit</button>
                    </form>
                </div>
                
                  <div class="table-responsive mt-6">
            <table class="table align-middle mb-0">
                <?php
$sql = "SELECT * FROM project_period WHERE user_id = '$userId' ORDER BY id DESC";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
?>
                <thead class="table-light">
                    <tr>
                        <th scope="col">Lower Bid Price</th>
                        <th scope="col">Higher Bid Price</th>
                        <th scope="col">Project period</th>
                        <th scope="col">Action</th>
                    </tr>
                </thead>
                <tbody>
                    <?php 
                      while ($row = $result->fetch_assoc()) {
        echo "<tr role='row' class='odd'><td>" . $row["lower_bid"] . "</td><td>" . $row["higher_bid"] . "</td><td>" . $row["period"] . " days</td><td><a class='btn btn-secondary btn-sm' href='edit-period.php?id=" . $row["id"] . "'>Edit</a><a class='mx-2 btn btn-danger btn-sm' onclick='if(confirm(\"Do you want to delete this?\")) { location.href=\"delete-period.php?id=" . $row["id"] . "\"; }'
>Delete</a></td></tr>";
    }
}
    ?>
                </tbody>
               
            </table>
            <!-- end table -->
        </div>
        
            </div>





        </center>
        <!-- container-fluid -->
    </div>

    <?php include 'footer.php'; ?>