<?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-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(43);?></h3>
        			<div class="card-tools">
                         <span class="mark1" onclick="copy(this);" title="Copy Invoice"><?php echo $invoice = invoice("INSR");?></span>
        				<button type="button" class="btn btn-tool" data-card-widget="remove">
        					<i class="fas fa-times"></i>
        				</button>
        			</div>
        		</div>

	            <form id="installment_form" action="#" method="post" autocomplete="off">
                <input type="hidden" name="ins_invoice" id="ins_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>
                                    <select id="cr_acid"   name="cr_acid"  onchange="lives();" class="form-control select2" style="width: 100%;" required="true">
                                    	<option value=""></option>
			                                <?php 
			                                    $ins_customer_query= mysqli_query($conn,"SELECT accounts.ac_name,accounts.acid,accounts.ac_alise,accounts.ac_number,accounts.balance,accounts.outlet_id FROM (intallment INNER JOIN accounts ON intallment.acid = accounts.acid) WHERE intallment.coid='$coid' AND intallment.bid='$bid' AND intallment.brid='$brid' AND intallment.status='unpaid'   GROUP BY intallment.acid ") or die (mysql_error());
			                                      while($ins_row= mysqli_fetch_assoc($ins_customer_query)){
			                                      echo '<option value='.$ins_row["acid"].','.$ins_row["balance"].'>'.$ins_row["ac_name"]."-".$ins_row["ac_number"].' '.'('.$ins_row["outlet_id"].')</option>';
			                                    } 
			                                ?>  
                                 </select>
                              </div>
                           </div>

                           <div class="col-md-12"><hr style="margin-top:-5px;margin-bottom: 7px;"></div>

                           <div  class="col-md-12" id="ins_list_show" class="mt-2"></div>

                           <div class="col-md-12"><hr style="margin-top: 7px;margin-bottom: 7px;"></div>

			                <div class="col-md-7">
			                  <div class="form-row">
			                     <div class="col-md-12">
			                      <div class="form-group">
			                        <label><?php echo language(150);?></label>
			                        <textarea name="details" class="form-control form-control-sm inputselect" rows="2" placeholder="Enter ...">N/A</textarea>
			                      </div>
			                    </div>
			                </div>
			            </div>

			




                <div class="col-md-1"></div>
                <div class="col-md-4">
                  <table class="ttable" cellpadding="0" cellspacing="0">
                    <tbody>

                           <tr>
                          <td class="text-right w-50"><dt><?php echo language(140);?></dt></td>
                          <td class="w-50"><dt class="text-center" id="instotal"></dt></td>
                      </tr>


                      <tr>
                        <td class="text-right w-50"><dt><?php echo language(88);?></dt></td>
                        <td class="w-50 text-right"><dt class="text-center">
                          <input type="number" name="ins_discount" id="ins_discount" oninput="live_cal();" class="form-control form-control-sm inputselect text-center" value="0"  required="true">
                        </dt></td>
                      </tr>
       
                      <tr>
                        <td class="text-right w-50"><dt><?php echo language(146);?> </dt></td>
                        <td class="w-50"><dt class="text-center" id="payable"></dt></td>
                      </tr>
                 
      
              
                    </tbody>
                  </table>
                </div>


               <div class="col-md-12">
			       <div class="form-row">  
                        <div class="col-md-4">
                              <div class="form-group">
                                 <label><?php echo language(239); ?><b class="text-danger"> *</b></label>
                                 <select  name="ins_uid"  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["uid"].'>'.$em_acname.' ('.$id_card.')</option>';
                                       }?> 
                                 </select>
                              </div>
                           </div>

			               <div class="col-md-2 ">
			                      <div class="form-group">
			                        <label> <?php echo language(151);?></label>
			                        <input type="text" name="m_invoice" value="0002" class="form-control form-control-sm text-center inputselect">
			                      </div>
			             </div>

    	                  <div class="col-md-4">
    	                      <div class="form-group">
    	                        <label><?php echo language(154);?></label>
    	                        <dt id="d_show" style="float: right;"></dt>
    	                        <input type="hidden" name="d_acid">
    	                        <select id="d_acid" onchange="balance(this.value,'#d_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('dr-exp')") 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-2 ">
		                      <div class="form-group">
		                        <label> <?php echo language(131);?> </label>
		                        <input type="text" name="tdate" class="form-control form-control-sm text-center" onmouseover="lcdate(this)" readonly="true" required="true" value="<?php echo $date;?>">
		                      </div>
		                    </div>

                             <div class="col-md-4 ">  </div>
			                    <div class="col-md-2 ">
			                      <div class="form-group mt-3">
			                        <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 float-right"><?php echo language(155);?></label>
			                        </div>
			                      </div>
			                    </div>

			                    <div class="col-md-2 ">
			                      <div class="form-group mt-3">
			                        <div class="custom-control custom-checkbox">
			                          <input name="sms" class="custom-control-input" type="checkbox" id="customCheckboxsmscash" value="yes">
			                          <label for="customCheckboxsmscash" class="custom-control-label float-right"><?php echo language(132);?></label>
			                        </div>
			                      </div>
			                    </div>
                               <div class="col-md-4 ">  </div>
			                    
			                 </div>
			              </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 type="text/javascript">
   $(document).ready(function(){
     $('select').select2();       
   });
     function lives() {
       let cr_acid        = $('#cr_acid').val();
       let ins_invoice    = $('#ins_invoice').val();
       $(".preloader").css('display','flex');
       $.get("../voucher/ins_order_list.php",
         {
           cr_acid      : cr_acid,
           ins_invoice  : ins_invoice
         },
         function(html) {
           $("#ins_list_show").html(html);
           $('.preloader').hide();
         });
     }

</script>

<script>
$(document).ready(function() {
$('#installment_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/ins_receive_index_db.php",$(this).serialize(),function(data){
     toastr.success(Successfully, yourdata);
     $('#installment_form')[0].reset();
     $('#save').prop('disabled', false);
     $('#save').html('Save');
     toastr.success(Showing, Successfully);
    $("#modal-lg").modal({backdrop: "static"});
          pagecall('../print_setup/ri_ins_preview.php?ri_ins_invoice=<?php echo $invoice?>','#mdview',0);
               $('#save').html('save');   });
  });
});  
</script>





<script>
  $("select").select2(); 
function live_cal(){
  let instotalval = Number($('#instotal').text());
  let instotaldis = Number($('#ins_discount').val());
  $('#payable').text(instotalval-instotaldis);

}
</script>
















<script>
  $(document).ready(function(){
    $("#dr_acid").select2();
      $("#dr_acid").select2({
          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','dr-exp'"
                  };
              },
              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");

  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>

