<?php
require_once '../connection/db.php';
redirectIfLoggedIn();
$pageTitle = 'Login';
$err = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  $wallet = trim($_POST['wallet'] ?? '');
  if (!$wallet) {
    $err = 'Please enter your TRX wallet address.';
  } else {
    $st = getDB()->prepare('SELECT member_id,user_id,walletAddress,topup_flag,account_status FROM meddolic_user_details WHERE walletAddress=? LIMIT 1');
    $st->execute([$wallet]);
    $user = $st->fetch();
    if (!$user) {
      $err = 'Wallet not found. Please register first.';
    } elseif ((int) $user['account_status'] === 0) {
      $err = 'Your account has been terminated. Contact support.';
    } elseif ((int) $user['topup_flag'] === 0) {
      $err = 'Account not activated yet. Complete registration payment.';
    } else {
      $_SESSION['member_id'] = $user['member_id'];
      $_SESSION['user_id'] = $user['user_id'];
      header('Location: ../User/Dashboard.php');
      exit;
    }
  }
}
?>
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width,initial-scale=1">
  <title>Login | Hissho Global</title>
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
  <link href="https://fonts.googleapis.com/css2?family=Orbitron:wght@700;900&family=Inter:wght@400;500;700&display=swap"
    rel="stylesheet">
  <link rel="shortcut icon" href="../User/assets/img/favicon.png" type="image/x-icon">
  <style>
    * {
      box-sizing: border-box;
      margin: 0;
      padding: 0
    }

    body {
      background: #0a0b0d;
      color: #e8e8e8;
      font-family: 'Inter', sans-serif;
      min-height: 100vh;
      display: flex;
      align-items: center;
      justify-content: center;
      padding: 20px;
      background-image: radial-gradient(ellipse 60% 50% at 50% 0%, rgba(241, 145, 7, .08) 0%, transparent 70%)
    }

    .card {
      background: #1a1d22;
      border: 1px solid rgba(241, 145, 7, .15);
      border-radius: 16px;
      padding: 44px 40px;
      width: 100%;
      max-width: 420px;
      box-shadow: 0 24px 64px rgba(0, 0, 0, .6)
    }

    .logo {
      text-align: center;
      margin-bottom: 30px
    }

    .logo-icon {
      width: 64px;
      height: 64px;
      border-radius: 50%;
      display: flex;
      align-items: center;
      justify-content: center;
      font-size: 28px;
      color: #000;
      margin: 0 auto 12px;
    }

    .logo-icon img {
      width: 180%;
    }

    .logo h1 {
      font-family: 'Orbitron', sans-serif;
      font-size: 22px;
      font-weight: 900;
      color: #f19107;
      letter-spacing: 3px
    }

    .logo p {
      font-size: 11px;
      letter-spacing: 2px;
      text-transform: uppercase;
      color: #555;
      margin-top: 4px
    }

    .form-group {
      margin-bottom: 18px
    }

    .form-group label {
      display: block;
      font-size: 12px;
      font-weight: 600;
      letter-spacing: .8px;
      text-transform: uppercase;
      color: #888;
      margin-bottom: 7px
    }

    .input-wrap {
      position: relative
    }

    .input-wrap i {
      position: absolute;
      left: 13px;
      top: 50%;
      transform: translateY(-50%);
      color: #555
    }

    .form-control {
      width: 100%;
      background: #111214;
      border: 1px solid rgba(241, 145, 7, .12);
      border-radius: 8px;
      padding: 11px 14px 11px 38px;
      color: #e8e8e8;
      font-size: 14px;
      outline: none;
      transition: .15s
    }

    .form-control:focus {
      border-color: rgba(241, 145, 7, .4);
      box-shadow: 0 0 0 3px rgba(241, 145, 7, .07)
    }

    .form-control::placeholder {
      color: #333
    }

    .btn {
      display: block;
      width: 100%;
      padding: 14px;
      background: linear-gradient(135deg, #f19107, #c97800);
      color: #000;
      border: none;
      border-radius: 10px;
      font-size: 13px;
      font-weight: 800;
      letter-spacing: 1.5px;
      text-transform: uppercase;
      cursor: pointer;
      font-family: 'Orbitron', sans-serif;
      transition: .2s
    }

    .btn:hover {
      transform: translateY(-1px);
      box-shadow: 0 8px 24px rgba(241, 145, 7, .35)
    }

    .err {
      background: rgba(239, 68, 68, .1);
      border: 1px solid rgba(239, 68, 68, .25);
      color: #ef4444;
      border-radius: 8px;
      padding: 12px 16px;
      margin-bottom: 16px;
      font-size: 13px
    }

    .detect-btn {
      background: rgba(241, 145, 7, .08);
      border: 1px solid rgba(241, 145, 7, .15);
      color: #f19107;
      padding: 8px 14px;
      border-radius: 7px;
      font-size: 12px;
      cursor: pointer;
      margin-top: 8px;
      width: 100%;
      transition: .15s
    }

    .detect-btn:hover {
      background: rgba(241, 145, 7, .15)
    }

    .links {
      text-align: center;
      margin-top: 20px;
      font-size: 13px;
      color: #555
    }

    .links a {
      color: #f19107;
      font-weight: 700
    }
  </style>
</head>

<body>
  <div class="card">
    <div class="logo">
      <div class="logo-icon"><img src="../User/assets/img/logo.png" alt=""></div>
      <h1>Hissho Global</h1>
      <p>Member Login</p>
    </div>

    <?php if ($err): ?>
      <div class="err"><i class="fas fa-exclamation-circle"></i> <?= e($err) ?></div>
    <?php endif; ?>

    <form method="POST">
      <div class="form-group">
        <label>TRX Wallet Address</label>
        <div class="input-wrap">
          <i class="fas fa-wallet"></i>
          <input type="text" name="wallet" id="walletInput" class="form-control"
            placeholder="Your TronLink wallet address" value="<?= e($_POST['wallet'] ?? '') ?>" required>
        </div>
        <button type="button" class="detect-btn" onclick="detectWallet()">
          <i class="fas fa-plug"></i> Auto-Detect TronLink
        </button>
      </div>

      <button type="submit" class="btn">
        <i class="fas fa-sign-in-alt"></i> LOGIN
      </button>
    </form>

    <div class="links">
      New here? <a href="Register.php">Create Account</a>
    </div>
  </div>

  <script>
    async function detectWallet() {
      if (typeof window.tronLink === 'undefined' && typeof window.tronWeb === 'undefined') {
        alert('TronLink not detected. Please install TronLink extension.'); return;
      }
      try {
        let addr = '';
        if (window.tronLink?.ready) addr = window.tronLink.tronWeb?.defaultAddress?.base58 || '';
        else if (window.tronWeb?.defaultAddress?.base58) addr = window.tronWeb.defaultAddress.base58;
        if (!addr && window.tronLink) {
          await window.tronLink.request({ method: 'tron_requestAccounts' });
          addr = window.tronLink.tronWeb?.defaultAddress?.base58 || '';
        }
        if (addr) document.getElementById('walletInput').value = addr;
        else alert('Could not detect wallet. Please unlock TronLink and try again.');
      } catch (e) { alert('Error: ' + e.message); }
    }
    window.addEventListener('load', () => setTimeout(detectWallet, 1000));
  </script>
</body>

</html>
