<script
    setup
    lang="ts"
>
    import { onMounted, ref } from 'vue';
    import { QSelect, QInput } from 'quasar';
    import { useUiStore } from 'src/stores/ui-store';
    import { SHIFT_OPTIONS } from 'src/modules/timesheets/utils/shift.util';
    import type { Shift } from 'src/modules/timesheets/models/shift.models';

    const ui_store = useUiStore();

    const COMMENT_LENGTH_MAX = 280;

    const shift = defineModel<Shift>('shift', { required: true });
    const shift_type_selected = ref(SHIFT_OPTIONS.find(option => option.value == shift.value.type));
    const select_ref = ref<QSelect | null>(null);
    const error_message = ref('');

    const { errorMessage = undefined, isTimesheetApproved = false } = defineProps<{
        dense?: boolean;
        isTimesheetApproved?: boolean;
        errorMessage?: string | undefined;
    }>();

    const emit = defineEmits<{
        'requestDelete': [void];
        'onTimeFieldBlur': [void];
    }>();

    const onTimeFieldBlur = (time_string: string) => {
        if (time_string.length < 1 || !time_string) {
            shift.value.has_error = true;
            error_message.value = 'timesheet.errors.SHIFT_TIME_REQUIRED'
        } else {
            shift.value.has_error = false;
            error_message.value = '';
            emit('onTimeFieldBlur');
        } 
    }

    const getCommentCounterColor = (comment_length: number) => {
        if (comment_length < 200) return 'primary';
        if (comment_length < 250) return 'warning';
        return 'negative';
    };

    onMounted(() => {
        if (ui_store.focus_next_component) {
            select_ref.value?.focus();
            select_ref.value?.showPopup();
            shift_type_selected.value = undefined;
            ui_store.focus_next_component = false;
        }

        if (errorMessage) 
            error_message.value = errorMessage;
    });
</script>

<template>
    <div :class="ui_store.is_mobile_mode ? 'column' : 'row'">
        <div
            class="row items-center text-uppercase rounded-5"
            :class="ui_store.is_mobile_mode ? 'col q-mb-xs q-px-xs' : 'col-4'"
        >
            <!-- shift type -->
            <q-select
                ref="select"
                v-model="shift_type_selected"
                :standout="$q.dark.isActive ? 'bg-blue-grey-3' : 'bg-blue-grey-9'"
                dense
                :borderless="(shift.is_approved && isTimesheetApproved)"
                :readonly="(shift.is_approved && isTimesheetApproved)"
                :options-dense="!ui_store.is_mobile_mode"
                hide-dropdown-icon
                :menu-offset="[0, 10]"
                menu-anchor="bottom middle"
                menu-self="top middle"
                :options="SHIFT_OPTIONS"
                class="col rounded-5 q-mx-xs bg-dark"
                :class="!shift.is_approved && !isTimesheetApproved ? '' : 'inset-shadow'"
                popup-content-class="text-uppercase text-weight-bold text-center rounded-5"
                :style="shift.is_approved ? 'background-color: #0a7d32 !important;' : ''"
                popup-content-style="border: 2px solid var(--q-accent)"
                @update:model-value="option => shift.type = option.value"
            >
                <template #selected-item="scope">
                    <div
                        class="row items-center text-weight-bold q-ma-none q-pa-none no-wrap ellipsis full-width"
                        :class="ui_store.is_mobile_mode ? 'full-height' : ''"
                        :tabindex="scope.tabindex"
                    >
                        <q-icon
                            :name="scope.opt.icon"
                            :color="scope.opt.icon_color"
                            size="sm"
                            class="col-auto"
                            :class="shift.is_approved ? 'q-mx-xs' : 'q-mr-xs'"
                        />
                        <span
                            style="line-height: 1.2em;"
                            class="col-auto ellipsis"
                            :class="!shift.is_approved ? '' : 'text-white'"
                        >
                            {{ $t(scope.opt.label) }}
                        </span>
                    </div>
                </template>

                <template #after>
                    <q-icon
                        v-if="shift.is_approved"
                        :name="shift.is_remote ? 'las la-laptop' : 'las la-building'"
                        size="1.2em"
                        color="white"
                        class="q-mr-sm"
                    >
                        <q-tooltip
                            anchor="top middle"
                            self="bottom middle"
                            :offset="[0, 10]"
                            class="text-uppercase text-weight-bold text-white bg-primary"
                        >
                            {{ shift.is_remote ? $t('timesheet.shift.types.REMOTE') :
                                $t('timesheet.shift.types.OFFICE') }}
                        </q-tooltip>
                    </q-icon>

                    <q-toggle
                        v-else
                        v-model="shift.is_remote"
                        :disable="shift.is_approved"
                        dense
                        keep-color
                        size="3em"
                        color="accent"
                        icon="las la-building"
                        checked-icon="las la-laptop"
                    >
                        <q-tooltip
                            anchor="top middle"
                            self="bottom middle"
                            :offset="[0, 10]"
                            class="text-uppercase text-weight-medium text-white bg-accent"
                        >
                            {{ shift.is_remote ? $t('timesheet.shift.types.REMOTE') :
                                $t('timesheet.shift.types.OFFICE') }}
                        </q-tooltip>
                    </q-toggle>
                </template>
            </q-select>
        </div>

        <div class="col row items-start text-uppercase rounded-5 q-pa-xs">
            <!-- punch in field -->
            <q-input
                ref="start_time"
                v-model="shift.start_time"
                dense
                :borderless="(shift.is_approved && isTimesheetApproved)"
                :readonly="(shift.is_approved && isTimesheetApproved)"
                type="time"
                :standout="$q.dark.isActive ? 'bg-blue-grey-3' : 'bg-blue-grey-9'"
                label-slot
                lazy-rules
                no-error-icon
                hide-bottom-space
                :error="shift.has_error"
                :error-message="errorMessage || error_message !== '' ? $t(errorMessage ?? error_message) : ''"
                :label-color="!shift.is_approved ? 'accent' : 'white'"
                class="col rounded-5 bg-dark q-mx-xs"
                :class="(shift.id === -2 ? 'bg-negative ' : ' ') + (!shift.is_approved && !isTimesheetApproved ? '' : 'cursor-not-allowed inset-shadow')"
                :input-class="'text-weight-medium ' + (shift.id === -2 ? 'text-white ' : ' ') + (shift.is_approved ? 'text-white cursor-not-allowed q-px-sm' : '')"
                input-style="font-size: 1.2em;"
                :style="shift.is_approved ? 'background-color: #0a7d32 !important;' : ''"
                @blur="onTimeFieldBlur(shift.start_time)"
            >
                <template #label>
                    <span
                        class="text-weight-bolder"
                        :class="shift.is_approved ? ' q-ml-md' : ''"
                        style="font-size: 0.95em;"
                    >{{ $t('shared.misc.in') }}</span>
                </template>
            </q-input>

            <!-- punch out field -->
            <q-input
                ref="end_time"
                v-model="shift.end_time"
                :standout="$q.dark.isActive ? 'bg-blue-grey-3' : 'bg-blue-grey-9'"
                dense
                :borderless="(shift.is_approved && isTimesheetApproved)"
                :readonly="(shift.is_approved && isTimesheetApproved)"
                type="time"
                label-slot
                no-error-icon
                hide-bottom-space
                :error="shift.has_error"
                :error-message="errorMessage || error_message !== '' ? $t(errorMessage ?? error_message) : ''"
                :label-color="!shift.is_approved ? 'accent' : 'white'"
                :input-class="'text-weight-medium ' + (shift.id === -2 ? 'text-white ' : ' ') + (shift.is_approved ? 'text-white cursor-not-allowed q-px-sm' : '')"
                input-style="font-size: 1.2em;"
                class="col rounded-5 bg-dark q-mx-xs"
                :class="(shift.id === -2 ? 'bg-negative ' : ' ') + (shift.is_approved ? 'cursor-not-allowed q-px-xs transparent inset-shadow' : (isTimesheetApproved ? 'inset-shadow' : ''))"
                :style="shift.is_approved ? 'background-color: #0a7d32 !important;' : ''"
                @blur="onTimeFieldBlur(shift.end_time)"
            >
                <template #label>
                    <span
                        class="text-weight-bolder"
                        :class="shift.is_approved ? ' q-ml-md' : ''"
                        style="font-size: 0.95em;"
                    >{{ $t('shared.misc.out') }}</span>
                </template>
            </q-input>

            <!-- comment and delete buttons -->
            <div
                class="row full-height"
                :class="ui_store.is_mobile_mode ? 'col-12' : 'col-auto flex-center'"
            >
                <!-- desktop comment button -->
                <q-btn
                    v-if="!ui_store.is_mobile_mode"
                    push
                    dense
                    :color="shift.is_approved ? 'white' : 'accent'"
                    :icon="shift.comment ? 'chat' : 'chat_bubble_outline'"
                    :text-color="shift.is_approved ? 'accent' : 'white'"
                    class="col"
                    :class="ui_store.is_mobile_mode ? 'q-mt-xs bg-dark' : ''"
                >
                    <q-badge
                        v-if="shift.comment"
                        floating
                        rounded
                        color="negative"
                    />

                    <q-popup-edit
                        v-model="shift.comment"
                        :title="$t('timesheet.shift.fields.header_comment')"
                        auto-save
                        v-slot="scope"
                        class="bg-dark"
                    >
                        <q-input
                            color="white"
                            v-model="scope.value"
                            dense
                            :readonly="shift.is_approved"
                            autofocus
                            counter
                            bottom-slots
                            :maxlength="COMMENT_LENGTH_MAX"
                            class="q-pb-lg"
                            :class="shift.is_approved ? 'cursor-not-allowed' : ''"
                            @keyup.enter="scope.set"
                        >
                            <template #append>
                                <q-icon name="edit" />
                            </template>

                            <template #counter>
                                <div class="row flex-center">
                                    <q-space />
                                    <q-knob
                                        :model-value="scope.value?.length"
                                        readonly
                                        :max="COMMENT_LENGTH_MAX"
                                        size="1.6em"
                                        :thickness="0.4"
                                        :color="getCommentCounterColor(scope.value?.length ?? 0)"
                                        track-color="grey-4"
                                        class="col-auto q-mr-xs"
                                    />
                                    <span
                                        :class="'col-auto text-weight-bolder text-' + getCommentCounterColor(scope.value?.length ?? 0)"
                                    >{{ 280 - (scope.value?.length ?? 0) }}</span>
                                </div>
                            </template>
                        </q-input>
                    </q-popup-edit>
                </q-btn>

                <q-btn
                    v-if="!shift.is_approved"
                    flat
                    dense
                    :disable="shift.is_approved"
                    tabindex="-1"
                    icon="las la-trash"
                    text-color="negative"
                    class="col"
                    size="1.2em"
                    :class="shift.is_approved ? 'invisible' : ''"
                    @click="$emit('requestDelete')"
                >
                </q-btn>
            </div>
        </div>
    </div>
</template>

<style scoped>
:deep(.q-field--error) {
    background-color: var(--q-negative) !important;
}

:deep(.q-field--error .q-field__bottom) {
    color: white;
    font-weight: 900;
    border-radius: 0 0 5px 5px;
    padding-top: 0;
    align-items: center;
}
</style>