<?php require_once('../auth/auth.php');
  $debitor_id=$_GET['debitor_id'];
   $custom_field= mysqli_query($conn,"SELECT customer FROM custom_field WHERE coid='$coid' LIMIT 1 ") or die(mysqli_error($conn));
      $rowcf= mysqli_fetch_assoc ($custom_field);
   
      $p=$rowcf['customer'];
      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(35); ?> </h3>
          <div class="card-tools">
            <button type="button" class="btn btn-tool" onclick="ExcelMe('#debitor_table')"  title="Download Excel">
              <i class="far fa-file-excel"></i>
            </button>
            <button type="button" onclick="BfpdfMe('#debitor_table');" class="btn btn-tool" title="Download Pdf"><i class="fas fa-download"></i></button>
            <button type="button" class="btn btn-tool" onclick="printMe('#receiable_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" oninput="lives();">
               </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" oninput="lives();">
               </div>
              </div> 

   
           <div class="col-2">      
            <div class="form-group" >
             <label> <?php echo language(207); ?> </label>
             <dt id="show1" style="float: right;"></dt>
             <select  id="ttype"  class="form-control" required="true" onchange="lives();">
              <option selected value="0"> <?php echo language(222); ?> </option> 
              <option value="dr"> <?php echo language(217); ?> (+) </option> 
              <option value="cr"> <?php echo language(218); ?> (-) </option>  
            </select>
          </div>
        </div>
         <div class="col-2">        
                     <div class="form-group">
                        <label><?php echo language(224); ?>  </label>
                        <select  id="areaid" name="areaid"  class="form-control" onchange="lives();">
                          <option value="0"><?php echo language(222); ?> </option>
                         <?php 
                           $em_Route= mysqli_query($conn,"SELECT * FROM route_area  WHERE coid='$coid' AND bid='$bid' AND brid='$brid' ") or die (mysqli_error($conn));
                           while($rot= mysqli_fetch_assoc($em_Route)){
                             echo '<option value='.$rot["roid"].'>'.$rot["route_name"]."-".$rot["road_name"].' </option>';
                          }?> 
                       </select>
                    </div>
        </div>
        
        
        <div class="col-md-3">
                      <div class="form-group">
                         <label><?php echo language(119);?><b class="text-danger"> *</b>  </label>
                         <select name="customer_type" id="customer_type" class="form-control" required="true" onchange="lives();">
                             <option selected value="0"> <?php echo language(222); ?> </option> 
                            <option value="retail">Retail</option>
                            <option value="ws">WholeSaler</option>
                            <option value="distri">Dristributor</option>
                            <option value="emi">EMI (installment) </option>
                            <option value="B2B">B2B (sales) </option>
                         </select>
                      </div>
        </div>
        
            <div class="col-2">
                  <div class="form-group">
                       <label><?php echo language(239); ?><b class="text-danger"> *</b></label>
                                 <select  name="us_em_id"  id="us_em_id" class="form-control" onchange="lives();">
                                     <option value="0"> <?php echo language(222);?> </option>
                                    <?php 
                                       $emdd_query= mysqli_query($conn,"SELECT uid,us_em_id FROM user  
                                          WHERE user.us_coid='$coid'  AND user.us_bid='$bid'  AND  user.us_brid='$brid'  AND user.us_status=1  ") or die (mysqli_error($conn));
                                       while($rouid= mysqli_fetch_assoc($emdd_query)){
                                          $get_emdata=$rouid["us_em_id"];
                                        $em_acname= mysqli_fetch_assoc(mysqli_query($conn,"SELECT em_acname FROM em_employee WHERE em_coid='$coid' AND em_bid='$bid' AND em_brid='$brid' AND em_id='$get_emdata' LIMIT 1"))['em_acname'];
                                        $id_card= mysqli_fetch_assoc(mysqli_query($conn,"SELECT id_card FROM em_employee WHERE em_coid='$coid' AND em_bid='$bid' AND em_brid='$brid' AND em_id='$get_emdata' LIMIT 1"))['id_card'];

                                        echo '<option value='.$rouid["us_em_id"].'>'.$em_acname.' ('.$id_card.')</option>';
                                       }?> 
                                 </select>
                 </div>
            </div>
                           
                           
        <input type="hidden" value="<?php echo $debitor_id; ?>" id="debitor_id" name="debitor_id">
     </div>
         </div>
        <div id="receiable_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('../customer/receivable_search_view.php?search=0'+'&bbid=<?php echo $bid;?>'+'&bbrid=<?php echo $brid;?>'+'&nrow=50'+'&debitor_id=<?php echo $debitor_id;?>'+'&ttype=0'+'&areaid=0'+'&ttype=0'+'&customer_type=0'+'&us_em_id=0','#receiable_display',0);
   });
     function lives() {
       var search      = $('#search').val()==''?0:$('#search').val();
       var bbid        = $('#bbid').val();
       var bbrid       = $('#bbrid').val();
       var debitor_id  = $('#debitor_id').val();
       var nrow        = $('#nrow').val()==''?50:$('#nrow').val();
       var ttype       = $('#ttype').val();
       var areaid      = $('#areaid').val();
       var customer_type = $('#customer_type').val();
       var us_em_id   = $('#us_em_id').val();
       $.get("../customer/receivable_search_view.php",
         {
           search      : search,
           bbid        : bbid,
           bbrid       : bbrid,
           debitor_id  : debitor_id,
           nrow        : nrow,
           ttype       : ttype,
           areaid      : areaid,
           customer_type:customer_type,
           us_em_id  : us_em_id
         },
         function(html) {
           $("#receiable_display").html(html);
         });
     }

      // fdate : fdate,
      //           edate : edate,

</script>

