<?php include ("../serive/samparka.php");?>
<?php 
if(isset($_GET['amount'])){
	$ramt = htmlspecialchars(mysqli_real_escape_string($conn, $_GET['amount']));
} else{
	$ramt = 0;
}
$dot_pos = strpos($ramt, '.');
if ($dot_pos === false) {
    $ramt = $ramt . '.00';
}else {
    $after_dot = substr($ramt, $dot_pos + 1);
    $after_dot_length = strlen($after_dot);
    if ($after_dot_length > 2) {
        $after_dot = substr($after_dot, 0, 2);
        $ramt = substr($ramt, 0, $dot_pos + 1) . $after_dot;
    } elseif ($after_dot_length < 2) {
        $zeros_to_add = 2 - $after_dot_length;
        $ramt = $ramt . str_repeat('0', $zeros_to_add);
    }
}
$date = date("Ymd");
$time = time();
$serial = 'P' . $date . $time . rand(1000,9999);

$tyid = htmlspecialchars(mysqli_real_escape_string($conn, $_GET['tyid']));
$uid = htmlspecialchars(mysqli_real_escape_string($conn, $_GET['uid']));
$sign = htmlspecialchars(mysqli_real_escape_string($conn, $_GET['sign']));
$urlInfo = htmlspecialchars(mysqli_real_escape_string($conn, $_GET['urlInfo']));
?>
<?php 
	$s_upi = "SELECT maulya FROM deyya WHERE sthiti='1'";
	$r_upi = mysqli_query($conn, $s_upi);
	$f_upi = mysqli_fetch_array($r_upi);
	$upi_id = $f_upi['maulya'];
	
	$selectupi_two=mysqli_query($conn,"select * from `images` where `status`=1");
	$selectupiresult_two=mysqli_fetch_array($selectupi_two);
?>
<?php
	$res = [
		'code' => 405,
		'message' => 'Illegal access!',
	];
	if (isset($_GET['tyid']) && isset($_GET['amount']) && isset($_GET['uid']) && isset($_GET['sign']) && isset($_GET['urlInfo'])) {
		$userId = $uid;
		$userPhoto = '1';
		
		$numquery = "SELECT mobile, codechorkamukala
		  FROM shonu_subjects
		  WHERE id = ".$userId;
		$numresult = $conn->query($numquery);
		$numarr = mysqli_fetch_array($numresult);
		
		$userName = '91'.$numarr['mobile'];
		$nickName = $numarr['codechorkamukala'];
		
		$creaquery = "SELECT createdate
		  FROM shonu_subjects
		  WHERE id = ".$userId;
		$crearesult = $conn->query($creaquery);
		$creaarr = mysqli_fetch_array($crearesult);
		
		$knbdstr = '{"userId":'.$userId.',"userPhoto":"'.$userPhoto.'","userName":'.$userName.',"nickName":"'.$nickName.'","createdate":"'.$creaarr['createdate'].'"}';
		$shonusign = strtoupper(hash('sha256', $knbdstr));
		
		$urlarr = explode (",", $urlInfo);
		$theirurl = $urlarr[0];
		$myurl = 'https://diuwin02.com';
		
		if($shonusign == $sign && $theirurl == $myurl){
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>LG Payment</title>
    <link rel="icon" href="/favicon.ico">
    <style>
        body { font-family: Arial, sans-serif; background: #f7f7f7; margin: 0; padding: 0; }
        .header { background: #2563eb; color: #fff; padding: 20px 0; text-align: center; position: relative; }
        .header .upi-logo { display: inline-block; vertical-align: middle; margin-right: 10px; }
        .header .upi-logo img { height: 32px; }
        .header .title { display: inline-block; font-size: 1.6em; font-weight: bold; vertical-align: middle; }
        .container { max-width: 400px; margin: 30px auto; background: #fff; border-radius: 12px; box-shadow: 0 2px 8px rgba(0,0,0,0.07); padding: 24px; }
        .amount-label { text-align: center; color: #333; font-size: 1.1em; margin-bottom: 8px; }
        .amount-value { text-align: center; color: #2563eb; font-size: 2.5em; font-weight: bold; margin-bottom: 12px; letter-spacing: 1px; }
        .qr-section { text-align: center; margin-bottom: 18px; }
        .qr-section img { width: 180px; height: 180px; border-radius: 8px; border: 2px solid #eee; }
        .download-btn { display: inline-block; margin-top: 12px; background: #2563eb; color: #fff; border: none; border-radius: 6px; padding: 10px 28px; font-size: 1em; cursor: pointer; text-decoration: none; font-weight: 500; }
        .pay-options { margin: 24px 0 12px 0; }
        .pay-options-label { font-size: 1em; color: #333; margin-bottom: 10px; }
        .pay-option { display: flex; align-items: center; background: #f5f7fa; border: 2px solid #e0e0e0; border-radius: 8px; padding: 10px 16px; margin-bottom: 10px; cursor: pointer; transition: border 0.2s; }
        .pay-option.selected { border: 2px solid #2563eb; background: #eaf1ff; }
        .pay-option img { width: 32px; height: 32px; margin-right: 12px; }
        .pay-option .option-label { font-size: 1.1em; color: #222; flex: 1; }
        .pay-option .checkmark { color: #2563eb; font-size: 1.3em; }
        .manual-label { font-size: 1em; color: #333; margin: 18px 0 6px 0; }
        .upi-box { display: flex; align-items: center; background: #fff0f6; border: 2px solid #e0b3d7; border-radius: 8px; padding: 10px 14px; margin-bottom: 10px; }
        .upi-id { color: #d63384; font-size: 1.1em; font-weight: 600; flex: 1; word-break: break-all; }
        .copy-btn { background: #2563eb; color: #fff; border: none; border-radius: 6px; padding: 6px 18px; font-size: 1em; cursor: pointer; font-weight: 500; }
        .utr-label { font-size: 1em; color: #333; margin: 18px 0 6px 0; }
        .utr-input { width: 100%; padding: 10px; font-size: 1.1em; border: 2px solid #2563eb; border-radius: 8px; margin-bottom: 12px; box-sizing: border-box; }
        .submit-btn { width: 100%; background: #2563eb; color: #fff; border: none; border-radius: 8px; padding: 12px 0; font-size: 1.1em; font-weight: bold; cursor: pointer; margin-top: 8px; }
        @media (max-width: 500px) { .container { padding: 10px; } }
    </style>
    <script src="assets/js/wepay/jquery-2.2.4.min.js"></script>
    <script src="assets/js/wepay/clipboard.min.js"></script>
</head>
<body>
    <div class="header">
        <span class="upi-logo"><img src="https://diuwin02.com/UPI.png " alt="UPI"></span>
        <span class="title">LG Payment</span>
    </div>
    <div class="container">
        <div class="amount-label">The amount you need to Pay</div>
        <div class="amount-value">₹<?php echo $ramt; ?></div>
        <div class="qr-section">
            <div>Use mobile scan code to pay.</div>     <img src="https://diuwin02.com/pay/qr1.png" id="qrCodeImage" alt="QR Code">
</div>

<div style="text-align:center;">   <!-- 👈 ye line add ki -->
    <a href="https://diuwin02.com/pay/qr1.png" download="QRCodeImage" class="download-btn">
        Download / Scan
    </a>
</div>

        <div class="pay-options-label">Select an option to pay(UPI)</div>
        <div class="pay-options">
            <div class="pay-option selected" id="paytm-option">
                <img src="https://diuwin02.com/PAYTM.png" alt="Paytm">
                <span class="option-label">Paytm</span>
                <span class="checkmark">&#10003;</span>
            </div>
            <div class="pay-option" id="phonepe-option">
                <img src="https://diuwin02.com/PHONE.png" alt="PhonePe">
                <span class="option-label">PhonePe</span>
            </div>
        </div>
        <div class="manual-label">Manual transfer</div>
        <div class="upi-box">
            <span class="upi-id" id="upi-id"><?php echo $upi_id;?></span>
            <button class="copy-btn" id="copy-upi">COPY</button>
        </div>
        <div class="utr-label">Paid? Submit UTR NO. for fast verification.</div>
        <input type="text" class="utr-input" id="refno" placeholder="Enter 12-digit UTR/Ref No." maxlength="12" minlength="12">
        <button class="submit-btn" id="savebtn">Submit UTR Number</button>
    </div>
    <script>
        // Payment option selection
        $(function() {
            $('#paytm-option').click(function() {
                $(this).addClass('selected');
                $('#phonepe-option').removeClass('selected');
            });
            $('#phonepe-option').click(function() {
                $(this).addClass('selected');
                $('#paytm-option').removeClass('selected');
            });
        });
        // Copy UPI ID
        var clipboard = new ClipboardJS('#copy-upi', {
            text: function() {
                return $('#upi-id').text();
            }
        });
        clipboard.on('success', function() {
            alert('UPI ID copied!');
        });
        clipboard.on('error', function() {
            alert('Failed to copy UPI ID. Please copy manually.');
        });
        // UTR input auto-submit
        $('#refno').on('input', function() {
            if ($(this).val().length === 12) {
                $('#savebtn').click();
            }
        });
        // Submit UTR
        $('#savebtn').on('click', function() {
            var e = $('#refno').val();
            var refNo = e;
            if (e.length !== 12) {
                alert('Invalid Ref Number');
                return false;
            }
            if (!confirm('For your money security, please confirm the following information carefully\n\nUPI: ' + $('#upi-id').text() + '\nTransfer amount: ₹<?php echo $ramt; ?>\nRef No: ' + refNo)) {
                return false;
            }
            // AJAX call to adddeposit.php
            $.ajax({
                type: 'POST',
                data: {
                    amt: '<?php echo $ramt; ?>',
                    refnum: refNo,
                    srl: '<?php echo $serial; ?>',
                    source: 'wepay',
                    upi: $('#upi-id').text(),
                    userId: '<?php echo $userId; ?>',
                    token: '<?php echo $shonusign; ?>'
                },
                url: 'adddeposit.php',
                success: function(html) {
                    var arr = html.split('~');
                    if (arr[0] == 1) {
                        alert('Success! Redirecting...');
                        window.location.href = 'depositconfirm.php?amt=<?php echo $ramt; ?>&refnum=' + refNo + '&srl=<?php echo $serial; ?>&userId=<?php echo $userId; ?>&token=<?php echo $shonusign; ?>';
                    } else if (arr[0] == 0) {
                        alert('Error');
                    } else if (arr[0] == 2) {
                        alert('Duplicate UTR');
                    } else if (arr[0] == 3) {
                        alert('Please Wait For 1 Minute');
                    } else if (arr[0] == 4) {
                        alert('Your recharge option is suspended. Contact Customer Support');
                    }
                },
                error: function() {
                    alert('Network error. Please try again.');
                }
            });
        });

        // Replace these with your dynamic PHP variables if needed
        var upiId = "<?php echo $upi_id; ?>";
        var amount = "<?php echo $ramt; ?>";
        var payeeName = "Merchant"; // You can fetch this from DB if available

        // Standard UPI deep link
        function getUpiUrl() {
            return "upi://pay?pa=" + encodeURIComponent(upiId) +
                   "&pn=" + encodeURIComponent(payeeName) +
                   "&am=" + encodeURIComponent(amount) +
                   "&cu=INR";
        }

        // Paytm deep link (fallback to UPI if not supported)
        function getPaytmUrl() {
            return "paytmmp://pay?pa=" + encodeURIComponent(upiId) +
                   "&pn=" + encodeURIComponent(payeeName) +
                   "&am=" + encodeURIComponent(amount) +
                   "&cu=INR";
        }

        // PhonePe deep link (fallback to UPI if not supported)
        function getPhonePeUrl() {
            return "phonepe://upi/pay?pa=" + encodeURIComponent(upiId) +
                   "&pn=" + encodeURIComponent(payeeName) +
                   "&am=" + encodeURIComponent(amount) +
                   "&cu=INR";
        }

        $('#paytm-option').on('click', function(e) {
            // Try Paytm deep link, fallback to UPI
            window.location.href = getPaytmUrl();
            setTimeout(function() {
                window.location.href = getUpiUrl();
            }, 1000);
        });

        $('#phonepe-option').on('click', function(e) {
            // Try PhonePe deep link, fallback to UPI
            window.location.href = getPhonePeUrl();
            setTimeout(function() {
                window.location.href = getUpiUrl();
            }, 1000);
        });
    </script>
</body>
</html>
<?php
		}
		else{
			$res['code'] = 10000;
			$res['success'] = 'false';
			$res['message'] = 'Sorry, The system is busy, please try again later!';
			
			header('Content-Type: text/html; charset=utf-8');
			http_response_code(200);
			echo json_encode($res);	
		}
	}
	else{
		header('Content-Type: application/json; charset=utf-8');
		http_response_code(200);
		echo json_encode($res);	
	}
?>