/* eslint-disable react-hooks/exhaustive-deps */
'use client'
import infoSekolahPaud from '@/app/admin/(dashboard)/_state/pendidikan/info-sekolah-paud';
import colors from '@/con/colors';
import {
  Box,
  Button,
  Group,
  Loader,
  Paper,
  Select,
  Stack,
  Text,
  TextInput,
  Title
} from '@mantine/core';
import { IconArrowBack } from '@tabler/icons-react';
import { useRouter } from 'next/navigation';
import { useEffect, useState } from 'react';
import { toast } from 'react-toastify';
import { useProxy } from 'valtio/utils';

function CreateSiswa() {
  const router = useRouter();
  const stateCreate = useProxy(infoSekolahPaud.siswa);
  const [isSubmitting, setIsSubmitting] = useState(false);

  // Check if form is valid
  const isFormValid = () => {
    return (
      stateCreate.create.form.nama?.trim() !== '' &&
      stateCreate.create.form.lembagaId?.trim() !== ''
    );
  };

  useEffect(() => {
    stateCreate.findMany.load();
    infoSekolahPaud.lembagaPendidikan.findMany.load();
  }, []);

  const resetForm = () => {
    stateCreate.create.form = {
      nama: '',
      lembagaId: '',
    };
  };

  const handleSubmit = async () => {
    try {
      setIsSubmitting(true);
      await stateCreate.create.create();
      resetForm();
      router.push('/admin/pendidikan/info-sekolah/siswa');
    } catch (error) {
      console.error('Error creating siswa:', error);
      toast.error('Gagal menambahkan siswa');
    } finally {
      setIsSubmitting(false);
    }
  };

  return (
    <Box px={{ base: 0, md: 'lg' }} py="xs">
      {/* Header + Back Button */}
      <Group mb="md">
        <Button variant="subtle" onClick={() => router.back()} p="xs" radius="md">
          <IconArrowBack color={colors['blue-button']} size={24} />
        </Button>
        <Title order={4} ml="sm" c="dark">
          Tambah Siswa
        </Title>
      </Group>

      {/* Form Card */}
      <Paper
        w={{ base: '100%', md: '50%' }}
        bg={colors['white-1']}
        p="lg"
        radius="md"
        shadow="sm"
        style={{ border: '1px solid #e0e0e0' }}
      >
        <Stack gap="md">
          <TextInput
            value={stateCreate.create.form.nama}
            onChange={(val) => {
              stateCreate.create.form.nama = val.target.value;
            }}
            label={<Text fw="bold" fz="sm">Nama</Text>}
            placeholder="Masukkan nama siswa"
            required
          />

          <Select
            value={stateCreate.create.form.lembagaId}
            onChange={(val) => {
              stateCreate.create.form.lembagaId = val ?? '';
            }}
            label={<Text fw="bold" fz="sm">Lembaga</Text>}
            placeholder="Pilih lembaga"
            data={
              infoSekolahPaud.lembagaPendidikan.findMany.data?.map((v) => ({
                value: v.id,
                label: v.nama,
              })) || []
            }
            required
          />

          <Group justify="right">
            <Button
              variant="outline"
              color="gray"
              radius="md"
              size="md"
              onClick={resetForm}
            >
              Reset
            </Button>

            {/* Tombol Simpan */}
            <Button
              onClick={handleSubmit}
              radius="md"
              size="md"
              disabled={!isFormValid() || isSubmitting}
              style={{
                background: !isFormValid() || isSubmitting
                  ? `linear-gradient(135deg, #cccccc, #eeeeee)`
                  : `linear-gradient(135deg, ${colors['blue-button']}, #4facfe)`,
                color: '#fff',
                boxShadow: '0 4px 15px rgba(79, 172, 254, 0.4)',
              }}
            >
              {isSubmitting ? <Loader size="sm" color="white" /> : 'Simpan'}
            </Button>
          </Group>
        </Stack>
      </Paper>
    </Box>
  );
}

export default CreateSiswa;
