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


$trial_days += 1;

$admin_id = 13;
$message = "";

if (isset($_POST['submit'])) {
    $wnumber = trim($_POST['mobileNumber']);
    $fname = trim($_POST['firstName']);
    $lname = trim($_POST['lastName']);
    $cname = $fname . " " . $lname;

    if (check_number($wnumber)) {
        $message = "<span class='error'>Whatsapp number already exists.</span>";
    } else {
        $today = date("Y-m-d H:i:s");
        $licenseKey = generate_license();
        $futureDate = date("Y-m-d H:i:s", strtotime("+".$trial_days." days"));

        $stmt = $conn->prepare("INSERT INTO `users` (`user_id`,`customer_name`,`whatsapp_number`, `license_key`, `act_date`, `end_date`, `life_time`, `plan_type`) VALUES (?, ?, ?, ?, ?, ?, 'false', 'Premium')");
        $stmt->bind_param("isssss", $admin_id, $cname, $wnumber, $licenseKey, $today, $futureDate);

        if ($stmt->execute()) {
            $message = "<span class='success'><i class='fa fa-key'>&nbsp;</i>Trial Key : <span id='key'><span style='font-weight:bold;'>$licenseKey</span></span></span>&nbsp;<span style='margin-left:15px;'><i class='fa fa-copy' id='copy_btn' style='color:#04d88a;font-weight:bold;cursor:pointer;'></i></span>";

            $client_message = urlencode("Your Whatsapp CRM Trial License Key is : *$licenseKey*, if any query please contact us : *$supportPhoneNumber*");
            $admin_message = urlencode("Whatsapp CRM Key Generated by *$cname*, Mobile No : *$wnumber*, Key : *$licenseKey*");

            // Send to client
            sendWhatsappMessage($wnumber, $client_message, $instanceId, $tokenId, $trialKeyBannerLink);

            // Send to admin
            sendWhatsappMessage($notificationPhoneNumber, $admin_message, $instanceId, $tokenId, $trialKeyBannerLink);

        } else {
            $message = "<span class='error'>Failed to save data.</span>";
        }

        $stmt->close();
    }
}

// Send WhatsApp function
function sendWhatsappMessage($mobileNo, $message, $instanceId, $tokenId, $banner)
{
    global $waziperLink;

    if (empty($instanceId) || empty($tokenId) || empty($waziperLink)) return false;

    $url = $waziperLink . "/api/send?number=$mobileNo&type=media&message=$message&media_url=$banner&instance_id=$instanceId&access_token=$tokenId";

    // Retry 2 times if failed
    for ($i = 0; $i < 2; $i++) {
        $messageId = urlExecuter($url);
        if (!empty($messageId)) return true;
    }
    return false;
}

// CURL Executor
function urlExecuter($url)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);

    $response = curl_exec($ch);
    $error = curl_error($ch);
    curl_close($ch);

    if ($response === false || empty($response)) {
        return null;
    }

    $responseArray = json_decode($response, true);
    return $responseArray['message']['key']['id'] ?? null;
}
?>


<!doctype html>
<html class="no-js" lang="zxx">
    
<!-- index28:48-->
<head>
        <meta charset="utf-8">
        <meta http-equiv="x-ua-compatible" content="ie=edge">
        <title>Store | Genius Devel</title>
        <meta name="description" content="">
        <!--<meta name="viewport" content="width=device-width, initial-scale=1">-->
        <meta name="viewport" content="width=1024">
      <!------------favicon-------->
        <link rel="icon" type="image/x-icon" href="myImages/favicons/geniusDevelFavicon.ico">
      
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
  
    <style>
          /* Width of the scrollbar */
        ::-webkit-scrollbar {
            width: 10px;
            
        }
        
        /* Track (the area behind the thumb) */
        ::-webkit-scrollbar-track {
            background: #f1f1f1;
            
        }
        
        /* Handle (the draggable part of the scrollbar) */
        ::-webkit-scrollbar-thumb {
            background: #888;
            border-radius:3px;
        }
        
        /* Handle on hover */
        ::-webkit-scrollbar-thumb:hover {
            background: #E2C103;
        }   
        
      
    .serialCircle{
        color:white;
        font-size:12px;
        border-radius:0%;
        background:black;
        padding:2px 5px 2px;
        margin-left:5px;
        box-shadow:3px 3px 3px #FEDF33;
        
    }
    
     .blink {
      animation: blink-animation 1s steps(2, start) infinite;
    }

    @keyframes blink-animation {
      50% {
        visibility: hidden;
      }
    }
    
</style>



        
    </head>

<style>
    input::placeholder {
    color: rgba(0, 0, 0, 0.2); /* Adjust the color and opacity */
}
</style>


<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">




<div class="formbold-main-wrapper" style='display:none;'>
    
  <div class="formbold-form-wrapper" style="display:block;box-shadow:7px 5px 15px grey;">

    
    
    
  </div>
</div>










<!------------- Program download form ------------->
<div  class="formbold-main-wrapper">
    
  <div class="formbold-form-wrapper" style="display:block;box-shadow:7px 5px 15px grey;">
    
    
    
    
    
    
    
   <center><div style="width:100px;height:100px;">
        <!--<img src="myImages/partners/geniusDevelUserImage.jpg" style="width:100px;height:100px;border-radius:50%;border:2px solid grey;">-->
        <img src="https://store.geniusdevel.com/myImages/programImages/13.jpg" style="width:100px;height:100px;border-radius:50%;border:2px solid grey;">
   </div></center>


      <div class="formbold-form-title">
        <h2 class="">Welcome!</h2>
        <p style='font-size:13px;color:red;'>
          <b style='color:black;'>Note : </b>Fill Detail and generate your trial key.
        </p>
      </div>
<form action = '' method = "POST">
      <div class="formbold-input-flex">
        <div>
          <label for="firstname" class="formbold-form-label">
            First name
          </label>
          <input
            type="text"
            name="firstName"
            id="firstName"
            class="formbold-form-input"
            style='text-align:left;'
          required/>
        </div>
        <div>
          <label for="lastName" class="formbold-form-label"> Last name </label>
          <input
            type="text"
            name="lastName"
            id="lastname"
            class="formbold-form-input"
            style='text-align:left;'
          / >
        </div>
      </div>
      
      <br>

      <div class="formbold-input-flex fluid">
        <div>
          <label for="mobileNumber" class="formbold-form-label"> Mobile Number </label>
          <input
            type="text"
            name="mobileNumber"
            id="wnumber"
            class="formbold-form-input"
            style='text-align:left;'
            placeholder="With country code"
          required/>
          <small style='color:red;font-style:italic;font-size:10px;letter-spacing:2px;'>Example : 919536377940</small>
        </div>
      </div>
        
   
      
      

      <button type="submit" class="formbold-btn btn-warning" style="color:black;background:#FCD00D;font-weight:bold;" name="submit" id='generate-license'><i class='fa fa-key'>&nbsp;</i>Generate Key</button>
      
      </form>
      <br>

      <div id="response-message" style='margin-top:30px;'>
         <?= $message ; ?>
      </div>
    
  </div>
</div>


<script>
        document.getElementById("copy_btn").addEventListener("click", function() {
            // Span ka content lena
            let text = document.getElementById("key").innerText;
            
            // Clipboard mein copy karne ke liye navigator API ka use
            navigator.clipboard.writeText(text).then(function() {
                alert("License Key copied!");
            }).catch(function(error) {
                console.error("Failed to copy text: ", error);
            });
        });
    </script>





<style>
  @import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap');
  * {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
  }
  body {
    font-family: 'Inter', sans-serif;
    background:#FCDD31;
   
  }
  .formbold-mb-3 {
    margin-bottom: 15px;
  }
  
    .success{
      margin-top:30px;
      width:500px;background:#04d88a;color:black;
      border-radius:15px;
      padding:5px 10px;
      box-shadow:3px 3px 2px 3px grey;
  }
  
  .error{
      margin-top:30px;
      width:500px;background:red;color:white;
      border-radius:15px;
      padding:5px 10px;
      box-shadow:3px 3px 2px 3px grey;
  }
  
  
  .formbold-relative {
    position: relative;
  }
  .formbold-opacity-0 {
    opacity: 0;
  }
  .formbold-stroke-current {
    stroke: currentColor;
  }
  #supportCheckbox:checked ~ div span {
    opacity: 1;
  }
  
  input{
      text-align:left;
  }
  .downloadArea{
      width:400px;
      height:200px;
      background:;
      color:white;
      margin-bottom:50px;
  }
  
  

  .formbold-main-wrapper {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 48px;
  }

  .formbold-form-wrapper {
    margin: 0 auto;
    max-width: 570px;
    width: 100%;
    background: white;
    padding: 40px;
  }

  .formbold-img {
    margin-bottom: 45px;
  }

  .formbold-form-title {
    margin-bottom: 30px;
  }
  .formbold-form-title h2 {
    font-weight: 600;
    font-size: 28px;
    line-height: 34px;
    color: #07074d;
  }
  .formbold-form-title p {
    font-size: 16px;
    line-height: 24px;
    color: #536387;
    margin-top: 12px;
  }

  .formbold-input-flex {
    display: flex;
    gap: 20px;
    margin-bottom: 15px;
  }
  .formbold-input-flex > div {
    width: 50%;
  }
  .formbold-form-input {
    text-align: center;
    width: 100%;
    padding: 13px 22px;
    border-radius: 5px;
    border: 1px solid #dde3ec;
    background: #ffffff;
    font-weight: 500;
    font-size: 16px;
    color: #536387;
    outline: none;
    resize: none;
  }
  .formbold-form-input:focus {
    border-color: #6a64f1;
    box-shadow: 0px 3px 8px rgba(0, 0, 0, 0.05);
  }
  .formbold-form-label {
    color: #536387;
    font-size: 14px;
    line-height: 24px;
    display: block;
    margin-bottom: 10px;
  }

  .formbold-checkbox-label {
    display: flex;
    cursor: pointer;
    user-select: none;
    font-size: 16px;
    line-height: 24px;
    color: #536387;
  }
  .formbold-checkbox-label a {
    margin-left: 5px;
    color: #6a64f1;
  }
  .formbold-input-checkbox {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border-width: 0;
  }
  .formbold-checkbox-inner {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 20px;
    height: 20px;
    margin-right: 16px;
    margin-top: 2px;
    border: 0.7px solid #dde3ec;
    border-radius: 3px;
  }

  .formbold-btn {
    font-size: 16px;
    border-radius: 5px;
    padding: 14px 25px;
    border: none;
    font-weight: 500;
    background-color: #6a64f1;
    color: white;
    cursor: pointer;
    margin-top: 25px;
  }
  .formbold-btn:hover {
    box-shadow: 0px 3px 8px rgba(0, 0, 0, 0.05);
  }
  
  /* Footer com referência ao criador */
  .creator-footer {
    text-align: center;
    padding: 20px;
    background-color: #f8f9fa;
    border-top: 1px solid #dee2e6;
    margin-top: 50px;
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    z-index: 1000;
  }
  
  .creator-footer p {
    margin: 0;
    color: #6c757d;
    font-size: 14px;
  }
  
  .creator-footer a {
    color: #007bff;
    text-decoration: none;
  }
  
  .creator-footer a:hover {
    text-decoration: underline;
  }
</style>

<!-- Footer com referência ao criador -->
<div class="creator-footer">
    <p>
        <a href="https://digitalavance.com.br/" target="_blank">Desenvolvido por Digital Avance</a> | 
        <a href="https://wa.me/5581995511599" target="_blank">Contato: (81) 99551-1599</a>
    </p>
</div>