<?php require_once('../auth/auth.php');?>
<?php 
   $g_acid  = $_GET['g_acid'];
   $tid     = $_GET['tid'];
$inedit= mysqli_query($conn,"SELECT t.tid,t.invoice,date(t.date) datee,t.amount,t.details,a.acid,a.balance,a.ac_name,a.ac_number,a.ac_alise,a1.acid a1acid,a1.balance a1balance,a1.ac_name a1ac_name,a1.ac_number a1ac_number,a1.ac_alise a1ac_alise
  FROM transaction t LEFT JOIN accounts a ON(a.acid=t.dr_acid)
  LEFT JOIN accounts a1 ON(a1.acid=t.cr_acid)
  WHERE t.coid='$coid' AND t.tid ='$tid' LIMIT 1") or die(mysqli_error($conn));
$ined= mysqli_fetch_assoc($inedit);
?>
<section class="content">
   <div class="container-fluid">
      <div class="row">
        	<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">Received Voucher Edit</h3>
        			<div class="card-tools">
                <span class="mark1" onclick="copy(this);" title="Copy Invoice"><?php echo $ined["invoice"];?></span>
        				<button type="button" class="btn btn-tool" data-card-widget="remove" data-dismiss="modal">
        					<i class="fas fa-times"></i>
        				</button>
        			</div>
        		</div>

	            <form id="rec_edit_form" action="#" method="post" autocomplete="off">
                <input type="hidden" name="edit_tid" value="<?php echo $tid;?>">
                <input type="hidden" name="dr_old_acid" value='<?php echo $ined["acid"];?>'>
                <input type="hidden" name="cr_old_acid" value='<?php echo $ined["a1acid"];?>'>
                <input type="hidden" name="old_amount" value='<?php echo $ined["amount"];?>'>
                     <div class="card-body">
                        <div class="col-md-12">
                          <div class="row">
                           <div class="col-md-6">
                              <div class="form-group">
                                 <label>Cash / Bank A/c (Debit)</label>
                                 <dt id="edit_show" style="float: right;"></dt>
                                 <input type="hidden" name="dr_edit_acid">
                                 <select id="dr_edit_acid" onchange="balance(this.value,'#edit_show')" class="form-control select2" style="width: 100%;" required="true">
                                   <option value='<?php echo $ined["acid"].",".$ined["balance"];?>'><?php echo $ined["ac_name"].'-'.$ined["ac_number"].' '.'('.$ined["ac_alise"].')';?></option>
                                <?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>Receipt By A/c (Credit)</label>
                                 <dt id="edit_show1" style="float: right;"></dt>
                                 <input type="hidden" name="cr_edit_acid">                                
                                 <select id="cr_edit_acid" onchange="balance(this.value,'#edit_show1')" class="form-control select2" style="width: 100%;" required="true">
                                  <option value='<?php echo $ined["a1acid"].",".$ined["a1balance"];?>'><?php echo $ined["a1ac_name"].'-'.$ined["a1ac_number"].' '.'('.$ined["a1ac_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>Amount</label></center>
                                  <input type="number" name="edit_amount" onblur="blcal(this.value)" class="form-control form-control-sm inputselect" value='<?php echo $ined["amount"];?>' 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>Narration</label>
              						      <textarea name="edit_details" class="form-control form-control-sm inputselect" rows="2" placeholder="Enter ..."><?php echo $ined["details"];?></textarea>
              						   </div>
              						</div>

                         <div class="col-md-3">
                          <div class="form-group">
          						      <div class="custom-control custom-checkbox">
          						        <input name="edit_email" class="custom-control-input" type="checkbox" id="customCheckbox1" value="yes">
          						         <label for="customCheckbox1" class="custom-control-label">E-Mail</label>
          						      </div>
          						   </div>
                        </div>
            						<div class="col-md-3">
            						   <div class="form-group">
            						      <div class="custom-control custom-checkbox">
            						         <input name="edit_sms" class="custom-control-input" type="checkbox" id="customCheckbox2" value="yes">
            						         <label for="customCheckbox2" class="custom-control-label">SMS</label>
            						      </div>
            						   </div>
            						</div>

            					 <div class="col-md-3">
            						<div class="form-group"><span style="float: right;margin: 2px -35px;">Date</span>
            						    <input type="text" name="edit_tdate" class="form-control form-control-sm" onmouseover="lcdate(this)" style="text-align:center;" readonly="true" required="true" value="<?php echo $ined["datee"];?>">
            						</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">Update</button>
          <button type="reset" onclick="pagecall('../voucher/re_money_receipt.php?g_acid=<?php echo $g_acid?>'+'&tid=<?php echo $tid;?>','#mdview');" class="btn btn-secondary btn-xs">Cancel</button>
        </div>
      </center>
	  </form>
	</div>
</div>
</div>
</section>

<script>
  $(document).ready(function(){
    $("#dr_edit_acid").select2();
      $("#cr_edit_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','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_edit_acid").val(),"#edit_show");
balance ($("#cr_edit_acid").val(),"#edit_show1");

  function blcal (value){
    if (!isNaN(value)) {   
     var v = $("#dr_edit_acid").val().split(","); 
     var v1 = $("#cr_edit_acid").val().split(",");
     var sbl  = Number(v[1]) + Number(value-<?php echo $ined["amount"]?>);
     var sbl1 = Number(v1[1]) - Number(value-<?php echo $ined["amount"]?>);
     if (sbl>=0) {
        $("#edit_show").css("color","green");
        $("#edit_show").text(bden.format(sbl) + ' Dr');
     } else {
        $("#edit_show").css("color","red");
        $("#edit_show").text(bden.format(-sbl) + ' Cr');
     }

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

    }
  }

  </script>

<script>
$(document).ready(function() {
$('#rec_edit_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/vou_edit_db.php",$(this).serialize(),function(data){
     toastr.success(Updated, yourdata);
     pagecall('../voucher/re_money_receipt.php?g_acid=<?php echo $g_acid?>'+'&tid=<?php echo $tid;?>','#mdview');
     pagecall('../accounts/ac_profile.php?acid=<?php echo $g_acid?>','#ac_view');
     $('#save').prop('disabled', false);
     $('#save').html('Update');
   });
  });
});  
</script>