<script
    setup
    lang="ts"
>
    import MenuPanelInputField from 'src/modules/profile/components/shared/menu-panel-input-field.vue';
    import MenuPanelSelectField from 'src/modules/profile/components/shared/menu-panel-select-field.vue';

    import { ref } from 'vue';
    import { deepEqual } from 'src/utils/deep-equal';
    import { unwrapAndClone } from 'src/utils/unwrap-and-clone';
    import { useAuthStore } from 'src/stores/auth-store';
    import { useEmployeeStore } from 'src/stores/employee-store';
    import { CAN_APPROVE_PAY_PERIODS } from 'src/modules/shared/models/user.models';
    import type { EmployeeProfile } from 'src/modules/employee-list/models/employee-profile.models';

    const COMPANY_OPTIONS = [
        { label: 'Targo', value: 271583 },
        { label: 'Solucom', value: 271585 }
    ];
    const SUPERVISOR_OPTIONS = [{ label: 'AAA', value: '1' }, { label: 'BBB', value: '2' }, { label: 'CCC', value: '3' }, { label: 'DDD', value: '4' }];

    const auth_store = useAuthStore();
    const employee_store = useEmployeeStore();

    const is_editing = ref<boolean>(false);
    const current_company_option = ref(COMPANY_OPTIONS.find(option => option.value === employee_store.employee.company_name) ?? { label: '', value: 0 })
    let initial_info: EmployeeProfile = unwrapAndClone(employee_store.employee);


    const onSubmit = () => {
        if (!is_editing.value) {
            is_editing.value = true;
            return;
        }

        is_editing.value = false;
        initial_info = unwrapAndClone(employee_store.employee);   // update initial value for future possible resets
        employee_store.employee.company_name = current_company_option.value.value;

        if (!deepEqual(employee_store.employee, initial_info)) {
            // save the new data here
            return;
        }
    };

    const onReset = () => {
        employee_store.employee = unwrapAndClone(initial_info);
        is_editing.value = false;
    }
</script>

<template>
    <q-form
        class="q-pa-md full-height"
        @submit="onSubmit"
        @reset="onReset"
    >
        <div :class="$q.screen.lt.md ? 'column' : 'row'">
            <MenuPanelInputField
                v-model="employee_store.employee.job_title"
                class="col"
                :is-editing="is_editing"
                :label-string="$t('profile.employee.job_title')"
            />
            <MenuPanelSelectField
                v-model="current_company_option"
                :options="COMPANY_OPTIONS"
                class="col"
                :is-editing="is_editing"
                :label-string="$t('profile.employee.company')"
            />
        </div>

        <div>
            <MenuPanelSelectField
                v-model="employee_store.employee.supervisor_full_name"
                :options="SUPERVISOR_OPTIONS"
                :label-string="$t('profile.employee.supervisor')"
                :is-editing="is_editing"
            />
        </div>


        <div :class="$q.screen.lt.md ? 'column' : 'row'">
            <MenuPanelInputField
                v-model="employee_store.employee.email"
                class="col"
                :is-editing="is_editing"
                :label-string="$t('profile.employee.email')"
            />
            <MenuPanelInputField
                v-model="employee_store.employee.first_work_day"
                readonly
                class="col-auto"
                type="date"
                :is-editing="is_editing"
                :label-string="$t('profile.employee.hired_date')"
            />
        </div>

        <div
            v-if="CAN_APPROVE_PAY_PERIODS.includes(auth_store.user?.role ?? 'GUEST')"
            class="absolute-bottom"
            :class="$q.screen.lt.md ? 'column' : 'row'"
        >
            <q-space />
            <q-btn
                v-if="is_editing"
                push
                color="negative"
                type="reset"
                icon="cancel"
                class="q-ma-sm q-py-xs"
                :label="$t('shared.label.cancel')"
            />
            <q-btn
                push
                color="accent"
                type="submit"
                :icon="is_editing ? 'save_alt' : 'create'"
                class="q-ma-sm q-py-xs"
                :label="is_editing ? $t('shared.label.save') : $t('shared.label.update')"
            />
        </div>
    </q-form>
</template>