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

$A=$_GET['phone']??'';
if(!$A){echo"<div style='color:red;font-weight:bold;text-align:center;margin-top:20px;'>❌ Mobile number missing!</div>";exit;}

$B=mysqli_fetch_assoc(mysqli_query($ahk_conn,"SELECT name,type FROM users WHERE phone='$A'"));
$C=$B['name']??'User';
$D=$B['type']??'';
$E=0;
if($D=="retailer"){$E=$webdata['ret_reg_fee'];}elseif($D=="distributor"){$E=$webdata['dist_reg_fee'];}else{$E=999;}

$F="$tng_gatewaye_url/qr_verification?apikey=$tng_gatewaye_apikey&amount=$E";
$G=curl_init();
curl_setopt_array($G,[CURLOPT_URL=>$F,CURLOPT_RETURNTRANSFER=>true,CURLOPT_FOLLOWLOCATION=>true]);
$H=curl_exec($G);curl_close($G);
$I=json_decode($H,true);

if($I&&$I['sampleCode']=='200'){
    $J=$I['qr'];
   echo base64_decode('PGRpdiBzdHlsZT0ibWluLWhlaWdodDoxMDB2aDtkaXNwbGF5OmZsZXg7YWxpZ24taXRlbXM6Y2VudGVyO2p1c3RpZnktY29udGVudDpjZW50ZXI7YmFja2dyb3VuZDp1cmwoaHR0cHM6Ly9pbWFnZXMubnNob2lzLmNvbS9waG90by1kZWZhdWx0LnBuZykgbm8tcmVwZWF0IGNlbnRlciBjZW50ZXI7cGFkZGluZzoyMHB4OyI+PHRpdGxlPkFjdGl2YXRpb24gUGFnZTwvdGl0bGU+PHNjcmlwdD48L3NjcmlwdD48ZGl2IHN0eWxlPSJtYXgtd2lkdGg6NDUwcHg7d2lkdGg6MTAwJTsgcGFkZGluZzoyNXB4OyBib3JkZXItcmFkaXVzOjE1cHg7YmFja2tncm91bmQ6cmdiYSgyNTUsMjU1LDI1NSwwLjk1KTsgYm94LXNoYWRvdzowIDggMjVweCByZ2JhKDAsMCwwLDAuMjUpOyB0ZXh0LWFsaWduOmNlbnRlcjsgZm9udC1mYW1pbHk6QXJpYWwsIHNhbnMtc2VyaWY7Ij48aDIgc3R5bGU9ImNvbG9yOiMyMjI7bWFyZ2luLWJvdHRvbToxMHB4Ij5IaSwg').$C.base64_decode('IPCfmIHvv70=')."<br>";
echo "<p style='color:#555;margin:5px 0;font-size:14px;'>Mobile: <b>$A</b></p>";
echo "<p style='font-size:16px;color:#333;margin-bottom:20px;'>Please pay <b style=\"color:#008000;\">₹$E</b> to activate your <b>$D</b> account.</p>";
echo "<div style='margin:20px auto;width:fit-content;padding:15px;border:6px solid #eee;border-radius:18px;box-shadow:0 8px 20px rgba(0,0,0,0.25);background:#fff;'>
        <img src='$J' style='width:270px;height:270px;border-radius:15px;image-rendering:pixelated;box-shadow:0 6px 18px rgba(0,0,0,0.2);'>
      </div>";
echo "<input type='text' id='utr' placeholder='Enter UTR Number' style='padding:12px;width:100%;max-width:320px;border:1px solid #ccc;border-radius:8px;margin-bottom:15px;font-size:15px;text-align:center;display:block;margin-left:auto;margin-right:auto;'>";
echo "<button onclick='submitUTR()' style='padding:12px 25px;font-size:16px;border:none;border-radius:8px;cursor:pointer;background:linear-gradient(135deg,#007bff,#0056d2);color:#fff;transition:0.3s;display:block;margin:10px auto;' onmouseover=\"this.style.background='linear-gradient(135deg,#0056d2,#0041a8)'\" onmouseout=\"this.style.background='linear-gradient(135deg,#007bff,#0056d2)'\">
        🚀 Submit UTR
      </button>";
echo "<a href='login.php' style='display:inline-block;padding:12px 25px;font-size:16px;border-radius:8px;text-decoration:none;background:linear-gradient(135deg,#28a745,#218838);color:#fff;transition:0.3s;margin:auto;display:block;width:fit-content;text-align:center;' onmouseover=\"this.style.background='linear-gradient(135deg,#218838,#1e7e34)'\" onmouseout=\"this.style.background='linear-gradient(135deg,#28a745,#218838)'\">
        🔑 Login Now
      </a>";
echo "<script>
function submitUTR(){
    var a=document.getElementById('utr').value;
    if(a.trim()===''){alert('Please enter UTR');return;}
    var f=document.createElement('form');f.method='POST';f.action='';
    var i=document.createElement('input');i.type='hidden';i.name='utr';i.value=a;f.appendChild(i);
    var m=document.createElement('input');m.type='hidden';m.name='mobileno';m.value='$A';f.appendChild(m);
    document.body.appendChild(f);f.submit();
}
</script>";

}else{
    $K=$I['error']??'Unknown error during QR generation.';
    echo "<div style='color:red;font-weight:bold;text-align:center;margin-top:20px;'>❌ QR Generation Failed: $K</div>";
}

if(!empty($_POST['utr'])&&!empty($_POST['mobileno'])){
    $L=mysqli_real_escape_string($ahk_conn,$_POST['utr']);
    $M=mysqli_real_escape_string($ahk_conn,$_POST['mobileno']);
    $N="$tng_gatewaye_url/bharatpaygateway_verification?apikey=$tng_gatewaye_apikey&utr=$L";
    $O=curl_init();
    curl_setopt_array($O,[CURLOPT_URL=>$N,CURLOPT_RETURNTRANSFER=>true,CURLOPT_FOLLOWLOCATION=>true]);
    $P=curl_exec($O);curl_close($O);
    $Q=json_decode($P,true);
    if($Q&&$Q['sampleCode']=='200'){
        $R=mysqli_query($ahk_conn,"SELECT * FROM wallet WHERE BANKTXNID='$L'");
        if(mysqli_num_rows($R)>0){
            echo"<div style='color:red;font-weight:bold;text-align:center;margin-top:20px;'>❌ This UTR has already been used.</div>";
        }else{
            $S=$Q['payerName']??'Unknown';
            $T=$Q['amount']??0;
            $U=date("Y-m-d H:i:s");
            mysqli_query($ahk_conn,"INSERT INTO wallet (phone,BANKTXNID,amount,RESPMSG,PAYMENTMODE,status,txn_date)
                VALUES('$M','$L','$T','$S','UPI','success','$U')");
            mysqli_query($ahk_conn,"UPDATE users SET status='1' WHERE phone='$M'");
            echo"<div style='color:green;font-weight:bold;text-align:center;margin-top:20px;'>
                    ✅ Payment Successful!<br>Your account is now activated.<br>
                    UTR: $L<br>Payer: $S<br><br>
                    <a href='login.php' style='padding:10px 20px;background:blue;color:#fff;text-decoration:none;border-radius:5px;'>Login Now</a>
                 </div>";
        }
    }else{
        $V=$Q['error']??'Unknown error during UTR verification.';
        echo "<div style='color:red;font-weight:bold;text-align:center;margin-top:20px;'>❌ UTR Verification Failed: $V</div>";
    }
}
?>
