<?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;} 
      }
?>
<section class="content">
 <div class="container-fluid">
  <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"><?php echo language(75);?></h3>
          <div class="card-tools">
              
            <button type="button" onclick="isync(this);" class="mr-3 btn btn-tool" title="Inventory Sync"> <i class="fas fa-rotate"></i>  Sync </button>
              
            <button type="button" class="btn btn-tool" onclick="ExcelMe('#stock')"  title="Download Excel">
              <i class="far fa-file-excel"></i>
            </button>
             

            <button type="button" onclick="BfpdfMe('#stock','header');" class="btn btn-tool" title="Download Pdf"><i class="fas fa-download"></i></button>
            

            <button type="button" class="btn btn-tool" onclick="printMe('#display','header')" title="Print Now"><i class="fas fa-print"></i></button>
            <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 p-1">
          <div class="col-md-12" style="border:1px solid #969c9d;background: hsla(0,0%,50.2%,.11);border-radius: 5px;">
           <div class="row mt-1 mb-n2">
        <?php if($bid_check>1 || $multi_bid > 1) {?>

             <div class="col-2">
              <div class="form-group">
                  <label> <?php echo language(231); ?>  </label>
                  <select id="bbid" class="form-control" onchange="lives();">
                          <option selected value="<?php echo $bid;?>"> 
                            <?php echo $bid_name=mysqli_fetch_assoc(mysqli_query($conn,"SELECT bid_name FROM business WHERE bid='$bid'  LIMIT 1 "))['bid_name'];  
                            ?>
                    </option>
                     <option value="0">  <?php echo language(222); ?> </option>
                     <?php 
                        $prr= mysqli_query($conn,"SELECT bid,bid_name FROM business WHERE coid='$coid'") or die(mysqli_error($conn));
                          while($prw= mysqli_fetch_assoc($prr)){
                          echo '<option value='.$prw["bid"].'>'.$prw["bid_name"].'</option>';
                        }?>
                  </select>
               </div>
             </div>
        <?php } else { ?>
             <input type="hidden" id="bbid" value="<?php echo $bid;?>">
        <?php } if( $brid_check > 1 ||  $multi_brid > 1) { ?>

             <div class="col-2">
               <div class="form-group" >
                  <label> <?php echo language(232); ?>  </label>
                  <select  id="bbrid"  class="form-control" required="true" onchange="lives();">
                      
                         <option selected value="<?php echo $brid;?>">
                           <?php echo $branch_name=mysqli_fetch_assoc(mysqli_query($conn,"SELECT branch_name FROM branch WHERE bid='$bid'  LIMIT 1 "))['branch_name'];  
                            ?>
                     </option>
                     
                     <option value="0">   <?php echo language(222); ?></option>
                     <?php 
                          $prr= mysqli_query($conn,"SELECT brid,branch_name FROM branch WHERE coid='$coid' AND bid='$bid'") or die(mysqli_error($conn));
                            while($prw= mysqli_fetch_assoc($prr)){
                            echo '<option value='.$prw["brid"].'>'.$prw["branch_name"].'</option>';
                          }?>
                  </select>
               </div>
             </div>
        <?php } else { ?>
             <input type="hidden" id="bbrid" value="<?php echo $brid;?>">
        <?php } ?>
             <div class="col-2">
               <div class="form-group" >
                  <label><?php echo language(68);?></label>
                  <input type="text" id="search" class="form-control form-control-sm text-center">
               </div>
             </div>  
              <div class="col-2 <?php if(input_check('t_group') !==1){ echo 'd-none';}?>">
                <div class="form-group">
                   <label><?php echo language(69);?></label>
                   <select id="item_gid" class="form-control" required="true" onchange="lives();">
                      <option value="0"><?php echo language(222); ?> </option>
                      <?php 
                         $pgroup_query= mysqli_query($conn,"SELECT pgid,pgroup_name FROM item_group  WHERE coid='$coid' AND bid='$bid' AND active=1 ORDER BY pgid ASC") or die (mysqli_error($conn));
                         while($rowgroup= mysqli_fetch_assoc($pgroup_query)){
                           echo '<option value='.$rowgroup["pgid"].'>'.$rowgroup["pgroup_name"].' </option>';
                         }?> 
                   </select>
                </div>
              </div>
              <div class="col-2 <?php if(input_check('t_category') !==1){ echo 'd-none';}?>">
               <div class="form-group">
                   <label><?php echo language(70);?></label>
                   <select id="item_cid" class="form-control" required="true" onchange="lives();">
                      <option value="0"><?php echo language(222); ?> </option>
                      <?php 
                         $pcatt_query= mysqli_query($conn,"SELECT pcid,pcat_name FROM item_catagory  WHERE coid='$coid' AND bid='$bid' AND active=1 ORDER BY pcid ASC") or die (mysqli_error($conn));
                         while($rowcat= mysqli_fetch_assoc($pcatt_query)){
                           echo '<option value='.$rowcat["pcid"].'>'.$rowcat["pcat_name"].' </option>';
                         }?> 
                   </select>
                </div>
              </div>
              <div class="col-2">
                <div class="form-group">
                   <label><?php echo language(71);?></label>
                   <select id="item_brnd" class="form-control" required="true" onchange="lives();">
                      <option value="0"><?php echo language(222); ?></option>
                      <?php 
                         $pbrand_query= mysqli_query($conn,"SELECT pbid,pbrand_name FROM item_brand  WHERE coid='$coid' AND bid='$bid' AND active=1 ORDER BY pbid ASC") or die (mysqli_error($conn));
                         while($rowbrand= mysqli_fetch_assoc($pbrand_query)){
                           echo '<option value='.$rowbrand["pbid"].'>'.$rowbrand["pbrand_name"].'</option>';
                         }?> 
                   </select>
                </div>
              </div>
             <div class="col-1">
               <div class="form-group" >
                  <label><?php echo language(72);?></label>
                  <input type="number" id="nrow" title="View Limit" class="form-control form-control-sm text-center" value="50">
               </div>
              </div> 
              
                     <!-- filter add -->
                      <!--  <div class="col-1 float-left text-left ">      -->
                      <!--      <div class="form-group" >-->
                      <!--                      <div>  <b><?php echo ' Filter';?></b>  </div>     -->

                      <!--          <button id="filter-button" > <i style="font-size:14px; background:" class="fa fa-filter ">  </i> </button></div>-->
                      <!--  </div>-->
                      <!--</div>  -->

                      <!--<div id="filter-div" style="display:none;">-->
                      <!--<div class="row mt-1 mb-n2">-->
                          <div class="col-1">
               <div class="form-group" >
                  <label><?php echo language(73);?></label>
                  <input type="number" id="qtymn" title="Minimum Quantity" class="form-control form-control-sm text-center">
               </div>
              </div> 
              <div class="col-1">
               <div class="form-group" >
                  <label><?php echo language(74);?></label>
                  <input type="number" id="qtymx" title="Maximum Quantity" class="form-control form-control-sm text-center">
               </div>
              </div> 
              
                 <div class="col-1 mr-3 ">
               <div class="form-group">
                   <label><?php echo language(90);?></label>
                   <select id="item_qty" class="form-control" required="true" onchange="lives();">
                      <option value="0">  <?php echo language(222); ?> </option>
                      <option value="zero"> <?php echo language(405);?> </option>
                      <option value="zeroup"> <?php echo 'Zero Up';?>  </option>

                      <option value="less"> <?php echo language(406);?>  </option>
                      <option value="decimal"> <?php echo language(407);?>  </option>
                   </select>
                </div>
              </div>
              
               <div class="col-1 ">
               <div class="form-group">
                   <label><?php echo 'A-Z';?></label>
                   <select id="asc_desc" class="form-control" required="true" onchange="lives();">
                      <option value="0">  <?php echo language(222); ?> </option>
                      <option value="ASC"> A-Z (<?php echo language(97)?> ) </option>
                      <option value="DESC"> Z-A (<?php echo language(97)?> )  </option>
                   </select>
                </div>
              </div>
                    <!--  </div>                   -->
                    <!--</div>-->
                   <!-- filter End -->
            
              
              

          </div>
         </div>
        <div id="display" class="mt-2"></div>
       </div>
    </div>
   </div>
  </div>
</div>
</section>
<script type="text/javascript">
   $("select").select2();
   function vprofile( 
     ){    
     $("#modal-xl").modal({backdrop: "static"});
     pagecall('../accounts/ac_profile.php?acid='+acid,'#xlmdview');   
   }  
</script>
<script type="text/javascript">
   $(document).ready(function(){
     $('select').select2();       
     pagecall('../itemstock/stock_list_live_search.php?search=0'+'&bbid=<?php echo $bid;?>'+'&bbrid=<?php echo $brid;?>'+'&nrow=50'+'&item_qty=0'+'&asc_desc=0','#display');
   });
     function lives() {
       var search    = $('#search').val()==''?0:$('#search').val();
       var item_gid  = $('#item_gid').val();
       var item_cid  = $('#item_cid').val();
       var item_brnd = $('#item_brnd').val();
       var bbid      = $('#bbid').val();
       var bbrid     = $('#bbrid').val();
       var nrow      = $('#nrow').val()==''?50:$('#nrow').val();
       var qtymn     = $('#qtymn').val();
       var qtymx     = $('#qtymx').val();
       var item_qty  = $('#item_qty').val();
       var asc_desc  = $('#asc_desc').val();
       $(".preloader").css('display','flex');
       $.get("../itemstock/stock_list_live_search.php",
         {
           search      : search,
           bbid        : bbid,
           bbrid       : bbrid,
           item_gid    : item_gid,
           item_cid    : item_cid,
           item_brnd   : item_brnd,
           nrow        : nrow,
           qtymn       : qtymn,
           qtymx       : qtymx,
           item_qty    : item_qty,
           asc_desc    : asc_desc
         },
         function(html) {
           $("#display").html(html);
           $('.preloader').hide();
         });
     }
    var timer = null;
    $('#search,#nrow,#qtymn,#qtymx').on('input',function(){
      clearTimeout(timer);
      timer = setTimeout(lives, 500);
    });
</script>
<script>
    function isync(select) {
        $(".preloader").css('display','flex');
        $(select).html('<i class="fas fa-cog fa-spin"></i> <span class="d-none d-md-inline">Loading..?</span>');
         $.post("../auth/item_sync.php",
          {},
        function(data){
           $(select).html('<i class="fa-regular fa-circle-check"></i> <span class="d-none d-md-inline">Complete</span>');
            $(select).children().fadeOut("slow", function(){
                $(select).html('<i class="fa-solid fa-rotate"></i> <span class="d-none d-md-inline"> Sync</span>');
                $('.preloader').hide();
            });
          toastr.success('Inventory Updated', 'Inventory Sync Complete');
        });
      }  
</script> 

<script>
    $(document).ready(function() {
    $('#filter-button').click(function() {
      $('#filter-div').slideToggle();
    });
  });
</script>