import { xfetch } from '~/composables/useXfetch' const mainUrl = '/api/v1/material' export async function getMaterials(params: any = null) { try { let url = mainUrl if (params && typeof params === 'object' && Object.keys(params).length > 0) { const searchParams = new URLSearchParams() for (const key in params) { if (params[key] !== null && params[key] !== undefined && params[key] !== '') { searchParams.append(key, params[key]) } } const queryString = searchParams.toString() if (queryString) url += `?${queryString}` } const resp = await xfetch(url, 'GET') const result: any = {} result.success = resp.success result.body = (resp.body as Record) || {} return result } catch (error) { console.error('Error fetching materials:', error) throw new Error('Failed to fetch materials') } } export async function getMaterialDetail(id: number | string) { try { const resp = await xfetch(`${mainUrl}/${id}`, 'GET') const result: any = {} result.success = resp.success result.body = (resp.body as Record) || {} return result } catch (error) { console.error('Error fetching material detail:', error) throw new Error('Failed to get material detail') } } export async function postMaterial(record: any) { try { const resp = await xfetch(mainUrl, 'POST', record) const result: any = {} result.success = resp.success result.body = (resp.body as Record) || {} return result } catch (error) { console.error('Error posting material:', error) throw new Error('Failed to post material') } } export async function patchMaterial(id: number | string, record: any) { try { const resp = await xfetch(`${mainUrl}/${id}`, 'PATCH', record) const result: any = {} result.success = resp.success result.body = (resp.body as Record) || {} return result } catch (error) { console.error('Error putting material:', error) throw new Error('Failed to put material') } } export async function removeMaterial(id: number | string) { try { const resp = await xfetch(`${mainUrl}/${id}`, 'DELETE') const result: any = {} result.success = resp.success result.body = (resp.body as Record) || {} return result } catch (error) { console.error('Error deleting record:', error) throw new Error('Failed to delete material') } }