Table.js 21.5 KB
Newer Older
tdgiang committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
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 'app/assets/Color'
import useStyles from 'app/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 } from 'app/config/Function'
tdgiang committed
32
import DialogReason from 'app/components/dialog/DialogReason'
tdgiang committed
33 34
import AcceptStore from 'app/components/dialog/AcceptStore'
import UpdateTimeStore from 'app/components/dialog/UpdateTimeStore'
tdgiang committed
35 36 37 38 39 40 41
const columns = [
    {
        id: 'index',
        label: 'STT',
        align: 'center',
        minWidth: 50,
    },
tdgiang committed
42 43 44 45 46 47
    // {
    //     id: 'id',
    //     label: 'id',
    //     align: 'center',
    //     minWidth: 50,
    // },
tdgiang committed
48 49 50 51 52 53
    {
        id: 'merchant_name',
        label: 'Thuộc pháp nhân',
        align: 'left',
        minWidth: 160,
    },
tdgiang committed
54 55 56 57 58

    {
        id: 'store_name',
        label: 'Tên cây xăng',
        align: 'left',
tdgiang committed
59
        minWidth: 160,
tdgiang committed
60 61
    },
    {
tdgiang committed
62 63
        id: 'field',
        label: 'Trụ/vòi',
tdgiang committed
64
        align: 'left',
tdgiang committed
65
        minWidth: 80,
tdgiang committed
66
    },
tdgiang committed
67

tdgiang committed
68
    {
tdgiang committed
69 70
        id: 'address',
        label: 'Địa chỉ',
tdgiang committed
71
        align: 'left',
tdgiang committed
72
        minWidth: 160,
tdgiang committed
73
    },
tdgiang committed
74

tdgiang committed
75
    {
tdgiang committed
76
        id: 'representative',
tdgiang committed
77 78
        label: 'Người đại diện',
        align: 'left',
tdgiang committed
79
        minWidth: 140,
tdgiang committed
80 81 82
    },

    {
tdgiang committed
83 84
        id: 'provice_name',
        label: 'Tỉnh/Thành phố',
tdgiang committed
85
        align: 'left',
tdgiang committed
86
        minWidth: 120,
tdgiang committed
87 88 89
    },

    {
tdgiang committed
90 91
        id: 'date_expried',
        label: 'Ngày hết hạn',
tdgiang committed
92
        align: 'left',
tdgiang committed
93
        minWidth: 100,
tdgiang committed
94
    },
tdgiang committed
95 96 97 98
    {
        id: 'status_text',
        label: 'Trạng thái',
        align: 'left',
tdgiang committed
99
        minWidth: 120,
tdgiang committed
100
    },
tdgiang committed
101 102 103 104 105 106 107 108 109 110 111 112 113 114
]

function TableList(props) {
    const {
        data,
        handeChangeActive,
        removeItem,
        changeActive,
        setChangeActive,
        setPageIndex,
        setPageSize,
        pageIndex,
        totalRecords,
        permissions,
tdgiang committed
115 116
        acceptStoreSub,
        acceptUpdateTime,
tdgiang committed
117 118 119 120 121 122 123 124 125
    } = props
    const classes = useStyles()
    let history = useHistory()
    const [selected, setSelected] = useState({
        name: '',
        id: '',
        title: '',
        content: '',
    })
tdgiang committed
126 127
    const [openAccept, setOpenAccpet] = useState(false)
    const [openUpdate, setOpenUpdate] = useState(false)
tdgiang committed
128
    const [open, setOpen] = React.useState(false)
tdgiang committed
129 130
    const [openReason, setOpenReason] = useState(false)

tdgiang committed
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
    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>
                            ))}
tdgiang committed
160
                            {/* {checkRole(user, '/gas-station/changeStatus') && (
tdgiang committed
161 162 163 164 165 166 167 168
                                <TableCell
                                    style={{
                                        backgroundColor: colors.headerTable,
                                        width: 125,
                                    }}
                                >
                                    Trạng thái
                                </TableCell>
tdgiang committed
169
                            )} */}
tdgiang committed
170 171 172 173 174

                            <TableCell
                                style={{
                                    textAlign: 'center',
                                    backgroundColor: colors.headerTable,
tdgiang committed
175
                                    width: 160,
tdgiang committed
176 177 178 179 180 181 182 183 184 185 186 187 188
                                }}
                            >
                                Hành động
                            </TableCell>
                        </TableRow>
                    </TableHead>
                    <TableBody className={classes.columnTable}>
                        {data.map((row) => {
                            return (
                                <TableRow
                                    hover
                                    role="checkbox"
                                    tabIndex={-1}
tdgiang committed
189
                                    key={row.id}
tdgiang committed
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
                                >
                                    {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>
                                        )
                                    })}
tdgiang committed
213
                                    {/* 
tdgiang committed
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
                                    {checkRole(
                                        user,
                                        '/gas-station/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}>
                                                    Khóa
                                                </MenuItem>
                                            </Select>
                                        </TableCell>
tdgiang committed
241
                                    )} */}
tdgiang committed
242
                                    <TableCell>
tdgiang committed
243
                                        {/* {checkRole(
tdgiang committed
244 245 246
                                            user,
                                            '/gas-station/delete'
                                        ) ? (
tdgiang committed
247 248 249 250 251 252
                                            <Tooltip title="Xoá">
                                                <IconButton
                                                    onClick={() => {
                                                        setSelected({
                                                            ...row,
                                                            title: 'Xóa cây xăng',
tdgiang committed
253
                                                            content: `Bạn có muốn xóa cây xăng ${row.store_name} hay không?`,
tdgiang committed
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
                                                        })
                                                        setOpen(true)
                                                    }}
                                                    className={classes.button}
                                                    aria-label="Delete"
                                                >
                                                    <Icon color="error">
                                                        delete
                                                    </Icon>
                                                </IconButton>
                                            </Tooltip>
                                        ) : null}
                                        {checkRole(
                                            user,
                                            '/gas-station/update'
                                        ) ? (
                                            <Tooltip title="Cập nhật">
                                                <IconButton
                                                    onClick={() => {
                                                        history.push({
                                                            pathname:
                                                                '/gas-station/update',
                                                            state: row.id,
                                                        })
                                                    }}
                                                    className={classes.button}
                                                    aria-label="edit"
                                                >
                                                    <Icon color="primary">
                                                        edit
                                                    </Icon>
                                                </IconButton>
                                            </Tooltip>
tdgiang committed
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
                                        ) : null} */}

                                        {row.status == 1 && (
                                            <Tooltip title="Ngừng hoạt động">
                                                <IconButton
                                                    onClick={() => {
                                                        setSelected({
                                                            ...row,
                                                            title: 'Ngừng hoạt động cây xăng',
                                                            content: `Bạn có muốn ngừng hoạt động cây xăng ${row.store_name} hay không?`,
                                                        })
                                                        setOpen(true)
                                                    }}
                                                    className={classes.button}
                                                    aria-label="Delete"
                                                >
                                                    <Icon color="error">
                                                        stop_circle
                                                    </Icon>
                                                </IconButton>
                                            </Tooltip>
                                        )}

                                        {row.status == 5 && (
                                            <Tooltip title="Hoạt động">
                                                <IconButton
                                                    onClick={() => {
                                                        setSelected({
                                                            ...row,
                                                            title: 'Cho phép hoạt động cây xăng',
                                                            content: `Bạn có muốn cho phép hoạt động cây xăng ${row.store_name} hay không?`,
                                                        })
tdgiang committed
319
                                                        setOpenAccpet(true)
tdgiang committed
320 321 322 323 324 325 326 327 328 329 330 331 332 333
                                                    }}
                                                    className={classes.button}
                                                    aria-label="Delete"
                                                >
                                                    <Icon
                                                        style={{
                                                            color: '#00C65B',
                                                        }}
                                                    >
                                                        not_started
                                                    </Icon>
                                                </IconButton>
                                            </Tooltip>
                                        )}
tdgiang committed
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
                                        {row.status == 3 && (
                                            <Tooltip title="Phê duyệt">
                                                <IconButton
                                                    onClick={() => {
                                                        setSelected({
                                                            ...row,
                                                            title: 'Cho phép hoạt động cây xăng',
                                                            content: `Bạn có muốn cho phép hoạt động cây xăng ${row.store_name} hay không?`,
                                                        })
                                                        setOpenAccpet(true)
                                                    }}
                                                    className={classes.button}
                                                    aria-label="Delete"
                                                >
                                                    <Icon
                                                        style={{
                                                            color: '#00C65B',
                                                        }}
                                                    >
                                                        task_alt
                                                    </Icon>
                                                </IconButton>
                                            </Tooltip>
                                        )}

                                        {row.status == 1 || row.status == 0 ? (
                                            <Tooltip title="Điều chỉnh thời gian">
                                                <IconButton
                                                    onClick={() => {
                                                        setSelected({
                                                            ...row,
                                                        })
                                                        setOpenUpdate(true)
                                                    }}
                                                    className={classes.button}
                                                    aria-label="Delete"
                                                >
                                                    <Icon
                                                        style={{
                                                            color: '#00C65B',
                                                        }}
                                                    >
                                                        today
                                                    </Icon>
                                                </IconButton>
                                            </Tooltip>
                                        ) : null}

tdgiang committed
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
                                        <Tooltip title="Chi tiết">
                                            <IconButton
                                                onClick={() => {
                                                    history.push({
                                                        pathname:
                                                            '/gas-station/detail',
                                                        state: row.id,
                                                    })
                                                }}
                                                className={classes.button}
                                                aria-label="edit"
                                            >
                                                <Icon color="primary">
                                                    visibility
                                                </Icon>
                                            </IconButton>
                                        </Tooltip>
tdgiang committed
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
                                        {row.status == 4 && (
                                            <Tooltip title="Lý do từ chối ">
                                                <IconButton
                                                    onClick={() => {
                                                        setSelected({
                                                            ...row,
                                                        })
                                                        setOpenReason(true)
                                                    }}
                                                    className={classes.button}
                                                    aria-label="edit"
                                                >
                                                    <Icon
                                                        style={{
                                                            color: '#FF9500',
                                                        }}
                                                    >
                                                        info
                                                    </Icon>
                                                </IconButton>
                                            </Tooltip>
                                        )}
tdgiang committed
421 422 423 424 425 426 427 428 429 430 431 432
                                    </TableCell>
                                </TableRow>
                            )
                        })}
                    </TableBody>
                </Table>
            </TableContainer>
            <DialogTransition
                data={selected}
                open={open}
                handleClose={handleClose}
                onAgree={() => {
tdgiang committed
433
                    removeItem(selected)
tdgiang committed
434 435 436 437
                    handleClose()
                }}
            />

tdgiang committed
438 439 440 441 442 443 444 445 446 447 448
            <DialogReason
                data={selected}
                open={openReason}
                handleClose={() => {
                    setOpenReason(false)
                }}
                onAgree={() => {
                    handleClose()
                }}
            />

tdgiang committed
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470
            <AcceptStore
                open={openAccept}
                handleClose={() => {
                    setOpenAccpet(false)
                }}
                onAgree={(forever, date) => {
                    setOpenAccpet(false)
                    acceptStoreSub(selected, forever, date)
                }}
            />

            <UpdateTimeStore
                open={openUpdate}
                handleClose={() => {
                    setOpenUpdate(false)
                }}
                onAgree={(forever, date) => {
                    setOpenUpdate(false)
                    acceptUpdateTime(selected, forever, date)
                }}
            />

tdgiang committed
471 472 473 474 475 476 477 478 479 480 481 482 483 484
            <TablePagination
                component="div"
                page={pageIndex}
                count={totalRecords}
                rowsPerPage={10}
                rowsPerPageOptions={[]}
                onChangePage={handleChangePage}
                onChangeRowsPerPage={handleChangeRowsPerPage}
            />
        </Paper>
    )
}

export default TableList