<?php
require_once __DIR__ . '/includes/initialize.php';
if (is_logged_in()) redirect('/adminconsole/index.php');
$err='';
if ($_SERVER['REQUEST_METHOD']==='POST'){
  $login_id=trim($_POST['login_id']??''); $pw=strval($_POST['password']??''); $tok=$_POST['csrf']??'';
  if(!verify_csrf($tok)){ $err='Invalid request. Please refresh and try again.'; }
  elseif($login_id===''||$pw===''){ $err='Please enter login and password.'; }
  else{
    $col=(strpos($login_id,'@')!==false)?'email':'mobile_no';
    $sql="SELECT id,name,email,mobile_no,password_hash,role_id,status FROM jos_admin_users WHERE status=1 AND $col=? LIMIT 1";
    if($st=$con->prepare($sql)){
      $st->bind_param('s',$login_id); $st->execute(); $u=$st->get_result()->fetch_assoc(); $st->close();
      if($u && password_verify($pw,$u['password_hash'])){
        session_regenerate_id(true); $_SESSION['admin_user']=$u; redirect($_GET['next']??'/adminconsole/index.php');
      } else $err='Invalid credentials.';
    } else $err='Server error. Try again.';
  }
}
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Admin Login</title>
<meta name="viewport" content="width=device-width,initial-scale=1">
<link rel="icon" type="image/jpeg" href="/adminconsole/uploads/logo.jpeg">
<style>
  :root{
    --primary: #2F4F88;
    --primary-dark: #233B68;
    --primary-hover: #3E5F9E;
    --bg-white: #FFFFFF;
    --text-dark: #111827;
    --text-secondary: #4B5563;
    --border: #E5E7EB;
    --danger: #EF4444;
    --shadow: 0 4px 20px rgba(0,0,0,0.08);
    --radius: 16px;
  }
  *{box-sizing:border-box}
  body{
    min-height:100vh;
    margin:0;
    display:flex;
    align-items:center;
    justify-content:center;
    padding:24px;
    background:var(--bg-white);
    color:var(--text-dark);
    font-family:system-ui,-apple-system,Segoe UI,Roboto,Arial,sans-serif;
  }
  .login-shell{
    width:100%;
    max-width:980px;
    min-height:560px;
    display:grid;
    grid-template-columns:minmax(280px,.95fr) minmax(320px,1.05fr);
    overflow:hidden;
    border:1px solid var(--border);
    border-radius:24px;
    background:var(--bg-white);
    box-shadow:var(--shadow);
  }
  .login-hero{
    display:flex;
    flex-direction:column;
    justify-content:space-between;
    padding:34px;
    color:#fff;
    background:linear-gradient(145deg,var(--primary-dark),var(--primary));
  }
  .login-hero h2{margin:0;font-size:34px;line-height:1.12;letter-spacing:0}
  .login-hero p{margin:14px 0 0;color:rgba(255,255,255,.82);line-height:1.6}
  .hero-badge{
    align-self:flex-start;
    padding:8px 12px;
    border-radius:999px;
    background:rgba(255,255,255,.16);
    font-weight:700;
    font-size:13px;
  }
  .card{
    width:100%;
    max-width:460px;
    margin:auto;
    padding:34px;
    background:var(--bg-white);
  }
  .brand{display:flex;align-items:center;justify-content:center;margin-bottom:18px}
  .brand img{max-width:220px;height:auto;display:block}
  h1.title{margin:0 0 6px;text-align:center;font-size:28px;color:var(--text-dark)}
  .subtitle{margin:0 0 24px;text-align:center;color:var(--text-secondary);font-size:14px}
  label{display:block;font-size:14px;margin:14px 0 7px;color:var(--text-secondary);font-weight:700}
  .control{
    width:100%;
    display:block;
    padding:13px 14px;
    border-radius:12px;
    border:1px solid var(--border);
    background:var(--bg-white);
    color:var(--text-dark);
    outline:none;
    transition:border-color .18s ease,box-shadow .18s ease;
  }
  .control:focus{border-color:var(--primary);box-shadow:0 0 0 3px rgba(47,79,136,.14)}
  .btn{
    width:100%;
    display:inline-block;
    text-align:center;
    padding:13px 14px;
    border-radius:12px;
    border:0;
    background:var(--primary);
    color:#fff;
    font-weight:800;
    margin-top:18px;
    cursor:pointer;
    transition:background .18s ease,transform .18s ease,box-shadow .18s ease;
  }
  .btn:hover{background:var(--primary-hover);transform:translateY(-1px);box-shadow:0 8px 18px rgba(47,79,136,.22)}
  .err{background:#FEE2E2;color:#991B1B;padding:11px 12px;border-radius:12px;margin:14px 0;text-align:center;border:1px solid #FECACA}
  @media(max-width:760px){
    body{padding:14px}
    .login-shell{display:block;min-height:0;border-radius:20px}
    .login-hero{padding:24px;gap:42px}
    .login-hero h2{font-size:26px}
    .card{padding:26px 22px}
  }
</style>
</head>
<body>
  <main class="login-shell">
    <section class="login-hero" aria-label="Admin portal">
      <div class="hero-badge">Pacific iConnect Admin</div>
      <div>
        <h2>Professional dashboard access</h2>
        <p>Sign in to manage operations, reports, teams, and account workflows from one secure console.</p>
      </div>
    </section>

    <div class="card">
      <div class="brand">
        <img
          src="/adminconsole/uploads/logo.jpeg?v=<?php echo time()%1000; ?>"
          onerror="this.onerror=null;this.src='/adminconsole/uploads/logo.png?v=<?php echo time()%1000; ?>';"
          alt="Pacific iConnect Logo">
      </div>

      <h1 class="title">Sign in</h1>
      <p class="subtitle">Use your email or mobile number to continue.</p>

      <?php if($err): ?><div class="err"><?=htmlspecialchars($err)?></div><?php endif; ?>

      <form method="post" autocomplete="off">
        <input type="hidden" name="csrf" value="<?=htmlspecialchars(csrf_token())?>">

        <label for="login_id">Email or Mobile</label>
        <input id="login_id" name="login_id" class="control" value="<?=htmlspecialchars($_POST['login_id']??'')?>" required>

        <label for="password">Password</label>
        <input id="password" name="password" type="password" class="control" required>

        <button class="btn" type="submit">Login</button>
      </form>

    </div>
  </main>
</body>
</html>
