<?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'];
}



if (isset($_POST['addJob'])) {
$job_id = $_POST['job_id'] ?? '';
$item_name = $_POST['item_name'] ?? '';
$item_size = $_POST['item_size'] ?? '';
$customer_id = $_POST['customer_id'] ?? '';
$customer_name = $_POST['customer_name'] ?? '';
$quantity = $_POST['quantity'] ?? '';
$delivery_date = $_POST['delivery_date'] ?? '';
$date_time = date('Y-m-d');
$user_name = $userName ?? 'admin';
$status = 'process';
$user_role = $userRole ?? 'superadmin';


$job_proces = $_POST['job_proces'] ?? [];
$noofdays = $_POST['noofdays'] ?? [];


if (!empty($job_proces) && !empty($noofdays)) {
foreach ($job_proces as $index => $process) {
$days = $noofdays[$index] ?? 0;


$query = "INSERT INTO add_jobs
(job_id, item_name, item_size, customer_id, customer_name, quantity, delivery_date, date_time, job_proces, noofdays, status, user_name, user_role)
VALUES
('$job_id', '$item_name', '$item_size', '$customer_id', '$customer_name', '$quantity', '$delivery_date', '$date_time', '$process', '$days', '$status', '$user_name', '$user_role')";


mysqli_query($connection, $query);
}
echo "<script>window.location.href = window.location.href;</script>";
exit;
} else {
echo "No process selected.";
}
}

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, tr, option{
	  	font-weight:700;
	  	color:#000;
	  	
		}
		.form-select-sm {
      width: auto; 
      min-width: 70px;
    }
    .form-check-input {
      margin-left: 0.5rem;
    }
    
  </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');
  $pr_id2 = str_pad(rand(0, 9999), 4, '0', STR_PAD_LEFT);
  ?>

                    <!-- 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">
                               
                             </div>
                         </div>
                        
                        
                    </div>
                    <br>
                     <form action="add_job.php" method="POST">
  <div class="row" style="padding:20px; background:#EEEEF2;border-radius:14px;border:1px solid #0A4657;">
    <div class="col-3">
      <div class="form-group">
        <label>Job - ID#</label>
        <input type="text" name="job_id" class="form-control" value="<?php echo $pr_id2; ?>" style="background:#0A4657;color:#fff;" readonly />
      </div>
    </div>
    <div class="col-3">
      <div class="form-group">
        <label>Item Name:</label>
        <input type="text" name="item_name" class="form-control">
      </div>
    </div>
    <div class="col-3">
      <div class="form-group">
        <label>Item Size:</label>
        <input type="text" name="item_size" class="form-control">
      </div>
    </div>
    <div class="col-3">
      <div class="form-group">
        <label>Customer - ID#</label>
        <input type="text" name="customer_id" class="form-control" />
      </div>
    </div>
    <div class="col-3">
      <div class="form-group">
        <label>Customer Name:</label>
        <input type="text" name="customer_name" class="form-control" />
      </div>
    </div>
    <div class="col-3">
      <div class="form-group">
        <label>Qty - Required</label>
        <input type="text" name="quantity" class="form-control" />
      </div>
    </div>
    <div class="col-3">
      <div class="form-group">
        <label>Delivery - Date</label>
        <input type="date" name="delivery_date" class="form-control" />
      </div>
    </div>
    <div class="col-3">
      <button type="submit" name="addJob" class="btn btn-primary" style="width:100%;font-weight:700; margin-top:30px;">Assign - Job</button>
    </div>
  </div>
  <br>
<div class="container my-3">
  <div class="row">
    <div class="col-md-6">
      <h5>Add New Process</h5>
      <div class="input-group mb-3">
        <input type="text" id="newProcess" class="form-control" placeholder="Enter Process Name">
        <input type="number" id="newDays" class="form-control" placeholder="Days" min="1" max="30">
        <button class="btn btn-success" onclick="addProcess()">Add</button>
      </div>

      <table class="table align-middle shadow-sm bg-white" id="processTable">
        <thead class="table-dark text-center">
          <tr class="text-center">
            <th style="width: 60px; color:#fff;">Select</th>
            <th style="color:#fff;">Process</th>
            <th style="width: 120px;color:#fff;">Days</th>
            <th style="width: 100px; color:#fff;">Action</th>
          </tr>
        </thead>
        <tbody id="processBody">
          <!-- Processes load here -->
        </tbody>
      </table>
    </div>
  </div>
</div>

<script>
// Load from localStorage if available
let processes = JSON.parse(localStorage.getItem("processes")) || [
  { name: "Tiyari", days: 5 },
  { name: "Forging", days: 10 },
  { name: "Naram", days: 3 }
];

// render function
function renderTable() {
  const tbody = document.getElementById("processBody");
  tbody.innerHTML = "";

  processes.forEach((proc, index) => {
    const row = document.createElement("tr");

    row.innerHTML = `
      <td class="text-center"><input type="checkbox"></td>
      <td>${proc.name}</td>
      <td>${proc.days} days</td>
      <td>
        <button class="btn btn-sm btn-danger" onclick="toggleProcess(this)">
          Hide
        </button>
      </td>
    `;

    tbody.appendChild(row);
  });
}

// add new process
function addProcess() {
  const name = document.getElementById("newProcess").value.trim();
  const days = parseInt(document.getElementById("newDays").value);

  if (!name || isNaN(days) || days <= 0) {
    alert("Enter valid process name and days!");
    return;
  }

  processes.push({ name, days });
  localStorage.setItem("processes", JSON.stringify(processes)); // save new process permanently

  document.getElementById("newProcess").value = "";
  document.getElementById("newDays").value = "";
  renderTable();
}

// toggle hide (temporary only, not saved in localStorage)
function toggleProcess(btn) {
  const row = btn.closest("tr");
  if (row.style.display === "none") {
    row.style.display = "";
    btn.textContent = "Hide";
    btn.className = "btn btn-sm btn-danger";
  } else {
    row.style.display = "none";
    btn.textContent = "Active";
    btn.className = "btn btn-sm btn-secondary";
  }
}

renderTable();
</script>


</form>

                  
                 




                </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>