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

export const useLoadsStore = defineStore('load', () => {

    const currentLoad = ref(null);
    const loads = ref([])
    const loadsDashboard = ref([]);
    const loadsTotal = ref(0)
    const loadsCurrentPage = ref(0)
    const proposalsOfLoads = ref([]);
    const openModalEdit = ref(false);
    const openAttachmentsModal = ref(false);
    const openProposalsModal = ref(false);

    const getLoadsAll = async(reload = false) => {
        const companyid = localStorage.getItem('id');
        if(loadsDashboard.value.length <= 0 || reload) {
            try {
                const endpoint = `/v1/loads/find?elements=100`;
                const {data} = await api.get(endpoint);
                loadsDashboard.value = data.data;
            } catch (error) {
                loadsDashboard.value = [];
                console.log(error);
            }
        }
    }

    const getProposalCompanyAll = async(reload = false) => {
        const companyId = localStorage.getItem('id');
        try {
            if(loadsDashboard.value.length <= 0 || reload) {
                const endpoint = `/v1/proposals/find?carrier=${companyId}&elements=100`;
                const {data} = await api.get(endpoint);
                console.log(data);
                loadsDashboard.value = data.data.map( (e) => e.load);
            }
        } catch (error) {
            loadsDashboard.value = [];
        }
    }

    const getCompanyLoads = async(filterQuery, reload = false) => {
        const companyid = localStorage.getItem('id');
        let filterArr = Object.values(filterQuery);
        let cleanfilterArr =  filterArr.filter(n=>n);
        var filterStr = "";
        if(cleanfilterArr.length >0){
            filterStr = cleanfilterArr.join("&");
        }
        if(loads.value.length <= 0 || reload) {
            try {
                const endpoint = `/v1/loads/find?company=${companyid}&${filterStr}&$sort%5BcreatedAt%5D=-1`;
                const {data} = await api.get(endpoint);
                loads.value = data.data;
                loadsTotal.value = data.total;
            } catch (error) {
                loads.value = [];
                loadsTotal.value = 0;
                console.log(error);
            }
        }
    }

    const getProposalsOfLoads = async(filterQuery) => {
        try {
            const endpoint = `/v1/proposals/find?load=${filterQuery}`;
            console.log(endpoint)
            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 = `/v1/loads/new`;
            const {data} = await api.post(endpoint, load);
            loadsTotal.value++;
            return data;
        } catch (error) {
            console.log(error);
            return null;
        }
    }

    const updateProposal = async(id, proposal) => {
        try {
            const endpoint = `/v1/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 = `/v1/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 = `/v1/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 = `/v1/loads/${loadId}`;
            console.log(endpoint);
            const {data} = await api.delete(endpoint);
            loadsTotal.value--;
            return data;
        } catch (error) {
            console.log(error);
            return null;
        }
    }

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

    const clear = () => {
        currentLoad.value = null;
        loads.value = [];
        loadsDashboard.value = [];
        loadsTotal.value = 0;
        loadsCurrentPage.value = 0;
        proposalsOfLoads.value = [];
        openModalEdit.value = false;
        openAttachmentsModal.value = false;
        openProposalsModal.value =  false;
    }


   return {
        clear,
       openModalEdit,
       openProposalsModal,
       openAttachmentsModal,
       getProposalsOfLoads,
       getCompanyLoads,
       getLoadsAll,
       getProposalCompanyAll,
       loadsDashboard,
       deleteLoad,
       getLoad,
       saveLoad,
       updateLoad,
       updateProposal,
       deleteProposal,
       loads,
       loadsCurrentPage,
       loadsTotal,
       currentLoad,
       proposalsOfLoads,
   }
});