import apiClient from "./apiClient";
import {Order} from "../types/order";

const router = '/orders';

const ordersApi = {
    getOrders: async ({page, orderBy, desc, status, shipmentDate, shipmentWarehouseId}: {
        page: number,
        orderBy: string,
        desc: boolean,
        shipmentDate: string,
        status: number,
        shipmentWarehouseId: number
    }): Promise<Order[]> => {
        const params = {
            page: page,
            orderBy: orderBy,
            desc: Number(desc), // Преобразование boolean в Number (0 или 1)
            status: status,
            shipmentDate: shipmentDate,
            shipmentWarehouseId: shipmentWarehouseId
        };
        let response = await apiClient.get(`${router}/getOrders`, {params})
        return response.data;
    },
    getOrdersByProduct: async (params: {
        productId: number,
        orderBy: string,
        desc: boolean,
        shippingWarehouseId: number,
    }): Promise<Order[]> => {
        let response = await apiClient.get(`${router}/getByProductId`, {
            params: {
                ...params,
                shippingWarehouse: params.shippingWarehouseId,
            }
        });
        return response.data;
    },
    getOrderById: async (orderId: number): Promise<Order> => {
        let response = await apiClient.get(`${router}/getOrderById?orderId=${orderId}`);
        return response.data;
    }
}

export default ordersApi;