import React, { useState } from 'react'
import {
    Paper,
    Table,
    TableBody,
    TableCell,
    TableHead,
    TableContainer,
    TablePagination,
    TableRow,
    IconButton,
    MenuItem,
    Tooltip,
    Icon,
    Typography,
} from '@material-ui/core'
import EditIcon from '@material-ui/icons/Edit'
import DeleteIcon from '@material-ui/icons/Delete'
import colors from 'app/assets/Color'
import useStyles from 'app/styles/Table'
import { Breadcrumb, SimpleCard } from 'app/components'
import { toPriceVnd } from 'app/config/Function'
import { checkRole } from 'app/config/Function'
import useAuth from 'app/hooks/useAuth'
import { useHistory } from 'react-router-dom'
import DialogTransition from 'app/components/dialog/DialogTransition'

const columns = [
    {
        id: 'index',
        label: 'STT',
        align: 'center',
        minWidth: 80,
    },
    {
        id: 'code',
        label: 'Mã phiếu đối soát',
        align: 'left',
        minWidth: 160,
    },
    {
        id: 'idPaymentPartner',
        label: 'Đối tác',
        align: 'left',
        minWidth: 80,
    },

    {
        id: 'create_date',
        label: 'Ngày tạo phiếu',
        align: 'left',
        minWidth: 110,
    },
    {
        id: 'update_date',
        label: 'Ngày cập nhật',
        align: 'left',
        minWidth: 110,
    },
    {
        id: 'date_reconciliation',
        label: 'Ngày đối soát',
        align: 'left',
        minWidth: 110,
    },

    {
        id: 'totalCommission',
        label: 'Hoa hồng',
        align: 'left',
        minWidth: 80,
        isMonney: true,
    },
]

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

    const handleChangePage = (event, newPage) => {
        setPageIndex(newPage)
    }
    const handleChangeRowsPerPage = (event) => {
        setPageSize(event.target.value)
    }
    const renderStatusBg = (status) => {
        if (status == 3) return '#ECFDF3'
        if (status == 2) return '#FFDC98'
        return '#F2F4F7'
    }

    const renderStatusColor = (status) => {
        if (status == 3) return '#027A48'
        if (status == 2) return '#F1AA1F'
        return '#344054'
    }
    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={{
                                    width: 120,
                                    backgroundColor: colors.headerTable,
                                }}
                            >
                                Trạng thái
                            </TableCell>
                            <TableCell
                                style={{
                                    textAlign: 'center',
                                    backgroundColor: colors.headerTable,
                                    width: 160,
                                }}
                            >
                                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}
                                                style={{
                                                    textAlign:
                                                        column.id === 'index'
                                                            ? 'center'
                                                            : typeof data ==
                                                              'string'
                                                            ? 'left'
                                                            : 'right',
                                                }}
                                            >
                                                {column.isMonney
                                                    ? toPriceVnd(data)
                                                    : data}
                                            </TableCell>
                                        )
                                    })}
                                    <TableCell align={'center'}>
                                        <Typography
                                            style={{
                                                padding: 5,
                                                backgroundColor: renderStatusBg(
                                                    row?.status
                                                ),
                                                color: renderStatusColor(
                                                    row?.status
                                                ),
                                                borderRadius: 10,
                                                textAlign: 'center',
                                            }}
                                        >
                                            {row?.status_name}
                                        </Typography>
                                    </TableCell>
                                    <TableCell>
                                        {checkRole(
                                            user,
                                            '/reconciliation/sheet'
                                        ) && row.status != 3 ? (
                                            <Tooltip title="Cập nhật">
                                                <IconButton
                                                    onClick={() => {
                                                        history.push({
                                                            pathname:
                                                                '/reconciliation/sheet/update',
                                                            state: { ...row },
                                                        })
                                                    }}
                                                    className={classes.button}
                                                    aria-label="edit"
                                                >
                                                    <Icon color="primary">
                                                        edit
                                                    </Icon>
                                                </IconButton>
                                            </Tooltip>
                                        ) : null}
                                        {checkRole(
                                            user,
                                            '/reconciliation/sheet'
                                        ) ? (
                                            <Tooltip title="Chi tiết">
                                                <IconButton
                                                    onClick={() => {
                                                        history.push({
                                                            pathname:
                                                                '/reconciliation/sheet/detail',
                                                            state: { ...row },
                                                        })
                                                    }}
                                                    className={classes.button}
                                                    aria-label="edit"
                                                >
                                                    <Icon color="primary">
                                                        visibility
                                                    </Icon>
                                                </IconButton>
                                            </Tooltip>
                                        ) : null}
                                        {checkRole(
                                            user,
                                            '/reconciliation/sheet'
                                        ) && row.status != 3 ? (
                                            <Tooltip title="Phê duyệt">
                                                <IconButton
                                                    onClick={() => {
                                                        setSelected({
                                                            ...row,
                                                            title: 'Phê duyệt phiếu đối soát',
                                                            content: `Bạn có muốn Phê duyệt phiếu đối soát ${row.code} trong phiếu đối soát hay không?`,
                                                        })
                                                        setOpen(true)
                                                    }}
                                                    className={classes.button}
                                                    aria-label="Delete"
                                                >
                                                    <Icon
                                                        style={{
                                                            color: '#027A48',
                                                        }}
                                                    >
                                                        check_circle
                                                    </Icon>
                                                </IconButton>
                                            </Tooltip>
                                        ) : null}
                                    </TableCell>
                                </TableRow>
                            )
                        })}
                    </TableBody>
                </Table>
            </TableContainer>
            <TablePagination
                component="div"
                page={pageIndex}
                count={totalRecords}
                rowsPerPage={10}
                rowsPerPageOptions={[]}
                onChangePage={handleChangePage}
                onChangeRowsPerPage={handleChangeRowsPerPage}
            />
            <DialogTransition
                data={selected}
                open={open}
                handleClose={handleClose}
                onAgree={() => {
                    removeItem(selected.id)
                    handleClose()
                }}
            />
        </Paper>
    )
}

export default TableList
