<?php require_once('../auth/auth.php');require_once('../auth/function.php'); ?>
<section class="content">
   <div class="container-fluid">
      <div class="row">

        <div class="col-md-2"></div>
        <div class="col-md-8">

        	<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(47); ?></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("CON");?></span>
        				<button type="button" class="btn btn-tool" data-card-widget="remove">
        					<i class="fas fa-times"></i>
        				</button>
        			</div>
        		</div>

	            <form id="con_form" action="#" method="post" autocomplete="off">
                <input type="hidden" name="con_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(149);?></label>
                                 <dt id="show" style="float: right;"></dt>
                                 <input type="hidden" name="cr_acid">
                                 <select id="cr_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(mysqli_error($conn));
                                      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(221);?></label>
                                 <dt id="show1" style="float: right;"></dt>
                                 <input type="hidden" name="dr_acid">                                
                                 <select id="dr_acid" onchange="balance(this.value,'#show1')" class="form-control select2" style="width: 100%;" required="true">
                                 	<option></option>
                                 <?php 
                                    $accat1= 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(mysqli_error($conn));
                                      while($acat1= mysqli_fetch_assoc($accat1)){
                                      echo '<option value='.$acat1["acid"].','.$acat1["balance"].'>'.$acat1["ac_name"]."-".$acat1["ac_number"].' '.'('.$acat1["ac_alise"].')</option>';
                                    }?>  
                                 </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" onblur="blcal(this.value)" class="form-control form-control-sm inputselect" placeholder="xxx" 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" placeholder="Enter ...">N/A</textarea>
              						   </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>
<div class="col-md-2"></div>
</div>
</div>
</section>

<script>
  $(document).ready(function(){
    $("#cr_acid").select2();
    $("#dr_acid").select2();
  });

  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 ($("#cr_acid").val(),"#show");

  function blcal (value){
    if (!isNaN(value)) {   
     var v = $("#cr_acid").val().split(","); 
     var v1 = $("#dr_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>
$(document).ready(function() {
$('#con_form').on('submit', function(e){
   var cr_acid = $("#cr_acid").val();
   var dr_acid = $("#dr_acid").val();
   

$('#save').prop('disabled', true);
$('#save').html('<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span> '+'Loading...');
e.preventDefault();

   if(cr_acid=='' || dr_acid==''  || dr_acid==cr_acid  ){
 toastr.error(Showing, Successfully);
  $('#con_form')[0].reset();
         $('#save').prop('disabled', false);
     $('#save').html('Vouchar Save');
   }
else{
    
     $.post("../voucher/con_voucher_db.php",$(this).serialize(),function(data){
     toastr.success(Successfully, yourdata);
     $('#con_form')[0].reset();
     $('#save').prop('disabled', false);
     $('#save').html('Vouchar Save');
     toastr.success(Showing, Successfully);
    //  pagecall('../voucher/con_money_receipt.php?tid='+tid,'#mdview');
   });
}
  });
});  
</script>