import * as React from 'react';

import { 
    rows,
    action_icon,
    Comparar,
    Cuerpo,
} from '../Components/Password/config.js';

import { 
    Table, TableBody, TableCell, TableContainer, 
    TableRow, TablePagination,
    Paper, Box, Switch, FormControlLabel,
    Checkbox,
} from '@mui/material';

import { EditSharp, MailSharp } from '@mui/icons-material'

import { TableHeadCustom as TableHead } from '../Components/Password/Header'
import { TableEncabezadoOperation as TableOperation } from '../Components/Password/HeaderOperation.jsx'

export function Contras() {

    const [order, setOrder] = React.useState('asc');
    const [orderBy, setOrderBy] = React.useState('pass');
    const [selected, setSelected] = React.useState([]);
    const [page, setPage] = React.useState(0);
    const [dense, setDense] = React.useState(false);
    const [rowsPerPage, setRowsPerPage] = React.useState(10);

    const CambiarTipoOrden = (_event, property) => {
        const isAsc = orderBy === property && order === 'asc';
        setOrder(isAsc ? 'desc' : 'asc');
        setOrderBy(property);
    };

    const SeleccionarTodos = (event) => {
        if (event.target.checked) {
            const newSelecteds = rows.map( (n) => n.pass);
            setSelected(newSelecteds);
            return;
        }
        setSelected([]);
    };

    const Seleccionar = (_event, name) => {

        const selectedIndex = selected.indexOf(name);
        let newSelected = [];

        if (selectedIndex === -1) {
            newSelected = newSelected.concat(selected, name);
        } else if (selectedIndex === 0) {
            newSelected = newSelected.concat(selected.slice(1));
        } else if (selectedIndex === selected.length - 1) {
            newSelected = newSelected.concat(selected.slice(0, -1));
        } else if (selectedIndex > 0) {
            newSelected = newSelected.concat(
                selected.slice(0, selectedIndex),
                selected.slice(selectedIndex + 1),
            );
        }

        setSelected(newSelected);
    };

    const handleChangePage = (_event, newPage) => {
        setPage(newPage);
    };

    const handleChangeRowsPerPage = (event) => {
        setRowsPerPage(parseInt(event.target.value, 10));
        setPage(0);
    };

    const handleChangeDense = (event) => {
        setDense(event.target.checked);
    };

    const isSelected = (name) => selected.indexOf(name) !== -1;

    const emptyRows = page > 0 ? Math.max(0, (1 + page) * rowsPerPage - rows.length) : 0;

    return (
        <div className="content-section">
            <div className="main">
                <Box sx={{ width: '100%' }}>
                    <Paper sx={{ width: '100%', mb: 2 }}>

                        <TableOperation numSelected={selected.length} />

                        <TableContainer>

                            <Table
                                sx={{ minWidth: 750 }}
                                aria-labelledby=""
                                size={dense ? 'small' : 'medium'} >

                                <TableHead
                                    numSelected={selected.length}
                                    order={order}
                                    orderBy={orderBy}
                                    onSelectAllClick={SeleccionarTodos}
                                    onRequestSort={CambiarTipoOrden}
                                    rowCount={rows.length} />


                                <TableBody>
                                    {Cuerpo(rows, Comparar(order, orderBy))
                                    .slice(page * rowsPerPage, page * rowsPerPage + rowsPerPage)
                                    .map( (row, index) => {

                                        const isItemSelected = isSelected(row.pass);
                                        const labelId = `enhanced-table-checkbox-${index}`;

                                        return (
                                            <TableRow
                                                hover
                                                onClick={(event) => Seleccionar(event, row.pass)}
                                                // onClick={(event) => console.log(event)}
                                                role="checkbox"
                                                aria-checked={isItemSelected}
                                                tabIndex={-1}
                                                key={row.pass}
                                                selected={isItemSelected}
                                            >
                                                <TableCell padding="checkbox">
                                                    <Checkbox
                                                        color="primary"
                                                        checked={isItemSelected}
                                                        inputProps={{
                                                            'aria-labelledby': labelId,
                                                        }}
                                                        />
                                                </TableCell>
                                                <TableCell
                                                    component="th"
                                                    id={labelId}
                                                    scope="row"
                                                    padding="none"
                                                >
                                                    {row.pass}
                                                </TableCell>
                                                <TableCell align="right">{row.nivel}</TableCell>
                                                <TableCell align="right">{row.puesto}</TableCell>
                                                <TableCell align="right">{row.cui}</TableCell>
                                                <TableCell align="right">{row.uso}</TableCell>
                                                <TableCell align="right">{row.picture}</TableCell>
                                                <TableCell align="right">{row.cv}</TableCell>
                                                <TableCell align="right">{row.estado}</TableCell>
                                                <TableCell align="center">
                                                    <EditSharp style={action_icon} />
                                                    <MailSharp style={action_icon}/>
                                                </TableCell>
                                            </TableRow>
                                        );
                                    })}
                                    {emptyRows > 0 && (
                                        <TableRow
                                            style={{
                                                height: (dense ? 33 : 53) * emptyRows,
                                            }}
                                        >
                                            <TableCell colSpan={6} />
                                        </TableRow>
                                    )}
                                </TableBody>
                            </Table>
                        </TableContainer>

                        <TablePagination
                            rowsPerPageOptions={[5, 10, 25]}
                            component="div"
                            count={rows.length}
                            rowsPerPage={rowsPerPage}
                            page={page}
                            onPageChange={handleChangePage}
                            onRowsPerPageChange={handleChangeRowsPerPage}
                            labelRowsPerPage={"Mostrar: "}
                            />


                    </Paper>

                    <FormControlLabel
                        control={<Switch checked={dense} onChange={handleChangeDense} />}
                        label="Comprimir"
                        />

                </Box>
            </div>
        </div>
    );

}
