<?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-success 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(46); ?></h3>
                  <div class="card-tools">
                     <span class="mark1" onclick="copy(this);" title="Copy Invoice"><?php echo $invoice = invoice("JUR");?></span>
                     <button type="button" class="btn btn-tool" data-card-widget="remove">
                     <i class="fas fa-times"></i>
                     </button>
                  </div>
               </div>
               <div class="card-body">
                  <form id="ac_sub" action="#" method="post" autocomplete="off">
                     <div class="col-md-12">
                        <div class="row">
                           <div class="col-md-5">
                              <div class="form-group">
                                 <label> <?php echo language(216); ?></label>
                                 <dt id="show" style="float: right;"></dt>
                                 <select id="acid" onchange="balance(this.value,'#show')" class="form-control select2" style="width: 100%;" required="true">  
                                 </select>
                              </div>
                           </div>
                           <div class="col-md-3">
                              <div class="form-group">
                                 <center><label><?php echo language(163);?></label></center>
                                 <input type="number" id="amount" step="0.001" class="form-control form-control-sm inputselect" placeholder="00.00" style="text-align:center;" required="true">
                              </div>
                           </div>
                           <div class="col-md-2">
                              <div class="form-group">
                                 <label><?php echo language(130); ?></label>
                                 <select id="ttype" class="form-control select2" style="width: 100%;" required="true">
                                    <option value="+"><?php echo language(217);?>(+)</option>
                                    <option value="-"><?php echo language(218);?>(-)</option>
                                 </select>
                              </div>
                           </div>
                           <div class="col-md-2">
                              <center>
                                 <button type="submit" class="btn btn-success btn-xs" style="margin-top:18.5px;"><i class="fas fa-circle-plus"></i> <?php echo language(220);?></button>
                              </center>
                           </div>
                           <div class="col-md-12">
                              <hr style="margin-top:-5px;margin-bottom: 7px;">
                           </div>
                        </div>
                     </div>
                  </form>
                  <form id="jur_sub" action="#" method="post" autocomplete="off">
                    <input type="hidden" name="jur_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">
                                          <th class="text-center" style="width:25%;"><?php echo language(219);?></th>
                                          <th class="text-center" style="width:25%;"><?php echo language(128);?></th>
                                          <th class="text-center" style="width:10%;"><?php echo language(217);?></th>
                                          <th class="text-center" style="width:10%;"><?php echo language(218);?></th>
                                          <th class="text-center" style="width:2%;">#</th>
                                       </tr>
                                    </thead>
                                    <tbody id="row_show"></tbody>
                                    <tfoot>
                                       <td class="text-right" colspan="2"><dt><?php echo language(143);?></dt></td>
                                       <td class="text-right"><dt id="dr_total"></dt></td>
                                       <td class="text-right"><dt id="cr_total"></dt></td>
                                       <td class="text-center"></td>
                                    </tfoot>
                                 </table>
                              </div>
                           </div>
                        <div class="col-md-12"><hr style="margin-top: 7px;margin-bottom: 7px;"></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>
                     <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>
         <div class="col-md-2"></div>
      </div>
   </div>
</section>
<script>
   $(document).ready(function(){
       $("select").select2();
       $("#acid").select2({
           ajax: {
               url: "../voucher/s_acc.php",
               type: "post",
               dataType: 'json',
               delay: 250,
               data: function (params) {
                   return {
                       search: params.term,
                       g_type: "'mcash','bank','du-adv','su-deb','stl','su-cre','dr-exp','in-exp','sl-exp','dr-inc','in-inc','owner','li-exp','sl-inc','fx-ass','sh-cap','cp-wip','invtr','cs-gds','sl-exp','scca' "
                   };
               },
               processResults: function (response) {
                   return {
                       results: response
                   };
               },
               cache: true
           }
       });
   });
   
   function balance (value,sid){
       var vvvv = value.split(",");
       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');
       }
   }

  function blcal(){
    let dr_total=0;
    let cr_total=0;

    $(".dr_amount").each(function(){
      dr_total += +Number($(this).val());
    });
    $(".cr_amount").each(function(){
      cr_total += +Number($(this).val());
    });

    $('#dr_total').text(dr_total);
    $('#cr_total').text(cr_total);  
  }   
</script>
<script>
   var clone = [];
   $(document).ready(function() {
   $('#ac_sub').on('submit', function(e){
    e.preventDefault();    
    let ac_row = '';
    let data   = $("#acid").val().split(",");
    let acname = $("#acid option:last").text();
    let ttype  = $("#ttype").val();
    let amount = $("#amount").val();
    let dr_val = (ttype=='+')?'value ="'+Number(amount)+'"':'value="0" readonly="true"';
    let cr_val = (ttype=='-')?'value ="'+Number(amount)+'"':'value="0" readonly="true"';
    if ($.inArray(Number(data[0]), clone) >= 0){
        $('#ac_sub')[0].reset();
        $("#acid option").remove();
        $("#ttype").trigger('change');
        alert("Duplicate Account Not Accepted");
    } else {
     ac_row += '<tr>';
     ac_row += '<td><dt>'+ acname +'<input type="hidden" name="acid[]" value="'+ Number(data[0]) +'" required="true"></dt></td>';
     ac_row += '<td><input type="text" name="details[]" class="form-control form-control-sm inputselect" value="N/A" required="true"></td>';
     ac_row += '<td><dt><input type="number" name="dr_amount[]" onkeyup="blcal()" class="form-control text-right form-control-sm inputselect dr_amount" required="true"' +dr_val+'></dt></td>';
     ac_row += '<td><dt><input type="number" name="cr_amount[]" onkeyup="blcal()" class="form-control text-right form-control-sm inputselect cr_amount" required="true"' +cr_val+'></dt></td>';
     ac_row += '<td class="text-center" onclick="removeac(this);" title="Remove This Account"><i class="fas fa-trash"></i></td>';
     ac_row += '</tr>';

      $('#row_show').append(ac_row);
      clone.push(Number(data[0])); 
      $('#ac_sub')[0].reset();
      $("#acid option").remove();
      $("#ttype").trigger('change');
      blcal();
          console.log(clone);
      }
     });
   });
   function removeac(del) {
      $(del).parent("tr").remove();
      clone = clone.filter(function(value) {
        return value !== Number($(del).parent("tr").children('td:first').find('input').val());
      });
      blcal();
      console.log(clone);
    } 
</script>

<script>
   $(document).ready(function() {
   $('#jur_sub').on('submit', function(e){
   e.preventDefault(); 
   if (Number($('#dr_total').text()) === Number($('#cr_total').text()) && Number($('#dr_total').text())>0 && Number($('#cr_total').text())>0) {
   $('#save').prop('disabled', true);
   $('#save').html('<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span> '+'Loading...'); 
        $.post("../voucher/jur_voucher_db.php",$(this).serialize(),function(data){
        toastr.success(data, yourdata);
        $('#jur_sub')[0].reset();
        $('#save').prop('disabled', false);
        $('#save').html('Vouchar Save');
        pagecall('../voucher/jur_voucher_index.php','#mview');
      });
      } else {
        alert('Must Be Balance Debit And Credit Amount...??');
      }
     });
   });  
</script>