<?php
include('../includes/session.php');
include('../includes/config.php');
include('../template/ahkweb/header.php');

if (isset($_POST['rasan_no']) && !empty($_POST['name']) && !empty($_POST['date']) && !empty($_POST['state_fee'])) {
    $name = mysqli_real_escape_string($ahk_conn, $_POST['name']);
    $rasan_no = mysqli_real_escape_string($ahk_conn, $_POST['rasan_no']);
    $date = mysqli_real_escape_string($ahk_conn, $_POST['date']);
    $state_fee = mysqli_real_escape_string($ahk_conn, $_POST['state_fee']);

    $appliedby = $udata['phone'];
    $userid = $udata['id'];

    // Get service fee from pricing table
    $priceResult = mysqli_query($ahk_conn, "SELECT price FROM pricing WHERE service_name = 'll_request_fee'");
    $fee = ($priceResult && mysqli_num_rows($priceResult) > 0) ? mysqli_fetch_assoc($priceResult)['price'] : 1000000;

    // Check balance balance
    $balanceResult = mysqli_query($ahk_conn, "SELECT balance FROM users WHERE id = '$userid'");
    $balanceData = mysqli_fetch_assoc($balanceResult);
    $balanceBalance = $balanceData['balance'];

    if ($balanceBalance < $fee) {
        echo "<script>
            $(function(){
                Swal.fire('Insufficient Balance', 'Please recharge your balance.', 'warning');
            });
            setTimeout(() => { window.location='wallet'; }, 2000);
        </script>";
        exit;
    }

    // Deduct balance
    $newBalance = $balanceBalance - $fee;
    mysqli_query($ahk_conn, "UPDATE users SET balance = '$newBalance' WHERE id = '$userid'");

    // Generate Application No
    $application_no = "Ll_request_" . rand(10000000, 99999999);

    // Insert application
    $submit = mysqli_query($ahk_conn, "INSERT INTO larning_exam (appliedby, name, rasan_no, date, state_fee, status, fee, application_no)
                VALUES ('$appliedby', '$name', '$rasan_no', '$date', '$state_fee', 'pending', '$fee', '$application_no')");

    // Log transaction
    mysqli_query($ahk_conn, "INSERT INTO service_summary (user_id, service, amount, status, created_at) 
        VALUES ('$userid', 'll_request_fee', '$fee', 'debit', NOW())");

    if ($submit) {
        echo "<script>
            $(function(){
                Swal.fire('Application Submitted Successfully', '', 'success');
            });
            setTimeout(() => { window.location='ll_request_list'; }, 1200);
        </script>";
    } else {
        echo "<script>
            $(function(){
                Swal.fire('Oops', 'Something went wrong. Try Again.', 'error');
            });
            setTimeout(() => { window.location='index.php'; }, 1200);
        </script>";
    }
}
?>



<!-- powered by CH Tech Solution https://www.saadguru.in -->
<!--start page wrapper -->
<div class="page-wrapper">
    <div class="page-content">
        <!-- Breadcrumb with Action Buttons -->
        <div class="page-breadcrumb d-flex align-items-center justify-content-between mb-4 p-3 bg-white rounded shadow-sm">
            <div class="d-flex align-items-center">
                <div class="breadcrumb-title pe-3">Services</div>
                <nav aria-label="breadcrumb">
                    <ol class="breadcrumb mb-0 ps-2">
                        <li class="breadcrumb-item active text-muted" aria-current="page">Learning Licence</li>
                    </ol>
                </nav>
            </div>
            <div class="btn-group">
                <a href="ll_request_list" class="btn btn-success">
                    <i class="bi bi-list-ul me-1"></i>Apply List
                </a>
                <a href="javascript:history.back()" class="btn btn-outline-primary">
                    <i class="bi bi-arrow-left me-1"></i> Back
                </a>
            </div>
        </div>
      
        <!--end breadcrumb-->
        <div class="col-18">
            <div class="bg-info p-2 mb-3 font-weight-bold">
                <div class="row">
                    <h3><strong>Learning Licence Test Exam</strong></h3>
                    <h5>DELIVERY TIME - 02-24 HOURS</h5>
                    <h4>Disclaimer :- CHARGE - 
                      <?php  
                                $p = mysqli_fetch_assoc(mysqli_query($ahk_conn,"SELECT price FROM pricing WHERE service_name='ll_request_fee'")); 
                                echo $p['price'];
                            ?>, FAST SERVICE
                    </h4>
                </div>
            </div>
        </div>
        <div class="card border-top border-0 border-4 border-primary">
            <div class="card-body p-5">
                <div class="card-title d-flex align-items-center">
                    <div><i class="fa-solid fa-id-card me-1 font-22 text-primary"></i></div>
                </div>

                <form action="" method="POST" class="row g-3">
                    <div class="col-md-3">
                        <div class="form-group">
                            <label for="name"><strong>Enter Application No</strong></label>
                            <input class="form-control" id="name" name="name" placeholder="Enter Application No" type="number" required>
                        </div>
                    </div>
                    <!-- Rasan No -->
                    <div class="col-md-3">
                        <label for="rasan_no"><strong>Date of Birth</strong></label>
                        <input class="form-control" name="rasan_no" id="rasan_no" placeholder="30/05/2001" type="text" required>
                    </div>
                    <!-- Password -->
                    <div class="col-md-3">
                        <div class="form-group">
                            <label for="date"><strong>Enter Password</strong></label>
                            <input class="form-control" id="date" name="date" placeholder="Enter Password" type="text" required>
                        </div>
                    </div>
                    <!-- State -->
                    <div class="col-sm-3">
                        <label>State</label>
                        <div class="form-group">
                            <select name="state_fee" id="state_fee" class="form-control" required>
                                <option value="">Select State</option>
                                <option value="ANI">Andaman and Nicobar Islands</option>
                                <option value="AP">Andhra Pradesh</option>
                                <option value="AR">Arunachal Pradesh</option>
                                <option value="AS">Assam</option>
                                <option value="BR">Bihar</option>
                                <option value="CH">Chandigarh</option>
                                <option value="CG">Chhattisgarh</option>
                                <option value="DNH">Dadra and Nagar Haveli</option>
                                <option value="DD">Daman and Diu</option>
                                <option value="DL">Delhi</option>
                                <option value="GA">Goa</option>
                                <option value="GJ">Gujarat</option>
                                <option value="HR">Haryana</option>
                                <option value="HP">Himachal Pradesh</option>
                                <option value="JK">Jammu and Kashmir</option>
                                <option value="JH">Jharkhand</option>
                                <option value="KA">Karnataka</option>
                                <option value="KL">Kerala</option>
                                <option value="Lakshadweep">Lakshadweep</option>
                                <option value="LA">Ladakh</option>
                                <option value="Telangana">Telangana</option>
                                <option value="Uttarakhand">Uttarakhand</option>
                                <option value="MP">Madhya Pradesh</option>
                                <option value="Maharashtra">Maharashtra</option>
                                <option value="Manipur">Manipur</option>
                                <option value="Meghalaya">Meghalaya</option>
                                <option value="Mizoram">Mizoram</option>
                                <option value="Nagaland">Nagaland</option>
                                <option value="Odisha">Odisha</option>
                                <option value="Puducherry">Puducherry</option>
                                <option value="Punjab">Punjab</option>
                                <option value="Rajasthan">Rajasthan</option>
                                <option value="Sikkim">Sikkim</option>
                                <option value="TN">Tamil Nadu</option>
                                <option value="Tripura">Tripura</option>
                                <option value="Uttaranchal">Uttaranchal</option>
                                <option value="UP">Uttar Pradesh</option>
                                <option value="WB">West Bengal</option>
                            </select>
                        </div>
                    </div>
                    <div class="col-12">
                        <button class="btn btn-success" type="submit" onclick="return confirm('Are You Sure ?');">Submit</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

<!--end page wrapper -->
<?php 
include('footer.php');
?>
<!-- Bootstrap JS -->
<script src="../template/ahkweb/assets/js/bootstrap.bundle.min.js"></script>
<!--plugins-->
<script src="../template/ahkweb/assets/js/jquery.min.js"></script>
<script src="../template/ahkweb/assets/plugins/simplebar/js/simplebar.min.js"></script>
<script src="../template/ahkweb/assets/plugins/metismenu/js/metisMenu.min.js"></script>
<script src="../template/ahkweb/assets/plugins/perfect-scrollbar/js/perfect-scrollbar.js"></script>
<script src="../template/ahkweb/assets/plugins/chartjs/chart.min.js"></script>
<script src="../template/ahkweb/assets/plugins/vectormap/jquery-jvectormap-2.0.2.min.js"></script>
<script src="../template/ahkweb/assets/plugins/vectormap/jquery-jvectormap-world-mill-en.js"></script>
<script src="../template/ahkweb/assets/plugins/jquery.easy-pie-chart/jquery.easypiechart.min.js"></script>
<script src="../template/ahkweb/assets/plugins/sparkline-charts/jquery.sparkline.min.js"></script>
<script src="../template/ahkweb/assets/plugins/jquery-knob/excanvas.js"></script>
<script src="../template/ahkweb/assets/plugins/jquery-knob/jquery.knob.js"></script>
<script>
$(function() {
    $(".knob").knob();
});
</script>
<script src="../template/ahkweb/assets/js/index.js"></script>
<!--app JS-->
<script src="../template/ahkweb/assets/js/app.js"></script>
<!-- datatable -->
<script src="../template/ahkweb/assets/plugins/datatable/js/jquery.dataTables.min.js"></script>
	<script src="../template/ahkweb/assets/plugins/datatable/js/dataTables.bootstrap5.min.js"></script>
<script>
		$(document).ready(function() {
			$('#example').DataTable();
		  } );
	</script>
	
	<script>
		$(document).ready(function() {
			var table = $('#example2').DataTable( {
				lengthChange: false,
				buttons: [ 'copy', 'excel', 'pdf', 'print']
			} );
		 
			table.buttons().container()
				.appendTo( '#example2_wrapper .col-md-6:eq(0)' );
		} );
	</script>
	
</body>



</html>