<?php include("includes/db_config.php");
if (isset($_POST['add_submit'])){
extract($_POST);
date_default_timezone_set('Asia/Kolkata');
$datetime = date('Y-m-d H:i:s');
$tmp_file = $_FILES['image']['tmp_name'];
$ext = pathinfo($_FILES["image"]["name"], PATHINFO_EXTENSION);
$rand = md5(uniqid().rand());
$itm_img = $rand.".".$ext;
move_uploaded_file($tmp_file,"images/".$itm_img);
$query = mysqli_query($conn,"SELECT SUM(total_amt) as ttl FROM sale_details WHERE customer_id = '".$_POST["customer_id"]."'");
$rows = mysqli_fetch_array($query); 
$sql_query = mysqli_query($conn,"SELECT SUM(received_amonut) as rttl FROM ah_sales_payment_in WHERE customer_id = '".$_POST["customer_id"]."'");
$row = mysqli_fetch_array($sql_query);
 if(!empty($row['rttl'])){ 
  $total_amt= $rows['ttl'] - $row['rttl'];
  }else{
  $total_amt= $rows['ttl']; }
$balance= $total_amt - $received_amount;
  $sql="INSERT into ah_sales_payment_in(receipt_no,date,customer_id,total_amount,received_amonut,payment_type,balance,desp,bank_name,transaction_id,opening_balance,as_on,image) values ('$receipt_no','$date','$customer_id','$total_amt','$received_amount','$payment_type','$balance','$desp','$bank_name','$transaction_id','$opening_balance','$as_on','$itm_img')";
  if(mysqli_query($conn, $sql)){
   echo "<script>location.replace('payment-in.php');</script>"; 
  }
} ?>
<!DOCTYPE html>
<html lang="en" dir="ltr" data-theme="light">
<meta http-equiv="content-type" content="text/html;charset=utf-8" />

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <?php include("includes/css.php");?>
    <style>
        .modal-body .form-label {
            font-weight: 600;
        }

        .viewptdt .form-label {
            font-weight: 600;
        }

        .viewptdt label {
            margin-top: 10px;
        }
    </style>
</head>

<body class="preload-active aside-active aside-mobile-minimized aside-desktop-maximized">
    <div class="holder">
        <?php include("includes/sidebar.php");?>
        <div class="wrapper">
            <?php include("includes/header.php");?>
            <div class="content">
                <div class="container-fluid g-5">
                    <div class="row">
                        <div class="col-12">
                            <div class="portlet">
                               <div class="portlet-header portlet-header-bordered row">
                                     <div class="col-md-12">
                                        <h3 class="portlet-title">Add Payment In</h3>
                                    </div>                                 
                                </div>
                     <?php $sqlfts=mysqli_fetch_array(mysqli_query($conn,"select * from ah_sales_payment_in order by id DESC"));
                    if(!empty($sqlfts['receipt_no'])){
                    $in_no= (int)$sqlfts['receipt_no'];
                    $receipt_no= ++$in_no; }else{
                    $receipt_no='1';
                    } ?>           
                     <div class="portlet-body">
                        <form action="" method="post" enctype="multipart/form-data">
                           <div class="row">
                             <div class="mb-3 col-md-4">
                                <label class="form-label">Customer Name</label>
                                 <select class="form-control" name="customer_id" id="cidd">
                                    <option value="">Select Customer Name</option>
                                    <?php $sqlct=mysqli_query($conn,"select * from ah_party");
                                    while($resct= mysqli_fetch_array($sqlct)){ ?>
                                    <option value="<?= $resct['id'];?>"><?= $resct['party_name'];?></option>
                                    <?php } ?>
                                 </select>
                                 <span id="amt_rm"></span>
                            </div>
                              <div class="mb-3 col-md-4">
                                <label class="form-label">Balance Amount(Rs.)</label>
                                <input type="text" value="" name="total_amt" id="amt_rmt" class="form-control">
                            </div>
                            <div class="mb-3 col-md-4">
                                <label class="form-label">Recieved Amount(Rs.)</label>
                                <input type="text" placeholder="Enter Amount" name="received_amount"  class="form-control" >
                            </div>
                          
                            <div class="mb-3 col-md-4">
                                <label class="form-label">Receipt No.</label>
                                <input type="text" placeholder="Enter Receipt No." value="<?= $receipt_no; ?>" name="receipt_no"  class="form-control">
                            </div>
                         <div class="mb-3 col-md-4">
                                <label class="form-label">Date</label>
                                <input type="date" placeholder="Choose Date" name="date"  class="form-control">
                            </div>
                           
                            <div class="mb-3 col-md-12">
                                <label class="form-label">Description</label>
                                <textarea type="text" placeholder="Description" name="desp" class="form-control" rows="2"></textarea>
                            </div>
                            <div class="mb-3 col-md-4">
                                <label class="form-label">Payment Type</label>
                                <select class="form-control" name="payment_type" id="type" onchange="yesnoCheck(this);">
                                    <option value="">Select Type</option>
                                    <option value="Cash">Cash</option>
                                    <option value="cheque-no">Cheque No.</option>
                                    <option value="bank-ac">Bank A/c</option>
                                    <option value="gpay">G Pay</option>
                                    <option value="phone-pe">Phone Pe</option>
                                    <option value="paytm">Paytm</option>
                                    <option value="UPI">UPI</option>
                                </select>
                            </div>     
                           <!--  <div class="mb-3 col-md-4">
                                <label class="form-label">Cheque No.</label>
                                <input type="text" placeholder="Enter Cheque No." name="cheque_no"  class="form-control">
                            </div> -->
                            <div id="bkdt" class="display:none">
                            <div class="mb-3 col-md-4">
                                <label class="form-label">Bank Name</label>
                                <input type="text" placeholder="" name="bank_name"  class="form-control">
                            </div>
                             <div class="mb-3 col-md-4">
                                <label class="form-label">Opening Balance</label>
                                <input type="text" placeholder="" name="opening_balance"  class="form-control">
                            </div> 
                            <div class="mb-3 col-md-4">
                                <label class="form-label"> As On</label>
                                <input type="date" value="" name="as_on" class="form-control">
                            </div>
                            </div>
                            <div class="mb-3 col-md-4" id="ifYes">
                                <label class="form-label">Reference No.</label>
                                <input type="text" placeholder="Enter Reference No." name="transaction_id"  class="form-control">
                            </div>
                            <div class="mb-3 col-md-4">
                                <label class="form-label">Images</label>
                                <input type="file" placeholder="" name="image"  class="form-control">
                            </div> 
                            <div class="clearfix"> </div>
                            <div class="mb-3 col-md-4"> 
                                <button class="btn btn-primary btn-lg" name="add_submit" type="submit">Submit</button>
                            </div>  
                            </div> 
                        </form>
                    </div>
                </div>
            </div>
             

            <?php include("includes/footer.php");?>
        </div>
    </div>
    <?php include("includes/js.php");?>
 <script type="text/javascript">
  /*  function yesnoCheck(that) {
    if (that.value == "cheque-no") {
        document.getElementById("ifYes").style.display = "block";
    }else if (that.value == "bank-ac") {
        document.getElementById("bkdt").style.display = "block";
    }else{
     document.getElementById("ifYes").style.display = "none";
     document.getElementById("bkdt").style.display = "none";   
    }
    }*/
$(function() {
    $('#ifYes').hide(); 
    $('#bkdt').hide(); 
    $('#type').change(function(){
        if($('#type').val() == 'cheque-no') {
            $('#ifYes').show(); 
        } else {
            $('#ifYes').hide(); 
        } 
     if($('#type').val() == 'bank-ac') {
            $('#bkdt').show(); 
        } else {
            $('#bkdt').hide(); 
        } 
    });
});
$(document).ready(function(){
    $('#cidd').on('change', function(){
        var cidd=$(this).val();
        $.ajax({
            type:'post',
            url:'get_due_amt.php',
            data:{cidd: cidd},
            dataType:'json',
            success:function(data){
                console.log(data);
                $('#amt_rm').text(data); 
                $('#amt_rmt').val(data); 
             //$('#container').container('show'); 
            },
            error:function(){}
        });
    });
  });  
$(document).ready(function(){
    $('#cidd').on('change', function(){
        var cidd=$(this).val();
       // alert(cidd);
        $.ajax({
            type:'post',
            url:'get_due_amt.php',
            data:{cidd: cidd},
            dataType:'json',
            success:function(data){
                console.log(data);
                $('#amt_rm').text(data); 
             //$('#container').container('show'); 
            },
            error:function(){}
        });
    });
  });  
/*$(function(){
      // bind change event to select
      $('#dynamic_select').on('change', function () {
          var url = $(this).val(); // get selected value
          if (url) { // require a URL
              window.location = url; // redirect
          }
          return false;
      });
    });*/
</script>    
</body>

</html>