<?php require_once('../auth/auth.php');?>
<?php
   $search     = $_GET['search'];
   $itype      = $_GET['itype'];
   $bbid       = $_GET['bbid'];
   $bbrid      = $_GET['bbrid'];
   $nrow       = $_GET['nrow'];
   $fdate      = $_GET['fdate'];
   $edate      = $_GET['edate'];

   $co_rs = mysqli_query($conn,"SELECT co_name,owner_name,co_address,co_phone,co_email FROM company WHERE coid='$coid'") or die (mysqli_error($conn));
   $cor= mysqli_fetch_assoc($co_rs);

    if ($bbid=='0') {
       $bbidcond = '';
    }elseif ($bbid >0){
        $bbidcond= " AND imei.bid=$bbid";
    }

    if ($bbrid=='0') {
       $bbridcond = '';
       $branch_name = 'All Branch';       
    }elseif ($bbrid >0){
        $bbridcond= " AND imei.brid=$bbrid";
        $branch_name = mysqli_fetch_assoc(mysqli_query($conn,"SELECT branch_name FROM branch WHERE coid='$coid' AND brid='$bbrid'"))['branch_name'];
    }



    if ($search =='0') {
         $blacksearch='';
      } else {
         $blacksearch = " AND (item.item_code LIKE '%$search%' || item.item_details LIKE '%$search%' || imei.imei_code LIKE '%$search%' || imei.imei_color  LIKE '%$search%' )";
    }


       if ($itype =='0') {
           $itypecond="";
    }elseif ($itype =='ECSL'){
        $itypecond= " AND imei.order_type='ECSL'"; 
    }elseif ($itype =='ERSL'){
        $itypecond= " AND imei.order_type='ERSL'";
    }elseif ($itype =='INSL'){
        $itypecond= " AND imei.order_type='INSL'";
    }elseif ($itype =='EPUR'){
        $itypecond= " AND imei.order_type='EPUR'";
    }elseif ($itype =='SOP'){
        $itypecond= " AND imei.order_type='SOP'";
    }


if ($itype =='ECSL'){

     $inv_query= mysqli_query($conn,"SELECT *,item.item_details,item.item_name,item.item_code,c_customer.c_name cus_name,c_customer.c_gsm outss, pgroup_name,pcat_name,pbrand_name,order_list.userid,em_acname
      FROM imei_tracker imei 
        INNER JOIN item ON (item.pro_id=imei.pro_id)
        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)
        LEFT JOIN order_list ON (order_list.invoice=imei.invoice)
        INNER JOIN c_customer ON (c_customer.c_id=imei.acid)
          LEFT JOIN user ON (user.uid=order_list.userid)
        LEFT JOIN em_employee ON (em_employee.em_id=user.us_em_id)


        WHERE imei.coid='$coid' $bbidcond $bbridcond  $blacksearch  $itypecond AND  date(imei.date) BETWEEN '$fdate' AND '$edate' GROUP BY imei.imei_code  LIMIT $nrow;") or die (mysqli_error($conn));
     $dataa = [];
     while ($invrow= mysqli_fetch_assoc($inv_query)){ $dataa[]=$invrow;}
}else{
    
        $inv_query= mysqli_query($conn,"SELECT *,item.item_details,item.item_name,item.item_code,accounts.ac_name cus_name,accounts.outlet_id outss,pgroup_name,pcat_name,pbrand_name,order_list.userid,em_acname
      FROM imei_tracker imei 
        INNER JOIN item ON (item.pro_id=imei.pro_id)
        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)
        LEFT JOIN order_list ON (order_list.invoice=imei.invoice)
        INNER JOIN accounts ON (accounts.acid=imei.acid)
        LEFT JOIN user ON (user.uid=order_list.userid)
        LEFT JOIN em_employee ON (em_employee.em_id=user.us_em_id)


        WHERE imei.coid='$coid' $bbidcond $bbridcond  $blacksearch  $itypecond AND  date(imei.date) BETWEEN '$fdate' AND '$edate'  GROUP BY imei.imei_code   LIMIT $nrow;") or die (mysqli_error($conn));
     $dataa = [];
     while ($invrow= mysqli_fetch_assoc($inv_query)){ $dataa[]=$invrow;}
    
}  
     
     
     
?>

 <table class="ttable border-bottom-0 d-none" id="header" cellpadding="0" cellspacing="0">
    <thead>
       <tr>
          <th rowspan="3" class="thborder border-bottom-0 text-center" style="width:10%;">
             <img class="profile-user-img img-fluid img-circle" src="../../dist/img/a.png" style="width:80px;height:77px;">
          </th>
          <th rowspan="3" class="thborder border-bottom-0" style="width:70%;">
                <dt style="font-size:18px;font-weight: 600;"><?php echo $cor['co_name'];?></dt>
                <dt><?php echo $cor['owner_name'];?></dt>
                <dt><?php echo $cor['co_address'];?></dt>
                <dt><?php echo $cor['co_phone'];?>#<?php echo $cor['co_email'];?></dt>
          </th>
          <th class="thborder text-center" style="width:20%;"><?php echo $branch_name;?></th>
       </tr>
       <tr>
          <th class="thborder text-center"><?php echo language(313);?></th>
       </tr>
       <tr>
          <th class="thborder text-center border-bottom-0"><?php echo date('d/m/y', strtotime($fdate));?> To <?php echo date('d/m/y', strtotime($edate));?>
          </th>
       </tr>
    </thead>
 </table>
<table id="pu_main_table" class="ttable" cellpadding="0" cellspacing="0">
<thead>
    <tr class="sticky-top">
        <th class="text-center" style="width:2%;"><?php echo language(15);?></th>
        <th class="text-center" style="width:15%;"><?php echo language(97)?></th>
        <th class="text-center" style="width:12%;"><?php echo language(215);?></th>
        <th  class="text-center" style="width:10%;"><?php echo language(387);?> </th>
        <th class="text-center" style="width:5%;"><?php echo language(388);?></th>
        <th class="text-center" style="width:5%;"><?php echo language(377);?></th>
        <th class="text-center" style="width:5%;"><?php echo language(151);?></th>
        <th class="text-center" style="width:5%;"><?php echo language(389);?></th>
        <th class="text-center" style="width:5%;"><?php echo language(390);?></th>
        <th class="text-center" style="width:5%;"><?php echo language(392);?></th>
        <th class="text-center" style="width:5%;"><?php echo language(131);?></th>
        <th class="text-center" style="width:5%;"><?php echo language(214);?></th>
        <th class="text-center" style="width:5%;"><?php echo language(90);?></th>




    </tr>
</thead>
 <tbody id="pinvr_show"></tbody>
  
</table>
<script id="invr_script" type="text/javascript">
   $(document).ready(function(){
       let dataa      = '';
       let i          = 0;


       
       $.each(<?php echo json_encode($dataa);?>, function(key,value){

  if (value.plus=='+') {
          imei_status_check = 'IN';
        } else if (value.plus=='-') {
          imei_status_check = 'Out';
        }  

       dataa += '<tr>';    
       dataa += '<td class="text-center">'+ ++i +'</td>';

       dataa += '<td>'+value.pgroup_name+'  '+value.pcat_name+'  '+value.pbrand_name+'  '+value.item_details+' '+ value.item_name +'   </td>';
              dataa += '<td>'+value.cus_name+' '+ value.outss +'</td>';

       dataa += '<td class="text-center">' + value.imei_code +  '</td>';
       dataa += '<td class="text-center">' + value.imei_color +  '</td>';

	   dataa += '<td class="text-center btn-outline-dark" onclick="vprintdd(this)" type="'+value.order_type+'" tid="'+value.acid+'">' + vtype(value.order_type) + '</td>';


       dataa += '<td class="text-center btn-outline-dark" onclick="vprint(this)" invoice="'+value.invoice+'" itype="'+value.order_type+'">  ' + (value.invoice).split("-")[5] + '</td>';

       dataa += '<td class="text-center">' + value.mfg_date +  '</td>';
       dataa += '<td class="text-center">' + value.exp_date +  '</td>';
       dataa += '<td class="text-center">' + imei_status_check  +  '</td>';

       dataa += '<td class="text-center">' + value.date +  '</td>';
       dataa += '<td class="text-center">' + value.em_acname +  '</td>';
       dataa += '<td class="text-center">' + value.active +  '</td>';









       dataa += '</tr>';
   });
      $("#pinvr_show").html(dataa);

      $("#invr_script").remove();
   });
</script>
<script>
 function itemprofile(productid){    
   $("#modal-xl").modal({backdrop: "static"});
    pagecall('../itemstock/item_profile.php?pro_id='+productid,'#xlmdview');   
   } 
</script>
<script>
  function vprint(tdd){
    <?php if ($printer_t=='P') { ?>
        $("#modal-sm").modal({backdrop: "static"});
        if ($(tdd).attr('itype')=='ECSL') {
            pagecall('../sales/c_sales_invoice_pos.php?invoice='+ $(tdd).attr('invoice'),'#smdview');
          } else if ($(tdd).attr('itype')=='ERSL') {
           pagecall('../sales/sales_invoice_pos.php?invoice='+ $(tdd).attr('invoice'),'#smdview');
          } else if ($(tdd).attr('itype')=='INSL') {
           pagecall('../sales/ins_sales_invoice.php?invoice='+ $(tdd).attr('invoice'),'#mdview');
        }
    <?php } elseif ($printer_t=='R') { ?>
        $("#modal-lg").modal({backdrop: "static"});
        if ($(tdd).attr('itype')=='ECSL') {
            pagecall('../sales/c_sales_invoice.php?invoice='+ $(tdd).attr('invoice'),'#mdview');
          } else if ($(tdd).attr('itype')=='ERSL') {
           pagecall('../sales/sales_invoice.php?invoice='+ $(tdd).attr('invoice'),'#mdview');
          } else if ($(tdd).attr('itype')=='INSL') {
           pagecall('../sales/ins_sales_invoice.php?invoice='+ $(tdd).attr('invoice'),'#mdview');
        }
    <?php } ?>
  }  
</script>
