<?php

// Dene the absolute path to the functions.php file
define('ABSPATH', dirname(__FILE__) . '/'); // Adjust the path as needed
// Include the database connection file
require_once(ABSPATH . 'header.php');


?>


<?php


// Function to sanitize user input
function sanitizeInput($input) {
    if (is_string($input)) {
        return htmlspecialchars(trim($input), ENT_QUOTES, 'UTF-8');
    } else {
        // Handle non-string input here (e.g., arrays, objects, etc.) if needed.
        return $input;
    }
}

if(isset($_POST['verifyotp'])) {
  

    $bbbyteuserid = $_SESSION['user_id'];
    $bbytepaytmuserid = $userdata['user_token'];
    $bbytepaytmusermid = ($_POST["MID"]);
    $bbytepaytmuserupiid = ($_POST["UPI"]);

    $sqlUpdateUser = "UPDATE users SET paytm_connected='Yes' WHERE user_token='$bbytepaytmuserid'";
    $resultUpdateUser = mysqli_query($conn, $sqlUpdateUser);

    $sqlw = "UPDATE paytm_tokens SET MID='$bbytepaytmusermid', Upiid='$bbytepaytmuserupiid', status='Active', user_id=$bbbyteuserid WHERE user_token='$bbytepaytmuserid'";
    $result = mysqli_query($conn, $sqlw);

    if ($result) {
        // Show SweetAlert2 success message
       
       echo '<script src="js/jquery-3.2.1.min.js"></script>';echo '<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11.0.18"></script>';echo '<script>$("#loading_ajax").hide();
            Swal.fire({
                icon: "success",
                title: "Congratulations! Your Paytm Has been Connected Successfully!",
                showConfirmButton: true,
                confirmButtonText: "Ok!",
                allowOutsideClick: false,
                allowEscapeKey: false
            }).then((result) => {
                if (result.isConfirmed) {
                    window.location.href = "connect_merchant";
                }
            });
        </script>';
        exit();
    } else {
        // Show SweetAlert2 error message
       
       echo '<script src="js/jquery-3.2.1.min.js"></script>';echo '<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11.0.18"></script>';echo '<script>$("#loading_ajax").hide();
            Swal.fire({
                icon: "error",
                title: "Please Try Again Later!!",
                showConfirmButton: true,
                confirmButtonText: "Ok!",
                allowOutsideClick: false,
                allowEscapeKey: false
            }).then((result) => {
                if (result.isConfirmed) {
                    window.location.href = "connect_merchant";
                }
            });
        </script>';
        exit();
    }
}

if(isset($_POST['Verify'])) { ///to open this page from last
  

    if ($userdata['paytm_connected'] == "Yes") {
        // Show SweetAlert2 error message
       
       echo '<script src="js/jquery-3.2.1.min.js"></script>';echo '<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11.0.18"></script>';echo '<script>$("#loading_ajax").hide();
            Swal.fire({
                icon: "error",
                title: "Merchant Already Connected !!",
                showConfirmButton: true,
                confirmButtonText: "Ok!",
                allowOutsideClick: false,
                allowEscapeKey: false
            }).then((result) => {
                if (result.isConfirmed) {
                    window.location.href = "connect_merchant";
                }
            });
        </script>';
        exit();
    }

    $paytm_mobile = ($_POST["paytm_mobile"]);
    ?>
    <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11.0.18"></script>
    <script>
        Swal.fire({
            title: 'Paytm UPI Settings',
            html: `
                <form id="paytmForm" method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
                    <div class="pm-field">
                        <label for="MID">Merchant ID</label>
                        <input type="text" name="MID" id="MID" placeholder="Enter Merchant ID" required>
                    </div>
                    <div class="pm-field">
                        <label for="Number">Mobile Number</label>
                        <input type="number" name="Number" id="Number" placeholder="Enter Number" value="<?php echo $paytm_mobile; ?>" minlength="10" maxlength="10" oninput="this.value = this.value.replace(/[^0-9]/g, '');" required>
                    </div>
                    <div class="pm-field">
                        <label for="UPI">UPI ID</label>
                        <input type="text" name="UPI" id="UPI" placeholder="Enter UPI" required value="dummy@Paytm">
                    </div>
                    <button type="submit" name="verifyotp" class="pm-submit"><i class="fas fa-shield-halved"></i> Verify Paytm</button>
                </form>
            `,
            showCancelButton: false,
            showConfirmButton: false,
            background: '#14141f',
            color: '#fff',
            customClass: {
                popup: 'swal2-custom-popup',
                title: 'swal2-title',
                content: 'swal2-content'
            },
            allowOutsideClick: false,
            allowEscapeKey: false
        });
    </script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css">
    <style>
        :root{
            --p:#6c63ff; --p2:#e040fb;
            --g: linear-gradient(135deg,#6c63ff,#e040fb);
        }
        .swal2-custom-popup {
            max-width: 420px;
            padding: 1.75em 1.5em 2em;
            border-radius: 20px;
            background: #14141f !important;
            border: 1px solid rgba(255,255,255,.08);
            box-shadow: 0 25px 60px rgba(0,0,0,.55);
        }
        .swal2-title {
            font-size: 1.3rem;
            margin-bottom: .6em;
            color: #fff !important;
            font-weight: 800;
            font-family: 'Inter', sans-serif;
        }
        .swal2-content { text-align: left; }
        .swal2-content form { display: flex; flex-direction: column; gap: 14px; }
        .pm-field label{
            display:block;font-size:.75rem;font-weight:700;color:rgba(255,255,255,.5);
            margin-bottom:6px;text-transform:uppercase;letter-spacing:.3px;
        }
        .pm-field input{
            width:100%;background:rgba(255,255,255,.05);border:1.5px solid rgba(255,255,255,.12);
            border-radius:10px;padding:11px 13px;color:#fff;font-size:.9rem;outline:none;
            transition:.2s;box-sizing:border-box;font-family:'Inter',sans-serif;
        }
        .pm-field input:focus{border-color:var(--p);background:rgba(108,99,255,.08);box-shadow:0 0 0 3px rgba(108,99,255,.15)}
        .pm-field input::placeholder{color:rgba(255,255,255,.3)}
        .pm-submit{
            width:100%;background:var(--g);border:none;color:#fff;padding:12px;border-radius:10px;
            font-size:.9rem;font-weight:700;cursor:pointer;display:flex;align-items:center;justify-content:center;gap:8px;
            margin-top:6px;transition:.2s;box-shadow:0 6px 18px rgba(108,99,255,.3);font-family:'Inter',sans-serif;
        }
        .pm-submit:hover{transform:translateY(-1px);box-shadow:0 10px 24px rgba(108,99,255,.45)}
        .swal2-popup{background:#14141f !important}
    </style>
    <?php
} //iset from last page if(isset($_POST['Verify'])) {

else{
    
echo '<script>
    Swal.fire({
        icon: "error",
        title: "Form Not Submitted!!",
        showConfirmButton: true, // Show the confirm button
        confirmButtonText: "Ok!", // Set text for the confirm button
        allowOutsideClick: false, // Prevent the user from closing the popup by clicking outside
        allowEscapeKey: false // Prevent the user from closing the popup by pressing Escape key
    }).then((result) => {
        if (result.isConfirmed) {
            window.location.href = "connect_merchant"; // Redirect to "connect_merchant" when the user clicks the confirm button
        }
    });
</script>';
exit;
}
?>

<!--bootstrap js-->
  <script src="assets/js/bootstrap.bundle.min.js"></script>

  <!--plugins-->
  <script src="assets/js/jquery.min.js"></script>
  <!--plugins-->
  <script src="assets/plugins/perfect-scrollbar/js/perfect-scrollbar.js"></script>
  <script src="assets/plugins/metismenu/metisMenu.min.js"></script>
  <script src="assets/plugins/simplebar/js/simplebar.min.js"></script>
  <script src="assets/js/main.js"></script>


</body>

</html>