<?php
session_start();
$where 					= " WHERE 1=1  ";
$next_page_condition 	= "";
$paging ="";
if($_REQUEST['start']){
	$paging .="&start=".$_REQUEST['start'];
} 

##################################  STRAT FILTER CONITIONS    ###############################

$order_ref 	= isset($_REQUEST['order_ref']) ? $_REQUEST['order_ref'] : "";
if($order_ref) {
	$order_ref = trim($order_ref);
    $where	.= " AND order_ref ='$order_ref' ";
    $next_page_condition.="&order_ref=$order_ref";
}
$supplier_id 	= isset($_REQUEST['supplier_id']) ? $_REQUEST['supplier_id'] : "";
if($supplier_id) {
	$supplier_id=trim($supplier_id);
    $where	.= " AND supplier_id = '$supplier_id' ";
    $next_page_condition.="&supplier_id=$supplier_id";
}

################################# END FILTER CONDITIONS ################################

###########################################  PAGING WITH PER PAGE  #####################################
$file_name		= "index"; // this is file name which is used during paging  , included at the bottom of the page
$table_name		= "purchase_list";
$paging_table_name	= $table_name;
$paging_query		= $where;
$join_query_for_paging = "SELECT * FROM $table_name  $where ";

//$join_query_for_paging = "SELECT supplier_management.id as main_supplier_id,$table_name.* FROM $table_name LEFT JOIN supplier_management ON $table_name.supplier_id = supplier_management.id $where ";
$page_header = "Purchase List";
include("paging/paging_query.inc.php");
################################## END PAGING WITH PER PAGE  ################################
$query = "SELECT * FROM $table_name $where $orderby LIMIT $eu, $limit";
//$query = "SELECT supplier_management.id as main_supplier_id,$table_name.* FROM $table_name LEFT JOIN supplier_management ON $table_name.supplier_id = supplier_management.id $where $orderby LIMIT $eu, $limit";

$sql=mysqli_query($conn,$query);
$result=mysqli_num_rows($sql);

$total_no_of_purchase_order = mysqli_num_rows(mysqli_query($conn,$join_query_for_paging));
#################################   DELETE    #########################################
// when we click on delete this area of php code will execute
if(isset($_REQUEST['action']) && $_REQUEST['action']=='delete')
{
	mysqli_query($conn,"DELETE FROM $table_name WHERE id = '".$_REQUEST['id']."' ");
	
	$_SESSION['info_message'] = "Purchase List deleted successfully";
	header("location: $file_name?page=view_purchase_list".$next_page_condition.$paging);
	exit();
}
#################################  END DELETE    #########################################
?>


<div class="content-header">
    <div class="container-fluid">
        <div class="row mb-2">
            <div class="col-sm-12">
                <h1 class="m-0"><?=$page_header;?></h1>
            </div>
        </div>
        <!-- /.row -->
    </div>
    <!-- /.container-fluid -->
</div>
<section class="content">
	<div class="container-fluid">
	<div class="row">
		<div class="col-lg-12">
		<div class="">
			<!----------------------- Message ------------------------------->
			<? if(isset($_SESSION['info_message'])){ ?>
			  <div class="alert alert-info alert-dismissable">
				<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
				<? echo $_SESSION['info_message']; unset($_SESSION['info_message']);?>
			  </div>
			  <? } ?>
			<!----------------------- End Message ------------------------------->
			<div class="card">
				<div class="card-body">
				<!-- search form -->
				<?php include_once('purchase_order_search_options.inc.php');?>
				<!-- /.search form -->
				Total No. of Purchase Items: <?=$total_no_of_purchase_order;?>
				<!--<div style="float:right;margin-bottom:4px;">Export sites Details &nbsp;&nbsp;<a href="index?page=view&action=export_job_details<?=$next_page_condition;?>"><i class="fa fa-download" style="font-size:20px;"></i></a></div>-->
				
					<form action="index?page=add" method="post" name="form1" id="form1">
					<div class="card-footer">
					   <button type="submit" name="add" class="btn btn-primary">Add Purchase List</button>
					</div>
					<? if($result>0){?>
					<div class="table-responsive">
					 <table id="example1" class="table table-bordered table-striped">
					 
						<thead>
						  <tr>
							<!--<th>#</th>-->
							<th>Item No.</th>
							<th>Quantity</th>
							<th>Sales Item Required Date</th>
							<th>Edit</th>
							<th>Delete</th>
							<!--<th class="no-sort" style="text-align:center;">
								<div class="icheck-primary d-inline">
									<input type="checkbox" name="check_all" id="check_all">
									<label for="check_all"></label>
								</div>
							</th>-->
						  </tr>
						</thead>
						<tbody>
							<?
								while($row =mysqli_fetch_array($sql)){ 
								$row=utility::validate_crypt_data($row);
								
					
							?>
						  <tr>			
							<td><?php  echo $gsi->getPartNumberById($row["item_id"]);?></td>
							<td><?php echo $row["quantity_requested"];?></td>
							<td>
								<?php
								$required_date = $row["required_date"];
								
								$timestamp = strtotime($required_date);
								
								$formatted_date = date("Y-m-d (l)", $timestamp);
								
								echo htmlspecialchars($formatted_date);
								?>
							</td>
							<!--<td>
								<?php
								
								$work_order_id = $row["work_order_id"];
								$query = "SELECT * FROM work_order WHERE id = $work_order_id";
								$result = $conn->query($query);
								if ($result && $result->num_rows > 0) {
									$work_order_row = $result->fetch_assoc();
									
									echo $work_order_row["work_order_no"];
								} else {
									echo "Not Found";
								}
								?>
							</td>-->
							<td><a href="index?page=add&action=edit&po_id=<?=urlencode(utility::app_crypt($row['id'],'e'));?>&id=<?=$row['id'].$next_page_condition.$paging;?>" <?=$edit_delete_display;?>> <i class="fa fa-fw fa-edit text-primary"></i> </a></td>
							<td><a href="index?page=view_purchase_list&action=delete&id=<?=$row['id'].$next_page_condition.$paging; ?>"onclick=" javascript:return confirm('Are you sure you want to delete?');" <?=$edit_delete_display;?>><i class="fa fa-fw fa-trash text-danger"></i> </a></td>
							<!--<td align="center">
								<div class="icheck-primary d-inline">
									<input class="checkbox" type="checkbox" name="site_ids[<?=$row['id']?>]" value="<?=$row['id']?>" id="primary">
									<label for="primary"></label>
								</div>
							</td>-->
						  </tr>
							<? } ?>
							
						</tbody>
					 </table>
					 </div>
					 <? }else{ ?>
						No purchase Item found...
					 <? } ?>
					 <div class="box-footer clearfix">
						<!-- Paging starts here  -->
						<? 
							$next_page_condition .="&page=view_purchase_list";
							include("paging/paging_row.inc.php") 
						?>
						<!-- Paging ends starts here  -->
					</div>
				</div>
				<div class="card-footer hide">
                   <button type="submit" name="add" class="btn btn-primary">Add Purchase List</button>
                </div>
				</form>
			</div>
		</div>
		</div>
	</div>
	</div>
</section>

<script type="text/javascript">
$(function () {
	$(".close_button").click(function(){
		$(".modal_iframe").attr("src","");
		location.reload();
	});
	
	$(".add_po_document").click(function(){
		var purchase = $(this).attr("data-purchase");
		$("#myModalLabel").html("Purchase Documents  <span style='color:Tomato'>"+purchase+"</span>");
		var hide_top_left = "&hide_top=1&hide_left=1";
		var purchase_order_id = $(this).attr("alt");	
		//alert(equipment_id);
		$('.modal-dialog').css({"max-width":"76%"});		
		$(".modal_iframe").attr("src","<?=$config->frontEndURL?>/admin/modules/fileuploads/image-files.php?purchase_order_id="+purchase_order_id+"&page=view&source=po"+hide_top_left);
	});
	$(".Mail_send").click(function(){
		var order_ref = $(this).attr("data-order_ref");
		$("#myModalLabel").html("Send to Supplier for <span style='color:Tomato'>"+order_ref+"</span>");
		var hide_top_left = "&hide_top=1&hide_left=1";
		var purchase_order_id = $(this).attr("alt");
		$('.modal-dialog').css({"max-width":"80%"});
		$(".modal_iframe").addClass("jspopup").attr("src","<?=$config->frontEndURL?>/admin/purchase_order/index?page=send_mail_to_client_for_po&purchase_order_id="+purchase_order_id+hide_top_left);
	});
});
</script>