import {defineStore} from 'pinia'
import {config} from '@/config'
import performRequest from '@/backend.js'


export const useReportsStore = defineStore('reports', {
        state() {
            return {
                reports: [],
                loading: false,
            }
        },
        getters: {},
        actions: {
            async fetchReports(materialId, supplierIds) {
                if (supplierIds == null || materialId == null) return;

                this.loading = true;
                this.reports = [];

                const params = new URLSearchParams();
                params.append('material', materialId);
                params.append('sources', supplierIds);

                const url = `${config.backendUrl}/reports/view/${params.size === 0 ? '' : '?'}${params.toString()}`;

                this.reports = await performRequest(this,'GET', url, null).catch(e => {
                    this.loading = false;
                });

                this.loading = false;
            }
        }
    }
);