<?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,t.dr_acid,t.cr_acid,a.acid,a1.acid a1acid,a1.balance,a1.ac_name,a1.ac_number,a1.ac_alise
    FROM transaction t LEFT JOIN accounts a ON(a.acid=t.dr_acid OR a.acid=t.cr_acid)
    LEFT JOIN accounts a1 ON(a1.acid=(IF(t.dr_acid= a.acid, t.cr_acid, t.dr_acid)))
    WHERE t.coid='$coid' AND a.ac_type='opening' 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-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">Opening 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="op_edit_form" action="#" method="post" autocomplete="off">
                <input type="hidden" name="edit_tid" value="<?php echo $tid;?>">
                <input type="hidden" name="cr_old_acid" value='<?php echo $ined['cr_acid'];?>'>
                <input type="hidden" name="dr_old_acid" value='<?php echo $ined['dr_acid'];?>'>
                <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-5">
                              <div class="form-group">
                                 <label>Select A/c (Debit/Credit)</label>
                                 <dt id="edit_show" style="float: right;"></dt>
                                 <input type="hidden" name="edit_acid">
                                 <select id="edit_acid" onchange="balance(this.value,'#edit_show')" class="form-control select2" style="width: 100%;" required="true"> 
                                 <option value='<?php echo $ined["a1acid"].",".$ined["balance"];?>'><?php echo $ined["ac_name"].'-'.$ined["ac_number"].' '.'('.$ined["ac_alise"].')';?></option> 
                                 </select>
                              </div>
                           </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-3">
                                <div class="form-group">
                                 <label>Debit/Credit</label>
                                 <select name="ttype" class="form-control select2" style="width: 100%;" required="true">
                                  <option <?php if ($ined['dr_acid']===$ined['a1acid']) {echo'selected="selected"';}?> value="+">Debit</option>
                                  <option <?php if ($ined['dr_acid']===$ined['acid']) {echo'selected="selected"';}?> value="-">Credit</option>
                                </select>
                              </div>
                            </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/op_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(){
      $("select").select2();
      $("#edit_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'"
                  };
              },
              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 ($("#edit_acid").val(),"#edit_show");

  function blcal (value){
    if (!isNaN(value)) {   
     var v = $("#edit_acid").val().split(","); 
       if (Number(v[1])>=0) {
         var sbl  = Number(v[1]) + Number(value - <?php echo $ined["amount"];?>);
       } else {
         var sbl  = Number(v[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');
     }
    }
  }
</script>

<script>
$(document).ready(function() {
$('#op_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/op_vou_edit_db.php",$(this).serialize(),function(data){
     toastr.success(Updated, yourdata);
     pagecall('../voucher/op_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>