<?php
session_start();
if(!isset($_SESSION['emailadd'])){
   header("Location: index.php");
}

include 'connect.php';
include("header.php");
include("footer.php");
include("nav.php");
$emailadd = $_SESSION['emailadd'];

if (isset($_POST['update'])) {
   $clientregno = $_POST['clientregno'];
   $jobname = $_POST['jobname'];
   $description = $_POST['description'];
   $startdate = $_POST['startdate'];
   $finishdate = $_POST['finishdate'];
   $budget = $_POST['budget'];
   $revenue = $_POST['revenue'];
   $finish = isset($_POST['finish']) ? 1 : 0;
   $jobno = $_POST['jobno'];

   $query = "UPDATE job SET clientregno='$clientregno', jobname='$jobname', description='$description', startdate='$startdate', finishdate='$finishdate', budget='$budget', revenue='$revenue', finish='$finish' WHERE jobno='$jobno'";

   if (mysqli_query($conn, $query)) {
      echo "<script>alert('Record updated successfully');</script>";
   } else {
      echo "Error: " . $query . "<br>" . mysqli_error($conn);
   }
}

if (isset($_GET['jobno'])) {
   $jobno = $_GET['jobno'];
   $query = "SELECT * FROM job WHERE jobno='$jobno'";
   $result = mysqli_query($conn, $query);
   $row = mysqli_fetch_assoc($result);
} else {
   header("Location: show_job.php");
}

?>

<div class="container">
   <main>
      <h2>Edit Job / Project</h2>
      <form action="edit_job.php" method="post">
         <div class="form-group">
            <label for="clientregno">Client Name:</label>
            <select id="clientregno" name="clientregno" required>
               <option value="">- Select Customer -</option>
               <?php
               $query = "SELECT clientregno, custname FROM customer";
               $result = mysqli_query($conn, $query);
               while ($row1 = mysqli_fetch_assoc($result)) {
                  if ($row1['clientregno'] == $row['clientregno']) {
                     echo "<option value='{$row1['clientregno']}' selected>{$row1['custname']}</option>";
                  } else {
                     echo "<option value='{$row1['clientregno']}'>{$row1['custname']}</option>";
                  }
               }
               ?>
            </select>
         </div>
         <div class="form-group">
            <label for="jobname">Job Name:</label>
            <input type="text" name="jobname" value="<?php echo $row['jobname']; ?>" required><br>
         </div>
         <div class="form-group">
            <label for="description">Description:</label>
            <input type="text" name="description" value="<?php echo $row['description']; ?>" required><br>
         </div>
         <div class="form-group">
            <label for="startdate">Start Date:</label>
            <input type="date" name="startdate" value="<?php echo $row['startdate']; ?>" required><br>
         </div>
         <div class="form-group">
            <label for="finishdate">Finish Date:</label>
            <input type="date" name="finishdate" value="<?php echo $row['finishdate']; ?>"><br>
</div>
<div class="form-group">
<label for="budget">Project Budget:</label>
<input type="text" name="budget" value="<?php echo $row['budget']; ?>"><br>
</div>
<div class="form-group">
<label for="revenue">Project Revenue:</label>
<input type="text" name="revenue" value="<?php echo $row['revenue']; ?>"><br>
</div>
<div class="form-group">
<label for="finish">Finish?</label>
<input type="checkbox" name="finish" value="1" <?php if ($row['finish'] == 1) { echo "checked"; } ?>><br>
</div>
<input type="hidden" name="jobno" value="<?php echo $row['jobno']; ?>">
<input type="submit" name="update" value="Update">
</form>

   </main>
</div>
