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

/* ===============================
   SERVICE CONFIG
================================ */
$fee = $udata['tng_pan_panfind'];
checkServiceStatus($udata,'tng_pan_panfind_status');

if ($_SERVER['REQUEST_METHOD'] === 'POST' && !empty($_POST['aadhar'])) {

    $aadhar = trim($_POST['aadhar']);
    $user   = $udata['phone'];
    $wallet = $udata['balance'];

    if ($wallet < $fee) {
        echo "<script src='https://cdn.jsdelivr.net/npm/sweetalert2@11'></script>
        <script>
            Swal.fire('Wallet Low','Please recharge your wallet','error')
            .then(()=>window.location='wallet.php');
        </script>";
        exit;
    }

    /* ===============================
       API CALL
    ================================ */
    $resdata = pan_find_verificationapi_v2_run(['aadhar' => $aadhar]);
    $sampleCode = $resdata['sampleCode'] ?? '';
    $errorMsg   = $resdata['error'] ?? 'Request Failed';

    if ($sampleCode !== "200") {
        echo "<script src='https://cdn.jsdelivr.net/npm/sweetalert2@11'></script>
        <script>
            Swal.fire('Failed','".addslashes($errorMsg)."','error');
        </script>";
        exit;
    }

    mysqli_query($tng_conn, "UPDATE users SET balance = balance - $fee WHERE phone='$user'");

    addCommission($tng_conn, $user, $fee, $udata['parent'] ?? null, 'Aadhar To PAN Find');

    mysqli_query($tng_conn, "INSERT INTO wallethistory (userid,amount,balance,purpose,status,type) VALUES('$user','$fee','".($wallet-$fee)."','Aadhar To PAN Find','1','Debit')");

    $jsonBase64 = base64_encode(json_encode($resdata));
    mysqli_query($tng_conn, "INSERT INTO all_pdf_records (service_name,name,username,number,pdf,date) VALUES('AADHAR_TO_PAN_FIND','".mysqli_real_escape_string($tng_conn, $resdata['pan'] ?? 'NA')."','$user','$aadhar','".mysqli_real_escape_string($tng_conn, $jsonBase64)."', NOW())");

    echo "<script src='https://cdn.jsdelivr.net/npm/sweetalert2@11'></script>
    <script>
        Swal.fire({
            icon:'success',
            title:'Success',
            text:'PAN Find completed successfully',
            timer:2200,
            showConfirmButton:false
        }).then(()=>window.location='all_history.php');
    </script>";
    exit;
}
?>

<!-- ===============================
     CLEAN UI - AADHAR TO PAN FIND
================================ -->

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

<style>
.card-shine {
    border-radius: 20px;
    box-shadow: 0 10px 30px rgba(0,0,0,0.1);
    transition: .3s;
    border: none;
}
.card-shine:hover {
    transform: translateY(-3px);
    box-shadow: 0 18px 40px rgba(0,0,0,0.15);
}
.container-center {
    min-height: 85vh;
    display: flex;
    align-items: center;
    justify-content: center;
}
.form-label {
    font-weight: 600;
    font-size: 14px;
    margin-bottom: 8px;
    color: #333;
}
.placeholder-small::placeholder {
    font-size: 13px;
    color: #aaa;
}
.single-line {
    width: 100%;
    height: 1px;
    background: linear-gradient(90deg, transparent, #e2e8f0, #e2e8f0, #e2e8f0, transparent);
    margin: 20px 0;
}
</style>

<div class="content-wrap">
<div class="main">
<div class="page-wrapper">
<div class="container-center">
<div class="page-content w-100 d-flex justify-content-center">

<div class="col-lg-4 col-md-6 col-sm-10">
<div class="card card-shine">
<div class="card-body p-4">

<!-- Back Button -->
<a href="javascript:history.back()" class="btn btn-light mb-4 w-100 py-2" style="border-radius: 12px;">
    ← Go Back
</a>

<!-- Title -->
<div class="text-center mb-4">
    <h5 class="fw-bold mb-1" style="color: #1e293b;">🆔 Aadhaar → PAN Find</h5>
    <p class="text-muted small">Find PAN details using Aadhaar number</p>
</div>

<form method="POST" id="panForm">

<!-- Aadhaar Number -->
<div class="mb-3">
    <label class="form-label">AADHAAR NUMBER *</label>
    <input type="text" name="aadhar" maxlength="12" class="form-control placeholder-small"
           placeholder="Enter 12-digit Aadhaar Number" 
           style="border-radius: 12px; padding: 12px;" required>
</div>

<!-- Ek Line Left se Right tak -->
<div class="single-line"></div>

<!-- Service Charge -->
<div class="d-flex justify-content-between align-items-center mb-4">
    <span class="fw-semibold" style="color: #475569;">💰 SERVICE CHARGE</span>
    <span class="fw-bold fs-5" style="color: #1e293b;">₹ <?= number_format($fee, 2) ?></span>
</div>

<!-- Submit Button -->
<button class="btn btn-success w-100 py-2" id="submitBtn" style="border-radius: 12px; font-weight: 600; font-size: 16px;">
    🔍 FIND PAN
</button>

</form>

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

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

<?php include('../template/tngweb/footer.php'); ?>

<!-- ===============================
     CONFIRMATION SCRIPT
================================ -->
<script>
document.addEventListener("DOMContentLoaded", function () {

let submitting = false;
const form = document.getElementById("panForm");
const btn  = document.getElementById("submitBtn");

form.addEventListener("submit", function(e){
  e.preventDefault();
  if (submitting) return;

  Swal.fire({
    title: 'Are you sure?',
    text: 'PAN Find hogi aur wallet se fee deduct hogi',
    icon: 'warning',
    showCancelButton: true,
    confirmButtonText: 'Yes, Proceed',
    cancelButtonText: 'Cancel',
    confirmButtonColor: '#10b981'
  }).then((result)=>{
    if(result.isConfirmed){
      submitting = true;
      btn.disabled = true;
      btn.innerText = 'Processing...';
      form.submit();
    }
  });
});

});
</script>

<script src="../template/tngweb/assets/js/bootstrap.bundle.min.js"></script>
<script src="../template/tngweb/assets/js/app.js"></script>
</html>