<?php

include 'load.php';

?>

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0">
    <meta name="description" content="POS - Payroll | Pos & Invetory System | Dashboard
">
    <meta name="keywords" content="Payroll | Pos & Invetory System | Dashboard
">
    <meta name="author" content="Payroll | Pos & Invetory System | Dashboard
">
    <meta name="robots" content="noindex, nofollow">

    <title>Home | Payroll | Pos & Invetory System | Dashboard
</title>

    <?php
require_once 'headercss.php';
?>

</head>

<body>

    <div class="main-wrapper">
        <?php
require_once 'header.php';
?>

        <?php
require_once 'navbar.php';
?>

        <div class="page-wrapper">
            <div class="content">
                <div class="page-header">
                    <div class="page-title">
                        <h4>Product List</h4>
                        <h6>Manage your products</h6>
                    </div>




                    <div class="page-btn">
                        <a href="addproduct.php" class="btn btn-added"><img src="assets/img/icons/plus.svg" alt="img"
                                class="me-1">Add New Product</a>
                    </div>
                </div>

                <div class="card">
                    <div class="card-body">
                        <div class="table-top">


                        </div>
						
						<div class="card mb-0" id="filter_inputs" style="display:block;">
							<div class="card-body pb-0">
								<div class="row">
									<div class="col-lg-12 col-sm-12">
										<form method="GET" enctype="multipart/form-data">
											<div class="row">
                        <div class="col-md-3">
                            <select class="form-control select2" name="supplier" required>
                                <option value="">Select Supplier</option>
                                <?php 
				
					$sql = "SELECT * FROM mobilesuppliers";
					$result = $conn->query($sql);

					if ($result->num_rows > 0) {
						while($row = $result->fetch_assoc()) { ?>
                                <option value="<?php echo $row['spid']; ?>"><?php echo $row['spname']; ?></option>
                                <?php }
					}
				?>
                            </select>
                        </div>
                        <div class="col-md-1">
                            <button class="btn btn-secondary" type="submit" name="submit">Search</button>
                            </form>
                        </div>
                        <div class="col-md-4">
                        <button onclick="deleteSelectedRows()" class="btn btn-danger">Delete Selected Rows</button>

                        </div>
                    </div>
										</form>

									</div>
								</div>
							</div>
						</div>
		
<?php 
	if(isset($_GET['supplier'])){
		$supplier = $_GET['supplier'];

		$search = "AND supplier='$supplier'";
	}else{
		$search = "";
	}
?>

<hr>

<div class="form-check">
  <input class="form-check-input" type="checkbox" value="" id="selectall">
  <label class="form-check-label" for="flexCheckDefault">
    Select All
  </label>
</div>
<style>
	.colorbox{
		width: 20px;
		height: 20px;
		display: inline-block;
	}
</style>
<div class="row mt-4 mb-4">
	<div class="col-2">
		<p>Already expired <span class="colorbox" style="background-color: #ba1004;"></span></p>
	</div>
	<div class="col-2">
		<p>Will expire within 24 hours <span class="colorbox" style="background-color: #eb3483;"></span></p>
	</div>
	<div class="col-2">
		<p>Will expire within a week <span class="colorbox" style="background-color: #53a81e;"></span></p>
	</div>
	<div class="col-2">
		<p>Will expire within a month <span class="colorbox" style="background-color: #1e37a8;"></span></p>
	</div>
</div>
                        <div class="table-responsive">
<table class="table" id="betable">
                                <thead>
                                    <tr>
                                        <th>#</th>
										<th>ID</th>
                                        <th>Action</th>
                                        <th>Status</th>
										<th>Expire Status</th>
                                        <th>Type</th>
                                        <th>Product Code</th>
                                        <th>Item Code/Barcode</th>
                                        <th>Product</th>
                                        <th>Category</th>
                                        <th>Sub Category</th>
                                        <th>Brand</th>
										<th>Size</th>
                                        <th>Supplier</th>
                                        <th>Unit</th>
                                        <th>Expire Date</th>
										<th>Minimum Qty</th>
                                        <th>Qty</th>
                                        <th>Discount</th>
                                        <th>Tax</th>
                                        <th>Cost Price</th>
										<th>Market Price</th>
										<th>Profit</th>
										<th>Sell Price</th>
										<th>Whole Sell Price</th>
										<th>Warranty</th>
                                        <th>Add Date</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php

										$count=0;
										
										$dis=0;
										$taxx=0;
										$totalrowqty=0;
										$totalrowdis=0;
										$totalrowtaxx=0;
										$totalrowprice=0;
										$totalrowmarket_price=0;
										$totalrowprofitmargin=0;
										$totalrowsellprice=0;
										
										$stmt = $DB_con->prepare('SELECT * FROM mobileproducts WHERE prid != 0 '.$search.' ORDER BY prid');

										$stmt->execute();

										if($stmt->rowCount() > 0)

										{

										while($row=$stmt->fetch(PDO::FETCH_ASSOC))

										{

										extract($row);
										
										$totalrowqty=$totalrowqty+$row["qty"];
										$totalrowdis=$totalrowdis+$dis;
										$totalrowtaxx=$totalrowtaxx+$taxx;
										$totalrowprice=$totalrowprice+$row["price"];
										$totalrowmarket_price=$totalrowmarket_price+$row["market_price"];
										$totalrowprofitmargin=$totalrowprofitmargin+$row["profitmargin"];
										$totalrowsellprice=$totalrowsellprice+$row["sellprice"];
										
										$count++;
										$exmsg = "";
										if($row['expdate']!="0000-00-00"){
											$expDate = new DateTime($row['expdate']);
											$now = new DateTime();
											$interval = $now->diff($expDate);
											$daysLeft = (int)$interval->format('%r%a');
											if ($daysLeft < 0) {
												$bgexpire = "#ba1004";
												$exmsg = "Already expired";
											} elseif ($daysLeft < 1) {
												$bgexpire = "#eb3483";
												$exmsg = "Will expire within 24 hours";
											} elseif ($daysLeft < 7) {
												$bgexpire = "#53a81e";
												$exmsg = "Will expire within a week";
											} elseif ($daysLeft < 30) {
												$bgexpire = "#1e37a8";
												$exmsg = "Will expire within a month";
											} else {
												$bgexpire = "";
												$exmsg = "";
											}
										}else{
											$bgexpire = "";
										}

										?>
                                    <tr style="background-color: <?php echo $bgexpire; ?>;">
                                        <td><input type="checkbox" class="delete-checkbox"
                                                value="<?php echo $row["prid"]; ?>"></td>
                                        <td><?php echo number_format($count,0); ?></td>
                                        <td>

                                            <a href="editproduct.php?prid=<?php echo $row["prid"]; ?>"
                                                class="btn btn-block btn-outline-primary active btn-sm text-white"><i
                                                    class="fa fa-edit" data-bs-toggle="tooltip" title=""
                                                    data-bs-original-title="fa fa-edit" aria-label="fa fa-edit"></i></a>
                                            <a href="deleteproduct.php?prid=<?php echo $row["prid"]; ?>"

                                                class="btn btn-block btn-outline-danger active btn-sm text-white" onclick="return confirm('Are you sure you want to delete this product?');"><i

                                                    class="fa fa-trash" data-bs-toggle="tooltip" title=""

                                                    data-bs-original-title="fa fa-trash"

                                                    aria-label="fa fa-trash"></i></a>

                                        </td>
                                        <td><?php 
						
														if($row['status'] == "1"){

														echo '<a class="btn btn-success btn-sm text-white">Active</a>';

													    }else if($row['status'] == "0"){

														echo '<a class="btn btn-danger btn-sm text-white">Deactive</a>';

														}
						
												
											  ?></td>
										 <td><?php echo $exmsg; ?></td>
                                        <td><?php echo $row['producttype']; ?></td>
                                        <td><a href="javascript:void(0);"><?php echo $row['prcode']; ?></a></td>
                                        <td><?php echo $row['itemcode']; ?></td>
                                        <td class="productimgname">
                                            <a class="product-img" href="productlist.php">
                                                <?php if($row['cover']==""){$pro_img="assets/img/empty.png";}else{$pro_img="images/products/cover/".$row['cover'];} ?><img
                                                    src="<?php echo $pro_img; ?>" width="42" height="42"
                                                    class="rounded-circle" alt="">
                                            </a>
                                            <span
                                                style="font-weight:600;color:#000000;"><?php echo $row['proname']; ?></span>
                                        </td>
                                        <td><?php
if($row['category'] == "0"){
													
												}else{	
												$id = $row['category'];  

												$query = $DB_con->prepare('SELECT catename FROM mobilecategory WHERE ctid='.$id);

												$query->execute();

												$result = $query->fetch();

												echo $result['catename'];
}
												?></td>
                                        <td><?php
if($row['subcategory'] == "0"){
													
												}else{	
												$id = $row['subcategory'];  

												$query = $DB_con->prepare('SELECT subname FROM mobilesubcategory WHERE sbid='.$id);

												$query->execute();

												$result = $query->fetch();

												echo $result['subname'];
}
												?></td>
                                        <td>
                                            <?php 
if($row['brand_id']){
$brand_id = $row['brand_id']; 
$brand= mysqli_fetch_array(mysqli_query($conn, "SELECT * FROM mobile_brand WHERE brand_id='$brand_id'"));

echo $brand['brand_name'];
}
?>

                                        </td>
<td>
                                            <?php 
if($row['prsize']){
$szid = $row['prsize']; 
$productsize= mysqli_fetch_array(mysqli_query($conn, "SELECT * FROM mobilesize WHERE szid='$szid'"));

echo $productsize['sizename'];
}
?>

                                        </td>

                                        <td><?php
if($row['supplier'] == "0"){
													
												}else{	
												$id = $row['supplier'];  

												$query = $DB_con->prepare('SELECT spname FROM mobilesuppliers WHERE spid='.$id);

												$query->execute();

												$result = $query->fetch();

												echo $result['spname'];
}
												?></td>
                                        <td><?php
if($row['unit'] == "0"){
													
												}else{	
												$id = $row['unit'];  

												$query = $DB_con->prepare('SELECT unit_name FROM unit WHERE unit_id='.$id);

												$query->execute();

												$result = $query->fetch();

												echo $result['unit_name'];
}
												?></td>
                                        <td><?php echo $row['expdate']; ?></td>
										<td><?php echo number_format((int)$row['mqty']); ?></td>
                                        <td><?php echo number_format((int)$row['qty']); ?></td>
                                        <td>Rs.<?php 
                                         if($row['discount_type1']==1){
                                            $dis = $row['discount1']."%";
                                        }else{
                                            $dis = $row['discount1'];
                                        }
                                        
                                        echo $dis; ?></td>
                                        <td>Rs.<?php 
                                        if($row['tax_type1']==1){
											$taxx = $row['tax1']."%";
										}else{
											$taxx = $row['tax1'];
										}
                                        
                                        echo $taxx; ?></td>
                                        <td>Rs.<?php echo number_format((float)$row['price'],2); ?></td>
                                        <td>Rs.<?php echo number_format((float)$row['market_price'],2); ?></td>
										<td>Rs.<?php echo number_format((float)$row['profitmargin'],2); ?></td>
										<td>Rs.<?php echo number_format((float)$row['sellprice'],2); ?></td>
										<td>Rs.<?php echo number_format((float)$row['whole_sale_price'],2); ?></td>
										<td><?php echo $row['warranty']; ?></td>
                                        <td>
										<?php echo date_format(date_create($row['add_date']), "M d, Y | h:i:s "); ?>
										</td>
                                    </tr> 
                                    <?php 
} 
}
?>
                                </tbody>
								<tfoot style="background-color:#008b0b;color:#ffffff;">
									<tr>
										<th></th>
										<th></th>
										<th></th>
										<th></th>
										<th></th>
										<th></th>
										<th></th>
										<th></th>
										<th></th>
										<th></th>
										<th></th>
										<th></th>
										<th></th>
										<th></th>
										<th></th>
										<th></th>
										<th><strong>All Total</strong></th>
										<th><?php echo number_format((float)$totalrowqty,2); ?></th>
										<th><?php echo number_format((float)$totalrowdis=$totalrowdis+$dis,2); ?></th>
										<th><?php echo number_format((float)$totalrowtaxx=$totalrowtaxx+$taxx,2); ?></th>
										<th>Rs.<?php echo number_format((float)$totalrowprice,2); ?></th>
										<th>Rs.<?php echo number_format((float)$totalrowmarket_price,2); ?></th>
										<th>Rs.<?php echo number_format((float)$totalrowprofitmargin,2); ?></th>
										<th>Rs.<?php echo number_format((float)$totalrowsellprice,2); ?></th>
										<th></th>
										<th></th>
									</tr>
								</tfoot>
                            </table>
                        </div>
                       
                    </div>
                </div>
            </div>
        </div>

    </div>
    </div>
    </div>


    <script>
		function printDiv(printableArea) {

			var printContents = document.getElementById(printableArea).innerHTML;
			var originalContents = document.body.innerHTML;

			document.body.innerHTML = printContents;

			window.print();

			document.body.innerHTML = originalContents;

		}
	</script>
	<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
	<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.1.0-rc.0/js/select2.min.js"></script>

	<script>
	// Initialize Select2 plugin with typing search functionality
	$('.select2').select2();
	</script>
	
	<script data-cfasync="false" src="../../cdn-cgi/scripts/5c5dd728/cloudflare-static/email-decode.min.js"></script>
							
	<script src="assets/js/jquery-3.6.0.min.js"></script>

	<script src="assets/js/feather.min.js"></script>

	<script src="assets/js/jquery.slimscroll.min.js"></script>

	<script src="assets/js/jquery.dataTables.min.js"></script>
	<script src="assets/js/buttons.dataTables.js"></script>
	<script src="assets/js/dataTables.bootstrap4.min.js"></script>

	<script src="assets/js/bootstrap.bundle.min.js"></script>

	<script src="assets/js/moment.min.js"></script>
	<script src="assets/js/bootstrap-datetimepicker.min.js"></script>
	<script src="assets/js/pdfmake.min.js"></script>
	<script src="assets/plugins/select2/js/select2.min.js"></script>
	<script src="assets/js/vfs_fonts.js"></script>

	<script src="assets/plugins/sweetalert/sweetalert2.all.min.js"></script>
	<script src="assets/plugins/sweetalert/sweetalerts.min.js"></script>
	<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
	<script src="assets/js/dataTables.buttons.min.js"></script>
	<script src="assets/js/jszip.min.js"></script>
	<script src="assets/js/buttons.html5.min.js"></script>
	<script src="assets/js/script.js"></script>
	
	<link href="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/toastr.min.css" rel="stylesheet">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/toastr.min.js"></script>

	<script>
		

		$(document).ready( function() {
			$('#betable').DataTable({
        dom: 'Bfrtip', 
        buttons: [
            'copy', 
            'csv',  
            {
                extend: 'excelHtml5', 
                customize: function(xlsx) {
                    var sheet = xlsx.xl.worksheets['sheet1.xml']; 
                    $('row c[r^="C"]', sheet).attr('s', '2'); 
                }
            },
            'pdf',  
            'print' 
        ]
    });
	} );
	</script>
	
	<style>
	#betable_filter{
		margin-bottom: 20px;
	}
	</style>
	
    <script>
    function deleteSelectedRows() {
        var checkboxes = document.querySelectorAll('.delete-checkbox:checked');

        var selectedIds = [];

        checkboxes.forEach(function(checkbox) {
            selectedIds.push(checkbox.value);
        });

        $.ajax({
            url: 'deleteproduct.php',
            type: 'POST',
            data: {
                selectedIds: selectedIds
            },
            success: function(response) {
                console.log('Rows deleted successfully');
                toastr.success('Rows deleted successfully');
                location.reload();
            },
            error: function(xhr, status, error) {
                console.error('Error deleting rows: ', error);
                toastr.error('Error deleting rows: ' + error);

            }

        });

    }
    </script>




<script>
	$(document).ready(function(){
		$('#selectall').click(function() {
			var id = $(this).attr('id');

			if($(this).is(':checked')){
				var check = 1;
				$('input[type="checkbox"]').prop('checked', true);
			}else{
				var check = 0;
				$('input[type="checkbox"]').prop('checked', false);
			}


			/*$.ajax({
					type: "POST",
					data: {check:check,id:id},
					url: "checkbox_select.php",
					success: function(data){
							
					}
				});*/

				

		});

	});
</script>
</body>

</html>