<?php
ob_start();
include("db/cn.php");
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

if (isset($_SESSION['user_name']) && isset($_SESSION['user_role']) && isset($_SESSION['outlet_name']) && isset($_SESSION['outlet_address'])) {
    $userName = $_SESSION['user_name'];
    $userRole = $_SESSION['user_role'];
    $outlet_address = $_SESSION['outlet_address'];
    $outlet_name = $_SESSION['outlet_name'];
}
@$customerName = $_GET['customer_name'];


ob_end_flush();
?>

<!DOCTYPE html>
<html lang="en">

<head>

    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="">
    <meta name="author" content="">

    <title>ALFALAH INDUSTRIES</title>

    <!-- Custom fonts for this template-->
    <link href="../vendor/fontawesome-free/css/all.min.css" rel="stylesheet" type="text/css">
   <link href="https://fonts.googleapis.com/css2?family=Quicksand:wght@200;300;400;500;600;700&display=swap" rel="stylesheet">

    <!-- Custom styles for this template-->
    <link href="../css/sb-admin-2.min.css" rel="stylesheet">
    
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
</head>
<style>
body {
    font-family: 'Quicksand', sans-serif;
    
}
		    .chart-container {
      position: relative;
      background: rgba(255, 255, 255, 0.9);
      border-radius: 10px;
      padding: 15px;
      box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
    }

    canvas {
      max-width: 100%;
      height: auto;
    }
.bg-gradient-primary{
    
	background:#0A4657;
	color:#fff;	
	
	}

.btn-primary{
	background:#0A4657;
	color:#fff;
	 border:1px #0A4657 solid;
	
	}  
.btn-primary:hover{
  
  background:#fff;
  border:1px #0A4657 solid;
	color:#0A4657;
	
	}	 
    .chart-container {
      position: relative;
      background: rgba(255, 255, 255, 0.9);
      border-radius: 10px;
      padding: 15px;
      box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
    }

    canvas {
      max-width: 100%;
      height: auto;
    }
	
	.fa-download:hover{
		color:#953E39;
		}
		
		 .form-control, label{
	  	font-weight:700;
	  	color:#000;
		}
  </style>

<body id="page-top">

    <!-- Page Wrapper -->
    <div id="wrapper">

        <!-- Sidebar -->
        <?php
        include("common/sd.php");
		?>
        <!-- End of Sidebar -->

        <!-- Content Wrapper -->
        <div id="content-wrapper" class="d-flex flex-column">

            <!-- Main Content -->
            <div id="content">

                <!-- Topbar -->
                <nav class="navbar navbar-expand navbar-light bg-white topbar mb-4 static-top shadow">

                    <!-- Sidebar Toggle (Topbar) -->
                    <button id="sidebarToggleTop" class="btn btn-link d-md-none rounded-circle mr-3">
                        <i class="fa fa-bars"></i>
                    </button>

                    <!-- Topbar Search -->
                    
  <?php include('common/log.php'); ?>
        <?php
      
        ?>
                    <!-- Topbar Navbar -->
                    
                </nav>
                <!-- End of Topbar -->

                <!-- Begin Page Content -->
                
                
             <div class="container-fluid">
                     <div class="">
                         <div class="row">
                             <div class="col-6">
                               <h1 class=" mb-0 text-gray-800" style="font-weight:700;">Job - Management </h1>  
                             </div>
                             <div class="col-6">
                                 <a href="view_jobs.php" class="btn btn-dark" style="float:right;"><i class="fas fa-arrow-left"></i></a>
                             </div>
                         </div>
                    </div>
                    <br>
                    <div class="container">
                   <table class="table table-bordered bg-white font-weight-bold text-capitalize text-center">
  <tr class="bg-dark text-white">
    <td>Sr#</td>
    <td>Job - ID#</td>
    <td>Customer</td>
    <td>Delivery</td>
    <td>Status</td>
    <td style="background:#516D45; color:#fff;">Next Step In Process</td>
    <td style="background:#0A4657; color:#fff;">Remain Days</td>
    <td>View Detail</td>
    <td>Edit</td>
  </tr>
  <?php
  $sr = 1;
  $r = mysqli_query($connection,"SELECT * FROM add_jobs WHERE status='process' && customer_name ='$customerName' GROUP BY job_id");
  while($t = mysqli_fetch_array($r)){
      $job_id = $t['job_id'];

      // fetch steps for this job dynamically
      $steps = [];
      $res_steps = mysqli_query($connection, "SELECT job_proces, noofdays FROM add_jobs WHERE job_id='$job_id' && customer_name = '$customerName' ORDER BY id ASC");
      while ($row = mysqli_fetch_assoc($res_steps)) {
          $steps[] = [
              'name' => $row['job_proces'],
              'duration' => (int)$row['noofdays']
          ];
      }

      // fetch saved statuses for this job
      $saved_statuses = [];
      $res = mysqli_query($connection, "SELECT step_process, status FROM job_status WHERE job_id='$job_id'");
      while ($row = mysqli_fetch_assoc($res)) {
          $saved_statuses[$row['step_process']] = $row['status'];
      }

      // find last completed step
      $lastCompletedIndex = -1;
      foreach ($steps as $index => $step) {
          if (isset($saved_statuses[$step['name']]) && $saved_statuses[$step['name']] === 'Complete') {
              $lastCompletedIndex = $index;
          }
      }

      // find next step
      $nextStep = "-";
      $remainDays = "-";
      if ($lastCompletedIndex + 1 < count($steps)) {
          $nextStep = $steps[$lastCompletedIndex + 1]['name'];
          $remainDays = $steps[$lastCompletedIndex + 1]['duration'];
      }
  ?>
  <tr class="font-weight-bold" style="color:#000;">
    <td><?php echo $sr++; ?></td>
    <td><?php echo $t['job_id']; ?></td>
    <td><?php echo $t['customer_name']; ?></td>
    <td><?php echo $t['delivery_date']; ?></td>
    <td class="text-capitalize"><?php echo $t['status']; ?></td>
    <td><?php echo $nextStep; ?></td>
    <td><?php echo $remainDays !== '-' ? $remainDays . ' days' : '-'; ?></td>
    <td><a href='job_detail.php?job_id=<?php echo $t['job_id']; ?>' class="btn btn-primary"><i class="fas fa-eye fa-1x"></i></a></td>
    <td><a href='job_edit.php?job_id=<?php echo $t['job_id']; ?>' class="btn btn-success"><i class="fas fa-edit fa-1x"></i></a></td>
  </tr>
  <?php } ?>
</table>

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

            </div>
            <br><br>
            <!-- End of Main Content -->
            <!-- Footer -->
            <?php include("common/main_ft.php");  ?>
            <!-- End of Footer -->
        </div>
        <!-- End of Content Wrapper -->
    </div>
    <!-- End of Page Wrapper -->

    <!-- Scroll to Top Button-->
    <a class="scroll-to-top rounded" href="#page-top">
        <i class="fas fa-angle-up"></i>
    </a>

    <!-- Logout Modal-->
    <div class="modal fade" id="logoutModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel"
        aria-hidden="true">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="exampleModalLabel">Ready to Leave?</h5>
                    <button class="close" type="button" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">×</span>
                    </button>
                </div>
                <div class="modal-body">Select "Logout" below if you are ready to end your current session.</div>
                <div class="modal-footer">
                    <button class="btn btn-secondary" type="button" data-dismiss="modal">Cancel</button>
                    <a class="btn btn-primary" href="">Logout</a>
                </div>
            </div>
        </div>
    </div>

 
</div>    
</body>
 <script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.10.2/dist/umd/popper.min.js"></script>

   <script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.10.2/dist/umd/popper.min.js"></script>
    <!-- Bootstrap core JavaScript-->
    <script src="../vendor/jquery/jquery.min.js"></script>
    <script src="../vendor/bootstrap/js/bootstrap.bundle.min.js"></script>

    <!-- Core plugin JavaScript-->
    <script src="../vendor/jquery-easing/jquery.easing.min.js"></script>

    <!-- Custom scripts for all pages-->
    <script src="../js/sb-admin-2.min.js"></script>

</body>

</html>