"use client";

import { useMemo } from "react";
import { Box, Typography } from "@mui/material";
import DataTableWithAuth from "@/core/components/DataTableWithAuth";
import Toolbar from "./Toolbar";
import { GET_AZMAYESH_LIST } from "@/core/utils/routes";
import ShowLocation from "./ShowLocation";
import moment from "jalali-moment";
import RowActions from "./RowActions";
import ShowSampleOfAzmayesh from "./RowActions/ShowSampleOfAzmayesh";

const AzmayeshList = () => {
    const columns = useMemo(
        () => [
            {
                accessorKey: "id",
                header: "کد یکتا",
                id: "id",
                enableColumnFilter: false,
                datatype: "text",
                filterMode: "notEquals",
                grow: false,
                size: 100
            },
            {
                accessorKey: "province_name",
                header: "استان",
                id: "province_name",
                enableColumnFilter: false,
                datatype: "text",
                filterMode: "notEquals",
            },
            {
                accessorKey: "azmayesh_type_name",
                header: "نوع آزمایش",
                id: "azmayesh_type_name",
                enableColumnFilter: false,
                datatype: "text",
                filterMode: "notEquals",
            },
            {
                accessorKey: "contract_subitem_id",
                header: "کد یکتا پروژه",
                id: "contract_subitem_id",
                enableColumnFilter: false,
                datatype: "text",
                filterMode: "equals",
            },
            {
                accessorKey: "project_name",
                header: "پروژه",
                id: "project_name",
                enableColumnFilter: false,
                datatype: "text",
                filterMode: "equals",
            },
            {
                accessorKey: "location",
                header: "نمایش مختصات",
                id: "location",
                enableColumnFilter: false,
                datatype: "text",
                enableSorting: false,
                filterMode: "equals",
                grow: false,
                size: 100,
                muiTableBodyCellProps: {
                    sx: {
                        borderLeft: "1px solid #e1e1e1",
                        py: 0,
                        "&:first-of-type": {
                            borderLeft: "unset",
                        },
                    },
                },
                Cell: ({ row }) => <ShowLocation lat={row.original.lat} lng={row.original.lng} />,
            },
            {
                accessorKey: "employer",
                header: "کارفرما",
                id: "employer",
                enableColumnFilter: false,
                datatype: "text",
                filterMode: "equals",
            },
            {
                accessorKey: "consultant",
                header: "مشاور",
                id: "consultant",
                enableColumnFilter: false,
                datatype: "text",
                filterMode: "equals",
            },
            {
                accessorKey: "contractor",
                header: "پیمانکار",
                id: "contractor",
                enableColumnFilter: false,
                datatype: "text",
                filterMode: "equals",
            },
            {
                accessorKey: "applicant",
                header: "متقاضی",
                id: "applicant",
                enableColumnFilter: false,
                datatype: "text",
                filterMode: "equals",
            },
            {
                accessorKey: "work_number",
                header: "شماره کار",
                id: "work_number",
                enableColumnFilter: false,
                datatype: "text",
                filterMode: "equals",
            },
            {
                accessorKey: "request_number",
                header: "شماره درخواست",
                id: "request_number",
                enableColumnFilter: false,
                datatype: "text",
                filterMode: "equals",
            },
            {
                accessorKey: "request_date",
                header: "تاریخ درخواست",
                id: "request_date",
                enableColumnFilter: false,
                datatype: "date",
                filterMode: "equals",
                grow: false,
                size: 100,
                Cell: ({ renderedCellValue }) => (
                    <Typography variant="body2">
                        {renderedCellValue ? moment(renderedCellValue).locale("fa").format("YYYY/MM/DD") : "-"}
                    </Typography>
                ),
            },
            {
                accessorKey: "report_date",
                header: "تاریخ گزارش",
                id: "report_date",
                enableColumnFilter: false,
                datatype: "date",
                filterMode: "equals",
                grow: false,
                size: 100,
                Cell: ({ renderedCellValue }) => (
                    <Typography variant="body2">
                        {renderedCellValue ? moment(renderedCellValue).locale("fa").format("YYYY/MM/DD") : "-"}
                    </Typography>
                ),
            },
            {
                accessorKey: "updated_at",
                header: "تاریخ بروزرسانی",
                id: "updated_at",
                enableColumnFilter: false,
                datatype: "date",
                filterMode: "equals",
                grow: false,
                size: 100,
                Cell: ({ renderedCellValue }) => (
                    <Typography variant="body2">
                        {renderedCellValue ? moment(renderedCellValue).locale("fa").format("HH:mm | YYYY/MM/DD") : "-"}
                    </Typography>
                ),
            },
            {
                accessorKey: "samples",
                header: "نمونه ها",
                id: "samples",
                enableColumnFilter: false,
                datatype: "text",
                enableSorting: false,
                filterMode: "equals",
                grow: false,
                size: 100,
                muiTableBodyCellProps: {
                    sx: {
                        borderLeft: "1px solid #e1e1e1",
                        py: 0,
                        "&:first-of-type": {
                            borderLeft: "unset",
                        },
                    },
                },
                Cell: ({ row }) => <ShowSampleOfAzmayesh rowData={row.original} />,
            },
        ],
        []
    );

    return (
        <>
            <Box sx={{ p: 1, border: 1, borderColor: "divider", borderRadius: 1 }}>
                <DataTableWithAuth
                    table_title={"لیست آزمایشات"}
                    need_filter={false}
                    columns={columns}
                    table_url={GET_AZMAYESH_LIST}
                    page_name={"azmayesh"}
                    table_name={"azmayeshList"}
                    TableToolbar={Toolbar}
                    enableRowActions
                    positionActionsColumn={"last"}
                    RowActions={RowActions}
                />
            </Box>
        </>
    );
};
export default AzmayeshList;
