import CustomSelectByDependency from "@/core/components/DataTable/filter/fieldsType/CustomSelectByDependency";
import DataTableWithAuth from "@/core/components/DataTableWithAuth";
import { GET_PRIVACY_ADMIN_LIST } from "@/core/utils/routes";
import useInquiryPrivacyState from "@/lib/hooks/useInquiryPrivacyState";
import AutorenewIcon from "@mui/icons-material/Autorenew";
import DangerousIcon from "@mui/icons-material/Dangerous";
import ManageAccountsIcon from "@mui/icons-material/ManageAccounts";
import MapsHomeWorkIcon from "@mui/icons-material/MapsHomeWork";
import PsychologyAltIcon from "@mui/icons-material/PsychologyAlt";
import ReceiptLongIcon from "@mui/icons-material/ReceiptLong";
import SecurityIcon from "@mui/icons-material/Security";
import WindowIcon from "@mui/icons-material/Window";
import { Box, Stack } from "@mui/material";
import moment from "jalali-moment";
import { useMemo } from "react";
import RowActions from "./RowActions";

import ShowPrimaryArea from "./ShowPrimaryArea";

const PrivacyOfficeList = () => {
    const stateIcon = (state_id) => {
        if (state_id === 1) return <MapsHomeWorkIcon sx={{ color: "#A0B9C6" }} />;
        if (state_id === 2 || state_id === 5 || state_id === 13) return <SecurityIcon sx={{ color: "#995FA3" }} />;
        if (state_id === 3 || state_id === 6 || state_id === 14)
            return <ManageAccountsIcon sx={{ color: "#9A98B5" }} />;
        if (state_id === 4 || state_id === 7 || state_id === 15) return <PsychologyAltIcon sx={{ color: "#1B9AAA" }} />;
        if (state_id === 8) return <ReceiptLongIcon sx={{ color: "#9A98B5" }} />;
        if (state_id === 9 || state_id === 11) return <WindowIcon sx={{ color: "#DDDBCB" }} />;
        if (state_id === 10) return <DangerousIcon sx={{ color: "error.main" }} />;
        if (state_id === 12) return <AutorenewIcon sx={{ color: "#899878" }} />;
    };

    const columns = useMemo(() => {
        return [
            {
                accessorKey: "panjare_vahed_id",
                header: "کدرهگیری درخواست",
                id: "panjare_vahed_id",
                enableColumnFilter: true,
                datatype: "text",
                filterMode: "equals",
                sortDescFirst: false,
                columnFilterModeOptions: ["equals", "contains"],
                grow: false,
                size: 100,
                Cell: ({ row }) => <>{row.original.panjare_vahed_id}</>,
            },
            {
                accessorKey: "national_id",
                header: "کد ملی",
                id: "national_id",
                enableColumnFilter: true,
                datatype: "text",
                filterMode: "equals",
                sortDescFirst: false,
                columnFilterModeOptions: ["equals", "contains"],
                grow: false,
                size: 100,
                Cell: ({ row }) => <>{row.original.national_id}</>,
            },
            {
                accessorKey: "state_id",
                header: "وضعیت",
                id: "state_id",
                enableColumnFilter: true,
                datatype: "numeric",
                filterMode: "equals",
                sortDescFirst: false,
                grow: false,
                size: 100,
                ColumnSelectComponent: (props) => {
                    const { itemsList, loadingItemsList, errorItemsList } = useInquiryPrivacyState();

                    const getColumnSelectOptions = useMemo(() => {
                        if (loadingItemsList) {
                            return [{ value: "loading", label: "در حال بارگذاری..." }];
                        }
                        if (errorItemsList) {
                            return [{ value: "error", label: "خطا در بارگذاری" }];
                        }
                        return [
                            { value: "", label: "همه موارد" },
                            ...itemsList.map((item) => ({
                                value: item.id,
                                label: item.name,
                            })),
                        ];
                    }, [itemsList, loadingItemsList, errorItemsList]);

                    return (
                        <CustomSelectByDependency
                            {...props}
                            value={loadingItemsList ? "loading" : props.filterParameters.value}
                            columnSelectOption={getColumnSelectOptions}
                        />
                    );
                },
                Cell: ({ row }) => (
                    <Box sx={{ display: "flex", alignItems: "center" }}>
                        {stateIcon(row.original.state_id)}
                        <Box component="span" ml={1}>
                            {row.original.state_name}
                        </Box>
                    </Box>
                ),
            },
            {
                accessorKey: "plan_title",
                header: "عنوان طرح",
                id: "plan_title",
                enableColumnFilter: true,
                datatype: "text",
                filterMode: "equals",
                sortDescFirst: false,
                columnFilterModeOptions: ["equals", "contains"],
                grow: false,
                size: 100,
                Cell: ({ row }) => <>{row.original.plan_title}</>,
            },
            {
                accessorKey: "plan_group",
                header: "گروه طرح",
                id: "plan_group",
                enableColumnFilter: true,
                datatype: "text",
                filterMode: "equals",
                sortDescFirst: false,
                columnFilterModeOptions: ["equals", "contains"],
                grow: false,
                size: 100,
                Cell: ({ row }) => <>{row.original.plan_group}</>,
            },
            {
                accessorKey: "requested_organization",
                header: "دستگاه صادر کننده",
                id: "requested_organization",
                enableColumnFilter: true,
                datatype: "text",
                filterMode: "equals",
                sortDescFirst: false,
                columnFilterModeOptions: ["equals", "contains"],
                grow: false,
                size: 100,
                Cell: ({ row }) => <>{row.original.requested_organization}</>,
            },
            {
                accessorKey: "worksheet_id",
                header: "شناسه کاربرگ",
                id: "worksheet_id",
                enableColumnFilter: true,
                datatype: "text",
                filterMode: "equals",
                sortDescFirst: false,
                columnFilterModeOptions: ["equals", "contains"],
                grow: false,
                size: 100,
                Cell: ({ row }) => <>{row.original.worksheet_id}</>,
            },
            {
                accessorKey: "primary_area",
                header: "نمایش محدوده طرح",
                id: "primary_area",
                enableColumnFilter: true,
                datatype: "text",
                filterMode: "equals",
                sortDescFirst: false,
                columnFilterModeOptions: ["equals", "contains"],
                grow: false,
                size: 100,
                Cell: ({ row }) =>
                    row.original.primary_area ? (
                        <Stack alignItems={"center"} justifyContent={"center"}>
                            <ShowPrimaryArea primaryArea={row.original.primary_area} />
                        </Stack>
                    ) : (
                        "-"
                    ),
            },
            {
                accessorKey: "province_id",
                header: "استان",
                id: "province_id",
                enableColumnFilter: false,
                datatype: "numeric",
                sortDescFirst: false,
                grow: false,
                size: 100,
                Cell: ({ row }) => <>{row.original.province_name}</>,
            },
            {
                accessorKey: "city_id",
                header: "شهر",
                id: "city_id",
                enableColumnFilter: false,
                datatype: "numeric",
                sortDescFirst: false,
                grow: false,
                size: 100,
                Cell: ({ row }) => <>{row.original.city_name}</>,
            },
            {
                accessorKey: "isic",
                header: "کد آیسیک",
                id: "isic",
                enableColumnFilter: true,
                datatype: "text",
                filterMode: "equals",
                sortDescFirst: false,
                columnFilterModeOptions: ["equals", "contains"],
                grow: false,
                size: 100,
                Cell: ({ row }) => <>{row.original.isic}</>,
            },
            {
                accessorFn: (row) => moment(row.request_date).locale("fa").format("YYYY/MM/DD"),
                header: "تاریخ درخواست",
                id: "request_date",
                enableColumnFilter: true,
                datatype: "date",
                filterMode: "between",
                grow: false,
                size: 100,
            },
        ];
    }, []);

    return (
        <>
            <Box sx={{ p: 1, border: 1, borderColor: "divider", borderRadius: 1 }}>
                <DataTableWithAuth
                    need_filter={true}
                    columns={columns}
                    table_url={GET_PRIVACY_ADMIN_LIST}
                    page_name={"PrivacyOffice"}
                    table_name={"PrivacyOfficeList"}
                    enableRowActions
                    positionActionsColumn={"first"}
                    RowActions={RowActions}
                />
            </Box>
        </>
    );
};

export default PrivacyOfficeList;
