<?php
include("include/conn.php");
include("include/function.php");

$query = "SELECT * FROM settings WHERE user_id = '1' LIMIT 1"; // Assuming user_id is 1 for simplicity
$result = mysqli_query($conn, $query);
$settings = mysqli_fetch_assoc($result);

// Construct the base URL dynamically
$url = $settings["checkst_apiurl"];
$orderid = $_GET["orderid"];
$token = $settings["api_token"];

// Data to be sent in the POST request
$data = array(
    'user_token' => $token,
    'order_id' => $orderid
);


$ch = curl_init();

// Set cURL options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);

if (curl_errno($ch)) {
    echo 'cURL Error: ' . curl_error($ch);
}

curl_close($ch);

$jsonResponse = json_decode($response, true);


$query1 = "SELECT id FROM users WHERE orderid = '$orderid' LIMIT 1";
$result1 = mysqli_query($conn, $query1);

?>
<!DOCTYPE html>
<html class="loading" lang="en" data-textdirection="ltr">

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0, minimal-ui">
    <meta name="description" content="Chameleon Admin is a modern Bootstrap 4 webapp &amp; admin dashboard html template with a large number of components, elegant design, clean and organized code.">
    <meta name="keywords" content="admin template, Chameleon admin template, dashboard template, gradient admin template, responsive admin template, webapp, eCommerce dashboard, analytic dashboard">
    <meta name="author" content="ThemeSelect">
    <title id="title"><?= $brandName ?> | Create License</title>
    <link rel="apple-touch-icon" href="assets/images/ico/apple-icon-120.png">
    <link rel="shortcut icon" type="image/x-icon" href="assets/images/ico/favicon.ico">
    <link href="https://fonts.googleapis.com/css?family=Muli:300,300i,400,400i,600,600i,700,700i%7CComfortaa:300,400,700" rel="stylesheet">
    <link href="https://maxcdn.icons8.com/fonts/line-awesome/1.1/css/line-awesome.min.css" rel="stylesheet">
    <!-- BEGIN VENDOR CSS-->
    <link rel="stylesheet" type="text/css" href="assets/css/vendors.css">
    <!-- END VENDOR CSS-->
    <!-- BEGIN CHAMELEON  CSS-->
    <link rel="stylesheet" type="text/css" href="assets/css/app-lite.css">
    <!-- END CHAMELEON  CSS-->
    <!-- BEGIN Page Level CSS-->
    <link rel="stylesheet" type="text/css" href="assets/css/core/menu/menu-types/vertical-menu.css">
    <link rel="stylesheet" type="text/css" href="assets/css/core/colors/palette-gradient.css">
    <!-- END Page Level CSS-->
    <!-- BEGIN Custom CSS-->
    <!-- END Custom CSS-->
</head>
<style>
    #response-message {
        text-align: center;
        /* Center-align the text horizontally */
        padding: 20px;
        /* Add padding for spacing */
        margin-top: 15px;
        /* Add some margin to separate it from other elements */
        font-size: 18px;
        /* Set the font size */
        font-weight: bold;
        /* Make the text bold */
    }

    /* Style for success messages */
    .success {
        background-color: #4CAF50;
        /* Green background color */
        color: #fff !important;
        /* White text color */
        border: 2px solid #45A049;
        /* Green border */
    }

    /* Style for error messages */
    .error {
        background-color: #FF5733;
        /* Red background color */
        color: #fff;
        /* White text color */
        border: 2px solid #D73925;
        /* Red border */
    }
</style>

<div class="app-content content">
    <div class="content-wrapper">
        <?php if(mysqli_num_rows($result1) > 0){ ?>
         <h2 class="text-danger">Failed To Create Licence Key !</h2>
        <p>Your Licence key is already generated to craete a new key buy or renew the plan. --- whatsapp support - <?= $number ?></p>
        <?php }else{ ?>
        
        <?php if($jsonResponse["status"] == 'COMPLETED'){ ?>
        <div class="content-wrapper-before"></div>
        <div class="content-header row">
            <div class="content-header-left col-md-4 col-12 mb-2">
                <!--<h3 class="content-header-title">Create License For User</h3>-->
            </div>
            <div class="content-header-right col-md-8 col-12">
                <div class="breadcrumbs-top float-md-right">
                    <div class="breadcrumb-wrapper mr-1">
                        <ol class="breadcrumb">
                            <li class="breadcrumb-item"><a href="index.php">Home</a>
                            </li>
                            <li class="breadcrumb-item active">Create License
                            </li>
                        </ol>
                    </div>
                </div>
            </div>
        </div>
        <div class="content-body"><!-- Basic Inputs start -->

            <div class="col-xl-12 col-lg-12 col-md-12">
                <div class="card">
                    <div class="card-header">
                        <h4 class="card-title">Create License</h4>
                        <div id="response-message"></div>
                    </div>
                    <div class="card-block">
                        <form id="generate">
                            <div class="card-body">
                                <fieldset class="form-group">
                                    <h6 class="card-title">Enter Whatsapp Number (With Country Code) example = 919858785878</h6>
                                    <input type="number" class="form-control" id="wnumber" name="wnumber">
                                </fieldset>
                                <fieldset class="form-group">
                                    <h6 class="card-title">Enter Client Name</h6>
                                    <input type="text" class="form-control" id="cname" name="cname">
                                </fieldset>
                             
                                <fieldset class="form-group">
                                    <button type="button" id="generate-license" class="btn btn-info btn-min-width mr-1 mb-1">Generate License</button>
                                </fieldset>
                            </div>
                        </form>
                    </div>
                </div>
            </div>

        </div>
        <?php }else{ ?>
        <h2 class="text-danger">Transaction Failed !</h2>
        <p>Your Transaction Failed for buy licence key ! try again later.  --- whatsapp support - <?= $number ?></p>
        <?php } ?>
        
        <?php } ?>
    </div>
</div>

<?php include("include/footer.php"); ?>

<script src="assets/vendors/js/vendors.min.js" type="text/javascript"></script>
<script src="assets/js/core/app-menu-lite.js" type="text/javascript"></script>
<script src="assets/js/core/app-lite.js" type="text/javascript"></script>
<script src="assets/vendors/js/forms/tags/form-field.js" type="text/javascript"></script>

<script>
    $(document).ready(function() {
        $('#title').html('Create License')
    });
    document.getElementById("sid-add").classList.add("active");
</script>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
    $(document).ready(function() {
        $('#generate-license').click(function() {
            // Get form data
            var wnumber = $('#wnumber').val();
            var cname = $('#cname').val(); 
            var orderid = "<?= $orderid ?>"; 
            

                if (!wnumber || !cname ) {

                var errorMessage = "Please fill in all required fields.";
                $('#response-message').html('<div class="error">' + errorMessage + '</div>');
                return;
            }

            
            var validity = 365; // Calculate days

            // Send AJAX request to your PHP script
            $.ajax({
                type: 'POST',
                url: 'function/generate-license.php',
                data: {
                    wnumber: wnumber,
                    validity: validity,
                    cname: cname,
                    orderid: orderid
                },
                success: function(response) {
                    if (response.status === true) {
                        $('#response-message').html('<div class="success">' + response.message + '</div>');
                    } else {
                        $('#response-message').html('<div class="error">' + response.message + '</div>');
                    }
                },
                error: function() {
                    $('#response-message').html('<div class="error">An error occurred while processing your request.</div>');
                }
            });
        });
    });
</script>

</body>

</html>
