<script src="https://cdn.jsdelivr.net/npm/sweetalert2@10"></script>

<?php
include('header.php');

$pdfData = '';
$application_no = '';

if (isset($_POST['docno'])) {
    $docno = $_POST['docno'];
    $doctype = $_POST['doctype'];
    $type = $_POST['type'];
    $application_no = "FLX_" . rand(100000, 999999);

    $price = mysqli_fetch_assoc(mysqli_query($ahk_conn, "SELECT * FROM pricing WHERE service_name='ration_pdf_flx_print_fee' "));
    $fee = $price['price'];
    $username = $udata['phone'];
    $wallet_amount = $udata['balance'];

    if ($wallet_amount > $fee) {
        $debit_fee = $wallet_amount - $fee;
        $api_key = urlencode($flexsecure_key);
        $url = "https://" . htmlspecialchars($flexsecureapiurl) . "/panel/serviceapi/ration/ration_pdf.php?apiKey=$api_key&docno=$docno&doctype=$doctype&pdftype=$type";

        $curl = curl_init();
        curl_setopt_array($curl, array(
            CURLOPT_URL => $url,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'GET',
        ));

        $response = curl_exec($curl);
        curl_close($curl);
        $resdata = json_decode($response, true);

        if (!$resdata['file']) {
            echo "<script>
                Swal.fire(
                    '" . $resdata['message'] . "',
                    'Ration Not Found',
                    'warning'
                );
            </script>";
        } else {
            $pdfData = $resdata['file']; // assumed base64 string or direct link

            $debit = mysqli_query($ahk_conn, "UPDATE users SET balance='$debit_fee' WHERE phone='$username'");
            if ($debit) {
                $insert = mysqli_query($ahk_conn, "INSERT INTO rasn_print (application_no, username, rasan_no, status, fee, front, back, pdf) VALUES ('$application_no','$username','$docno','success', '$fee', '$pdfData','$pdfData','$pdfData');");

                if ($insert) {
                    echo "<script>
                        Swal.fire(
                            'Success',
                            'Ration NO: $docno PDF generated successfully!',
                            'success'
                        );
                    </script>";
                }
            }
        }
    } else {
        echo "<script>
            Swal.fire(
                'Oops!',
                'Wallet Balance Insufficient! Please Recharge.',
                'error'
            );
            setTimeout(() => {
                window.location.href = 'wallet.php';
            }, 2000);
        </script>";
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Ration PDF Download</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="https://jeremyfagis.github.io/dropify/dist/css/dropify.min.css">
    <script src="https://cdn.jsdelivr.net/npm/sweetalert2@10"></script>
    <style>
        iframe {
            border: 1px solid #ccc;
        }
    </style>
</head>
<body>

<!--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">Ration Pdf</li>
                    </ol>
                </nav>
            </div>
            <div class="btn-group">
                <a href="Ration_Pdf_flx_list" class="btn btn-success">
                    <i class="bi bi-list-ul me-1"></i> Ration Pdf 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>

        <!-- Main Layout -->
        <div class="container-fluid pt-4 px-4">
            <div class="row">
                <!-- Left Side: Form -->
                <div class="col-lg-6 mb-4">
                    <div class="card">
                        <div class="card-body">
                            <h3 class="card-title text-center text-dark"><strong>Ration PDF Download Instant</strong></h3>
                            <a href="" class="btn btn-dark w-100 mb-3">Ration No & Aadhaar No To Ration PDF Download</a>
                            <form method="POST">
                                <div class="mb-3">
                                    <label>Document Type</label>
                                    <select class="form-control" required name="doctype">
                                        <option value="aadhar">Aadhar NO</option>
                                        <option value="ration">Ration NO</option>
                                    </select>
                                </div>
                                <div class="mb-3">
                                    <label>DOC No.</label>
                                    <input class="form-control" placeholder="Enter Document No." name="docno" type="number" required>
                                </div>
                                <div class="mb-3">
                                    <label>PDF Type</label>
                                    <select class="form-control" required name="type">
                                        <option value="new">NEW</option>
                                        <option value="old">OLD</option>
                                    </select>
                                </div>
                                <div class="text-center">
                                    <button type="submit" name="submit" class="btn btn-primary">Submit</button>
                                     <div class="col-12 ml-2 d-flex justify-content-center">
    <div class="fee-container text-center">
        <h5 class="text-warning">Application Fee: 
          <?php  
                $price = mysqli_fetch_assoc(mysqli_query($ahk_conn,"SELECT price FROM pricing WHERE service_name='ration_pdf_flx_print_fee'")); 
                echo "₹" . $price['price'];
            ?>
        </h5>
    </div>
</div>

<style>
    .fee-container {
        background-color: #fff;
        border-radius: 8px;
        box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
        padding: 10px 15px;
        max-width: 300px;
        width: 100%;
        margin-top: 10px;
        margin-bottom: 10px;
    }

    .fee-container h5 {
        color: #ff9800; /* Bright yellow/orange color for emphasis */
        font-size: 18px;
        font-weight: 600;
        margin: 0;
    }
</style>

                                </div>
                            </form>
                        </div>
                    </div>
                </div>

                <!-- Right Side: PDF Preview -->
                <div class="col-lg-6 mb-4">
                    <?php if ($pdfData): ?>
                        <div class="card">
                            <div class="card-body">
                                <h4 class="text-center">PDF Preview</h4>
                                <iframe src="data:application/pdf;base64,<?php echo $pdfData; ?>" width="100%" height="600px"></iframe>
                                <div class="text-center mt-3">
                                    <a href="data:application/pdf;base64,<?php echo $pdfData; ?>" class="btn btn-success" download="Ration_<?php echo $application_no; ?>.pdf">Download PDF</a>
                                </div>
                            </div>
                        </div>
                    <?php else: ?>
                        <div class="alert alert-info text-center">
                            Fill the form to view the PDF preview.
                        </div>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
</html>


    <!-- JavaScript to show/hide form fields based on selected state -->
    <script>
        const stateDropdown = document.getElementById('state');
        const areaRuralField = document.getElementById('areaRuralField');
        const districtField = document.getElementById('districtField');
        const rasanNoField = document.getElementById('rasanNoField');

        stateDropdown.addEventListener('change', function () {
            const selectedState = stateDropdown.value;

            if (selectedState === 'BR') {
                areaRuralField.style.display = 'block';
                districtField.style.display = 'block';
                rasanNoField.style.display = 'block';
            } else {
                areaRuralField.style.display = 'none';
                districtField.style.display = 'none';
                rasanNoField.style.display = 'block'; // You may adjust this based on your requirements for other states.
            }
        });
    </script>
<!--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>