import { useEffect, useState } from 'react'
import { useServerIps, useServers } from '../hooks/swrHooks'
import { Autocomplete, CloseButton, Loader, Table } from '@mantine/core'
import { IServer } from '../interfaces/servers'

export default function ServerIpsView() {
    const [selectedOption, setSelectedOption] = useState<number | null>(null)
    const { servers } = useServers()

    const { serverIps, isLoading: serversLoading } = useServerIps(selectedOption, 0, 10)

    const serverIpsColumns = [
        { field: 'id', headerName: 'ID', type: 'number' },
        { field: 'server_id', headerName: 'Server ID', type: 'number' },
        { field: 'name', headerName: 'Название', type: 'string' },
        { field: 'is_actual', headerName: 'Действителен', type: 'boolean' },
        { field: 'ip', headerName: 'IP', type: 'string' },
        { field: 'servername', headerName: 'Название сервера', type: 'string' },
    ]

    useEffect(() => {
        console.log(serverIps)
    }, [serverIps])

    return (
        <>
            <form>
                <Autocomplete
                    placeholder="Сервер"
                    flex={'1'}
                    data={servers ? servers.map((item: IServer) => ({ label: item.name, value: item.id.toString() })) : []}
                    onSelect={(e) => console.log(e.currentTarget.value)}
                    //onChange={(value) => setSearch(value)}
                    onOptionSubmit={(value) => setSelectedOption(Number(value))}
                    rightSection={
                        //search !== '' && 
                        (
                            <CloseButton
                                size="sm"
                                onMouseDown={(event) => event.preventDefault()}
                                onClick={() => {
                                    //setSearch('')
                                    setSelectedOption(null)
                                }}
                                aria-label="Clear value"
                            />
                        )
                    }
                //value={search}
                />
            </form>

            {serversLoading ?
                <Loader />
                :
                <Table highlightOnHover>
                    <Table.Thead>
                        <Table.Tr>
                            {serverIpsColumns.map(column => (
                                <Table.Th key={column.field}>{column.headerName}</Table.Th>
                            ))}
                        </Table.Tr>
                    </Table.Thead>
                    <Table.Tbody>
                        <Table.Tr
                        //bg={selectedRows.includes(element.position) ? 'var(--mantine-color-blue-light)' : undefined}
                        >
                            {serverIpsColumns.map(column => (
                                <Table.Td key={column.field}>{servers ? servers[column.field] : ''}</Table.Td>
                            ))}
                        </Table.Tr>
                    </Table.Tbody>
                </Table>
            }
        </>
    )
}