<?php require_once('../auth/auth.php');require_once('../auth/function.php'); ?>
<?php 
  if (isset($_GET['pro_id'])) {
    $item_id = $_GET['pro_id'];
    $invoice = $_GET['invoice'];
    $op_row= mysqli_fetch_assoc(mysqli_query($conn,"SELECT item_name,item_details FROM item WHERE coid='$coid' AND bid='$bid' AND brid='$brid' AND  pro_id='$item_id' LIMIT 1"));
    $item_name    = $op_row['item_name'];
    $item_details = $op_row['item_details'];

        $order_row= mysqli_fetch_assoc(mysqli_query($conn,"SELECT eoid,pu_price,sale_price FROM order_list_edit WHERE coid='$coid' AND bid='$bid' AND brid='$brid' AND  pro_id='$item_id' AND invoice='$invoice' LIMIT 1"));
        $Get_eoid   = $order_row['eoid'];
        $pu_price   = $order_row['pu_price'];
        $sale_price = $order_row['sale_price'];
 }
?>


<section class="content">
  <div class="row">
    <div class="col-md-12">
      <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 mt-1"> <?php echo language(57);?> <?php echo language(386);?>  <strong class="ml-3 text-danger text-last"> <?php echo @$item_name.@$item_details; ?>  >>  <?php echo @$invoice; ?> </strong>
          </h3>
          <div class="card-tools mt-1">
            <button type="button" class="btn btn-tool"  data-card-widget="remove" data-dismiss="modal">
            <i class="fas fa-times"></i>
            </button>
          </div>
        </div>

        <div class="card-body">
         <form class="mt-2" id="imei_form" >
          <input type="hidden" name="item_id" value="<?php echo $_GET['pro_id'];?>">
          <input type="hidden" name="invoice" value="<?php echo $invoice;?>">
          <input type="hidden" name="eoid" value="<?php echo $Get_eoid;?>">

            <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" 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" class="form-control form-control-sm">
	                      </div>
	                    </div>

	                   <input type="hidden" name="pu_price"   value="<?php echo @$pu_price;?>" >
	                   <input type="hidden" name="sale_price" value="<?php echo @$sale_price; ?>">

	                   <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 $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 $date;?>">
	                      </div>
	                    </div>
                    </div>
                </div>
            </div>
            <center>
               <div class="card-footer" style="padding: 8px 5px;">
                  <button type="submit" id="savee" class="btn btn-success btn-xs"><i class="fas fa-save"></i> <?php echo language(23)?></button>
               </div>
            </center>
         </form>
      </div>
    </div>

   
      <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 mt-1"><?php echo language(57);?> <?php echo language(391);?></h3>
            <div class="card-tools mt-1">
               <input type="text" class="form-control form-control-sm search" placeholder="">
            </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(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>
                      <th class="text-center w-5">#</th>

                    </tr>
                  </thead>
                  <tbody class="actived">
                    <?php
                     $iteq= mysqli_query($conn,"SELECT *,item.item_name,item.item_details FROM imei_tracker_edit INNER JOIN item ON item.pro_id=imei_tracker_edit.pro_id WHERE  imei_tracker_edit.coid='$coid' AND imei_tracker_edit.bid='$bid' AND imei_tracker_edit.brid='$brid' AND imei_tracker_edit.pro_id='$item_id' ORDER BY edit_imei_id DESC  ") 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'];
                          $ssinvoice    = $iter['invoice'];
                          $imei_color   = $iter['imei_color'];
                          $mfg_date     = $iter['mfg_date'];
                          $exp_date     = $iter['exp_date'];
                          $date         = $iter['date'];
                          $active       = $iter['active'];
       
                    ?>

                     <?php if ($order_type=='SOP') { ?>
                    <tr   class="btn-outline-dark tr-hover" >
                    <?php } else {  ?>
                    <tr class="btn-outline" style="background-color:#e7e7e7;">
                     <?php }  ?>

                        <td class="text-center"><?php echo ++$i ?></td>

                        <td > <?php echo $iter['item_name'];?> <?php echo $iter['item_details'];?></td>

                        <td class="text-center">
                          <?php   if ($order_type=='SOP')
                        {
                        	echo "Opening";
                        }else if($order_type=='EPUR'){
                            echo "Purchase";
                        }
                        ?></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 if($iter['active'] == 1  ){ ?>
                        <td  class="text-center " > <i class="fa fa-check-circle text-success" aria-hidden="true"></i> </td>
                        <?php } else if($iter['active'] == 0) { ?>
                        <td class="text-center bg-danger" > <i class="fa fa-times-circle-o" aria-hidden="true"></i>  </td>
                        <?php  } ?>


                        <?php if ($order_type =='EPUR' AND $invoice == $ssinvoice) { ?>
                        <td class="text-center" onclick="dell(<?php echo $iter['edit_imei_id'];?>)"><i class="fas fa-trash"></i></td>
                        <?php } else {  ?>
                        <td class="text-center text-danger"><i class="fas fa-close"></i></td>
                        <?php }  ?>

                    </tr>
                    <?php } ?>

                      


                 </tbody>
               </table>
             </div>


   </div>
</div>
</section>
<script>
	function dell(imei_id) {
   $.post("../imei_tracking/delete_imei.php",
    {edit_imei_id : imei_id,pu_imei_invoice:'<?php echo $invoice;?>',eoid:'<?php echo $Get_eoid;?>'},
       function(data){
       pagecall('../purchase/order_list.php?invoice=<?php echo $invoice?>','#row_show<?php echo $invoice;?>',0);
        $("#modal-xl").modal({backdrop: "static"});
        pagecall('../imei_tracking/purchase_imei_index.php?pro_id='+<?php echo $item_id ;?>+'&invoice='+'<?php echo $invoice; ?>'+'&eoid='+'<?php echo $Get_eoid;?>','#xlmdview'); 
        });		
 }
</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>


<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/purchase_imei_db.php",$(this).serialize(),function(data){
	      $('#imei_form')[0].reset();
	      $('#savee').prop('disabled', false);

	      pagecall('../purchase/order_list.php?invoice=<?php echo $invoice?>','#row_show<?php echo $invoice;?>',0);
	      $("#modal-xl").modal({backdrop: "static"});
	      pagecall('../imei_tracking/purchase_imei_index.php?pro_id='+<?php echo $item_id ;?>+'&invoice='+'<?php echo $invoice; ?>'+'&eoid='+'<?php echo $Get_eoid;?>','#xlmdview');   
	       toastr.success(Showing, yourdata); 
        });
      });
    });  
</script>