<?php
session_start();
$where = " WHERE 1=1 ";
$next_page_condition = "";
$paging = "";

if ($_REQUEST['start']) {
	$paging .= "&start=" . $_REQUEST['start'];
}
if (isset($_REQUEST["from"]) && $_REQUEST["from"] != "") {
	$_SESSION["from"] = $_REQUEST["from"];
}
if (isset($_REQUEST['id']) && $_REQUEST['id'] != "") {
	$where .= " AND id = " . $_REQUEST['id'] . " ";
}
$work_order_id = utility::app_crypt($_REQUEST['work_order_id'], 'd');
if ($_REQUEST['id'] != $work_order_id) {
	die("Invaild URL");
}

$query_work_order_main = "SELECT * FROM work_order WHERE id = $work_order_id";
$rs_work_order_main = mysqli_query($conn, $query_work_order_main);
$work_order_main_row = mysqli_fetch_assoc($rs_work_order_main);
$work_order_main_row = utility::validate_crypt_data($work_order_main_row);
$product_id = $work_order_main_row['product_id'];
$customer_id = $work_order_main_row['customer_id'];
$order_quantity = $work_order_main_row['order_quantity'];
$require_date = $work_order_main_row['require_date'];
$shift_ids = $work_order_main_row['shift_ids'];

$query_count = "SELECT COUNT(DISTINCT product_code) as distinct_count FROM work_order_detail WHERE work_order_id = $work_order_id";
$rs_count = mysqli_query($conn, $query_count);
$row_count = mysqli_fetch_assoc($rs_count);
$distinct_no_of_operation = $row_count['distinct_count'];

// Query to fetch grouped data by product_code
$query = "SELECT * FROM work_order_detail WHERE work_order_id = $work_order_id ORDER BY product_code DESC";

$rs = mysqli_query($conn, $query);
$work_order_data = [];
while ($row = mysqli_fetch_assoc($rs)) {
    $work_order_data[$row['product_code']][] = $row; // Group by product_code
}
//print_r($work_order_data);
?>

<section class="content-header responsebar">
	<div class="container-fluid">
		<div class="row">
			<div class="col-lg-12 col-md-6 col-sm-6">
				<div class="col-sm-6">
					<h1 style="float:left;clear:right;">
						Work Order Creation: - <span style='color:#ed5c5c;'><?= $gsi->getworkItemById($product_id); ?></span>
					</h1>
				</div>
				<?
				$query_string = (isset($_POST['query_string'])) ? $_POST['query_string'] : $_SERVER['QUERY_STRING'];
				$query_string = str_ireplace("page=view_work_order_detail", "page=view", $query_string);
				?>
				<button type="button" id="back" name="back" value="back" style="float:right;margin-bottom: 15px;"
					class="btn btn-primary"
					onclick="window.location.href = 'index?<?= $query_string . $next_page_condition . $paging; ?>'">Back</button>
			</div>
		</div>
	</div>
</section>

<section class="content">
    <div class="container-fluid">
        <div class="row">
            <div class="col-lg-12 no-padding">
                <div class="row">
                   <div class="form-group col-lg-6 d-flex">
						<label for="Item No." class="col-sm-4 control-label">Group No.</label>
						<div class="col-sm-8">
							<? echo $gsi->getworkItemById($product_id); ?>
						</div>
					</div>
					<div class="form-group col-lg-6 d-flex">
						<label for="Tax Code" class="col-sm-4 control-label">Order Quantity</label>
						<div class="col-sm-8">
							<? echo $order_quantity; ?>
						</div>
					</div>
					<div class="form-group col-lg-6 d-flex">
						<label for="Tax Code" class="col-sm-4 control-label">Required Date</label>
						<div class="col-sm-8">
							<? echo  utility::getFormatedDate($require_date); ?>
						</div>
					</div>
					<div class="form-group col-lg-6 d-flex">
						<label for="Tax Code" class="col-sm-4 control-label">Customer</label>
						<div class="col-sm-8">
							<? echo $gsi->getCustomerById($customer_id); ?>
						</div>
					</div>
					<div class="form-group col-lg-6 d-flex">
						<label for="Tax Code" class="col-sm-4 control-label">Shifts</label>
						<div class="col-sm-8">
							<? $shift_id_array = explode(',', $shift_ids);
							   $shift_id_name = ""; 

								foreach ($shift_id_array as $shift_id) {
									$shift_id_name .= $gsi->getShiftNameById($shift_id) . ", ";
								}

								$shift_id_name = substr($shift_id_name, 0, -2); 

								echo $shift_id_name;
							?>
						</div>
					</div>
                </div>
            </div>
        </div>
    </div>
</section>
<section class="content">
    <div class="container-fluid">
        <div class="row">
            <div class="col-lg-12 no-padding">
                <div class="row">
                    <?php
                    if ($distinct_no_of_operation > 0) {
                        $col_width = 12 / $distinct_no_of_operation; // Calculate column width
                        foreach ($work_order_data as $product_code => $rows) {
                            ?>
                            <div class="col-lg-<?php echo $col_width; ?> col-md-<?php echo $col_width; ?> col-sm-12" style="min-height: 400px;">
                                <div class="scrollbar-default box box-warning card" style="height: 350px; overflow: hidden; overflow-y: scroll;">
                                    <div class="box-header">
                                        <h3 class="box-title"><?php echo htmlspecialchars($product_code); ?></h3>
                                    </div>
									<div class="box-body">

										<?php
										foreach ($rows as $row) {
										$color = ''; // Default color
										if ($row['process_type'] == 4) {
											$color = 'background-color: lightcoral;'; // Light red for process_type 4
										} elseif (in_array($row['process_type'], [12, 21])) {
											$color = 'background-color: lightblue;'; // Light blue for process_type 12 and 21
										} elseif ($row['process_type'] == 6) {
											$color = 'background-color: lightyellow;'; // Yellow for process_type 6
										}
									
										// Output the data with the corresponding background color
										echo "<p style='$color'>"
											. "Product: "
											. htmlspecialchars($row['product_id']).' '. htmlspecialchars($row['product_name'])
											. " - "
											. htmlspecialchars($row['operation_description'])
											. " "
											. "Qty:"
											. " - "
											. htmlspecialchars($row['quantity'])
											. " "
											. "Required Date:"
											. " - "
											. htmlspecialchars($row['required_date'])
											. "</p>";
									}
									?>
										
									</div><!-- /.box-body -->
                                </div><!-- /.box -->
                            </div>
                            <?php
                        }
                    }
                    ?>
                </div>
            </div>
        </div>
    </div>
</section>