<?php
declare(strict_types=1);
session_start();
require_once '../config.php';

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

$conn = getDBConnection();
$error = '';
$success = trim((string) ($_GET['success'] ?? ''));

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $phone = preg_replace('/\D+/', '', (string) ($_POST['phone'] ?? ''));
    $password = (string) ($_POST['password'] ?? '');

    if ($conn instanceof mysqli) {
        $stmt = $conn->prepare(
            "SELECT id, full_name, phone, password_hash
             FROM parent_accounts
             WHERE phone = ? AND is_active = 1
             LIMIT 1"
        );
        $stmt->bind_param('s', $phone);
        $stmt->execute();
        $row = $stmt->get_result()->fetch_assoc();
        $stmt->close();

        if ($row && password_verify($password, (string) $row['password_hash'])) {
            session_regenerate_id(true);
            $_SESSION['parent_id'] = (int) $row['id'];
            $_SESSION['parent_name'] = (string) $row['full_name'];
            $_SESSION['parent_phone'] = (string) $row['phone'];

            $update = $conn->prepare("UPDATE parent_accounts SET last_login = NOW() WHERE id = ?");
            $update->bind_param('i', $row['id']);
            $update->execute();
            $update->close();

            header('Location: dashboard.php');
            exit;
        }
    }

    $error = 'Invalid phone or password';
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Parent Portal</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="assets/css/portal.css">
</head>
<body>
    <div class="portal-auth-wrap">
        <div class="portal-auth-card">
            <div class="portal-auth-logo"><i class="fas fa-user-friends"></i></div>
            <h1 class="h4 text-center mb-2">Parent Portal</h1>
            <p class="portal-subtitle text-center mb-4">Sign in to manage your child's account</p>

            <?php if ($error !== ''): ?>
                <div class="alert alert-danger"><?= htmlspecialchars($error) ?></div>
            <?php endif; ?>
            <?php if ($success !== ''): ?>
                <div class="alert alert-success"><?= htmlspecialchars($success) ?></div>
            <?php endif; ?>

            <form method="POST">
                <div class="mb-3">
                    <label class="portal-label" for="phone">Phone Number</label>
                    <input type="text" id="phone" name="phone" class="portal-input" data-phone-input required>
                </div>
                <div class="mb-3">
                    <label class="portal-label" for="password">Password</label>
                    <input type="password" id="password" name="password" class="portal-input" required>
                </div>
                <button type="submit" class="portal-btn-primary w-100">Sign In</button>
            </form>

            <div class="d-flex justify-content-between align-items-center mt-3">
                <a href="forgot_password.php">Forgot Password?</a>
                <small class="portal-muted">Need help? Contact school office</small>
            </div>
            <p class="portal-muted text-center mt-4 mb-0">For account setup, contact the school office</p>
        </div>
    </div>
    <script src="assets/js/portal.js"></script>
</body>
</html>
