<script setup>
    import { onMounted, ref, watch } from 'vue';
    import { useCompanyStore } from '../stores/company';
    import { useAuthStore } from '../stores/auth';
    import Spiner from '../components/ui/Spiner.vue';
    import CardBudget from '../components/CardBudget.vue';
    import CardEmpty from '../components/CardEmpty.vue';
    import CreateBudgetModal from '../components/CreateBudgetModal.vue';

    const companyStore = useCompanyStore();
    const authStore = useAuthStore();

    const loading = ref(false);
    const filterQuery = ref([]);
    const query = ref('');
    const currentBudget = ref(null);
    const openModal = ref(false);

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

    const getInitData = async() => {
        loading.value = true;
        filterQuery.value.company = "company="+ localStorage.getItem('id');
        await companyStore.getBudgetsCompany(filterQuery.value, false)
        loading.value = false;
    }

    const getBudgetsWithFilters = async(filter) => {
        loading.value = true;
        await companyStore.getBudgetsCompany(filter, true);
        loading.value = false;
    }

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

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

    const clearFilter = () => {
        filterQuery.value.search = "";
        filterQuery.value.company = "company="+ localStorage.getItem('id');

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

    const handleSetCurrentBudget = (budget) => {
        openModal.value = true;
        currentBudget.value = budget;
    }

    const handleResetCurrentBudget = () => {
        openModal.value = false;
        currentBudget.value = null;
    }

</script>

<template>
    <CreateBudgetModal
        v-if="openModal === true"
        :budget="currentBudget"
        @reset-budget="handleResetCurrentBudget"
    />
    <div>
        <h2 class="title my-2">Calculadora</h2>
    </div>
    <div class="box-filters">
        <div class="box-search">
            <input class="form-control custom-search" type="search" name="" placeholder="Buscar por cliente" 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
            class="btn-primary-sm radius-sm"
            data-toggle="modal" data-target="#budgetModal"
            @click="handleSetCurrentBudget(null)"
        ><i class="fa-solid fa-plus"></i> <span class="clear-sm"> Crear</span><span class="clear-md"> presupuesto</span></button>
    </div>
    <Spiner v-if="loading"/>
    <div v-else>
        <CardBudget
            v-if="companyStore.budgets.length > 0"
            v-for="budget in companyStore.budgets"
            :budget="budget"
            @set-budget="handleSetCurrentBudget(budget)"
        />
        <CardEmpty 
            v-else 
            text="No hay presupuestos agregados"
        />
    </div>
</template>

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

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

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

    @media (max-width: 768px) {

        .box-search {
            width: 100%;
        }
        .box-filters {
            gap: .3rem;
        }
    }
</style>