<?php
session_start();
if(!isset($_SESSION['emailadd'])){
   header("Location: index.php");
}

include 'connect.php';
include("header.php");
include("footer.php");
include("nav.php");
include 'function.php';

$emailadd = $_SESSION['emailadd'];




if (isset($_POST['save'])) {
   $clientregno = $_POST['clientregno'];
   $jobno = get_next_docno();
   $jobname = $_POST['jobname'];
   $description = $_POST['description'];   
   $startdate = $_POST['startdate'];  
   $finishdate = $_POST['finishdate']; 
   $finish = isset($_POST['finish']) ? 1 : 0;
   $budget = $_POST['budget'];
   $revenue = $_POST['revenue'];


   $query = "INSERT INTO job (clientregno, jobno, jobname, description, startdate, finishdate, finish, budget, revenue)
             VALUES ('$clientregno', '$jobno', '$jobname', '$description', '$startdate', '$finishdate', '$finish', '$budget', '$revenue')";

// Insert into running doc no

$query03 = "INSERT INTO rundocno (docno) VALUES ('$jobno')";
$result03 = mysqli_query($conn, $query03);              

if (mysqli_query($conn, $query)) {
   echo "<script>alert('New record created successfully');</script>";
} else {
   echo "Error: " . $query . "<br>" . mysqli_error($conn);
}

 }

?>


<div class="container">

    <main>
      <h2>Add New Job / Project</h2>
      <form action="job.php" method="post">
        <div class="form-group">
          <label for="clientregno">Nama Client:</label>
        <select id="clientregno" name="clientregno" required>
          <option value="">- Select Customer -</option>
          <?php
          include 'connect.php';
          $q = $_GET['q'];
        $query = "SELECT clientregno, custname FROM customer WHERE clientregno LIKE '%$q%' OR custname LIKE '%$q%'";
          $result = mysqli_query($conn, $query);
          while ($row = mysqli_fetch_assoc($result)) {
            echo "<option value='{$row['clientregno']}'>{$row['custname']}</option>";
          }
          ?>
        </select>
        </div>
        <div class="form-group">
          <label for="jobname">Job Name :</label>
          <input type="text" name="jobname" required><br>
        </div>
        <div class="form-group">
          <label for="description">Descriptions :</label>
          <input type="text" name="description" required><br>      
        </div>
        <div class="form-group">
          <label for="startdate">Start Date :</label>
          <input type="date" name="startdate" required><br>      
        </div>
        <div class="form-group">
          <label for="finishdate">Finish Date :</label>
          <input type="date" name="finishdate" required><br>      
        </div>
        <div class="form-group">
          <label for="budget">Project Budget :</label>
          <input type="text" name="budget"><br>      
        </div>
        <div class="form-group">
          <label for="revenue">Project Revenue :</label>
          <input type="text" name="revenue"><br>      
        </div>        
        <div>
          <label for="finish">Finish?</label>
          <input type="checkbox" name="finish" value="1"><br>      
        </div>
        <input type="submit" name="save" value="Save">
      </form>
    </main>
  </div>