<?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">Purchase Invoice</h3>
                  <div class="card-tools">
                     <span class="mark1" onclick="copy(this);" title="Copy Invoice"><?php echo $invoice = invoice("GPUR");?></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="pr_sub" action="#" method="post" autocomplete="off">
                      <div class="col-md-12">
                        <div class="row">
                          <div class="col-md-2"></div>
                           <div class="col-md-4">
                              <div class="form-group">
                                 <label>Select Project</label>
                                 <select onchange="$('#project').val(this.value)" class="form-control select2" style="width: 100%;" required="true">
                                  <option></option>
                                <?php 
                                    $prr= mysqli_query($conn,"SELECT prid,pr_name FROM project WHERE pr_coid='$coid' AND pr_brid='$brid' AND pr_active=1") or die(mysqli_error($conn));
                                      while($prw= mysqli_fetch_assoc($prr)){
                                      echo '<option value='.$prw["prid"].'>'.$prw["pr_name"].'</option>';
                                    }?>
                                 </select>
                              </div>
                           </div>
                           <div class="col-md-4">
                              <div class="form-group">
                                 <label>Select Supplier</label>
                                 <dt id="s_show" style="float: right;"></dt>
                                 <input type="hidden">
                                 <select id="s_acid" onchange="$('#abc').val((this.value).split(',')[0]);balance(this.value,'#s_show')" class="form-control select2" style="width: 100%;" required="true">
                                  <option></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('su-cre')") 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>
                           <div class="col-md-12"><hr style="margin-top:-5px;margin-bottom: 7px;"></div>
                        </div>
                      </div>
                     <div class="col-md-12">
                        <div class="row">
                           <div class="col-md-7">
                              <div class="form-group">
                                 <label>Item Details</label>
                                 <input type="text" id="p_details" class="form-control form-control-sm inputselect" required="true">
                              </div>
                           </div>
                           <div class="col-md-3">
                              <div class="form-group">
                                 <center><label>Amount</label></center>
                                 <input type="number" id="p_price" class="form-control form-control-sm inputselect" placeholder="xxx" style="text-align:center;" required="true">
                              </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> Add Product</button>
                              </center>
                           </div>
                           <div class="col-md-12">
                              <hr style="margin-top:-5px;margin-bottom: 7px;">
                           </div>
                        </div>
                     </div>
                  </form>
                <form id="pur_sub" action="#" method="post" autocomplete="off">
                  <input type="hidden" name="s_acid" id="abc">
                  <input type="hidden" name="project" id="project">
                    <input type="hidden" name="gpur_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:65%;">Product Details</th>
                                          <th class="text-center" style="width:15%;">Discount</th>
                                          <th class="text-center" style="width:15%;">Amount</th>
                                          <th class="text-center" style="width:2%;">#</th>
                                       </tr>
                                    </thead>
                                    <tbody id="row_show"></tbody>
                                    <tfoot>
                                       <td class="text-right" colspan="2"><dt>Total</dt></td>
                                       <td class="text-right"><dt id="price_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">E-Mail</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">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="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 class="col-md-12"><hr style="margin-top: 7px;margin-bottom: 7px;"></div>
                           <div class="col-md-4">
                              <div class="form-group">
                                 <label>Select Discount A/C</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-inc')") 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">
                                 <center><label>Amount</label></center>
                                 <input type="number" name="d_amount" class="form-control form-control-sm inputselect" value="0" style="text-align:center;" required="true">
                              </div>
                           </div>
                           <div class="col-md-4">
                              <div class="form-group">
                                 <label>Select Payment Method</label>
                                 <dt id="m_show" style="float: right;"></dt>
                                 <input type="hidden" name="m_acid">
                                 <select id="m_acid" onchange="balance(this.value,'#m_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-2">
                              <div class="form-group">
                                 <center><label>Amount</label></center>
                                 <input type="number" name="m_amount" class="form-control form-control-sm inputselect" value="0" style="text-align:center;" required="true">
                              </div>
                           </div>

                        </div>
                     </div>
                     <center>
                        <div class="card-footer" style="padding: 8px 5px;">
                           <button type="submit" id="save" class="btn btn-success btn-xs">Save Invoice</button>
                           <button type="reset" class="btn btn-secondary btn-xs">Reset</button>
                        </div>
                     </center>
                  </form>
               </div>
            </div>
         </div>
         <div class="col-md-2"></div>
      </div>
   </div>
</section>
<script>
  $("select").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 ($("#s_acid").val(),"#s_show");
balance ($("#d_acid").val(),"#d_show");
balance ($("#m_acid").val(),"#m_show");

  function blcal(aaa=false){
    let price_total=0;
    if (aaa!==false) {
      $(aaa).parent().next().find("input").val(Number($(aaa).parent().next().find("input").val())- Number($(aaa).val()));
    }
    $(".p_price").each(function(){
      price_total += +Number($(this).val());
    });

    $('#price_total').text(price_total);  
  }   
</script>
<script>  
   $(document).ready(function() {
   $('#pr_sub').on('submit', function(e){ 
    e.preventDefault();   
    let ac_row = '';
    let p_details = $("#p_details").val();
    let p_price   = $("#p_price").val();
     ac_row += '<tr>';
     ac_row += '<td><input type="text" name="p_details[]" class="form-control form-control-sm inputselect" required="true" value="' +p_details+'"></td>';
     ac_row += '<td><input type="number" name="p_discount[]" onblur="blcal(this)" class="form-control text-right form-control-sm inputselect p_discount" required="true" value="0"></td>';
     ac_row += '<td><dt><input type="number" name="p_price[]" onkeyup="blcal()" class="form-control text-right form-control-sm inputselect p_price" required="true" value="'+p_price+'"></dt></td>';
     ac_row += '<td class="text-center" onclick="removepr(this);" title="Remove This Item"><i class="fas fa-trash"></i></td>';
     ac_row += '</tr>';

      $('#row_show').append(ac_row);
      $("#p_details").val('');
      $("#p_price").val('');
      blcal();
      });
     });
   function removepr(del) {
      $(del).parent("tr").remove();
      blcal();
    } 

</script>

<script>
   $(document).ready(function() {
   $('#pur_sub').on('submit', function(e){
   e.preventDefault(); 
   if (Number($('#price_total').text()) >0) {
   $('#save').prop('disabled', true);
   $('#save').html('<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span> '+'Loading...'); 
        $.post("../g_purchase/g_purchase_db.php",$(this).serialize(),function(data){
        toastr.success(Successfully, yourdata);
        $('#pur_sub')[0].reset();
        $('#pr_sub')[0].reset();
        $('#save').prop('disabled', false);
        $('#save').html('Save Invoice');
        pagecall('../g_purchase/g_purchase_index.php','#mview');
      });
      } else {
        alert('Please Select Item...??');
      }
     });
   });  
</script>