<?php require_once('../auth/auth.php');require_once('../auth/function.php');
if(isset($_GET['recacid'])){
   $recacid= $_GET['recacid'];
   $account_name=mysqli_query($conn,"SELECT ac_name,ac_number,ac_alise,balance FROM accounts WHERE acid='$recacid' LIMIT 1 ");
   $row=mysqli_fetch_assoc($account_name);
   $ac_name=$row['ac_name'];
   $ac_number=$row['ac_number'];
   $ac_alise=$row['ac_alise'];
   $newbalance=$row['balance'];
 }
  ?> 
<section class="content">
   <div class="container-fluid">
      <div class="row">

 <?php if(isset($_GET['recacid'])){  ?>
     <div class="col-md-12">
 <?php } else{ ?>         
     <div class="col-md-2"></div>
     <div class="col-md-8">
 <?php } ?>

     

        	<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: 5px 8px;">
        			<h3 class="card-title"> <?php echo language(42);?></h3>
        			<div class="card-tools">
        				<!-- <span title="3 New Messages" class="badge bg-primary">3</span> -->
                <span class="mark1" onclick="copy(this);" title="Copy Invoice"><?php echo $invoice = invoice("REC");?></span>
        				<button type="button" class="btn btn-tool" data-card-widget="remove">
        					<i class="fas fa-times"></i>
        				</button>
        			</div>
        		</div>

	            <form id="rec_form" action="#" method="post" autocomplete="off">
                <input type="hidden" name="rec_invoice" value="<?php echo $invoice;?>">
                     <div class="card-body">
                        <div class="col-md-12">
                          <div class="row">
                           <div class="col-md-6">
                              <div class="form-group">
                                 <label><?php echo language(221);?></label>
                                 <dt id="show" style="float: right;"></dt>
                                 <input type="hidden" name="dr_acid">
                                 <select id="dr_acid" onchange="balance(this.value,'#show')" class="form-control select2" style="width: 100%;" required="true">
                                <?php 
                                    $accat= mysqli_query($conn,"SELECT acid,ac_name,ac_alise,ac_number,balance FROM accounts LEFT JOIN ac_sub_g_category ON(accounts.gcatid=ac_sub_g_category.ac_g_catid) WHERE accounts.coid='$coid' AND bid='$bid' AND brid='$brid' AND active=1 AND g_type IN('mcash','bank')") or die (mysql_error());
                                      while($acat= mysqli_fetch_assoc($accat)){
                                      echo '<option value='.$acat["acid"].','.$acat["balance"].'>'.$acat["ac_name"]."-".$acat["ac_number"].' '.'('.$acat["ac_alise"].')</option>';
                                    }?>   
                                 </select>
                              </div>
                           </div>

                           <div class="col-md-6">
                              <div class="form-group">
                                 <label><?php echo language(162);?></label>
                                 <dt id="show1" style="float: right;"></dt>
                                 
                                 <!--if else working need-->
                                 <input type="hidden" name="cr_acid">                                
                                 <select id="cr_acid" onchange="balance(this.value,'#show1')" class="form-control select2" style="width: 100%;" required="true">
                                         <?php if ( @$recacid>0) { ?>
                                        <option selected="selected" value="<?php echo $recacid ?>,<?php echo $newbalance ?>">
                                <?php echo $ac_name.'-'.$ac_alise.','.$ac_number ?> </option> <?php } ?>
                             
                                 </select>
                              </div>
                           </div>
                        <div class="col-md-12"><hr style="margin-top:-5px;margin-bottom: 7px;"></div>
                        <div class="col-md-4"></div>
                           <div class="col-md-4">
                              <div class="form-group">
                                 <center><label><?php echo language(163);?></label></center>
                                  <input type="number" name="amount" id="amount"  oninput="live_cal();" onblur="blcal(this.value)" class="form-control form-control-sm inputselect" placeholder="00.00" style="text-align:center;" required="true">
                              </div>
                           </div>                           
                        <div class="col-md-4"></div>
                        <div class="col-md-12"><hr style="margin-top:-5px;margin-bottom: 7px;"></div>
              						<div class="col-md-12">
              						   <div class="form-group">
              						      <label><?php echo language(128);?></label>
              						      <textarea name="details" class="form-control form-control-sm inputselect" rows="2" >N/A</textarea>
              						   </div>
              						</div>


<?php if ($administrator == 'primary'){ ?>
                            <div class="col-md-4">
                            <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" >
                                    <?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>
<?php } else{  ?>  
   <input type="hidden" name="us_em_id"  value="<?php echo $us_em_id;?>"  required="true">
<?php }  ?>

                    <div class="col-md-4">
                      <div class="form-group">
                        <label><?php echo language(154);?></label>
                          <dt id="show3" style="float: right;"></dt>
                                 <input type="hidden" name="d_acid">
                        <select  id="d_acid"  onchange="balance(this.value,'#show3')" style="width: 100%;">
                       
                       <option value=""> Please Select  </option>
                        </select>
                      </div>
                    </div>

                    <div class="col-md-2">
                        <div class="form-group">
                            <label><?php echo language(88);?></label>
                             <input type="number" name="R_discount" id="R_discount" oninput="live_cal();" class="form-control form-control-sm inputselect text-center" value="0"  required="true">
                         </div>
                    </div>

                    <div class="col-md-2">
                        <div class="form-group">
                          <center><label><?php echo 'Net Payment';?></label></center>  
                             <dt style="padding:1px;" class="text-center bg-danger  " id="payable">00.00</dt>
                         </div>
                    </div>


                    <div class="col-md-3">
                      <div class="form-group">
      						      <div class="custom-control custom-checkbox">
      						        <input name="email" class="custom-control-input" type="checkbox" id="customCheckbox1" value="yes">
      						         <label for="customCheckbox1" class="custom-control-label"><?php echo language(155);?></label>
      						      </div>
      						   </div>
                    </div>


						<div class="col-md-3">
						   <div class="form-group">
						      <div class="custom-control custom-checkbox">
						         <input name="sms" class="custom-control-input" type="checkbox" id="customCheckbox2" value="yes">
						         <label for="customCheckbox2" class="custom-control-label"><?php echo language(132);?></label>
						      </div>
						   </div>
						</div>

            			<div class="col-md-3">
            				<div class="form-group"><span style="float: right;margin: 2px -35px;"><?php echo language(131);?></span>
            				   <input type="text" name="tdate" 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>


        </div>

      </div>
   </div>

  		<center>
  			<div class="card-footer" style="padding: 8px 5px;">
  				<button type="submit" id="save" class="btn btn-success btn-xs"><?php echo language(23);?></button>
          <button type="reset" class="btn btn-secondary btn-xs"><?php echo language(24);?></button>
        </div>
      </center>
	  </form>
	</div>

</div>
 <?php if(!isset($_GET['recacid'])){  ?>
<div class="col-md-2"></div>
<?php } ?>

</div>
</div>
</section>
<script>
function live_cal(){
  let amounttotalval = Number($('#amount').val());
  let R_discount = Number($('#R_discount').val());
  $('#payable').text(amounttotalval-R_discount);
}
</script>


<script>
  $(document).ready(function(){
    $("#us_em_id").select2();
    
    $('#dr_acid').select2({
   "language": {
       "noResults": function(){
           return "No Results <a href='#' onclick='new_accountsR()' class='float-right text-success' style='font-weight:600;'> <i class='fa fa-plus'> </i> New Add </a>";
       }
   },
    escapeMarkup: function (markup) {
        return markup;
    },
       ajax: {
              url: "../voucher/s_acc.php",
              type: "post",
              dataType: 'json',
              delay: 250,
              data: function (params) {
                  return {
                      search: params.term,
                      g_type: "'mcash','bank'"
                  };
              },
              processResults: function (response) {
                  return {
                      results: response
                  };
              },
              cache: true
          }
});
    
    
    
   $('#d_acid').select2({
   "language": {
       "noResults": function(){
           return "No Results <a href='#' onclick='new_accountsR()' class='float-right text-success' style='font-weight:600;'> <i class='fa fa-plus'> </i> New Add </a>";
       }
   },
    escapeMarkup: function (markup) {
        return markup;
    },
       ajax: {
              url: "../voucher/s_acc.php",
              type: "post",
              dataType: 'json',
              delay: 250,
              data: function (params) {
                  return {
                      search: params.term,
                      g_type: "'dr-exp' "
                  };
              },
              processResults: function (response) {
                  return {
                      results: response
                  };
              },
              cache: true
          }
});
      $("#cr_acid").select2({
            "language": {
       "noResults": function(){
           return "No Results <a href='#' onclick='new_accountsR()' class='float-right text-success' style='font-weight:600;'> <i class='fa fa-plus'> </i> New Add </a>";
       }
   },
   escapeMarkup: function (markup) {
        return markup;
    },
          ajax: {
              url: "../voucher/s_acc.php",
              type: "post",
              dataType: 'json',
              delay: 250,
              data: function (params) {
                  return {
                      search: params.term,
                      g_type: "'du-adv','su-deb','stl','su-cre','owner','dr-inc','in-inc','sh-cap','scca'"
                  };
              },
              processResults: function (response) {
                  return {
                      results: response
                  };
              },
              cache: true
          }
      });
  });

  function balance (value,sid){
      var vvvv = value.split(",");
      $(sid).next().val(Number(vvvv[0]));
      var bl  = Number(vvvv[1]);
      if (bl >=0) {
        $(sid).css("color","green");
        $(sid).text(bden.format(bl) + ' Dr');
      } else {
        $(sid).css("color","red");
        $(sid).text(bden.format(-bl) + ' Cr');
      }
  }

balance ($("#dr_acid").val(),"#show");
balance ($("#cr_acid").val(),"#show1");
balance ($("#d_acid").val(),"#show3");


  function blcal (value){
    if (!isNaN(value)) {   
     var v = $("#dr_acid").val().split(","); 
     var v1 = $("#cr_acid").val().split(",");
     var sbl  = Number(v[1]) + Number(value);
     var sbl1 = Number(v1[1]) - Number(value);
     if (sbl>=0) {
        $("#show").css("color","green");
        $("#show").text(bden.format(sbl) + ' Dr');
     } else {
        $("#show").css("color","red");
        $("#show").text(bden.format(-sbl) + ' Cr');
     }

     if (sbl1>=0) {
        $("#show1").css("color","green");
        $("#show1").text(bden.format(sbl1) + ' Dr');
     } else {
        $("#show1").css("color","red");
        $("#show1").text(bden.format(-sbl1) + ' Cr');
     }

    }
  }

  </script>
<script>
 function new_accountsR(){    
   $("#modal-lg").modal({backdrop: "static"});
    pagecall('../accounts/add_new_accounts.php?type='+'received','#mdview');   
   } 
</script>
<script>
$(document).ready(function() {
$('#rec_form').on('submit', function(e){
$('#save').prop('disabled', true);
$('#save').html('<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span> '+'Loading...');
e.preventDefault();  
$.post("../voucher/re_voucher_db.php",$(this).serialize(),function(data){
$('#rec_form')[0].reset(); 
$('#save').button('reset');
toastr.success(Showing, Successfully);
          pagecall('../voucher/re_voucher_index.php','#mview',0);

   });
  });
});  
</script>