import { defineStore } from "pinia";
import { ref } from "vue";
import api from "../lib/axios";

export const useLoadsStore = defineStore('load', () => {
    const companyid = localStorage.getItem('id');
    const currentLoad = ref(null);
    const loads = ref([])
    const proposalsOfLoads = ref([]);
    const openModalEdit = ref(false);
    const openAttachmentsModal = ref(false);
    const openProposalsModal = ref(false);


    const getCompanyLoads = async(filterQuery) => {
        let filterArr = Object.values(filterQuery);
        let cleanfilterArr =  filterArr.filter(n=>n);
        var filterStr = "";
        if(cleanfilterArr.length >0){
            filterStr = cleanfilterArr.join("&");
        }
        // console.log(filterStr);
        try {
            const endpoint = `/loads?company=${companyid}&${filterStr}`;
            const {data} = await api.get(endpoint);
            loads.value = data.data;
        } catch (error) {
            loads.value = [];
            console.log(error);
        }
    }

    const getProposalsOfLoads = async(filterQuery) => {
        try {
            const endpoint = `/proposals/?load=${filterQuery}`;
            const {data} = await api.get(endpoint);
            // console.log(data);
            proposalsOfLoads.value = data.data;
        } catch (error) {
            proposalsOfLoads.value = [];
            console.log(error);
        }
    }
    
    const saveLoad = async(load) => {
        try {
            const endpoint = `/loads/`;
            const {data} = await api.post(endpoint, load);
            return data;
        } catch (error) {
            console.log(error);
            return null;
        }
    }

    const updateProposal = async(id, proposal) => {
        try {
            const endpoint = `/proposals/${id}`;
            const {data} = await api.patch(endpoint, proposal);
            // console.log(data);
            return data;
        } catch (error) {
            console.log(error);
            return null;
        }
    }

    const deleteProposal = async(id) => {
        try {
            const endpoint = `/proposals/${id}`;
            const {data} = await api.delete(endpoint);
            // console.log(data);
            return data;
        } catch (error) {
            console.log(error);
            return null;
        }
    }

    const updateLoad = async(loadId, load) => {
        try {
            const endpoint = `/loads/${loadId}`;
            const {data} = await api.patch(endpoint, load);
            // console.log(data);
            return data;
        } catch (error) {
            console.log(error);
            return null;
        }
    }

    const deleteLoad = async(loadId) => {
        try {
            const endpoint = `/loads/${loadId}`;
            console.log(endpoint);
            const {data} = await api.delete(endpoint);
            return data;
        } catch (error) {
            console.log(error);
            return null;
        }
    }

    const getLoad = async(filterQuery) => {
        try {
            const endpoint = `/loads/${filterQuery}`;
            const {data} = await api.get(endpoint);
            return data;
        } catch (error) {
            console.log(error);
            return [];
        }
    }


   return {
       openModalEdit,
       openProposalsModal,
       openAttachmentsModal,
       getProposalsOfLoads,
       getCompanyLoads,
       deleteLoad,
       getLoad,
       saveLoad,
       updateLoad,
       updateProposal,
       deleteProposal,
       loads,
       currentLoad,
       proposalsOfLoads,
   }
});