<?php require_once('../auth/auth.php');require_once('../auth/function.php'); ?>
<?php 
     if (isset($_GET['pro_id'])){
         $item_id = $_GET['pro_id'];

     if (isset($_GET['imei_id'])) {
       $imei_id = $_GET['imei_id'];
       $imei_tracker_query= mysqli_query($conn,"SELECT  `imei_id`,`coid`, `bid`, `brid`, `acid`, `invoice`, `order_type`, `wh_id`, `pro_id`, `imei_code`, `imei_color`, `mfg_date`, `exp_date`, `plus`, `pu_price`, `sale_price`, `date`, `active`     FROM imei_tracker WHERE imei_id='$imei_id' LIMIT 1 ") or die (mysqli_error($conn));
           $row_im= mysqli_fetch_assoc($imei_tracker_query);
     }
?>
<div class="row">
   <div class="col-md-12">
      <div class="card card-danger 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(386);?></h3>
            <div class="card-tools m-0">
               <span class="mark1" onclick="copy(this);" title="Copy Invoice"><?php echo $invoice = invoice("SOP");?></span>
               <button type="button" class="btn btn-tool" data-card-widget="remove">
               <i class="fas fa-times"></i>
               </button>
            </div>
        </div>
        <form class="mt-2" id="imei_form" >
            <?php if (isset($_GET['imei_id'])) {?>
                <input type="hidden" name="up_imei_id" value="<?php echo $_GET['imei_id'];?>">
            <?php } else{ ?>
                <input type="hidden" name="up_imei_id" value="0">
            <?php } ?>
                <input type="hidden" name="item_id"    value="<?php echo $_GET['pro_id'];?>">
                <input type="hidden" name="op_invoice" value="<?php echo $invoice;?>">

            <div class="card-body">
                <div class="col-md-12">
                    <div class="row">
	                    <div class="col-md-3">
	                      <div class="form-group">
	                        <label><?php echo language(387);?> <b class="text-danger">*</b>  <span id="number-availability-status"></span> </label>
	                        <input type="text" name="imei_code" id="imei_number"  onBlur="checkenumberAvailability()" class="form-control form-control-sm" value="<?php echo @$row_im['imei_code'] ?>" required="true">
	                      </div>
	                    </div>

	                    <div class="col-md-3">
	                      <div class="form-group">
	                        <label><?php echo language(388);?> </label>
	                        <input type="text" name="imei_color" value="<?php echo @$row_im['imei_color']; ?>" class="form-control form-control-sm">
	                      </div>
	                    </div>

	                    <div class="col-md-3">
	                      <div class="form-group">
	                        <label><?php echo language(389);?> <b class="text-danger">*</b></label>
	                        <input type="text" name="mfg_date"  class="form-control form-control-sm" onmouseover="lcdate(this)" style="text-align:center;" readonly="true" required="true" value="<?php echo @$row_im['mfg_date'] ? $row_im['mfg_date']:$date;?>">
	                      </div>
	                    </div>

	                    <div class="col-md-3">
	                      <div class="form-group">
	                        <label><?php echo language(390);?> <b class="text-danger">*</b></label>
	                        <input type="text" name="exp_date" class="form-control form-control-sm" onmouseover="stdate(this)" style="text-align:center;" readonly="true" required="true" value="<?php echo @$row_im['exp_date'] ? $row_im['exp_date'] : $date;?>">
	                      </div>
	                    </div>
                    </div>
                </div>
            </div>
            <center>
               <div class="card-footer" style="padding: 8px 5px;">
                <button type="submit" id="savee" style="margin-top:19px;" class="btn btn-success btn-xs"><?php if (@$_GET['imei_id']) {echo language(25);}else{echo language(23);}?></button>
               </div>
            </center>
         </form>
      </div>
    </div>
<?php   } ?>
   <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(391);?></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 FROM imei_tracker 
                            LEFT JOIN item ON item.pro_id=imei_tracker.pro_id 
                            LEFT JOIN accounts ON accounts.acid=imei_tracker.acid
                            WHERE  imei_tracker.coid='$coid' AND imei_tracker.bid='$bid' AND order_type in('SOP','EPUR') AND  imei_tracker.brid='$brid' AND item.pro_id='$item_id' 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 ) { ?>
                    <tr   class="btn-outline-dark bg-danger" >
                          <?php }else{ ?>
                    <tr   class="btn-outline-dark " >
                         <?php  } ?>
                  
                        <td class="text-center"><?php echo ++$i ?></td>
                        
                        
                          <?php if ($order_type=='SOP') { ?>
                        <td onclick="pagecall('../imei_tracking/opening_imei_index.php?imei_id='+'<?php echo $iter['imei_id'];?>'+'&pro_id=<?php echo $pro_id; ?>','#pview')"> <?php echo $iter['item_name'];?> <?php echo $iter['item_details'];?></td>
                        <?php }else{ ?>
                        <td > <?php echo $iter['item_name'];?> <?php echo $iter['item_details'];?></td>
                         <?php  } ?>

                        

                        <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 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 ) { ?>
                        <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>
    function dell(imei_id) {
        $.post("../imei_tracking/delete_imei.php",
            {imei_id : imei_id},
         function(data){
 pagecall('../imei_tracking/opening_imei_index.php?pro_id=<?php echo $item_id;?>','#pview');
       toastr.danger(Showing, yourdata); 
               });     
    }
</script>
<script type="text/javascript">
   function checkenumberAvailability() {
      jQuery.ajax({
         url: "../duplicate_check/number_check.php",
         data:'imei_number_check='+$("#imei_number").val(),
         type: "POST",
         success:function(data){
         $("#number-availability-status").html(data);
         },
         error:function (){}
      });
   }


   $(document).ready(function(){
     $('#imei_form').on('submit', function(e){
      e.preventDefault();
     $('#savee').prop('disabled', true);
     $('#savee').html('<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span> '+'Loading...'); 
     $.post("../imei_tracking/opening_imei_db.php",$(this).serialize(),function(data){
      $('#imei_form')[0].reset();
      $('#savee').prop('disabled', false);
      pagecall('../imei_tracking/opening_imei_index.php?pro_id=<?php echo $item_id;?>','#pview');
       toastr.success(Showing, yourdata); 
    });
   });
  });  
</script>

<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>