<?php require_once('../auth/auth.php');
  $custom_field= mysqli_query($conn,"SELECT item FROM custom_field WHERE coid='$coid' LIMIT 1 ") or die(mysqli_error($conn));
      $rowcf= mysqli_fetch_assoc ($custom_field);
   
      $p=$rowcf['item'];
      function input_check($ap)
      {
       if (strpos($GLOBALS['p'], $ap) !== false) { return 1;} 
      }
?>
<div class="row">
   <div class="col-md-12 pt-1">
      <div class="card card-primary card-outline direct-chat direct-chat-primary shadow-none" style="border-left:1px solid #969c9d;border-right:1px solid #969c9d;border-bottom:1px solid #969c9d;">
         <div class="card-header" style="padding: 3px 8px;">
          <h3 class="card-title"><?php echo  language(394);?></h3>
            <div class="card-tools m-0">
                 <input type="text" class="form-control form-control-sm search" placeholder="Searching">
            </div>
          </div>
              <div class="card-body p-1">
                <table class=" ttable" cellpadding="0" cellspacing="0">
                  <thead>
                    <tr class="sticky-top">
                      <th class="text-center w-5"><?php echo language(15)?></th>
                      <th class="w-20"><?php echo language(97)?></th>
                      <th class="text-center w-10"><?php echo language(215);?></th> 
                      <th class="text-center w-10"><?php echo language(377);?></th> 
                      <th class="text-center w-10"><?php echo language(387);?> </th>
                      <th class="text-center w-10"><?php echo language(388);?> </th>
                      <th class="text-center w-10"><?php echo language(81);?></th>
                      <th class="text-center w-10"><?php echo language(84);?></th>
                      <th class="text-center w-10"><?php echo language(389);?> </th>
                      <th class="text-center w-10"><?php echo language(390);?> </th>
                      <th class="text-center w-10"><?php echo language(131);?> </th>
                      <th class="text-center w-5"><?php echo language(90);?></th>
                    </tr>
                  </thead>
                  <tbody class="actived">
                    <?php
                        $iteq= mysqli_query($conn,"SELECT *,item.item_name,item.item_details,accounts.ac_name,pgroup_name,pcat_name,pbrand_name FROM imei_tracker 
                            LEFT JOIN item ON (item.pro_id=imei_tracker.pro_id) 
                            LEFT JOIN accounts ON (accounts.acid=imei_tracker.acid)
                            LEFT JOIN item_group ON (item.pgid=item_group.pgid)
                            LEFT JOIN item_catagory ON (item.pcid=item_catagory.pcid)
                            LEFT JOIN item_brand ON (item.pbid=item_brand.pbid)
                            WHERE  imei_tracker.coid='$coid' AND imei_tracker.bid='$bid' AND order_type in('SOP','EPUR') AND  imei_tracker.brid='$brid'  AND imei_tracker.active='1'") or die (mysqli_error($conn));
                          $i=0;
                          while($iter= mysqli_fetch_assoc($iteq)){
                              $pro_id       = $iter['pro_id'];
                              $order_type         = $iter['order_type'];
                              $imei_code    = $iter['imei_code'];
                              $invoice      = $iter['invoice'];
                              $imei_color   = $iter['imei_color'];
                              $mfg_date     = $iter['mfg_date'];
                              $exp_date     = $iter['exp_date'];
                              $date         = $iter['date'];
                              $active       = $iter['active'];
                    ?>



                    <?php
                        $ch_imei_one=mysqli_query($conn,"SELECT imei_code FROM imei_tracker WHERE order_type in('ECSL','ERSL','INSL')  AND  coid='$coid' AND bid='$bid' AND brid='$brid' AND imei_code='$imei_code' LIMIT 1");
                           if (mysqli_num_rows($ch_imei_one ) > 0 ) { }else{  ?>
                       <tr class="btn-outline-dark" >
                            <td class="text-center"><?php echo ++$i ?></td>
                            <td>
								<?php if(input_check('t_group')==1){ echo $iter['pgroup_name'];  }  
								      if(input_check('t_category')==1){ echo $iter['pcat_name']; } ?>
								<?php echo $iter['pbrand_name'];?> <?php echo $iter['item_name'];?> 
								<?php echo $iter['item_details'];?>
							</td>
	                        <td><?php echo $iter['ac_name'];?></td>
	                        <td class="text-center"> 
	                        	    <?php   if ($order_type=='SOP')
				                        {
				                            echo "Opening";
				                        }else if($order_type=='EPUR'){
				                            echo "Purchase";
				                        }else if($order_type=='ERSL'){
				                            echo "Retail Sales";
				                        }else if($order_type=='INSL'){
				                            echo "Installment Sales";
				                        }else if($order_type=='ECSL'){
				                            echo "Cash Sales";
				                        } 
				                    ?>
	                        </td>
                        <td class="text-center"><?php echo $imei_code;?></td>
                        <td class="text-center"><?php echo $imei_color;?></td>
                        <td class="text-center"><?php echo $iter['pu_price'];?></td>
                        <td class="text-center"><?php echo $iter['sale_price'];?></td>
                        <td class="text-center"><?php echo $mfg_date;?></td>
                        <td class="text-center" title="<?php echo $exp_date;?>">
                            <?php 
                                $difference = strtotime($mfg_date) - strtotime($exp_date);
                                 $days = abs($difference/(60 * 60)/24);
                                echo " ".$days." days.";
                            ?>
                        </td>
                        <td class="text-center"><?php echo $date;?></td>
                        <?php
                            $ch_imei_one=mysqli_query($conn,"SELECT imei_code FROM imei_tracker WHERE order_type='ECSL' AND  coid='$coid' AND bid='$bid' AND brid='$brid' AND imei_code='$imei_code' LIMIT 1");
                                if (mysqli_num_rows($ch_imei_one ) > 0 ) { ?>
                        <td  class="text-center " > Sold </td>
                          <?php }else{ ?>
                        <td  class="text-center " > Not Sold </td>
                         <?php  } ?>
                    </tr>
                    <?php } } ?>
                 </tbody>
               </table>
             </div>
        </div>
   </div>
</div>


<script>
$(document).ready(function(){
  $('.search').on('keyup',function(){
    var searchTerm = $(this).val().toLowerCase();
    $('.actived tr').each(function(){
      var lineStr = $(this).text().toLowerCase();
      if(lineStr.indexOf(searchTerm) === -1){
        $(this).hide();
      }else{
        $(this).show();
      }
    });
  });
});
</script>