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

/**
 * Listing Daily Monitoring
 */
export const getDailyMonitoringList = async (  ): Promise<DailyMonitoringListType[]> => {
    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 Monitoring Detail
 */
export const AddMonitoringDetail = async ( claim_code: string,data: DetailMonitoringListType ): Promise<boolean> => {
    data.lab_date = data.lab_date != '' && data.lab_date != null ? fDateOnly(data.lab_date) : '';

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

    const response = await axios.post(`/case_management/daily_monitoring/detail/${claim_code}/add-request`, 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 Monitoring Detail List
 */
export const getMonitoringDetailList = async ( claim_code: string ): Promise<DetailMonitoringListType[]> => {
    const response = await axios.get(`/case_management/daily_monitoring/detail/${claim_code}/list`)
        .then((res) =>{
            return res.data.data.detail_list;
        })
        .catch((res) => {
            enqueueSnackbar("server error !", {
                variant: 'error',
            });

            return [];
        });

    return response;
};
