import { useState } from 'react';
import { Form, Head } from '@inertiajs/react';
import InputError from '@/components/input-error';
import { Button } from '@/components/ui/button';
import { Checkbox } from '@/components/ui/checkbox';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import { Spinner } from '@/components/ui/spinner';
import AuthLayout from '@/layouts/auth-layout';
import { Link } from '@inertiajs/react';

type Props = {
    status?: string;
};

export default function LoginLivreur({ status }: Props) {
    const [remember, setRemember] = useState(false);
    const [showPassword, setShowPassword] = useState(false);
    return (
        <AuthLayout
            title="Espace Livreur"
            showLogo={true}
        >
            <Head title="Connexion Livreur" />

            <div className="w-full max-w-md mx-auto">
                {/* En-tête avec branding livreur */}
                

                {/* Bannière de status */}
                {status && (
                    <div className="mb-6 rounded-xl bg-gradient-to-r from-green-50 to-emerald-50 border border-green-200 p-4 dark:from-green-900/20 dark:to-emerald-900/20 dark:border-green-700">
                        <div className="flex items-center justify-center gap-2">
                            <svg className="h-5 w-5 text-green-600 dark:text-green-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                <path strokeLinecap="round" strokeLinejoin="round" strokeWidth="2" d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z" />
                            </svg>
                            <span className="text-sm font-medium text-green-700 dark:text-green-300">
                                {status}
                            </span>
                        </div>
                    </div>
                )}

                {/* Formulaire de connexion */}
                <div className="relative overflow-hidden rounded-2xl border border-gray-200 bg-white p-8 shadow-2xl dark:border-gray-700 dark:bg-gray-800">
                    {/* Effets décoratifs */}
                    <div className="absolute -top-20 -right-20 h-40 w-40 rounded-full bg-gradient-to-br from-blue-500/10 to-transparent blur-3xl"></div>
                    <div className="absolute -bottom-20 -left-20 h-40 w-40 rounded-full bg-gradient-to-tr from-green-500/10 to-transparent blur-3xl"></div>
                    <div className="absolute top-1/2 left-1/2 -translate-x-1/2 -translate-y-1/2 h-60 w-60 rounded-full bg-gradient-to-r from-red-500/5 to-blue-500/5 blur-3xl"></div>

                    <Form
                        action="/login-livreur"
                        method="post"
                        resetOnSuccess={['password']}
                        className="relative z-10 flex flex-col gap-6"
                    >
                        {({ processing, errors }) => (
                            <>
                                {/* Champ identifiant livreur */}
                                <div className="space-y-3">
                                    <Label htmlFor="email" className="text-sm font-medium text-gray-700 dark:text-gray-300 flex items-center gap-2">
                                        <svg className="h-4 w-4 text-blue-600 dark:text-blue-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                            <path strokeLinecap="round" strokeLinejoin="round" strokeWidth="2" d="M16 7a4 4 0 11-8 0 4 4 0 018 0zM12 14a7 7 0 00-7 7h14a7 7 0 00-7-7z" />
                                            <path strokeLinecap="round" strokeLinejoin="round" strokeWidth="2" d="M12 14l9-5-9-5-9 5 9 5z" />
                                        </svg>
                                        Identifiant livreur
                                    </Label>
                                    <div className="relative">
                                        <Input
                                            id="email"
                                            type="text"
                                            name="email"
                                            required
                                            autoFocus
                                            tabIndex={1}
                                            autoComplete="username"
                                            placeholder="votre.email@exemple.com"
                                            className={`h-12 rounded-lg border-2 bg-gray-50 px-4 pl-12 text-gray-900 transition-all duration-300 focus:border-blue-500 focus:bg-white focus:ring-2 focus:ring-blue-200 dark:bg-gray-700 dark:text-white dark:focus:border-blue-400 dark:focus:ring-blue-500/20 ${
                                                errors.email 
                                                    ? 'border-red-300 dark:border-red-500' 
                                                    : 'border-gray-300 dark:border-gray-600'
                                            }`}
                                        />
                                        <div className="absolute left-4 top-1/2 -translate-y-1/2">
                                            <svg className="h-5 w-5 text-gray-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                                <path strokeLinecap="round" strokeLinejoin="round" strokeWidth="2" d="M3 8l7.89 4.26a2 2 0 002.22 0L21 8M5 19h14a2 2 0 002-2V7a2 2 0 00-2-2H5a2 2 0 00-2 2v10a2 2 0 002 2z" />
                                            </svg>
                                        </div>
                                    </div>
                                    <InputError message={errors.email} className="text-sm" />
                                </div>

                                {/* Champ mot de passe */}
                                <div className="space-y-3">
                                    <Label htmlFor="password" className="text-sm font-medium text-gray-700 dark:text-gray-300 flex items-center gap-2">
                                        <svg className="h-4 w-4 text-blue-600 dark:text-blue-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                            <path strokeLinecap="round" strokeLinejoin="round" strokeWidth="2" d="M12 15v2m-6 4h12a2 2 0 002-2v-6a2 2 0 00-2-2H6a2 2 0 00-2 2v6a2 2 0 002 2zm10-10V7a4 4 0 00-8 0v4h8z" />
                                        </svg>
                                        Mot de passe
                                    </Label>
                                    <div className="relative">
                                        <Input
                                            id="password"
                                            type={showPassword ? 'text' : 'password'}
                                            name="password"
                                            required
                                            tabIndex={2}
                                            autoComplete="current-password"
                                            placeholder="Votre mot de passe"
                                            className={`h-12 rounded-lg border-2 bg-gray-50 px-4 pl-12 pr-12 text-gray-900 transition-all duration-300 focus:border-blue-500 focus:bg-white focus:ring-2 focus:ring-blue-200 dark:bg-gray-700 dark:text-white dark:focus:border-blue-400 dark:focus:ring-blue-500/20 ${
                                                errors.password 
                                                    ? 'border-red-300 dark:border-red-500' 
                                                    : 'border-gray-300 dark:border-gray-600'
                                            }`}
                                        />
                                        <div className="absolute left-4 top-1/2 -translate-y-1/2">
                                            <svg className="h-5 w-5 text-gray-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                                <path strokeLinecap="round" strokeLinejoin="round" strokeWidth="2" d="M15 7a2 2 0 012 2m4 0a6 6 0 01-7.743 5.743L11 17H9v2H7v2H4a1 1 0 01-1-1v-2.586a1 1 0 01.293-.707l5.964-5.964A6 6 0 1121 9z" />
                                            </svg>
                                        </div>
                                        <button
                                            type="button"
                                            onClick={() => setShowPassword((v) => !v)}
                                            className="absolute right-4 top-1/2 -translate-y-1/2 text-gray-400 hover:text-gray-600 dark:hover:text-gray-300 focus:outline-none"
                                            tabIndex={-1}
                                            aria-label={showPassword ? 'Masquer le mot de passe' : 'Afficher le mot de passe'}
                                        >
                                            {showPassword ? (
                                                <svg className="h-5 w-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                                    <path strokeLinecap="round" strokeLinejoin="round" strokeWidth="2" d="M13.875 18.825A10.05 10.05 0 0112 19c-4.478 0-8.268-2.943-9.543-7a9.97 9.97 0 011.563-3.029m5.858.908a3 3 0 114.243 4.243M9.878 9.878a4.5 4.5 0 106.262 6.262M4.031 11.117A8.959 8.959 0 003 12c0 4.478 2.943 8.268 7 9.543 3.947-1.239 7.5-4.08 9.542-9.542" />
                                                </svg>
                                            ) : (
                                                <svg className="h-5 w-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                                    <path strokeLinecap="round" strokeLinejoin="round" strokeWidth="2" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z" />
                                                    <path strokeLinecap="round" strokeLinejoin="round" strokeWidth="2" d="M2.458 12C3.732 7.943 7.523 5 12 5c4.478 0 8.268 2.943 9.542 7-1.274 4.057-5.064 7-9.542 7-4.477 0-8.268-2.943-9.542-7z" />
                                                </svg>
                                            )}
                                        </button>
                                    </div>
                                    <InputError message={errors.password} className="text-sm" />
                                </div>

                                {/* Options de connexion */}
                                <div className="flex items-center justify-between">
                                    <div className="flex items-center space-x-3">
                                        <input
                                            type="checkbox"
                                            id="remember"
                                            name="remember"
                                            value="1"
                                            tabIndex={3}
                                            checked={remember}
                                            onChange={(e) => setRemember(e.target.checked)}
                                            className="h-5 w-5 rounded border-2 border-gray-300 accent-green-600 focus:ring-2 focus:ring-green-200 dark:border-gray-600 dark:accent-green-500"
                                        />
                                        <Label htmlFor="remember" className="text-sm text-gray-700 dark:text-gray-300 cursor-pointer">
                                            Garder ma session active
                                        </Label>
                                    </div>
                                    <Link
                                        href="/forgot-password"
                                        className="text-sm font-medium text-blue-600 hover:text-blue-700 dark:text-blue-400 dark:hover:text-blue-300 transition-colors"
                                        tabIndex={5}
                                    >
                                        Mot de passe oublié ?
                                    </Link>
                                </div>

                                {/* Bouton de connexion */}
                                <Button
                                    type="submit"
                                    className="group relative h-12 w-full rounded-lg bg-gradient-to-r from-green-600 to-green-500 text-white shadow-lg transition-all duration-300 hover:from-green-700 hover:to-green-600 hover:shadow-xl hover:scale-[1.02] active:scale-[0.98]"
                                    tabIndex={4}
                                    disabled={processing}
                                >
                                    <div className="absolute inset-0 rounded-lg bg-gradient-to-r from-blue-600 to-blue-500 opacity-0 transition-opacity duration-300 group-hover:opacity-20"></div>
                                    {processing ? (
                                        <div className="relative flex items-center justify-center gap-2">
                                            <Spinner className="h-5 w-5" />
                                            <span className="font-semibold">Connexion en cours...</span>
                                        </div>
                                    ) : (
                                        <div className="relative flex items-center justify-center gap-2">
                                            <svg className="h-5 w-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                                <path strokeLinecap="round" strokeLinejoin="round" strokeWidth="2" d="M13 10V3L4 14h7v7l9-11h-7z" />
                                            </svg>
                                            <span className="font-semibold">Commencer les livraisons</span>
                                        </div>
                                    )}
                                </Button>

                                {/* Informations supplémentaires */}
                                <div className="pt-2 text-center">
                                    <p className="text-sm text-gray-600 dark:text-gray-400">
                                        Vous n'êtes pas encore livreur ?{' '}
                                        <Link 
                                            href="#" 
                                            className="font-semibold text-red-600 hover:text-red-700 dark:text-red-400 dark:hover:text-red-300 transition-colors"
                                        >
                                            Postulez maintenant
                                        </Link>
                                    </p>
                                </div>
                            </>
                        )}
                    </Form>
                </div>

               
                {/* Retour à l'accueil */}
                <div className="mt-8 text-center">
                    <Link 
                        href="/" 
                        className="inline-flex items-center gap-2 text-sm text-gray-600 hover:text-gray-900 dark:text-gray-400 dark:hover:text-white transition-colors"
                    >
                        <svg className="h-4 w-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path strokeLinecap="round" strokeLinejoin="round" strokeWidth="2" d="M10 19l-7-7m0 0l7-7m-7 7h18" />
                        </svg>
                        Retour à l'accueil
                    </Link>
                </div>
            </div>
        </AuthLayout>
    );
}