<?php
session_start();
require("php_con.php");

require("function.php");

if(isset($_GET['remove'])){
	$remove=mysqli_real_escape_string($connation,$_GET['remove']);
	mysqli_query($connation,"DELETE FROM quotation_details WHERE qd_bill_id='$remove'");
	mysqli_query($connation,"DELETE FROM sale_list WHERE sale_billid='$remove'");
	echo "<script>window.location='quotation?removed';</script>";
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" href="favicon.ico">
<title><?php echo $system_name; ?></title>
<link rel="stylesheet" href="main_css.css">
	
<script src="data_table/js/jquery-3.5.1.js"></script>
<script src="data_table/js/jquery.dataTables.min.js"></script>
<script src="data_table/js/dataTables.bootstrap4.min.js"></script>
<link rel="stylesheet" href="data_table/css/bootstrap.css">
<link rel="stylesheet" href="data_table/css/dataTables.bootstrap4.min.css">
	
<link rel="stylesheet" href="icon/css/font-awesome.css">
<link rel="stylesheet" href="icon/css/font-awesome.min.css">
	
<link rel="stylesheet" href="alert/sweetalert2.min.css">
<script src="alert/sweetalert2.min.js"></script>
</head>

<body>
<script>
window.addEventListener("load",function(){
	var load_screen=document.getElementById('load_screen');
	document.body.removeChild(load_screen);
});
</script>

<div id="load_screen"><div id="loading"><img src="image/loading.gif" width="80px;"></div></div>
	
<div class="contaner">
	
<?php if(!isset($_GET['sale_details_customer'])){ ?>
<form method="post">	
<table class="mb-2">
<tbody>
<tr>
<td><label>From Date</label></td>
<td><label>To Date</label></td>
<td></td>
</tr>
<tr>
<td><input name="from_date" type="date" class="form-control form-control-sm" id="from_date" value="<?php if(isset($_POST['from_date'])){echo $_POST['from_date'];}else{echo $current_date;} ?>" required max="<?php echo $current_date; ?>"></td>
<td><input name="to_date" type="date" class="form-control form-control-sm" id="to_date" value="<?php if(isset($_POST['to_date'])){echo $_POST['to_date'];}else{echo $current_date;} ?>" required max="<?php echo $current_date; ?>"></td>
<td><button type="submit" name="filter" class="btn btn-dark btn-sm ml-2"><i class="fa fa-filter"></i> Filter</button></td>
</tr>
</tbody>
</table>
</form>
<?php } ?>
	
<div class="row">
<div class="table-responsive">
<table class="table list-table" style="width:100%; font-size: 14px; text-transform: capitalize;">
        <thead>
            <tr>
                <th>No.</th>
				<th>&nbsp;</th>
                <th>Quotation ID</th>
				<th>Create Time</th>
				<th>User</th>
            </tr>
        </thead>
        <tbody>
			<?php
			$count_no=0;
			if(isset($_POST['filter'])){
			$qury=mysqli_query($connation,"SELECT qd.*,CONCAT(ud.user_fname,' ',ud.user_lname) AS c_user
FROM quotation_details qd INNER JOIN user_details ud ON qd.qd_user=ud.user_id
WHERE qd.qd_c_date BETWEEN '$_POST[from_date]' AND '$_POST[to_date]' ORDER BY qd.qd_id DESC");
			}
			else{
			$qury=mysqli_query($connation,"SELECT qd.*,CONCAT(ud.user_fname,' ',ud.user_lname) AS c_user
FROM quotation_details qd INNER JOIN user_details ud ON qd.qd_user=ud.user_id
WHERE qd.qd_c_date='$current_date' ORDER BY qd.qd_id DESC");
			}
			while($resalt=mysqli_fetch_assoc($qury)){
			$count_no++;
			?>
            <tr>
                <td><?php echo number_format((float)$count_no); ?></td>
				<td align="center">
				<a href="quotation_print.php?q_id=<?php echo $resalt['qd_bill_id']; ?>" target="print_load" class="badge badge-info"><i class="fa fa-print"></i></a>
				
				<?php
				$a=mysqli_query($connation,"SELECT * FROM sale_details WHERE sale_details_billid='$resalt[qd_bill_id]'");
				if(mysqli_num_rows($a)>0){
				?>
				<a href="bill_print.php?print=<?php echo $resalt['qd_bill_id']; ?>" target="bill_load" class="badge badge-secondary"><i class="fa fa-print"></i></a>
				<?php }else{ ?>
				<a href="poin-of-sale?bill_id=<?php echo $resalt['qd_bill_id']; ?>" class="badge badge-success" target="_parent"><i class="fa fa-dollar"></i></a>
				<?php } ?>
					
				<?php /*?><a href="quotation?remove=<?php echo $resalt['qd_bill_id']; ?>" class="badge badge-danger" onClick="return confirm('Are your sure?');"><i class="fa fa-trash"></i></a><?php */?>
				</td>
                <td><?php echo $resalt['qd_bill_id']; ?></td>
				<td><?php echo date("Y-m-d h:i:s A",strtotime($resalt['qd_c_date']." ".$resalt['qd_c_time'])); ?></td>
				<td><?php echo $resalt['c_user']; ?></td>
            </tr>
            <?php
			}
			?>
        </tbody>
	<tfoot>
	</tfoot>
    </table>
</div>
	
<iframe src="" style="display: none; width: 0px; height: 0px;" name="print_load"></iframe>
<iframe src="" style="display: none; width: 0px; height: 0px;" name="bill_load"></iframe>
		
</div>
</div>	
	
<iframe name="re_print_bill" style="display: none;"></iframe>
	
</body>
</html>
<?php @mysqli_close($connation); ?>