<style>
.style15 {
    font-family: Verdana, Arial, Helvetica, sans-serif;
    font-size: 14px;
    font-weight: bold;
}
</style>

<script>
function edit_job_details(edit_id, jobcard_no) {
    window.location.href = "main_fram.php?file=jobcard_creation&main_id=" + edit_id + "&jobcard_no=" + jobcard_no;
}
</script>

<?php
error_reporting(0);
include ("../../include/db.php");
ob_start();
session_start();
$user_id = $_SESSION['login_user_id'];

$show = 20;
$num_links = 19;

// PHP 5.6 compatible $_GET checks
$from = isset($_GET['from']) ? $_GET['from'] : 0;
$cust_name = isset($_GET['customer_name']) ? $_GET['customer_name'] : '';
$from_date = isset($_GET['from_date']) ? $_GET['from_date'] : '';
$to_date = isset($_GET['to_date']) ? $_GET['to_date'] : '';
$status = isset($_GET['jobtype']) ? $_GET['jobtype'] : '';
$job_card_num = isset($_GET['job_card_num']) ? $_GET['job_card_num'] : '';
$status1 = isset($_GET['status1']) ? $_GET['status1'] : '';
$fabric_types = isset($_GET['fabric_type']) ? $_GET['fabric_type'] : '';

$value = explode(',', $fabric_types);
$fabric1 = '';
for ($i = 0; $i < count($value); $i++) {
    $fabric1 .= $value[$i];
    if ($i < count($value) - 1) {
        $fabric1 .= "','";
    }
}

$current_date = date('Y-m-d');

$customer_name1 = ($cust_name != "") ? "cust_name='$cust_name'" : '';
$from_date1 = ($from_date != "") ? "DATE_FORMAT(entry_date,'%Y-%m-%d')>='$from_date'" : "DATE_FORMAT(entry_date,'%Y-%m-%d')='$current_date'";
$to_date1 = ($to_date != "") ? "DATE_FORMAT(entry_date,'%Y-%m-%d')<='$to_date'" : "DATE_FORMAT(entry_date,'%Y-%m-%d')='$current_date'";
$jobtype1 = ($status != "") ? "job_type='$status'" : '';
$job_card_nums = ($job_card_num != "") ? "jobcard_no='$job_card_num'" : '';
$fabric_type1 = ($fabric_types != "") ? "fabric_type IN ('$fabric1')" : '';
$status1_condition = ($status1 != "") ? "approval_status='$status1'" : '';

$all_value10 = $customer_name1 . ";" . $from_date1 . ";" . $to_date1 . ";" . $jobtype1 . ";" . $job_card_nums . ";" . $fabric_type1 . ";" . $status1_condition;
$all_array10 = explode(';', $all_value10);

$get_query102 = '';
foreach ($all_array10 as $value10) {
    if ($value10 != '') {
        $get_query102 .= $value10 . " AND ";
    }
}

// SQL query
if ($from_date == '' && $to_date == '') {
    $sql = "SELECT * FROM jobcard_main WHERE DATE_FORMAT(entry_date,'%Y-%m-%d')='$current_date' ORDER BY jobcard_no ASC";
} else {
    $sql = "SELECT * FROM jobcard_main WHERE $get_query102 main_id!='' ORDER BY jobcard_no ASC";
}

$html_query = "main_fram.php?file=jobcard_creation_list";
$result = mysql_query($sql);
$num_results = mysql_num_rows($result);

if ($num_results == 0) {
    $confirmation = "No Records!";
} elseif ($num_results == 1) {
    $confirmation = "Found 1 Record.";
} else {
    $confirmation = "Found $num_results Records.";
}
?>

<style type="text/css">
.style1 { font-size: 16px; }
</style>

<table width="100%">
<tr>
<td width="1%">&nbsp;</td>
<td width="7%" class="style15">&nbsp;</td>
<td width="13%" align="center">&nbsp;</td>
<td width="1%">&nbsp;</td>
<td width="6%" class="style15">&nbsp;</td>
<td width="14%" align="center" class="style15">&nbsp;</td>
<td width="2%">&nbsp;</td>
<td width="11%" class="style15">&nbsp;</td>
<td width="24%" align="left">&nbsp;</td>
<td width="8%" align="center">&nbsp;</td>
<td width="2%">
<a onclick="javascript:jobcard_creation_report_overall_excel('<?php echo $cust_name; ?>','<?php echo $from_date; ?>','<?php echo $to_date; ?>','<?php echo $status; ?>','<?php echo $job_card_num; ?>','<?php echo $fabric_types; ?>')" style="float:right">
<img align="right" src="img/excel-icon.png" width="30" height="30" border="0" title="click to export">
</a>
</td>
<td width="7%" align="center">
<a href="javascript:print_jobcard('jobcard/jobcard_entry/jobcard_main_print.php?customer_name=<?php echo $cust_name;?>&from_date=<?php echo $from_date;?>&to_date=<?php echo $to_date;?>&jobtype=<?php echo $status; ?>&job_card_num=<?php echo $job_card_num; ?>&fabric_types=<?php echo $fabric_types; ?>');" style="text-decoration:none;">
<img src="img/PrinterBlack.png" width="35" height="35" title="Print"/>
</a>
</td>
</tr>
</table>

<div class="row-fluid">
<div class="span12">
<div class="table-responsive">
<div class="CSSTableGenerator">
<table width="99%" border="0" align="center" cellpadding="0" cellspacing="0" style="border:2px solid #eaeaea;border:1px solid #ddd">
<tr>
<th align="center" style="border:1px solid #ddd">S.No</th>
<th scope="col" align="left" style="border:1px solid #ddd">&nbsp;Date</th>
<th width="120" align="left" style="border:1px solid #ddd" scope="col">&nbsp;&nbsp;Jobcard No</th>
<th scope="col" align="left" style="border:1px solid #ddd">Created By</th>
<th width="255" align="left" scope="col" style="border:1px solid #ddd">Customer Name</th>
<th width="131" align="left" scope="col" style="border:1px solid #ddd">Job Type</th>
<th width="120" align="left" style="border:1px solid #ddd" scope="col">&nbsp;&nbsp;Fabric Type</th>
<th width="108" align="right" scope="col" style="border:1px solid #ddd">Total Qty&nbsp;</th>
<th width="90" align="right" scope="col" style="border:1px solid #ddd">Total Value&nbsp;</th>
<th width="90" align="right" scope="col" style="border:1px solid #ddd">Avg Rate&nbsp;</th>
<th width="90" align="right" scope="col" style="border:1px solid #ddd">L- confirm rate&nbsp;</th>
<th width="90" align="right" scope="col" style="border:1px solid #ddd">H- confirm rate&nbsp;</th>
<th width="85" align="center" scope="col" style="border:1px solid #ddd">View</th>
<?php if ($user_id == '1') { ?>
<th width="45" align="center" scope="col" style="border:1px solid #ddd">Edit</th>
<th width="69" align="center" scope="col" style="border:1px solid #ddd">Delete</th>
<?php } ?>
<th width="69" align="center" scope="col" style="border:1px solid #ddd" colspan="2">Approavl</th>
</tr>

<?php
// Include DB again if needed
include ("../../include/db.php");

// Functions
function place_branch_name($book_branch) {
    $sql_user = "SELECT * FROM branch_creation WHERE branch_id='$book_branch' AND delete_status!='1'";
    $rs_user = mysql_query($sql_user);
    while ($rsdata_user = mysql_fetch_object($rs_user)) {
        $branch_name = $rsdata_user->branch_name;
    }
    return $branch_name;
}

function get_tot_qty_jobcard($jobcard_no) {
    $sql_user = "SELECT SUM(qty) AS qty FROM jobcard_sublist WHERE jobcard_no='$jobcard_no'";
    $rs_user = mysql_query($sql_user);
    while ($rsdata_user = mysql_fetch_object($rs_user)) {
        $qty12 = $rsdata_user->qty;
    }
    return $qty12;
}

function book_place_names($book_place) {
    $sql_user = "SELECT * FROM place_creation WHERE id='$book_place'";
    $rs_user = mysql_query($sql_user);
    while ($rsdata_user = mysql_fetch_object($rs_user)) {
        $location = $rsdata_user->location;
    }
    return $location;
}

function gets_book_count($booking_no) {
    $sql_user = "SELECT * FROM booking_sub_list WHERE book_no='$booking_no'";
    $rs_user = mysql_query($sql_user);
    $rs_count = mysql_num_rows($rs_user);
    return $rs_count;
}



// Main query
$rs = mysql_query($sql);
$sno = 0;

while ($rsdata = mysql_fetch_array($rs)) {
    $sno++;
    $from++;
    $main_id = $rsdata['main_id'];
    $jobcard_no = $rsdata['jobcard_no'];
    $created_by = $rsdata['created_by'];
    $cust_name = $rsdata['cust_name'];
    $book_place_names = book_place_names($book_place);
    $entry_date = $rsdata['entry_date'];
    $book_branch_names = place_branch_name($book_branch);
    $job_type = $rsdata['job_type'];
    $fabric_type = $rsdata['fabric_type'];
    $net_total = $rsdata['net_total'];
    $item_image = $rsdata['item_image'];
    $approval_status = $rsdata['approval_status'];
    $tot_qty = get_tot_qty_jobcard($jobcard_no);
    $overal_width = $rsdata['overal_width'];
    $overal_height = $rsdata['overal_height'];
    $overal_qty = $rsdata['overal_qty'];
    $overall_value = $rsdata['overall_value'];
  ?>
  <?php

  
$rate_sql = " SELECT MIN(conform_rate) AS least_rate, 
        MAX(conform_rate) AS highest_rate 
    FROM jobcard_sublist 
    WHERE jobcard_no='$jobcard_no'";
// echo $rate_sql; // Debugging line to check the query
$rate_rs = mysql_query($rate_sql);
$rate_data = mysql_fetch_array($rate_rs);
$least_value = $rate_data['least_rate'];
$highest_value = $rate_data['highest_rate'];

// // ✅ Display only once — after loop
// echo "<br><strong>Least Conform Rate:</strong> " . $least_value;
// echo "<br><strong>Highest Conform Rate:</strong> " . $highest_value."<br>";
// Fetch sublist details
$sub_sql = "SELECT * FROM jobcard_sublist WHERE jobcard_no='$jobcard_no'";
$sub_rs = mysql_query($sub_sql);
$overall_values = 0;
$qtys = 0;
$multi_total = 0;
$mq_total = 0;
$sno_sub = 0;
while ($sub_data = mysql_fetch_array($sub_rs)) {
    $sno_sub++;
    $job_id = $sub_data['job_id'];
    $jobcard_no = $sub_data['jobcard_no'];
    $particulars = $sub_data['particulars'];
    $width = $sub_data['width'];
    $height = $sub_data['height'];
    $rate = $sub_data['rate'];
    $amount = $sub_data['amount'];
    $conformed_amt = $sub_data['conformed_amt'];
    $qty = $sub_data['qty'];
    $jobcard_values = $sub_data['jobcard_values'];

    $multi = $width * $height;
    $mq = $multi * $qty;
    $conformed_rate = ($conformed_amt) / ($width * $height);

    $overall_values += $jobcard_values;
    $qtys += $qty;
    $multi_total += $multi;
    $mq_total += $mq;

    $avg_amt = $overall_values / $qtys;
    $avg_sq_inch = $mq_total / $qtys;
    $avg_rate = $avg_amt / $avg_sq_inch;
?>

<?php } // End sublist loop ?>
<tr>
<td width="55" align="center" style="border:1px solid #ddd"><?php echo $from; ?></td>
<td width="93" align="left" style="border:1px solid #ddd">&nbsp;&nbsp;<?php echo date("d-m-Y", strtotime($entry_date)); ?></td>
<td align="left" style="border:1px solid #ddd">&nbsp;&nbsp;&nbsp;&nbsp;<?php echo $jobcard_no; ?></td>
<td width="125" align="left" style="border:1px solid #ddd">&nbsp;&nbsp;<?php echo $created_by; ?></td>
<td align="left" style="border:1px solid #ddd">&nbsp;&nbsp;<?php echo $cust_name; ?></td>
<td align="left" style="border:1px solid #ddd">&nbsp;<?php echo $job_type; ?></td>
<td align="left" style="border:1px solid #ddd">&nbsp;&nbsp;<?php echo $fabric_type; ?></td>
<td align="right" style="border:1px solid #ddd"><?php echo number_format($tot_qty, 2); ?>&nbsp;</td>
<td align="right" style="border:1px solid #ddd"><?php echo number_format($net_total, 2); ?>&nbsp;</td>
<td align="right" style="border:1px solid #ddd"><?php echo number_format($avg_rate, 3); ?>&nbsp;</td>
<td align="right" style="border:1px solid #ddd"><?php echo number_format($least_value, 3); ?>&nbsp;</td>
<td align="right" style="border:1px solid #ddd"><?php echo number_format($highest_value, 3); ?>&nbsp;</td>

<td align="center" style="cursor:pointer; border:1px solid #ddd">
<a href="javascript:individual_print_jobcard('jobcard/jobcard_entry/jobcard_view_print.php?jobcard_no=<?php echo $jobcard_no;?>&cust_name=<?php echo $cust_name;?>&item_image=<?php echo $item_image;?>');" style="text-decoration:none;">
<img src="img/PrinterBlack.png" width="30" height="30" title="Print"/>
</a>
</td>

    <?php if(($user_id=='1'))
	 { 
	 ?> <td align="center" style="cursor:pointer; border:1px solid #ddd"><img src="img/edit.png" width="17" height="20" title="EDIT" onclick="javascript:edit_job_details('<?php echo $main_id;?>','<?php echo $jobcard_no;?>');"/></td> <?php } ?>
	 
	 <?php if(($user_id=='1'))
	 { 
	 ?> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.7.2/font/bootstrap-icons.css">
    <td align="center" style="border:1px solid #ddd" ><img src="img/trash.png" style="cursor:pointer" onClick="del_job_main('<?php echo $main_id;?>','<?php echo  $jobcard_no;?>')" width="20px" height="22px">
    </td>
	
	<?php } $query="select * from   jobcard_sublist where jobcard_no='$jobcard_no'";
 $rack=mysql_query($query);
 				$app=2;
   		while($luckey=mysql_fetch_array($rack))
  			{ 
  				$width=$luckey['width'];
				$height=$luckey['height'];
				$conformed_amt=$luckey['conformed_amt'];
    		$rate=$luckey['rate'];
    		$conformed_rate=($conformed_amt)/($width*$height);
    		$jerry=number_format($conformed_rate,4);
    		// $jerry=number_format($rate,4);
    		  // echo $jerry.'<br>'."Lokie";
				}?><input type="hidden" name="app" id="app" value="<?php echo $jerry; ?>">
				<?php $app=2;  if($jerry<=0.02){  if(($user_id=='1')) { ?>
   <td align="center"><?php  if($approval_status=='1'){?><span id="boot-icon" class="bi bi-hand-thumbs-up" style="font-size: 10rem; color: rgb(50,205,50);font-size: 1.5rem;"></span> <?php } else{?><input type="button" id="approval" name="approval" value="Approve" style="color: blue;" onclick="approval_check('<?php echo $main_id; ?>','<?php echo $jobcard_no; ?>')"><?php } ?>
 <?php }}else{ $test1=mysql_query("update jobcard_main set approval_status = '$app' where main_id = $main_id");?><?php } ?>  
  <?php } ?>
  </td>
  </tr>
</table>
</div>
<table border="0" width="99%"  align="center" cellpadding="0" cellspacing="0" >

 <tr>
    <th height="19" align="left" style="font-size:12px; font-family:sans-serif; padding-top:10px;" scope="col"><?php echo $confirmation;?></th>
	<th scope="cols" width="8%" align="right"><?php  
			  if ($num_results > $show) 
			  { 			  $show;
			include("footer_links1.php");
			  }
	?></th>
 </tr>
</table>
</div>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.4/jquery.min.js"></script>
<script type="text/javascript">
	function approval_check(main_id,jobcard_no){
		  // alert(jobcard_no);
		$.ajax({
    		type: "POST",
			url: "jobcard/jobcard_entry/approval.php?action=SUBMIT",
			data: "main_id="+main_id+"&jobcard_no="+jobcard_no,
			success: function(data){

				// alert(data);
			window.location.reload(true);
						document.getElementById('#approval').style.visibility = 'hidden';
   		},
			
		});
		btn.style.display = 'none';
	}


</script>
