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

const ShowSampleList = ({ sampleInfo, rowData }) => {
    const parsedData = (data) => {
        return data.data.map((item) => ({
            ...JSON.parse(item.data),
            id: item.id,
            updated_at: item.updated_at,
        }));
    };

    const columns = useMemo(
        () => [
            {
                accessorKey: "id",
                header: "کد یکتا",
                id: "id",
                enableColumnFilter: false,
                datatype: "text",
            },
            ...sampleInfo.map((item) => ({
                accessorKey: item.id.toString(),
                header: item.name,
                id: item.id.toString(),
                enableColumnFilter: false,
                datatype: "text",
            })),
            {
                accessorKey: "updated_at",
                header: "تاریخ بروزرسانی",
                id: "updated_at",
                enableColumnFilter: false,
                datatype: "date",
                Cell: ({ renderedCellValue }) => (
                    <Typography variant="body2">
                        {renderedCellValue ? moment(renderedCellValue).locale("fa").format("HH:mm | YYYY/MM/DD") : "-"}
                    </Typography>
                ),
            },
        ],
        [sampleInfo]
    );

    const ToolbarWithRowData = (props) => <Toolbar rowData={rowData} {...props} />;
    const RowActionsWithRowData = (props) => <RowActions rowData={rowData} {...props} />;

    return (
        <Box>
            <AzmayeshInfo rowData={rowData} />
            <Box sx={{ p: 1, border: 1, borderColor: "divider", borderRadius: 1 }}>
                <DataTableWithAuth
                    table_title={"نمونه های ثبت شده"}
                    need_filter={false}
                    columns={columns}
                    table_url={`${GET_SAMPLE_LIST}/index/${rowData.id}`}
                    page_name={`azmayesh_${rowData.azmayesh_type_id}`}
                    table_name={"sample_list"}
                    TableToolbar={ToolbarWithRowData}
                    specific_data={parsedData}
                    enableRowActions
                    positionActionsColumn={"last"}
                    RowActions={RowActionsWithRowData}
                />
            </Box>
        </Box>
    );
};

export default ShowSampleList;
