<script setup>
    import { onMounted, ref, watch } from 'vue';
    import Spiner from '../components/ui/Spiner.vue';
    import CardLoad from '../components/CardLoad.vue';
    import AttachmentsModal from '../components/AttachmentsModal.vue';
    import { useLoadsStore } from '../stores/loads';
    import FormLoadModal from '../components/FormLoadModal.vue';
import ProposalsModal from '../components/ProposalsModal.vue';
import CardEmpty from '../components/CardEmpty.vue';

    const loadStore = useLoadsStore();
    const loading = ref(false);
    const query = ref('');
    const filterQuery = ref([]);

    onMounted(() =>{
        getDataLoadsInit();
    })

    watch(query, () => {
        if(query.value.length === 0){
            filterQuery.value.search = "";
            getLoadWithFilters(filterQuery.value);
        }
    });

    const getDataLoadsInit = async() => {
        filterQuery.value.status = "status[$ne]="+"Closed";
        loading.value = true;
        await loadStore.getCompanyLoads(filterQuery.value);
        loading.value = false;
    }
    
    const getLoadWithFilters = async(filter) => {
        loading.value = true;
        await loadStore.getCompanyLoads(filter);
        loading.value = false;
    }

    const search = () => {
        if(query.value.length >= 2){
            // filterQuery.value = "company_name[$regex]=" + query.value + "&company_name[$options]=i";
            filterQuery.value.search = "posted_by_name[$regex]="+query.value+"&posted_by_name[$options]=i";
            getLoadWithFilters(filterQuery.value);
        } 
    
    }

    const clearFilter = () => {
        filterQuery.value.search = ""
        filterQuery.value.status = "status[$ne]="+"Closed";

        if(query.value == ''){
            getDataLoadsInit();
        } else {
            query.value = '';
        }
    }

    const loadHistory = () => {
        filterQuery.value.status = ""
        filterQuery.value.status = "status="+ "Closed";
        getLoadWithFilters(filterQuery.value);
    }
</script>

<template>
    <AttachmentsModal v-if="loadStore.openAttachmentsModal"/>
    <FormLoadModal v-if="loadStore.openModalEdit"/>
    <ProposalsModal v-if="loadStore.openProposalsModal"/>
    <div>
        <h2 class="title mb-5">Mis cargas publicadas</h2>
        <div>
            <div class="box-filters">
                <div class="box-search">
                    <input class="form-control custom-search" type="search" name="" placeholder="Nombre de la persona que publica" id="" @:input="search()" v-model="query" aria-label="Search">
                </div>
                <button 
                    class="btn btn-danger bg-dark" type="button" @click="clearFilter">
                    <i class="fa-solid fa-arrow-rotate-right"></i>
                    <span class="clear-sm"> Reset</span><span class="clear-md"> filtros</span>
                </button>
                <button
                    @click="loadHistory"
                    class="btn-primary-sm radius-sm"
                ><i class="fa-solid fa-clock-rotate-left"></i><span class="clear-sm"> Historial</span><span class="clear-md"> de cargas</span></button>
                <button
                    class="btn-primary-sm radius-sm"
                    data-toggle="modal" data-target="#formLoadModal"
                    @click="loadStore.openModalEdit = true"
                ><i class="fa-solid fa-plus"></i> <span class="clear-sm"> Crear</span><span class="clear-md"> carga</span></button>
            </div>
            <Spiner v-if="loading"/>
            <div v-else>
                <CardLoad 
                    v-if="loadStore.loads.length > 0"
                    v-for="load in loadStore.loads"
                    :key="load._id"
                    :load="load" 
                />
                <CardEmpty v-else text="No hay cargas agregadas"/>
            </div>
        </div>
    </div>
</template>

<style scoped>
    .box-filters {
        display: flex;
        flex-direction: row;
        justify-content: end;
        gap: 1rem;
    }

    .box-search {
        width: 40%;
    }

    .radius-sm{
        border-radius: 5px;
    }

    .custom-search {
        width: 100%;
        padding: 12px 20px;
    }

    @media (max-width: 1024px) {
        .box-search {
            width: 50%;
        }
        .box-filters {
            gap: .4rem;
        }
    }

    @media (max-width: 768px) {

        .box-search {
            width: 100%;
        }
        .box-filters {
            gap: .3rem;
        }
    }

</style>