<?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'); 
?>

<link href="https://fonts.googleapis.com/css2?family=Sora:wght@400;500;600;700;800&display=swap" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>

<style>
:root{
  --br:#2563eb;--glow:rgba(37,99,235,.16);
  --bg:#f0f4ff;--sur:#fff;--sur2:#f8faff;--bdr:#e2e8f8;
  --tx:#0f172a;--mu:#64748b;--ff:'Sora',sans-serif;
}
*,*::before,*::after{box-sizing:border-box;margin:0;padding:0;}
.vp{display:flex;justify-content:center;padding:20px 14px 44px;
  background:radial-gradient(ellipse 90% 40% at 50% 0%,rgba(37,99,235,.11),transparent 65%),var(--bg);
  min-height:100vh;}
.vc{width:100%;max-width:500px;background:var(--sur);border-radius:18px;
  box-shadow:0 18px 50px rgba(37,99,235,.11),0 3px 10px rgba(0,0,0,.05);
  border:1px solid var(--bdr);overflow:hidden;
  animation:su .4s cubic-bezier(.22,1,.36,1) both;font-family:var(--ff);}
@keyframes su{from{opacity:0;transform:translateY(20px)}to{opacity:1;transform:translateY(0)}}
.vh{background:linear-gradient(135deg,#1e3a8a 0%,#2563eb 55%,#0891b2 100%);
  padding:16px 18px 14px;position:relative;overflow:hidden;}
.vh::before{content:'';position:absolute;top:-35px;right:-35px;width:120px;height:120px;border-radius:50%;background:rgba(255,255,255,.07);}
.vh::after{content:'';position:absolute;bottom:-40px;left:-10px;width:100px;height:100px;border-radius:50%;background:rgba(255,255,255,.05);}
.vh-top{display:flex;justify-content:space-between;align-items:center;position:relative;z-index:1;}
.vh-icon{width:40px;height:40px;background:rgba(255,255,255,.18);border-radius:10px;
  display:flex;align-items:center;justify-content:center;font-size:19px;border:1px solid rgba(255,255,255,.25);}
.vh-link{display:flex;align-items:center;gap:5px;background:rgba(255,255,255,.14);
  border:1px solid rgba(255,255,255,.28);padding:5px 12px;border-radius:30px;
  font-size:12px;font-weight:600;color:#fff;text-decoration:none;font-family:var(--ff);transition:background .18s;}
.vh-link:hover{background:rgba(255,255,255,.22);color:#fff;}
.dot{width:6px;height:6px;border-radius:50%;background:#4ade80;box-shadow:0 0 5px #4ade80;
  animation:pl 1.6s infinite;display:inline-block;}
@keyframes pl{0%,100%{opacity:1}50%{opacity:.35}}
.vh-title{color:#fff;font-size:17px;font-weight:800;margin-top:11px;position:relative;z-index:1;letter-spacing:-.2px;}
.vh-sub{color:rgba(255,255,255,.65);font-size:11.5px;margin-top:2px;position:relative;z-index:1;}
.vs{display:flex;border-bottom:1px solid var(--bdr);}
.vsi{flex:1;padding:9px 12px;border-right:1px solid var(--bdr);display:flex;align-items:center;gap:7px;background:var(--sur2);}
.vsi:last-child{border-right:none;}
.vsi-ico{width:28px;height:28px;border-radius:8px;display:flex;align-items:center;justify-content:center;font-size:13px;flex-shrink:0;}
.ib{background:rgba(37,99,235,.09);}.ic{background:rgba(6,182,212,.09);}.ig{background:rgba(16,185,129,.09);}
.vsi-lbl{font-size:10px;color:var(--mu);font-weight:500;}
.vsi-val{font-size:12.5px;color:var(--tx);font-weight:700;}
.vb{padding:16px 18px 18px;}
.sec-lbl{font-size:10px;font-weight:700;color:var(--br);letter-spacing:1.2px;text-transform:uppercase;
  margin-bottom:11px;display:flex;align-items:center;gap:7px;}
.sec-lbl::after{content:'';flex:1;height:1px;background:linear-gradient(90deg,var(--bdr),transparent);}
.vf{margin-bottom:12px;}
.vl{display:flex;align-items:center;gap:5px;font-size:12px;font-weight:600;color:#334155;margin-bottom:5px;}
.viw{position:relative;}
.vi{width:100%;height:46px;padding:0 14px 0 38px;font-size:13.5px;font-family:var(--ff);font-weight:500;
  color:var(--tx);background:var(--sur2);border:1.5px solid var(--bdr);border-radius:11px;
  outline:none;transition:border .18s,box-shadow .18s;appearance:none;}
.vi::placeholder{color:#94a3b8;font-weight:400;}
.vi:focus{border-color:var(--br);background:#fff;box-shadow:0 0 0 3px var(--glow);}
.vii{position:absolute;left:10px;top:50%;transform:translateY(-50%);font-size:15px;pointer-events:none;}
.lbb{background:#fef2f2;border:1.5px solid #fca5a5;border-radius:10px;padding:9px 12px;
  font-size:12px;color:#991b1b;display:flex;align-items:center;gap:8px;margin-bottom:12px;font-family:var(--ff);}
.vbtn{width:100%;height:48px;border:none;border-radius:12px;
  background:linear-gradient(135deg,#1d4ed8,#2563eb,#0891b2);
  color:#fff;font-family:var(--ff);font-size:14.5px;font-weight:700;
  cursor:pointer;display:flex;align-items:center;justify-content:center;gap:8px;
  margin-top:4px;position:relative;overflow:hidden;
  transition:transform .14s,box-shadow .14s;box-shadow:0 4px 20px rgba(37,99,235,.25);}
.vbtn::before{content:'';position:absolute;inset:0;background:linear-gradient(135deg,rgba(255,255,255,.12),transparent);border-radius:inherit;}
.vbtn:hover{transform:translateY(-2px);box-shadow:0 8px 22px rgba(37,99,235,.30);}
.vbtn:active{transform:translateY(0);}
.vbtn:disabled{opacity:.7;transform:none;cursor:not-allowed;}
.fpill{background:rgba(255,255,255,.2);border:1px solid rgba(255,255,255,.3);padding:2px 9px;border-radius:20px;font-size:12px;}
.vn{display:flex;gap:9px;background:linear-gradient(135deg,#fffbeb,#fef9c3);
  border:1.5px solid #fde68a;padding:10px 12px;border-radius:10px;margin-top:13px;}
.vn-ico{font-size:16px;flex-shrink:0;margin-top:1px;}
.vn-txt{font-size:11.5px;color:#78350f;line-height:1.7;font-family:var(--ff);}
.vn-txt b{color:#92400e;}
.vll{display:flex;align-items:center;justify-content:center;gap:5px;margin-top:11px;padding:9px;
  border:1.5px dashed var(--bdr);border-radius:10px;color:var(--br);font-size:12.5px;
  font-weight:600;text-decoration:none;transition:background .18s,border-color .18s;font-family:var(--ff);}
.vll:hover{background:rgba(37,99,235,.04);border-color:var(--br);}
@media(max-width:400px){.vs{flex-direction:column;}.vsi{border-right:none;border-bottom:1px solid var(--bdr);}}
</style>

<div class="page-wrapper">
<div class="page-content">
<div class="vp">
<div class="vc">

  <div class="vh">
    <div class="vh-top">
      <div class="vh-icon">🪪</div>
      <a href="Pan_check_status.php" class="vh-link"><span class="dot"></span> My Requests</a>
      <a href="javascript:history.back()" class="vh-link" style="margin-left:6px;">← Back</a>
    </div>
    <div class="vh-title">PAN Find Service</div>
    <div class="vh-sub">Aadhar se PAN Number dhundhe</div>
  </div>

  <div class="vs">
    <div class="vsi"><div class="vsi-ico ib">💰</div>
      <div><div class="vsi-lbl">Wallet</div><div class="vsi-val">₹<?php echo number_format($udata['balance'],2); ?></div></div>
    </div>
    <div class="vsi"><div class="vsi-ico ic">🏷️</div>
      <div><div class="vsi-lbl">Service Fee</div><div class="vsi-val">₹<?php echo $fee; ?></div></div>
    </div>
    <div class="vsi"><div class="vsi-ico ig">⏱️</div>
      <div><div class="vsi-lbl">Processing</div><div class="vsi-val">Manual</div></div>
    </div>
  </div>

  <div class="vb">

    <?php if(floatval($udata['balance']) < $fee): ?>
    <div class="lbb">⚠️ <div><b>Low Balance!</b> ₹<?php echo $fee; ?> chahiye — wallet recharge karo.</div></div>
    <?php endif; ?>

    <div class="sec-lbl">Aadhar Details</div>

    <form id="panSubmitForm">
      <div class="vf">
        <div class="vl">🪪 Aadhar Number</div>
        <div class="viw">
          <span class="vii">🔢</span>
          <input type="text" name="uid" class="vi" required
                 placeholder="Enter 12-digit Aadhar Number"
                 maxlength="12" inputmode="numeric"
                 oninput="this.value=this.value.replace(/\D/g,'')">
        </div>
      </div>

      <button type="submit" id="btnSubmit" class="vbtn">
        🔍 Find PAN Number <span class="fpill">₹<?php echo $fee; ?></span>
      </button>
    </form>

    <div class="vn">
      <div class="vn-ico">⏰</div>
      <div class="vn-txt">
        <b>Result Delivery Time:</b><br>
        🌅 <b>Subah 11 baje se pehle</b> apply kiya → Result <b>aaj raat 8–9 baje</b> tak milega.<br>
        ☀️ <b>Subah 11 baje ke baad</b> apply kiya → Result <b>kal dopahar</b> tak milega.<br><br>
        ✅ <b>Reject hone par</b> amount wallet mein wapas aa jayega.
      </div>
    </div>

    <a href="Pan_check_status.php" class="vll">📋 View All My Requests →</a>

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

<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$('#panSubmitForm').on('submit', function(e){
    e.preventDefault();
    $('#btnSubmit').prop('disabled', true).html('⏳ Processing... <span class="fpill">Please wait</span>');
    $.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).html('🔍 Find PAN Number <span class="fpill">₹<?php echo $fee; ?></span>');
            }
        }
    });
});
</script>

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