"use server";

import { BASE_API_URL } from "@/config/api";
import { Claim, ProjectCombo, SupportingDocument } from ".";
import { serverFetchJson } from "@/app/utils/fetchUtil";
import { revalidateTag } from "next/cache";

export interface ClaimInputFormByStaff {
    id: number | null;
    code: string | null;
    expenseType: string;
    status: string;

    addClaimDetails: ClaimDetailTable[]

    // is grid editing
    isGridEditing: boolean | null;
}

export interface ClaimDetailTable {
    id: number;
    invoiceDate: Date;
    description: string;
    project: number;
    amount: number;
    supportingDocumentName: string;
    oldSupportingDocument: SupportingDocument;
    newSupportingDocument: File;
    isNew: boolean;
}

export interface SaveClaimResponse {
    claim: Claim;
    message: string;
}

export const saveClaim = async (data: FormData) => {
    console.log(data)
    const saveCustomer = await serverFetchJson<SaveClaimResponse>(
        `${BASE_API_URL}/claim/save`,
        {
            method: "POST",
            body: data,
            // headers: { "Content-Type": "multipart/form-data" },
        },
    );

    revalidateTag("claims");

    return saveCustomer;
};