<?php
session_start();
require 'db/config.php';
$db = db();

if (isset($_SESSION['user_id'])) {
    header('Location: dashboard.php');
    exit;
}

$workers = $db->query("SELECT * FROM users WHERE role = 'worker'")->fetchAll();
$admins = $db->query("SELECT * FROM users WHERE role = 'admin'")->fetchAll();
$isSetup = isset($_GET['setup']) && isset($_SESSION['pending_setup_user_id']);
?>
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <title>M-TRACK | Login</title>
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.0/font/bootstrap-icons.css">
  <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">
  <style>
    :root {
      --bg: #f8fafc;
      --card-bg: #ffffff;
      --primary: #1e293b;
      --accent: #3b82f6;
      --text: #334155;
      --border: #e2e8f0;
    }
    body {
      font-family: 'Inter', system-ui, -apple-system, sans-serif;
      background-color: var(--bg);
      color: var(--text);
      display: flex;
      align-items: center;
      justify-content: center;
      min-height: 100vh;
      margin: 0;
    }
    .login-card {
      width: 100%;
      max-width: 400px;
      padding: 2.5rem;
      background: var(--card-bg);
      border: 1px solid var(--border);
      border-radius: 4px;
      box-shadow: 0 4px 6px -1px rgb(0 0 0 / 0.1);
    }
    .login-header {
      text-align: center;
      margin-bottom: 2rem;
    }
    .login-header h1 {
      font-weight: 700;
      font-size: 1.5rem;
      color: var(--primary);
      letter-spacing: -0.025em;
      margin-bottom: 0.25rem;
    }
    .login-header p {
      font-size: 0.875rem;
      color: #64748b;
    }
    .nav-tabs {
      border-bottom: 2px solid var(--border);
      margin-bottom: 1.5rem;
    }
    .nav-link {
      color: #64748b;
      font-weight: 500;
      border: none !important;
      padding: 0.75rem 0;
      margin-right: 1.5rem;
      position: relative;
    }
    .nav-link.active {
      color: var(--accent) !important;
      background: none !important;
    }
    .nav-link.active::after {
      content: '';
      position: absolute;
      bottom: -2px;
      left: 0;
      width: 100%;
      height: 2px;
      background-color: var(--accent);
    }
    .form-label {
      font-weight: 500;
      font-size: 0.875rem;
      color: var(--primary);
      margin-bottom: 0.5rem;
    }
    .form-select, .form-control {
      border-radius: 4px;
      border-color: var(--border);
      padding: 0.625rem;
      font-size: 0.875rem;
    }
    .form-select:focus, .form-control:focus {
      border-color: var(--accent);
      box-shadow: 0 0 0 2px rgba(59, 130, 246, 0.1);
    }
    .btn-primary {
      background-color: var(--primary);
      border: none;
      padding: 0.625rem;
      font-weight: 600;
      font-size: 0.875rem;
      border-radius: 4px;
      width: 100%;
      margin-top: 1rem;
    }
    .btn-primary:hover {
      background-color: #0f172a;
    }
    .alert {
      font-size: 0.8125rem;
      padding: 0.75rem;
      border-radius: 4px;
      margin-bottom: 1.5rem;
    }
  </style>
</head>
<body>

<div class="login-card">
  <div class="login-header">
    <h1>M-TRACK</h1>
    <p>Manufacturing Control System</p>
  </div>

  <?php if (isset($_GET['error'])): ?>
    <div class="alert alert-danger">
      <i class="bi bi-exclamation-circle me-2"></i> Invalid selection or PIN.
    </div>
  <?php endif; ?>

  <?php if ($isSetup): ?>
    <form action="auth.php" method="POST">
      <input type="hidden" name="action" value="setup_pin">
      <div class="mb-3">
        <label class="form-label">Create Admin PIN (4-6 digits)</label>
        <input type="password" name="pin" class="form-control" required pattern="\d{4,6}" inputmode="numeric">
        <div class="form-text mt-2">Initial setup for <?= htmlspecialchars($_SESSION['pending_setup_user_id']) ?></div>
      </div>
      <button type="submit" class="btn btn-primary">Set PIN & Login</button>
    </form>
  <?php else: ?>
    <ul class="nav nav-tabs" id="loginTabs" role="tablist">
      <li class="nav-item" role="presentation">
        <button class="nav-link active" id="worker-tab" data-bs-toggle="tab" data-bs-target="#worker" type="button" role="tab">Worker</button>
      </li>
      <li class="nav-item" role="presentation">
        <button class="nav-link" id="admin-tab" data-bs-toggle="tab" data-bs-target="#admin" type="button" role="tab">Admin</button>
      </li>
    </ul>

    <div class="tab-content" id="loginTabsContent">
      <!-- Worker Tab -->
      <div class="tab-pane fade show active" id="worker" role="tabpanel">
        <form action="auth.php" method="POST">
          <input type="hidden" name="action" value="login_worker">
          <div class="mb-3">
            <label class="form-label">Select Name</label>
            <select name="user_id" class="form-select" required>
              <option value="">Select...</option>
              <?php foreach ($workers as $w): ?>
                <option value="<?= $w['id'] ?>"><?= htmlspecialchars($w['name']) ?></option>
              <?php endforeach; ?>
            </select>
          </div>
          <button type="submit" class="btn btn-primary">Login</button>
        </form>
      </div>

      <!-- Admin Tab -->
      <div class="tab-pane fade" id="admin" role="tabpanel">
        <form action="auth.php" method="POST">
          <input type="hidden" name="action" value="login_admin">
          <div class="mb-3">
            <label class="form-label">Select Name</label>
            <select name="user_id" class="form-select" required>
              <option value="">Select...</option>
              <?php foreach ($admins as $a): ?>
                <option value="<?= $a['id'] ?>"><?= htmlspecialchars($a['name']) ?></option>
              <?php endforeach; ?>
            </select>
          </div>
          <div class="mb-3">
            <label class="form-label">Enter PIN</label>
            <input type="password" name="pin" class="form-control" placeholder="••••" inputmode="numeric">
          </div>
          <button type="submit" class="btn btn-primary">Login</button>
        </form>
      </div>
    </div>
  <?php endif; ?>
</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>