<?php require_once('../auth/auth.php');require_once('../auth/function.php'); ?>
<?php
  $g_itemid = (isset($_GET['g_itemid']))?$_GET['g_itemid']:0;
  $custom_field= mysqli_query($conn,"SELECT purchase FROM custom_field WHERE coid='$coid' LIMIT 1") or die(mysqli_error($conn));
  $rowcf= mysqli_fetch_assoc ($custom_field);

  @$p=$rowcf['purchase'];
  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-1"></div>
         <div class="col-md-10">
            <div class="card card-warning 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: 5px 8px;">
                  <h3 class="card-title"> <?php echo language(199);?> </h3>
                  <div class="card-tools m-0">
                     <span class="mark1" onclick="copy(this);" title="Copy Invoice"><?php echo $invoice = invoice("EADJ");?></span>
                    <div class="btn-group">
                                    <?php if($g_itemid==0){ ?>
    
                        
                     <button type="button" class="btn btn-tool" data-dismiss="modal" data-card-widget="remove" onclick="$.post('../demo_itemadjust/order_list_delete.php',{cinvoice:'<?php echo $invoice;?>'});">
                     <i class="fas fa-times"></i>
                     </button>
                     
                     <?php } ?>
                    </div>                      
                  </div>
               </div>

               <div class="card-body mt-2"> 
                <?php if($g_itemid==0){ ?>
                <div class="col-md-12">
                <?php if(input_check('item_scan_status')==1){ ?>
                  <form id="adj_sub" action="#" method="post" autocomplete="off">
                <?php } ?>
                    <div class="row">
                      <div class="col-md-3"></div>
                      <div class="col-md-6">
                        <div class="form-group">
                          <label> <?php echo language(138);?></label>
                          <input type="text" id="aditem_details" class="form-control form-control-sm inputselect" required="true" autofocus="true">
                        </div>
                      </div>
                      <div class="col-md-3"></div>
                      <div class="col-md-12">
                        <hr style="margin-top:-5px;margin-bottom: 7px;">
                      </div>
                     </div>
                <?php if(input_check('item_scan_status')==1){ ?>
                  </form> 
                <?php } ?>
                </div>
              <?php } ?>

                <form id="adj_form" action="#" method="post" autocomplete="off">
                    <input type="hidden" name="adj_invoice" value="<?php echo $invoice;?>">
                     <div class="col-md-12">
                        <div class="row">
                           <div class="col-md-12">
                              <div class="card-body table-responsive cscroll" style="max-height: 450px;">
                                 <table class="ttable" cellpadding="0" cellspacing="0">
                                    <thead>
                                       <tr class="sticky-top">
                                        <?php if($g_itemid==0){ ?>
                                          <th class="text-center" style="width:2%;"><?php echo language(15);?></th>
                                          <th style="width:35%;"><?php echo language(77);?></th>
                                        <?php } ?>
                                          <th class="text-center" style="width:10%;"><?php echo language(208);?></th>
                                          <th class="text-center" style="width:8%;"><?php echo language(139);?></th>
                                          <th class="text-center <?php if(input_check('u_type') !==1){echo 'd-none';}?>" style="width:10%;"><?php echo language(98);?></th>
                                         <th class="text-center" style="width:10%;"><?php echo language(87);?></th>

                                          <th class="text-center" style="width:10%;"><?php echo language(81);?></th>
                                          <th class="text-center" style="width:8%;"><?php echo language(207);?></th>
                                          <th class="text-center" style="width:8%;"><?php echo language(268);?></th>
                                        <?php if($g_itemid==0){ ?>
                                          <th class="text-center" style="width:2%;">#</th>
                                        <?php } ?>
                                       </tr>
                                    </thead>
                                    <tbody id="row_show<?php echo $invoice;?>"></tbody>
                                 </table>
                              </div>
                           </div>
                        <div class="col-md-12"><hr style="margin-top: 7px;margin-bottom: 7px;"></div>
                        <div class="col-md-12">
                         <div class="form-row">
                          <input type="hidden" id="adsm_totalc">
                          <div class="col-md-12 <?php if(input_check('narration') !==1){echo 'd-none';}?>">
                             <div class="form-group">
                                <label><?php echo language(150);?></label>
                                <textarea name="addetails" class="form-control form-control-sm inputselect" rows="2" placeholder="Enter ..."></textarea>
                             </div>
                          </div> 
                          <div class="col-md-12 <?php if(input_check('narration') !==1){echo 'd-none';}?>"><hr style="margin-top: 7px;margin-bottom: 7px;"></div>
                         
                           <div class="col-md-4 <?php if(input_check('wharehouse') !==1){echo 'd-none';}?>">
                            <div class="form-group">
                               <label><?php echo language(104);?></label>
                               <select name="adwh_id" class="form-control select2" style="width: 100%;" required="true">
                                  <?php 
                                     $swh = mysqli_fetch_assoc(mysqli_query($conn,"SELECT bwid,war_location FROM business_w_house WHERE coid='$coid' AND bid='$bid' AND brid='$brid' AND active=1 AND default_set='primary' LIMIT 1"));
                                   ?>
                                <option value="<?php echo $swh['bwid'];?>"><?php echo $swh['war_location'];?></option>
                              <?php 
                                  $bw= mysqli_query($conn,"SELECT bwid,war_location FROM business_w_house WHERE coid='$coid' AND bid='$bid' AND brid='$brid' AND active=1 ORDER BY bwid ASC") or die(mysqli_error($conn));
                                    while($wh= mysqli_fetch_assoc($bw)){
                                    echo '<option value='.$wh["bwid"].'>'.$wh["war_location"].'</option>';
                                  }?>   
                               </select>
                            </div>
                           </div> 
                           
                           <div class="col-md-4 <?php if(input_check('invoice_date') !==1){echo 'd-none';}?>">
                              <div class="form-group">
                                <label><?php echo language(131);?></label>
                                 <input type="text" name="adtdate" class="form-control form-control-sm" onmouseover="lcdate(this)" style="text-align:center;" readonly="true" required="true" value="<?php echo $date;?>">
                              </div>
                           </div>


                           
                                              
                          </div>
                        </div>
                       </div>
                     </div>
                        <div class="card-footer mt-1" align="center" style="padding: 8px 5px;">
                              <button type="submit" id="adsave" class="btn btn-success"><i class="fas fa-save"></i> <?php echo language(23);?></button>
                        </div>
                  </form>
               </div>
            </div>
         </div>
         <div class="col-md-1"></div>
      </div>
   </div>
</section>
<script>
  $("select").select2(); 
  pagecall('../demo_itemadjust/order_list.php?invoice=<?php echo $invoice?>','#row_show<?php echo $invoice;?>',0);    
</script>

<script>
   $(document).ready(function() {
   $('#adj_form').on('submit', function(e){
   e.preventDefault(); 
   if (Number($('#adsm_totalc').val()) >0) {
   $('#adsave').prop('disabled', true);
   $('#adsave').html('<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span> '+'Loading...'); 
        $.post("../demo_itemadjust/adjust_db.php",$(this).serialize(),function(data){
        toastr.success(Successfully, yourdata);
        <?php if($g_itemid==0){ ?>
          pagecall('../demo_itemadjust/adjust_index.php','#stockhome');
        <?php } else { ?>
          pagecall('../demo_itemadjust/adjust_index.php?g_itemid=<?php echo $g_itemid;?>','#pview');
        <?php } ?>
      });
      } else {
        alert('Please Select Item...??');
      }
     });
   });  
</script>

<script>
  $(function() {
    $( "#aditem_details" ).autocomplete({
   //  minLength: 0,
       source: '../demo_itemadjust/item.php',
       autoFocus: true,
   //  delay:500,
   //  scroll: true,
       // focus: function( event, ui ) {
       //    $( "#item_details" ).val( ui.item.label );
       //       return false;
       // },
       select: function( event, ui ) {
          $( "#aditem_details" ).val( ui.item.label );
          $.post("../demo_itemadjust/order_list_db.php",
            {item_id : ui.item.value,oinvoice:'<?php echo $invoice;?>'},
         function(data){
          toastr.success(Successfully, yourdata);
          pagecall('../demo_itemadjust/order_list.php?invoice=<?php echo $invoice?>','#row_show<?php echo $invoice;?>',0);
        });  
        $( "#aditem_details" ).val('');        
          return false;
       }
    });
  });
</script>

<!-- for scan  -->
<script>
   $(document).ready(function() {
   $('#adj_sub').on('submit', function(e){
   e.preventDefault();
      let item_code = $('#aditem_details').val();
        $.post("../demo_itemadjust/order_list_db.php",{item_code : item_code,sinvoice:'<?php echo $invoice;?>'},function(data){
          toastr.success(Successfully, yourdata);
          pagecall('../demo_itemadjust/order_list.php?invoice=<?php echo $invoice?>','#row_show<?php echo $invoice;?>',0);
          $( "#aditem_details" ).val('');
        });
      });
   });  
</script>

<!-- for scan  -->


<!-- for Item Profile  -->
<?php if ($g_itemid>0) { ?>
  <script>
    $.post("../demo_itemadjust/order_list_db.php",{item_id : '<?php echo $g_itemid;?>',oinvoice:'<?php echo $invoice;?>'},function(data){
      pagecall('../demo_itemadjust/order_list.php?invoice=<?php echo $invoice?>'+'&g_itemid=<?php echo $g_itemid;?>','#row_show<?php echo $invoice;?>',0);
    });
  </script>
<?php } ?>
<!-- for Item Profile  -->