import api from './apiClient'; // ----------------------------- // Country API // ----------------------------- export const countryAPI = { // GET /api/v1/Country (with pagination) async list(params = {}) { const { currentPage = 1, pageSize = 10, ...otherParams } = params; const res = await api.get('/api/v1/Country', { params: { currentPage, pageSize, ...otherParams }, skipAuthRedirect: true }); return res?.data?.data?.data || []; }, // GET /api/v1/Country/All (all countries without pagination) async listAll() { const res = await api.get('/api/v1/Country/All', { skipAuthRedirect: true }); return res?.data?.data || []; }, // POST /api/v1/Country async create(country) { const payload = { Name: String(country?.name || ''), PhoneCode: String(country?.phoneCode || ''), CurrencyCode: String(country?.currencyCode || ''), TimeZoneName: String(country?.timeZoneName || country?.timeZone || ''), }; const res = await api.post('/api/v1/Country', payload, { skipAuthRedirect: true }); return res?.data; }, // PUT /api/v1/Country/{countryId} async update(countryId, country) { if (!countryId) { throw new Error('Country ID is required'); } // ساخت payload - سرور انتظار فیلدها با حرف بزرگ دارد const payload = { Name: country?.name ? String(country.name).trim() : '', PhoneCode: country?.phoneCode ? String(country.phoneCode).trim() : '', CurrencyCode: country?.currencyCode ? String(country.currencyCode).trim() : '', TimeZoneName: country?.timeZoneName || country?.timeZone ? String((country.timeZoneName || country.timeZone).trim()) : '', }; const url = `/api/v1/Country/${encodeURIComponent(countryId)}`; console.log('Country API Update:', { url, countryId, payload }); try { const res = await api.put(url, payload, { skipAuthRedirect: true }); return res?.data; } catch (error) { console.error('Country API Update Error:', { url, countryId, payload, error: error?.response?.data || error?.message || error }); throw error; } }, // POST /api/v1/Country/{countryId}/Province async createProvince(countryId, province) { const payload = { CountryId: countryId, ProvinceName: String(province?.provinceName || ''), }; const res = await api.post(`/api/v1/Country/${encodeURIComponent(countryId)}/Province`, payload, { skipAuthRedirect: true }); return res?.data; }, // GET /api/v1/Country/{countryId}/Province async getProvinces(countryId) { const res = await api.get(`/api/v1/Country/${encodeURIComponent(countryId)}/Province`, { skipAuthRedirect: true }); return res?.data?.data || []; }, };