<?php
session_start();
include 'config.php';

$error = '';

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $phone = $conn->real_escape_string($_POST['phone']);
    $password = $_POST['password'];
    
    $result = $conn->query("SELECT * FROM captains WHERE phone = '$phone' AND status = 'active'");
    
    if ($result && $result->num_rows == 1) {
        $captain = $result->fetch_assoc();
        if (password_verify($password, $captain['password'])) {
            $_SESSION['captain_id'] = $captain['id'];
            $_SESSION['captain_name'] = $captain['name'];
            $_SESSION['captain_phone'] = $captain['phone'];
            header('Location: dashboard.php');
            exit();
        } else {
            $error = 'كلمة المرور غير صحيحة';
        }
    } else {
        $error = 'رقم الهاتف غير مسجل أو غير نشط';
    }
}
?>
<!DOCTYPE html>
<html lang="ar" dir="rtl">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
    <title>تسجيل الدخول - كابتن ماكس</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.rtl.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Tajawal:wght@400;500;700;800&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.0/font/bootstrap-icons.css">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        body {
            font-family: 'Tajawal', sans-serif;
            background: linear-gradient(135deg, #F57C00, #E65100);
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 20px;
        }
        .login-card {
            background: white;
            border-radius: 28px;
            overflow: hidden;
            box-shadow: 0 20px 40px rgba(0,0,0,0.2);
            max-width: 400px;
            width: 100%;
        }
        .login-header {
            background: linear-gradient(135deg, #F57C00, #E65100);
            color: white;
            padding: 30px;
            text-align: center;
        }
        .login-header h2 {
            font-size: 1.5rem;
            margin-bottom: 5px;
        }
        .login-form {
            padding: 30px;
        }
        .btn-login {
            background: linear-gradient(135deg, #F57C00, #E65100);
            border: none;
            padding: 12px;
            border-radius: 40px;
            font-weight: 700;
            width: 100%;
            color: white;
        }
        .btn-login:hover {
            background: #E65100;
        }
        .form-control {
            border-radius: 12px;
            padding: 12px;
            border: 1px solid #e5e7eb;
        }
        .form-control:focus {
            border-color: #F57C00;
            box-shadow: 0 0 0 3px rgba(245,124,0,0.1);
        }
    </style>
</head>
<body>
    <div class="login-card">
        <div class="login-header">
            <i class="bi bi-person-badge fs-1"></i>
            <h2>تطبيق الكابتن</h2>
            <p class="mb-0">تسجيل الدخول</p>
        </div>
        <div class="login-form">
            <?php if ($error): ?>
                <div class="alert alert-danger"><?php echo $error; ?></div>
            <?php endif; ?>
            <form method="POST" action="">
                <div class="mb-3">
                    <label class="form-label">رقم الهاتف</label>
                    <input type="tel" name="phone" class="form-control" required placeholder="05xxxxxxxx">
                </div>
                <div class="mb-3">
                    <label class="form-label">كلمة المرور</label>
                    <input type="password" name="password" class="form-control" required>
                </div>
                <button type="submit" class="btn-login">
                    <i class="bi bi-box-arrow-in-left"></i> تسجيل الدخول
                </button>
            </form>
        </div>
    </div>
</body>
</html>