<?php
/**
 * cashier_login.php - صفحة تسجيل دخول الكاشير المتطورة
 */

require_once 'config.php';

// إذا كان الكاشير مسجل دخوله بالفعل
if (isset($_SESSION['cashier_id'])) {
    header('Location: cashier_dashboard.php');
    exit();
}

$error = '';

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $phone = $conn->real_escape_string($_POST['phone']);
    $password = $_POST['password'];
    
    // استخدام prepared statement للأمان
    $stmt = $conn->prepare("SELECT * FROM cashiers WHERE phone = ? AND status = 'active'");
    $stmt->bind_param("s", $phone);
    $stmt->execute();
    $result = $stmt->get_result();
    
    if ($result->num_rows == 1) {
        $cashier = $result->fetch_assoc();
        
        // التحقق من كلمة المرور
        if (password_verify($password, $cashier['password'])) {
            // تحديث آخر دخول
            $update = $conn->prepare("UPDATE cashiers SET last_login = NOW(), login_count = login_count + 1 WHERE id = ?");
            $update->bind_param("i", $cashier['id']);
            $update->execute();
            $update->close();
            
            $_SESSION['cashier_id'] = $cashier['id'];
            $_SESSION['cashier_name'] = $cashier['name'];
            $_SESSION['cashier_branch'] = $cashier['branch'];
            $_SESSION['cashier_branch_id'] = $cashier['branch_id'];
            $_SESSION['cashier_permissions'] = [
                'view_orders' => $cashier['can_view_orders'],
                'assign_captain' => $cashier['can_assign_captain'],
                'process_payments' => $cashier['can_process_payments']
            ];
            
            header('Location: cashier_dashboard.php');
            exit();
        }
    }
    
    $error = "رقم الهاتف أو كلمة المرور غير صحيحة";
}
?>

<!DOCTYPE html>
<html lang="ar" dir="rtl">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>تسجيل دخول الكاشير - Max</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@300;400;500;700&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.0/font/bootstrap-icons.css">
    <style>
        body {
            font-family: 'Tajawal', sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            height: 100vh;
            display: flex;
            align-items: center;
        }
        .login-container {
            background: white;
            border-radius: 20px;
            box-shadow: 0 20px 60px rgba(0,0,0,0.3);
            overflow: hidden;
            animation: slideUp 0.5s ease;
        }
        @keyframes slideUp {
            from {
                opacity: 0;
                transform: translateY(50px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }
        .login-header {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 2.5rem;
            text-align: center;
        }
        .login-header i {
            font-size: 3rem;
            margin-bottom: 1rem;
        }
        .login-header h2 {
            font-size: 1.8rem;
            margin-bottom: 0.5rem;
        }
        .login-form {
            padding: 2.5rem;
        }
        .form-control {
            border-radius: 10px;
            padding: 0.75rem 1rem;
            border: 2px solid #e0e0e0;
            transition: all 0.3s;
        }
        .form-control:focus {
            border-color: #667eea;
            box-shadow: none;
        }
        .btn-login {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border: none;
            padding: 0.75rem;
            border-radius: 10px;
            font-weight: 600;
            font-size: 1.1rem;
            transition: transform 0.3s;
        }
        .btn-login:hover {
            transform: translateY(-2px);
            box-shadow: 0 10px 20px rgba(102, 126, 234, 0.4);
        }
        .admin-link {
            color: #667eea;
            text-decoration: none;
            font-size: 0.9rem;
            transition: color 0.3s;
        }
        .admin-link:hover {
            color: #764ba2;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="row justify-content-center">
            <div class="col-md-5">
                <div class="login-container">
                    <div class="login-header">
                        <i class="bi bi-person-workspace"></i>
                        <h2>نظام الكاشير</h2>
                        <p class="mb-0">تسجيل الدخول للوحة التحكم</p>
                    </div>
                    <div class="login-form">
                        <?php if ($error): ?>
                            <div class="alert alert-danger alert-dismissible fade show" role="alert">
                                <i class="bi bi-exclamation-triangle-fill me-2"></i>
                                <?php echo $error; ?>
                                <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                            </div>
                        <?php endif; ?>
                        
                        <form method="POST" id="loginForm">
                            <div class="mb-4">
                                <label for="phone" class="form-label fw-bold">رقم الهاتف</label>
                                <div class="input-group">
                                    <span class="input-group-text bg-light"><i class="bi bi-telephone"></i></span>
                                    <input type="text" class="form-control" id="phone" name="phone" 
                                           required placeholder="أدخل رقم الهاتف" value="<?php echo isset($_POST['phone']) ? htmlspecialchars($_POST['phone']) : ''; ?>">
                                </div>
                            </div>
                            <div class="mb-4">
                                <label for="password" class="form-label fw-bold">كلمة المرور</label>
                                <div class="input-group">
                                    <span class="input-group-text bg-light"><i class="bi bi-lock"></i></span>
                                    <input type="password" class="form-control" id="password" name="password" 
                                           required placeholder="أدخل كلمة المرور">
                                </div>
                            </div>
                            <button type="submit" class="btn btn-login text-white w-100 mb-3">
                                <i class="bi bi-box-arrow-in-right me-2"></i>
                                تسجيل الدخول
                            </button>
                            
                            <div class="text-center">
                                <a href="login.php" class="admin-link">
                                    <i class="bi bi-shield-lock me-1"></i>
                                    دخول المدير
                                </a>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    <script>
        // إضافة تأثيرات بسيطة
        document.getElementById('loginForm').addEventListener('submit', function(e) {
            const btn = this.querySelector('button[type="submit"]');
            btn.innerHTML = '<span class="spinner-border spinner-border-sm me-2"></span> جاري تسجيل الدخول...';
            btn.disabled = true;
        });
    </script>
</body>
</html>