<script
    setup
    lang="ts"
>
    import { ref } from 'vue';
    import { deepEqual } from 'src/utils/deep-equal';
    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 type { EmployeeProfile } from 'src/modules/employee-list/models/employee-profile.models';
    import { unwrapAndClone } from 'src/utils/unwrap-and-clone';

    const employee_profile = defineModel<EmployeeProfile>({required: true});

    const is_editing = ref<boolean>(false);
    let initial_info: EmployeeProfile = unwrapAndClone(employee_profile.value);

    const supervisor_options = [{ label: 'AAA', value: '1' }, { label: 'BBB', value: '2' }, { label: 'CCC', value: '3' }, { label: 'DDD', value: '4' }];

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

        is_editing.value = false;
        initial_info = unwrapAndClone(employee_profile.value);   // update initial value for future possible resets

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

    const onReset = () => {
        employee_profile.value = 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_profile.job_title"
                class="col"
                :is-editing="is_editing"
                :label-string="$t('profile.employee.job_title')"
            />
            <MenuPanelInputField
                v-model="employee_profile.company_name"
                class="col"
                :is-editing="is_editing"
                :label-string="$t('profile.employee.company')"
            />
        </div>

        <div class="q-mx-xs">
            <MenuPanelSelectField
                v-model="employee_profile.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_profile.email"
                class="col"
                :is-editing="is_editing"
                :label-string="$t('profile.employee.email')"
            />
            <MenuPanelInputField
                v-model="employee_profile.first_work_day"
                readonly
                class="col"
                type="date"
                :is-editing="is_editing"
                :label-string="$t('profile.employee.hired_date')"
            />
        </div>

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