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

$fee = (isset($udata['tng_pan_panfind'])) ? floatval($udata['tng_pan_panfind']) : 0;
$api_key = "75be25-7dd29-00d56-648c1e-7c9127-1c39"; 

if ($_SERVER['REQUEST_METHOD'] === 'POST' && !empty($_POST['uid'])) {
    header('Content-Type: application/json');
    $uid = strtoupper(trim($_POST['uid']));
    $user = $udata['phone'];
    $wallet = floatval($udata['balance']);

    if ($wallet < $fee) {
        echo json_encode(['status' => 'error', 'msg' => 'Insufficient Balance!']); exit;
    }

    $temp_order_id = "ADHAR-PAN-" . strtoupper(substr(md5(time()), 0, 8));

    mysqli_begin_transaction($tng_conn);
    try {
        mysqli_query($tng_conn, "UPDATE users SET balance = balance - $fee WHERE phone='$user'");
        mysqli_query($tng_conn, "INSERT INTO wallethistory (userid, amount, balance, purpose, status, type) VALUES ('$user', '$fee', '".($wallet - $fee)."', 'Aadhar To PAN Find', '1', 'Debit')");

        $sql = "INSERT INTO pan_find_requests (username, aadhar_no, order_id, status) VALUES ('$user', '$uid', '$temp_order_id', 'Pending')";
        mysqli_query($tng_conn, $sql);

        $api_url = "https://dashbood.brotool.online/api/pan_find/aadhar_to_pan_find.php?api_key=$api_key&aadhar_number=$uid&api_orderid=$temp_order_id";
        $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $api_url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        $response = curl_exec($ch); curl_close($ch);

        mysqli_commit($tng_conn);
        echo json_encode(['status' => 'success', 'msg' => 'Submitted Successfully!']);
    } catch (Exception $e) { mysqli_rollback($tng_conn); echo json_encode(['status' => 'error', 'msg' => 'Database Error']); }
    exit;
}
include('../template/tngweb/header.php'); 
?>

<div class="page-wrapper">
    <div class="page-content">
        <div class="container d-flex justify-content-center" style="margin-top: 50px;">
            <div class="card shadow-lg border-0" style="max-width: 450px; width: 100%; border-radius: 15px;">
                <div class="card-header bg-primary text-white text-center py-3">
                    <h5 class="mb-0">AADHAR TO PAN FIND</h5>
                </div>
                <div class="card-body p-4">
                    <div class="alert alert-info py-2 small fw-bold">Wallet: ₹<?php echo $udata['balance']; ?> | Fee: ₹<?php echo $fee; ?></div>
                    <form id="panSubmitForm">
                        <div class="mb-3">
                            <label class="form-label small fw-bold">Aadhaar Number</label>
                            <input type="text" name="uid" class="form-control form-control-lg" maxlength="12" placeholder="XXXX XXXX XXXX" required>
                        </div>
                        <button type="submit" id="btnSubmit" class="btn btn-primary w-100 btn-lg shadow-sm">SUBMIT REQUEST</button>
                    </form>
                    <div class="text-center mt-3"><a href="Pan_check_status.php" class="small text-secondary"><i class="fa fa-history"></i> View History & Status</a></div>
                </div>
            </div>
        </div>
    </div>
</div>

<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
<script>
$('#panSubmitForm').on('submit', function(e){
    e.preventDefault();
    $('#btnSubmit').prop('disabled', true).text('Processing...');
    $.ajax({
        url: window.location.href,
        type: 'POST',
        data: $(this).serialize(),
        success: function(res){
            if(res.status == 'success'){
                Swal.fire('Success', res.msg, 'success').then(()=> window.location.href='Pan_check_status.php');
            } else {
                Swal.fire('Error', res.msg, 'error');
                $('#btnSubmit').prop('disabled', false).text('SUBMIT REQUEST');
            }
        }
    });
});
</script>
<?php include('../template/tngweb/footer.php'); ?>