// src/pages/admin/LoginAdmin.jsx
import React, { useState } from 'react';
import axios from 'axios';
import './LoginAdmin.css'; 

const LoginAdmin = () => {
    const [name, setName] = useState('');
    const [password, setPassword] = useState('');
    const [error, setError] = useState('');

    const handleLogin = async () => {
        try {
            // Send login request to the backend
            await axios.post('http://localhost:3001/loginAdmin', { name, password }, { withCredentials: true });
            // Redirect to DashboardChat after successful login
            window.location.href = '/Dashboardchat';
        } catch (err) {
            setError('Login failed. Please check your credentials and try again.');
        }
    };

    return (
        <div className="login-container">
            <h1>Admin Login</h1>
            <input
                type="text"
                placeholder="Name"
                value={name}
                onChange={(e) => setName(e.target.value)}
            />
            <input
                type="password"
                placeholder="Password"
                value={password}
                onChange={(e) => setPassword(e.target.value)}
            />
            <button onClick={handleLogin}>Login</button>
            {error && <p className="error-message">{error}</p>}
        </div>
    );
};

export default LoginAdmin;
