import axios from '@/utils/axios';
import { makeFormData } from '@/utils/jsonToFormData';
import { enqueueSnackbar } from 'notistack';
import { DetailLabResultListType, LaboratoriumResultListType, ResponseListingClaimType } from "./Types";
import { fDate } from '@/utils/formatTime';

/**
 * Listing Daily Monitoring
 */
export const getDailyMonitoringList = async (  ): Promise<LaboratoriumResultListType[]> => {
    const response = await axios.get('/case_management/memberlist')
        .then((res) =>{
            return res.data.data.member_list;
        })
        .catch((res) => {
            enqueueSnackbar("server error !", {
                variant: 'error',
            });

            return [];
        });

    return response;
};

/**
 * Listing Claim
 */
export const getClaimList = async ( member_id: string ): Promise<ResponseListingClaimType> => {
    const response = await axios.get(`/case_management/claimlist/${member_id}`)
        .then((res) =>{
            return res.data.data;
        })
        .catch((res) => {
            enqueueSnackbar("server error !", {
                variant: 'error',
            });

            return null;
        });

    return response;
};

/**
 * Add Lab Result Detail
 */
export const AddLabResultDetail = async ( claim_code: string,data: DetailLabResultListType ): Promise<boolean> => {
    data.date = data.date != '' && data.date != null ? fDate(data.date) : '';

    const formData = makeFormData({...data});

    const response = await axios.post(`/case_management/laboratorium_result/detail/${claim_code}/add`, formData)
        .then((res) =>{
            enqueueSnackbar(res.data.message, {
                variant: 'success',
            });

            return true;
        })
        .catch((res) => {
            if (res.response.status == 400) {
                let arr_message = res.response.data.message;

                for (const key in arr_message) {
                    enqueueSnackbar(arr_message[key][0], {
                        variant: 'warning',
                    });
                }
            }
            else {
                enqueueSnackbar("server error !", {
                    variant: 'error',
                });
            }

            return false;
        });

    return response;
};

/**
 * Get Lab Result Detail List
 */
export const getLabResultDetailList = async ( claim_code: string ): Promise<DetailLabResultListType[]> => {
    const response = await axios.get(`/case_management/laboratorium_result/detail/${claim_code}/list`)
        .then((res) =>{
            return res.data.data.lab_result_list;
        })
        .catch((res) => {
            enqueueSnackbar("server error !", {
                variant: 'error',
            });

            return [];
        });

    return response;
};
