import React, { useState } from 'react'
import {
    Paper,
    Table,
    TableBody,
    TableCell,
    TableHead,
    TableContainer,
    TablePagination,
    TableRow,
    IconButton,
    MenuItem,
    Select,
    Modal,
    Fade,
    Grid,
    Backdrop,
    Link,
    Button,
    Tooltip,
    Icon,
} from '@material-ui/core'
import EditIcon from '@material-ui/icons/Edit'
import DeleteIcon from '@material-ui/icons/Delete'
import colors from '../../../assets/Color'
import useStyles from '../../../styles/Table'
import { Breadcrumb, SimpleCard } from 'app/components'
import DialogTransition from 'app/components/dialog/DialogTransition'
import { useHistory } from 'react-router-dom'
import ListActive from 'app/components/ListActive/ListActive'

import useAuth from 'app/hooks/useAuth'
import { checkRole } from 'app/config/Function'

const columns = [
    {
        id: 'index',
        label: 'STT',
        align: 'center',
        minWidth: 50,
    },

    {
        id: 'code',
        label: 'Mã',
        align: 'left',
        minWidth: 'auto',
    },
    {
        id: 'name',
        label: 'Tên trang tĩnh',
        align: 'left',
        minWidth: 'auto',
    },

    // {
    //     id: 'description',
    //     label: 'Mô tả',
    //     align: 'left',
    //     minWidth: 'auto',
    // },
    {
        id: 'date_created',
        label: 'Thời gian tạo',
        align: 'left',
        minWidth: 'auto',
    },

    {
        id: 'date_updated',
        label: 'Thời gian sửa',
        align: 'left',
        minWidth: 'auto',
    },
]

function TableList(props) {
    const {
        data,
        handeChangeActive,
        removeItem,
        changeActive,
        setChangeActive,
        setPageIndex,
        setPageSize,
        pageIndex,
        totalRecords,
        permissions,
    } = props
    const classes = useStyles()
    let history = useHistory()
    const [selected, setSelected] = useState({
        name: '',
        id: '',
        title: '',
        content: '',
    })
    const [open, setOpen] = React.useState(false)

    const handleChangePage = (event, newPage) => {
        setPageIndex(newPage)
    }
    const handleChangeRowsPerPage = (event) => {
        setPageSize(event.target.value)
    }
    const handleClose = () => {
        setOpen(false)
    }
    const { user } = useAuth()
    return (
        <Paper className={classes.root}>
            <TableContainer className={classes.container}>
                <Table stickyHeader aria-label="sticky table">
                    <TableHead>
                        <TableRow>
                            {columns.map((column) => (
                                <TableCell
                                    key={column.id}
                                    align={'center'}
                                    style={{
                                        width: column.minWidth,
                                        backgroundColor: colors.headerTable,
                                    }}
                                >
                                    {column.label}
                                </TableCell>
                            ))}

                            <TableCell
                                style={{
                                    backgroundColor: colors.headerTable,
                                    width: 125,
                                }}
                            >
                                Trạng thái
                            </TableCell>

                            <TableCell
                                style={{
                                    textAlign: 'center',
                                    backgroundColor: colors.headerTable,
                                    width: 120,
                                }}
                            >
                                Hành động
                            </TableCell>
                        </TableRow>
                    </TableHead>
                    <TableBody className={classes.columnTable}>
                        {data.map((row) => {
                            return (
                                <TableRow
                                    hover
                                    role="checkbox"
                                    tabIndex={-1}
                                    key={row.id}
                                >
                                    {columns.map((column) => {
                                        const imageUrl = row[column.id]
                                        return (
                                            <TableCell
                                                key={column.id}
                                                align={column.align}
                                            >
                                                {column.format ? (
                                                    <img
                                                        src={column.format(
                                                            imageUrl
                                                        )}
                                                        className={
                                                            classes.image
                                                        }
                                                    />
                                                ) : (
                                                    imageUrl
                                                )}
                                            </TableCell>
                                        )
                                    })}
                                    {console.log(175, row)}

                                    <TableCell className={classes.border}>
                                        <ListActive
                                            handeChangeActive={
                                                handeChangeActive
                                            }
                                            row={row}
                                        />
                                    </TableCell>
                                    <TableCell>
                                        {checkRole(
                                            user,
                                            '/system/page-static/delete'
                                        ) ? (
                                            <Tooltip title="Xoá">
                                                <IconButton
                                                    onClick={() => {
                                                        setSelected({
                                                            ...row,
                                                            title: 'Xóa trang tĩnh',
                                                            content: `Bạn có muốn xóa trang tĩnh ${row.name} hay không?`,
                                                        })
                                                        setOpen(true)
                                                    }}
                                                    className={classes.button}
                                                    aria-label="Delete"
                                                >
                                                    <Icon color="error">
                                                        delete
                                                    </Icon>
                                                </IconButton>
                                            </Tooltip>
                                        ) : null}
                                        {checkRole(
                                            user,
                                            '/system/page-static/update'
                                        ) ? (
                                            <Tooltip title="Cập nhật">
                                                <IconButton
                                                    onClick={() => {
                                                        history.push({
                                                            pathname:
                                                                '/system/page-static/update',
                                                            state: row.id,
                                                        })
                                                    }}
                                                    className={classes.button}
                                                    aria-label="edit"
                                                >
                                                    <Icon color="primary">
                                                        edit
                                                    </Icon>
                                                </IconButton>
                                            </Tooltip>
                                        ) : null}
                                    </TableCell>
                                </TableRow>
                            )
                        })}
                    </TableBody>
                </Table>
            </TableContainer>
            <DialogTransition
                data={selected}
                open={open}
                handleClose={handleClose}
                onAgree={() => {
                    removeItem(selected.id)
                    handleClose()
                }}
            />

            <TablePagination
                component="div"
                page={pageIndex}
                count={totalRecords}
                rowsPerPage={10}
                rowsPerPageOptions={[]}
                onChangePage={handleChangePage}
                onChangeRowsPerPage={handleChangeRowsPerPage}
            />
        </Paper>
    )
}

export default TableList
