<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Subscription Payment</title>
    <script src="https://sdk.cashfree.com/js/v3/cashfree.js"></script>
    <style>
      body {
        font-family: Arial, sans-serif;
        margin: 0;
        padding: 0;
        background-color: #f4f4f9;
        color: #333;
        display: flex;
        justify-content: center;
        align-items: center;
        height: 100vh;
      }

      .payment-container {
        background: #fff;
        padding: 20px 30px;
        border-radius: 8px;
        box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
        text-align: center;
        max-width: 400px;
        width: 100%;
      }

      .payment-container h1 {
        color: #4caf50;
        margin-bottom: 10px;
        font-size: 1.5rem;
      }

      .payment-container p {
        margin: 10px 0;
        font-size: 1rem;
        line-height: 1.5;
      }

      .payment-container .price {
        font-size: 1.5rem;
        font-weight: bold;
        color: #333;
      }

      .payment-container .btn {
        margin-top: 20px;
        padding: 10px 20px;
        background: #4caf50;
        color: white;
        border: none;
        border-radius: 4px;
        cursor: pointer;
        font-size: 1rem;
      }

      .payment-container .btn:hover {
        background: #45a049;
      }

      .footer {
        margin-top: 20px;
        font-size: 0.9rem;
        color: #666;
      }

      .footer a {
        color: #4caf50;
        text-decoration: none;
      }

      .footer a:hover {
        text-decoration: underline;
      }
    </style>
  </head>
  <body>
    <div class="payment-container">
      <h1>Subscribe Now</h1>
      <p>Unlock all premium features with a subscription for just:</p>
      <p class="price">₹500 / month</p>

    
      <!-- Button to proceed with payment -->
      <button type="button" class="btn" id="renderBtn">Pay Now</button>
      
    <p>
      <!-- Button to validate passcode -->
      <button type="button" class="btn" id="validatePasscodeBtn">Passcode</button>
      
      <!-- Passcode input box -->
      <input type="text" id="passcode" placeholder="Enter passcode for free subscription" />
    </p>
      <div class="footer">
        <p>Need help? Contact us at <a href="mailto:contact@efficientrix.in">contact@efficientrix.in</a> to claim free passcode or any other query.</p>
      </div>
    </div>

    <script>
      // Function to initialize payment or validate passcode
      function initPayment() {
        // Get the fingerprint and mobile number from the URL query parameters
        const urlParams = new URLSearchParams(window.location.search);
        const fpr = urlParams.get('fpr');
        const mobile = urlParams.get('mobile');

        if (!fpr || !mobile) {
          alert("fpr or Mobile Number is missing!");
          return; // Exit if fingerprint or mobile is missing
        }

        const passcodeInput = document.getElementById("passcode");
        const validatePasscodeBtn = document.getElementById("validatePasscodeBtn");
        const renderBtn = document.getElementById("renderBtn");

        // Add event listener to validate passcode
        validatePasscodeBtn.addEventListener("click", function () {
          const passcode = passcodeInput.value.trim();

          if (passcode === "EFX100") {
            console.log('Passcode is correct. Initiating free subscription...');
            
            // Free subscription for 30 days
            fetch('validate_passcode.php', {
              method: 'POST',
              headers: { 'Content-Type': 'application/json' },
              body: JSON.stringify({ fpr, mobile, passcode })
            })
              .then(response => response.json())
              .then(data => {
                console.log(data); // Debugging response
                if (data.status === 'success') {
                  alert(data.message); // Notify the user
                  // Optionally, hide the payment button if subscription is granted
                  renderBtn.style.display = "none";
                  // You may also show a message or handle other UI elements if needed
                } else {
                  alert(data.message);
                }
              })
              .catch(error => {
                console.error('Error:', error);
                alert("An error occurred while validating passcode.");
              });
          } else {
            // Passcode is incorrect, so proceed to payment
            console.log('Invalid passcode. Proceeding with payment...');
            fetch(`test_cashfree.php?fpr=${fpr}&mobile=${mobile}`)
              .then((response) => response.json())
              .then((data) => {
                if (data.error) {
                  throw new Error(data.error);
                }

                const cashfree = Cashfree({
                  mode: "sandbox", // Change to "production" for live
                });

                renderBtn.addEventListener("click", () => {
                  console.log('Proceeding to checkout...');
                  cashfree.checkout({
                    paymentSessionId: data.paymentSessionId,
                  });
                });
              })
              .catch((error) => {
                console.error("Error:", error);
                alert("An error occurred while initializing the payment. Please try again.");
              });
          }
        });

        // Add event listener for Pay Now button
        renderBtn.addEventListener("click", function () {
          console.log('Proceeding to checkout...');
          fetch(`test_cashfree.php?fpr=${fpr}&mobile=${mobile}`)
            .then((response) => response.json())
            .then((data) => {
              if (data.error) {
                throw new Error(data.error);
              }

              const cashfree = Cashfree({
                mode: "sandbox", // Change to "production" for live
              });

              cashfree.checkout({
                paymentSessionId: data.paymentSessionId,
              });
            })
            .catch((error) => {
              console.error("Error:", error);
              alert("An error occurred while initializing the payment. Please try again.");
            });
        });
      }

      // Call the initPayment function when the page loads
      initPayment();
    </script>
  </body>
</html>

