<?php 
require("php_con.php");
?>
<table class="table list-table bg-white" style="font-size: 14px;">
    <thead>
      <tr style="position: sticky; top: -1px; background-color: white;">
        <th>No.</th>
        <th>Product Name</th>
        <th>Barcode</th>
        <th>Sale Price</th>
        <th>QTY</th>
        <th>Dis. On/Off</th>
        </tr>
      </thead>
    <tbody id="myTable">
      <?php
			$count_no=0;
		if($_GET['search_type']=="product"){
			$list_qury=mysqli_query($connation,"SELECT *
			FROM price_details prd LEFT JOIN product_details pd ON prd.price_systemid=pd.product_systemid
			WHERE pd.product_barcode LIKE '%$_GET[search_text]' OR pd.product_productid LIKE '%$_GET[search_text]' OR pd.product_name LIKE '%$_GET[search_text]%' OR pd.product_name_sinhala LIKE '%$_GET[search_text]%'
			ORDER BY pd.product_name ASC");
		}
			if($_GET['search_type']=="price"){
			$list_qury=mysqli_query($connation,"SELECT *
			FROM price_details prd LEFT JOIN product_details pd ON prd.price_systemid=pd.product_systemid
			WHERE prd.price_sale='$_GET[search_text]'
			ORDER BY prd.price_id DESC");
		}
			if(mysqli_num_rows($list_qury)>0){
			while($list_resalt=mysqli_fetch_array($list_qury)){
			$count_no++;
			
			$in=0;
			$out=0;
			$stock_qury=mysqli_query($connation,"SELECT * FROM stock_details WHERE stock_systemid='$list_resalt[product_systemid]' ORDER BY stock_id DESC");
			while($stock_resalt=mysqli_fetch_array($stock_qury)){

			if($stock_resalt['stock_type']==1){
			$in=$in+$stock_resalt['stock_qty'];
			}
			if($stock_resalt['stock_type']==0){
			$out=$out+$stock_resalt['stock_qty'];
			}

			}
				
			$availbel_stock=$in-$out;
			?>
      <tr class="active" onClick="JavaScript:list_value_send('<?php echo $list_resalt['product_barcode']; ?>'); model_close();" style="cursor: pointer;">
        <td><?php echo number_format($count_no,0); ?></td>
        <td><?php echo $list_resalt['product_name']; ?><br><?php echo $list_resalt['product_name_sinhala']; ?></td>
        <td><?php echo $list_resalt['product_barcode']; ?></td>
        <td><b><?php echo number_format($list_resalt['price_sale'],2); ?></b></td>
        <td align="right"><?php echo number_format($availbel_stock,0); ?></td>
        <td align="center">
          <?php if($list_resalt['product_add_discount']==0){ ?>
          <span class="badge badge-danger">Off</span>
          <?php } ?>
          
          <?php if($list_resalt['product_add_discount']==1){ ?>
          <span class="badge badge-success">On</span>
          <?php } ?>
          </td>
        </tr>
      <?php } }else{ ?>
			
      <tr>
        <td colspan="6" align="center">Product Not Found!</td>
        </tr>
			
		<?php } ?>
      </tbody>
    </table>