<script setup>
import { onMounted, ref, watch } from 'vue';
import { useQuasar } from 'quasar';
import { useArrayData } from 'composables/useArrayData';
import VnInput from 'src/components/common/VnInput.vue';
import useRedirect from 'src/composables/useRedirect';
import { useI18n } from 'vue-i18n';
import { useStateStore } from 'src/stores/useStateStore';

const quasar = useQuasar();
const { t } = useI18n();
const state = useStateStore();

const props = defineProps({
    dataKey: {
        type: String,
        required: true,
    },
    label: {
        type: String,
        required: false,
        default: 'Search',
    },
    info: {
        type: String,
        required: false,
        default: '',
    },
    redirect: {
        type: Boolean,
        required: false,
        default: true,
    },
    url: {
        type: String,
        default: '',
    },
    filter: {
        type: Object,
        default: null,
    },
    where: {
        type: Object,
        default: null,
    },
    order: {
        type: String,
        default: '',
    },
    limit: {
        type: Number,
        default: 10,
    },
    userParams: {
        type: Object,
        default: null,
    },
    staticParams: {
        type: Array,
        default: () => [],
    },
    exprBuilder: {
        type: Function,
        default: null,
    },
    customRouteRedirectName: {
        type: String,
        default: '',
    },
});

let arrayData = useArrayData(props.dataKey, { ...props });
let store = arrayData.store;
const searchText = ref('');
const { navigate } = useRedirect();

watch(
    () => props.dataKey,
    (val) => {
        arrayData = useArrayData(val, { ...props });
        store = arrayData.store;
    }
);

onMounted(() => {
    const params = store.userParams;
    if (params && params.search) {
        searchText.value = params.search;
    }
});

async function search() {
    const staticParams = Object.entries(store.userParams).filter(
        ([key, value]) => value && (props.staticParams || []).includes(key)
    );
    store.skip = 0;
    await arrayData.applyFilter({
        params: {
            ...Object.fromEntries(staticParams),
            search: searchText.value,
        },
    });

    if (!props.redirect) return;

    navigate(store.data, {
        customRouteRedirectName: props.customRouteRedirectName,
        searchText: searchText.value,
    });
}
</script>
<template>
    <Teleport to="#searchbar" v-if="state.isHeaderMounted()">
        <QForm @submit="search" id="searchbarForm">
            <VnInput
                id="searchbar"
                v-model="searchText"
                :placeholder="t(props.label)"
                dense
                standout
                autofocus
            >
                <template #prepend>
                    <QIcon
                        v-if="!quasar.platform.is.mobile"
                        class="cursor-pointer"
                        name="search"
                        @click="search"
                    />
                </template>
                <template #append>
                    <QIcon
                        v-if="props.info && $q.screen.gt.xs"
                        name="info"
                        class="cursor-info"
                    >
                        <QTooltip>{{ t(props.info) }}</QTooltip>
                    </QIcon>
                </template>
            </VnInput>
        </QForm>
    </Teleport>
</template>

<style lang="scss" scoped>
@media screen and (min-width: $breakpoint-sm-max) {
    .q-field {
        width: 450px;
    }
}

.q-field {
    transition: width 0.36s;
}
</style>

<style lang="scss">
.cursor-info {
    cursor: help;
}
#searchbar {
    .q-field--standout.q-field--highlighted .q-field__control {
        background-color: white;
        color: black;
        .q-field__native,
        .q-icon {
            color: black !important;
        }
    }
}
</style>
