import { Head, useForm, Link } from '@inertiajs/react';
import React from 'react';
import { Button } from '@/components/ui/button';
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select";
import { Separator } from '@/components/ui/separator';
import AppLayout from '@/layouts/app-layout';

// Definisi Tipe Data Employee yang diterima dari Controller
interface Employee {
    id: number;
    nip: string;
    gender: string;
    birth_date: string;
    place_of_birth: string;
    address: string;
    phone_number: string;
    department_id: number;
    position_id: number;
    status: string;
    join_date: string;
    user: {
        name: string;
        email: string;
    };
}

interface PageProps {
    employee: Employee;
    departments: { id: number; name: string }[];
    positions: { id: number; name: string }[];
    errors: Partial<Record<string, string>>;
}

export default function Edit({ employee, departments, positions }: PageProps) {
    // Inisialisasi Form dengan Data Lama (Pre-filled)
    const { data, setData, put, processing, errors } = useForm({
        name: employee.user.name,
        email: employee.user.email,
        nip: employee.nip,
        gender: employee.gender,
        birth_date: employee.birth_date,
        place_of_birth: employee.place_of_birth || '',
        address: employee.address || '',
        phone_number: employee.phone_number || '',
        department_id: String(employee.department_id),
        position_id: String(employee.position_id),
        status: employee.status,
        join_date: employee.join_date,
    });

    const submit = (e: React.FormEvent) => {
        e.preventDefault();
        put(`/admin/employees/${employee.id}`);
    };

    return (
        <AppLayout>
            <Head title={`Edit Karyawan: ${employee.user.name}`} />
            
            <div className="p-4 md:p-8 max-w-5xl mx-auto">
                <Button variant="outline" asChild className="mb-6">
                    <Link href="/admin/employees">← Kembali</Link>
                </Button>

                <Card>
                    <CardHeader>
                        <CardTitle>Edit Data Karyawan</CardTitle>
                        <p className="text-sm text-muted-foreground">Perbarui informasi karyawan di bawah ini.</p>
                    </CardHeader>
                    <Separator />
                    <CardContent className="pt-6">
                        <form onSubmit={submit} className="space-y-8">
                            
                            {/* INFORMASI PRIBADI */}
                            <div className="space-y-4">
                                <h3 className="text-lg font-medium flex items-center gap-2">
                                    <span className="bg-orange-100 text-orange-600 w-6 h-6 rounded-full flex items-center justify-center text-xs">1</span>
                                    Informasi Pribadi
                                </h3>
                                
                                <div className="grid grid-cols-1 md:grid-cols-2 gap-6">
                                    {/* Kolom Kiri */}
                                    <div className="space-y-4">
                                        <div className="space-y-2">
                                            <Label>Nama Lengkap</Label>
                                            <Input 
                                                value={data.name} 
                                                onChange={e => setData('name', e.target.value)} 
                                            />
                                            {errors.name && <div className="text-red-500 text-xs">{errors.name}</div>}
                                        </div>

                                        <div className="space-y-2">
                                            <Label>Email</Label>
                                            <Input 
                                                type="email" 
                                                value={data.email} 
                                                onChange={e => setData('email', e.target.value)} 
                                            />
                                            {errors.email && <div className="text-red-500 text-xs">{errors.email}</div>}
                                        </div>

                                        <div className="space-y-2">
                                            <Label>No. Telepon</Label>
                                            <Input 
                                                value={data.phone_number} 
                                                onChange={e => setData('phone_number', e.target.value)} 
                                            />
                                        </div>
                                    </div>

                                    {/* Kolom Kanan */}
                                    <div className="space-y-4">
                                        <div className="grid grid-cols-2 gap-4">
                                            <div className="space-y-2">
                                                <Label>Tempat Lahir</Label>
                                                <Input 
                                                    value={data.place_of_birth} 
                                                    onChange={e => setData('place_of_birth', e.target.value)} 
                                                />
                                            </div>
                                            <div className="space-y-2">
                                                <Label>Tanggal Lahir</Label>
                                                <Input 
                                                    type="date" 
                                                    value={data.birth_date} 
                                                    onChange={e => setData('birth_date', e.target.value)} 
                                                />
                                                {errors.birth_date && <div className="text-red-500 text-xs">{errors.birth_date}</div>}
                                            </div>
                                        </div>

                                        <div className="space-y-2">
                                            <Label>Jenis Kelamin</Label>
                                            <Select value={data.gender} onValueChange={(val) => setData('gender', val)}>
                                                <SelectTrigger><SelectValue placeholder="Pilih Gender" /></SelectTrigger>
                                                <SelectContent>
                                                    <SelectItem value="L">Laki-laki</SelectItem>
                                                    <SelectItem value="P">Perempuan</SelectItem>
                                                </SelectContent>
                                            </Select>
                                            {errors.gender && <div className="text-red-500 text-xs">{errors.gender}</div>}
                                        </div>

                                        <div className="space-y-2">
                                            <Label>Alamat Lengkap</Label>
                                            <Input 
                                                value={data.address} 
                                                onChange={e => setData('address', e.target.value)} 
                                            />
                                        </div>
                                    </div>
                                </div>
                            </div>

                            <Separator />

                            {/* DATA KEPEGAWAIAN */}
                            <div className="space-y-4">
                                <h3 className="text-lg font-medium flex items-center gap-2">
                                    <span className="bg-orange-100 text-orange-600 w-6 h-6 rounded-full flex items-center justify-center text-xs">2</span>
                                    Data Kepegawaian
                                </h3>
                                
                                <div className="grid grid-cols-1 md:grid-cols-2 gap-6">
                                    {/* Kolom Kiri */}
                                    <div className="space-y-4">
                                        <div className="space-y-2">
                                            <Label>Nomor Induk (NIP)</Label>
                                            <Input 
                                                value={data.nip} 
                                                onChange={e => setData('nip', e.target.value)} 
                                            />
                                            {errors.nip && <div className="text-red-500 text-xs">{errors.nip}</div>}
                                        </div>

                                        <div className="space-y-2">
                                            <Label>Departemen</Label>
                                            <Select value={data.department_id} onValueChange={(val) => setData('department_id', val)}>
                                                <SelectTrigger><SelectValue placeholder="Pilih Departemen" /></SelectTrigger>
                                                <SelectContent>
                                                    {departments.map((dept) => (
                                                        <SelectItem key={dept.id} value={String(dept.id)}>{dept.name}</SelectItem>
                                                    ))}
                                                </SelectContent>
                                            </Select>
                                        </div>
                                    </div>

                                    {/* Kolom Kanan */}
                                    <div className="space-y-4">
                                        <div className="space-y-2">
                                            <Label>Jabatan</Label>
                                            <Select value={data.position_id} onValueChange={(val) => setData('position_id', val)}>
                                                <SelectTrigger><SelectValue placeholder="Pilih Jabatan" /></SelectTrigger>
                                                <SelectContent>
                                                    {positions.map((pos) => (
                                                        <SelectItem key={pos.id} value={String(pos.id)}>{pos.name}</SelectItem>
                                                    ))}
                                                </SelectContent>
                                            </Select>
                                        </div>

                                        <div className="grid grid-cols-2 gap-4">
                                            <div className="space-y-2">
                                                <Label>Status</Label>
                                                <Select value={data.status} onValueChange={(val) => setData('status', val)}>
                                                    <SelectTrigger><SelectValue placeholder="Pilih Status" /></SelectTrigger>
                                                    <SelectContent>
                                                        <SelectItem value="PKWT">PKWT (Kontrak)</SelectItem>
                                                        <SelectItem value="PKWTT">PKWTT (Tetap)</SelectItem>
                                                        <SelectItem value="Magang">Magang</SelectItem>
                                                    </SelectContent>
                                                </Select>
                                            </div>

                                            <div className="space-y-2">
                                                <Label>Tanggal Masuk</Label>
                                                <Input 
                                                    type="date" 
                                                    value={data.join_date} 
                                                    onChange={e => setData('join_date', e.target.value)} 
                                                />
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>

                            <Separator />

                            {/* TOMBOL AKSI */}
                            <div className="flex justify-end gap-4 pt-2">
                                <Button type="button" variant="ghost" asChild>
                                    <Link href="/admin/employees">Batal</Link>
                                </Button>
                                <Button type="submit" disabled={processing} className="px-8">
                                    Perbarui Data
                                </Button>
                            </div>

                        </form>
                    </CardContent>
                </Card>
            </div>
        </AppLayout>
    );
}