import logo_koperasi from '@/assets/koperasi.png';
import bg from '@/assets/bg.jpeg';
import { ButtonSubmit } from '@/components/costum/button_submit';
import { useForm } from '@inertiajs/react';
import { Link, useNavigate } from 'react-router';

export default function LoginPage() {
    const navigate = useNavigate();

    const { data, setData, processing, errors, reset, post } = useForm({
        username: '',
        password: '',
    });

    const handleLogin = async () => {
        post('/login', {
            onError: (error) => {
                if (error.password) {
                    reset('password');
                }
            },
            onSuccess: () => {
                navigate('/app/dashboard');
            },
        });
    };

    const handleSubmit = async (event: React.FormEvent<HTMLFormElement>) => {
        event.preventDefault();
        return await handleLogin();
    };

    return (
        <div className="d-flex justify-content-center align-items-center" style={{ height: '100vh', background : `url(${bg}) no-repeat center center fixed`, backgroundSize: 'cover' }}>
            <form onSubmit={handleSubmit}>
                <div className="card card-sign shadow-lg">
                    <div className="card-header mb-2 text-center">
                        <Link to="/login" className="mb-4">
                            <img src={logo_koperasi} height={200} />
                        </Link>
                        <h3 className="card-title">Selamat Datang</h3>
                        <p className="card-text">Silahkan masuk untuk melanjutkan.</p>
                    </div>
                    <div className="card-body py-0">
                        {errors.username && <div className="alert alert-danger mb-0">{errors.username}</div>}
                        {errors.password && <div className="alert alert-danger mb-0">{errors.password}</div>}
                    </div>
                    <div className="card-body py-2">
                        <div className="mb-4">
                            <label className="form-label">Username</label>
                            <input
                                type="text"
                                className="form-control"
                                placeholder="Username"
                                value={data.username}
                                onChange={(e) => setData('username', e.target.value)}
                                required
                            />
                        </div>
                        <div className="mb-4">
                            <label className="form-label d-flex justify-content-between">Password</label>
                            <input
                                type="password"
                                className="form-control"
                                placeholder="Password"
                                value={data.password}
                                onChange={(e) => setData('password', e.target.value)}
                                required
                            />
                        </div>
                        <ButtonSubmit className="btn btn-primary btn-sign" disable={processing}>
                            {processing && <span className="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span>}
                            Sign In
                        </ButtonSubmit>
                    </div>
                    <div className="card-footer"></div>
                </div>
            </form>
        </div>
    );
}
