<?php 
require_once('../install/core_init.php');
$installFile = "rental.license";

if (is_file($installFile)) {
  $api = new LicenseBoxExternalAPI(); 

  $errors = false;
  $today = date('Y-m-d');
  $csrf_token_rand = substr(str_shuffle(MD5(microtime())), 0, 10);
  $csrf_cookie_rand = substr(str_shuffle(MD5(microtime())), 0, 10);
  $session_rand = substr(str_shuffle(MD5(microtime())), 0, 10);
  $encrypt_rand = substr(str_shuffle(MD5(microtime())), 0, 20);
  $htaccess_file = '../.htaccess';

  @chmod($installFile,0777);
  @chmod($config_file,0777);
  @chmod($config_file_sample,0777);
  $step = isset($_GET['step']) ? $_GET['step'] : '';
  ?>

<div class="container main_body"> 
<div class="section">
  <div class="column is-three-fifths is-offset-one-fifth">
<center>
  <img class="p-t-lg m-t-sm" src="/install/assets/images/RentalpanelShop.png" width="200" alt="TWG OFFICIAL" style="margin-bottom: 15px;">
  <h2 class="title is-3 p-t-md p-b-md" style="margin: 0; font-weight: bold; color: #333; font-family: 'Courier New', monospace;">
    Welcome to the Installation !<br> <span style="color: #007BFF;">SCRIPT BY TWG - SMMGONE OFFICIAL.</span>
  </h2>
</center>

  </div>
  <div class="column is-6 is-offset-3">
    <?php 
    if($step==''){
      $update_data = $api->get_latest_version();
      if(!empty($update_data['latest_version'])&&(version_compare($update_data['latest_version'],  $api->get_current_version())>0)){ ?>
      <center><article class="message is-warning">
      <div class="message-body">
       New LicenseBox version <?php echo $update_data['latest_version']; ?> available, <a href="https://codecanyon.net/downloads" target="_blank" rel="noopener noreferrer">download now</a>!
      </div>
    </article></center><br>
    <?php }
    } ?>
    <div class="box"><?php
 switch ($step) {
        default: ?>  
  <div class="tabs is-fullwidth">
    <ul>
      <li class="is-active">
        <a>
          <span><b>Requirements</b></span>
        </a>
      </li>
      <li>
        <a>
          <span>Verify</span>
        </a>
      </li>

      <li>
        <a>
          <span>Finish!</span>
        </a>
      </li>
    </ul>
  </div>                                                      
<?php 
if(version_compare(PHP_VERSION, '5.6.0')<0){
$errors = true;
  echo "<div class='notification is-danger' style='padding:12px;'><i class='fa fa-times p-r-xs'></i> Current PHP version is ".phpversion()."! minimum PHP 5.6 or higher required.</div>";
}else{
  echo "<div class='notification is-success' style='padding:12px;'><i class='fa fa-check p-r-xs'></i> You are running PHP version ".phpversion()."</div>";
}
if(!extension_loaded('pdo')){
  $errors = true; 
  echo "<div class='notification is-danger' style='padding:12px;'><i class='fa fa-times p-r-xs'></i> PDO PHP extension is missing!</div>";
}else{
  echo "<div class='notification is-success' style='padding:12px;'><i class='fa fa-check p-r-xs'></i> PDO PHP extension is available.</div>";
}
if(!extension_loaded('openssl')){
  $errors = true; 
  echo "<div class='notification is-danger' style='padding:12px;'><i class='fa fa-times p-r-xs'></i> Openssl PHP extension is missing!</div>";
}else{
  echo "<div class='notification is-success' style='padding:12px;'><i class='fa fa-check p-r-xs'></i> Openssl PHP extension is available.</div>";
}
if(!extension_loaded('curl')){
  $errors = true; 
  echo "<div class='notification is-danger' style='padding:12px;'><i class='fa fa-times p-r-xs'></i> Curl PHP extension is missing!</div>";
}else{
  echo "<div class='notification is-success' style='padding:12px;'><i class='fa fa-check p-r-xs'></i> Curl PHP extension is available.</div>";
}
if(!extension_loaded('zip')||!class_exists('ZipArchive')){
  $errors = true; 
  echo "<div class='notification is-danger' style='padding:12px;'><i class='fa fa-times p-r-xs'></i> Zip PHP extension is missing!</div>";
}else{
  echo "<div class='notification is-success' style='padding:12px;'><i class='fa fa-check p-r-xs'></i> Zip PHP extension is available.</div>";
}

if(!is_writeable($installFile)){
  $errors = true; 
  echo "<div class='notification is-danger' style='padding:12px;'><i class='fa fa-times p-r-xs'></i> Installation file (".$installFile.") is not writable!</div>";
}

if(!file_exists($htaccess_file)){
  $errors = true; 
  echo "<div class='notification is-danger' style='padding:12px;'><i class='fa fa-times p-r-xs'></i> .htaccess file (".$htaccess_file.") is missing!</div>";
}
?>
<div class="columns is-mobile is-centered is-vcentered">
  <div class="column">
    <p class='help has-text-grey p-t-xs has-text-centered'>By clicking "Next", you agree to our <a class="has-text-grey" href="https://www.rentalpanel.shop">Terms & Privacy Policy</a>.</p>
  </div>
  <div class="column is-narrow">
    <div style='text-align: right;'>
      <?php if($errors==true){ ?>
        <a href="#" class="button is-link" disabled>Next</a>
      <?php }else{ ?>
        <a href="index.php?step=0" class="button is-link">Next</a>
      <?php } ?>
    </div>
  </div>
</div>                 
<?php
break;
case "0": 
?>
<div class="tabs is-fullwidth">
  <ul>
    <li>
      <a>
        <span><i class="fas fa-check-circle"></i> Requirements</span>
      </a>
    </li>
    <li class="is-active">
      <a>
        <span><b>Verify</b></span>
      </a>
    </li>

    <li>
      <a>
        <span>Finish!</span>
      </a>
    </li>
  </ul>
</div>   
<?php
    $license_code = null;
    $client_name = null;
if(!empty($_POST['license'])&&!empty($_POST['client'])&&!empty($_POST['email'])){
    $license_code = strip_tags(trim((string) $_POST["license"]));
    $client_name = strip_tags(trim((string) $_POST["client"]));

    $verify_response = $api->activate_license($license_code, $client_name, strip_tags(trim((string) $_POST['email'])));

    if(empty($verify_response)){
     $msg = 'Server is unavailable at the moment, please try again.';
    }else{
      $msg = $verify_response['message'];
    }

    if ($verify_response['status'] != true) { ?>
   <form action="index.php?step=0" method="POST">
   <div class="notification is-danger"><?php echo ucfirst($msg); ?></div>
  <div class="field">
    <label class="label">licenses code <small class="has-text-weight-normal has-text-grey"> </small></label>
    <div class="control">
      <input class="input" type="text" placeholder="Enter your licenses code" name="license" required>
    </div>
  </div>
  <div class="field">
    <label class="label"> username</label>
    <div class="control">
      <input class="input" type="text" placeholder="Enter your  username" name="client" required>
    </div>
  </div>
  <div class="field">
    <label class="label">Email address <small class="has-text-weight-normal has-text-grey"> (It will be used for sending password reset emails.)</small></label>
    <div class="control">
      <input class="input" type="email" placeholder="Enter your email address" name="email" required>
    </div>
  </div>
  <div style='text-align: right;'>
  <button type="submit" class="button is-link">Verify</button>
</div>
</form><?php 
 } else { 
?>
  <form action="index.php?step=1" method="POST">
    <div class="notification is-success"><?php echo ucfirst($msg); ?></div>
    <input type="hidden" name="user_email" id="user_email" value="<?php echo strip_tags(trim($_POST['email'])); ?>">
    <input type="hidden" name="prc3" id="prc3" value="<?php echo ucfirst($verify_response['status']); ?>">
    <input type="hidden" name="sql_data" id="sql_data" value="<?php echo $verify_response['data']; ?>">
    <div class="columns is-mobile is-centered is-vcentered">
      <div class="column">
        <p class='p-t-xs has-text-centered'>Click next to proceed with the installation.</p>
      </div>
      <div class="column is-narrow">
        <div style='text-align: right;'>
          <button type="submit" class="button is-link">Next</button>
        </div>
      </div>
    </div>
  </form> 
<?php
  } 
}else{  ?>
  <form action="index.php?step=0" method="POST">
    <div class="field">
      <label class="label">licenses code <small class="has-text-weight-normal has-text-grey"> </small></label>
      <div class="control">
        <input class="input" type="text" placeholder="Enter your licenses code" name="license" required>
      </div>
    </div>
    <div class="field">
      <label class="label">username</label>
      <div class="control">
        <input class="input" type="text" placeholder="Enter your envato username" name="client" required>
      </div>
    </div>
    <div class="field">
      <label class="label">Email address <small class="has-text-weight-normal has-text-grey"> </small></label>
      <div class="control">
        <input class="input" type="email" placeholder="Enter your email address" name="email" required>
      </div>
    </div>
    <div style='text-align: right;'>
      <button type="submit" class="button is-link">Verify</button>
    </div>
  </form>
<?php } ?>
<?php
  break;
  case "1":
  ?>
<div class="tabs is-fullwidth">
  <ul>
    <li>
      <a>
        <span><i class="fas fa-check-circle"></i> Requirements</span>
      </a>
    </li>
    <li>
      <a>
        <span><i class="fas fa-check-circle"></i> Verify</span>
      </a>
    </li>

    <li>
      <a>
        <span>Finish!</span>
      </a>
    </li>
  </ul>
</div> 
<?php
$user_email = null;

if ($_POST && (isset($_POST["prc3"])) && !empty($_POST["user_email"])) {
    $valid = $_POST["prc3"];
    $user_email = strip_tags(trim((string) $_POST["user_email"]));

    if (!empty($user_email)) {
        // Assume successful validation and skip database-related logic
        ?>
        <form action="index.php?step=2" method="POST">
            <div class='notification is-success'>installation has been successfully configured.</div>
            <input type="hidden" name="prc4" id="prc4" value="true">
            <div class="columns is-mobile is-centered is-vcentered">
                <div class="column">
                    <p class='p-t-xs has-text-centered'>Click next to complete the installation.</p>
                </div>
                <div class="column is-narrow">
                    <div style='text-align: right;'>
                        <button type="submit" class="button is-link">Next</button>
                    </div>
                </div>
            </div>
        </form>
        <?php
    } else {
        ?>
        <div class='notification is-danger'>Please provide a valid email address.</div>
        <form action="index.php?step=1" method="POST">
            <input type="hidden" name="prc3" id="prc3" value="<?php echo $valid; ?>">
            <div class="field">
                <label class="label">User Email</label>
                <div class="control">
                    <input class="input" type="email" id="user_email" placeholder="Your email" name="user_email" required>
                </div>
            </div>
            <div class="columns is-mobile is-centered is-vcentered">
                <div class="column">
                    <p class='help has-text-grey has-text-centered p-t-xs'>Please provide a valid email address.</p>
                </div>
                <div class="column is-narrow">
                    <div style='text-align: right;'>
                        <button type="submit" class="button is-link">Submit</button>
                    </div>
                </div>
            </div>
        </form>
        <?php
    }
} else {
    ?>
    <div class='notification is-danger'>Sorry, something went wrong. Please re-run the installation.</div>
    <?php
}

?>
<?php
break;
case "2":
?>    
<div class="tabs is-fullwidth">
    <ul>
        <li>
            <a>
                <span><i class="fas fa-check-circle"></i> Requirements</span>
            </a>
        </li>
        <li>
            <a>
                <span><i class="fas fa-check-circle"></i> Verify</span>
            </a>
        </li>

        <li class="is-active">
            <a>    
                <span><b>Finish!</b></span>
            </a>
        </li>
    </ul>
</div>
<?php 
if ($_POST && (isset($_POST["prc4"]))) {
    $valid = $_POST["prc4"];
    if (is_writeable($installFile)) {
        unlink($installFile);
    } else { ?>
        <div class='notification is-danger'>Installation file was not removed, you should delete the (<strong><?php echo $installFile; ?></strong>) file to lock the installer.</div>
    <?php }?>
    <center>
    <p><strong>License has been successfully installed.</strong></p><br>
    <p>You can now log in using your email or username: <strong>admin</strong> and default password: <strong>smmgone10k#</strong></p><br>
    <strong>
        <p><a class='button is-link' href='/admin'>Login</a></p>
    </strong><br>
    <p class='help has-text-grey'>The first thing you should do is change your account details to secure your system.</p>
    <p class='help has-text-danger'>Please note: It is your responsibility to change the default password immediately after logging in to prevent unauthorized access.</p>
</center>
<?php }
else { ?>
<div class='notification is-danger'>Sorry, something went wrong. Please re-run the installation.</div>
<?php } ?>
<?php
break;}
?>    
</div>

</div>
</div> 
</div>
<?php } else{ ?>
<div class="container main_body"> 
    <div class="section text-center">
        <h1 class="title p-t-xxl m-t-xl text-danger stylish-text">
            🚀 Script Installer is Locked!  
        </h1>
        <p class="text-muted">
            Please check the documentation or contact support for assistance.
        </p>
        <a href="https://wa.me/8801864614704" target="_blank" class="wp-button">
             Contect on WhatsApp
        </a>
    </div>
</div>


<style>
/* Custom Styling */
.stylish-text {
    font-size: 2.2rem;
    font-weight: bold;
    color: #d9534f;
    text-transform: uppercase;
    text-shadow: 2px 2px 8px rgba(0, 0, 0, 0.2);
}

.wp-button {
    display: inline-block;
    background: #25d366;
    color: white;
    padding: 15px 25px;
    font-size: 18px;
    font-weight: bold;
    border-radius: 50px;
    text-decoration: none;
    transition: 0.3s ease-in-out;
    box-shadow: 0 4px 10px rgba(0, 0, 0, 0.2);
    position: relative;
    overflow: hidden;
}

/* WhatsApp Icon Animation */
.wp-button i {
    margin-right: 8px;
    animation: shake 1.5s infinite;
}

/* Hover Effects */
.wp-button:hover {
    background: #1ebe57;
    box-shadow: 0 6px 15px rgba(0, 0, 0, 0.3);
}

/* Keyframe Animation for Icon */
@keyframes shake {
    0% { transform: rotate(0deg); }
    20% { transform: rotate(-10deg); }
    40% { transform: rotate(10deg); }
    60% { transform: rotate(-10deg); }
    80% { transform: rotate(10deg); }
    100% { transform: rotate(0deg); }
}
</style>

<?php } ?>
