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 useAuth from 'app/hooks/useAuth'
import { checkRole, toPriceVnd } from 'app/config/Function'

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

    {
        id: 'gas_name',
        label: 'Tên',
        align: 'left',
        minWidth: 200,
    },

    {
        id: 'phone',
        label: 'SĐT',
        align: 'left',
        minWidth: 120,
    },

    {
        id: 'address',
        label: 'Địa chỉ',
        align: 'left',
        minWidth: 200,
    },

    {
        id: 'email',
        label: 'Email',
        align: 'left',
        minWidth: 200,
    },
    {
        id: 'suppcontruction_name',
        label: 'Đơn vị cung cấp ATC',
        align: 'left',
        minWidth: 160,
    },

    {
        id: 'num_fuel',
        label: 'Số trụ',
        align: 'center',
        minWidth: 80,
    },
    {
        id: 'num_gas',
        label: 'Số vòi',
        align: 'center',
        minWidth: 80,
    },
    {
        id: 'package_name',
        label: 'Gói đăng ký',
        align: 'left',
        minWidth: 120,
    },
    {
        id: 'money',
        label: 'Tổng tiền ước tính',
        align: 'left',
        minWidth: 160,
        isMonney: true,
    },
    {
        id: 'status_name',
        label: 'Trạng thái',
        align: 'left',
        minWidth: 120,
    },
    {
        id: 'date_created',
        label: 'Ngày đăng ký',
        align: 'left',
        minWidth: 180,
    },
]

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 { user } = useAuth()
    const handleChangePage = (event, newPage) => {
        setPageIndex(newPage)
    }
    const handleChangeRowsPerPage = (event) => {
        setPageSize(event.target.value)
    }
    const handleClose = () => {
        setOpen(false)
    }

    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>
                            ))}
                            {/* {checkRole(
                                user,
                                '/package-function/changeStatus'
                            ) && (
                                <TableCell
                                    style={{
                                        backgroundColor: colors.headerTable,
                                        width: 135,
                                    }}
                                >
                                    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 data = row[column.id]
                                        return (
                                            <TableCell
                                                key={column.id}
                                                align={column.align}
                                            >
                                                {column.isMonney
                                                    ? toPriceVnd(data)
                                                    : data}
                                            </TableCell>
                                        )
                                    })}
                                    {/* {checkRole(
                                        user,
                                        '/package-function/changeStatus'
                                    ) && (
                                        <TableCell className={classes.border}>
                                            <Select
                                                variant={'outlined'}
                                                labelId="demo-simple-select-placeholder-label-label"
                                                id="demo-simple-select-placeholder-label"
                                                onChange={(e) =>
                                                    handeChangeActive(
                                                        row.id,
                                                        e.target.value
                                                    )
                                                }
                                                displayEmpty
                                                defaultValue={row.status}
                                                className={classes.formControl}
                                            >
                                                <MenuItem value={1}>
                                                    Hoạt động
                                                </MenuItem>
                                                <MenuItem value={2}>
                                                    Khóa
                                                </MenuItem>
                                            </Select>
                                        </TableCell>
                                    )} */}
                                    <TableCell>
                                        {checkRole(
                                            user,
                                            '/package-register/detail'
                                        ) ? (
                                            <Tooltip title="Chi tiết">
                                                <IconButton
                                                    onClick={() => {
                                                        history.push({
                                                            pathname:
                                                                '/package-register/detail',
                                                            state: row.id,
                                                        })
                                                    }}
                                                    className={classes.button}
                                                    aria-label="edit"
                                                >
                                                    <Icon color="primary">
                                                        visibility
                                                    </Icon>
                                                </IconButton>
                                            </Tooltip>
                                        ) : null}

                                        {row.status != 1 ? (
                                            <Tooltip title="Thanh toán">
                                                <IconButton
                                                    onClick={() => {
                                                        setSelected({
                                                            ...row,
                                                            title: 'Xác nhận thanh toán',
                                                            content: `Bạn có muốn xác nhận thanh toán ${row.gas_name} hay không?`,
                                                        })
                                                        setOpen(true)
                                                    }}
                                                    className={classes.button}
                                                    aria-label="Delete"
                                                >
                                                    <Icon
                                                        style={{
                                                            color: '#07BA00',
                                                        }}
                                                    >
                                                        paid
                                                    </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
