<script setup lang="ts">
import BackButton from '@/components/button/BackButton.vue';
import AdminLayout from '@/layouts/AdminLayout.vue';
import type { EmployeeListItem, EnumOption } from '@/types/employee';
import type { MediaItem } from '@/types/media';
import { Head } from '@inertiajs/vue3';
import { computed } from 'vue';
import EmployeeForm from './form/EmployeeForm.vue';
import { index, update } from '@/routes/admin/hr/employees';

const props = defineProps<{
    employee: EmployeeListItem;
    genders: EnumOption[];
    employmentStatuses: EnumOption[];
    roles: EnumOption[];
    profilePhoto?: MediaItem | null;
}>();

const initialData = computed(() => ({
    email: props.employee.email ?? '',
    username: props.employee.username ?? '',
    full_name: props.employee.profile?.full_name ?? '',
    phone_number: props.employee.profile?.phone_number ?? '',
    gender: props.employee.profile?.gender ?? '',
    birth_date: props.employee.profile?.birth_date_input ?? '',
    address: props.employee.profile?.address ?? '',
    join_date: props.employee.employee?.join_date_input ?? '',
    employment_status: props.employee.employee?.employment_status ?? '',
    base_salary:
        props.employee.employee?.base_salary != null
            ? String(props.employee.employee.base_salary)
            : '',
    role: props.employee.roles?.[0]?.name ?? '',
}));
</script>

<template>
    <Head title="Ubah Pegawai" />

    <AdminLayout>
        <div
            class="flex flex-col gap-4 sm:flex-row sm:items-center sm:justify-between"
        >
            <div class="space-y-1">
                <h2 class="text-2xl font-bold tracking-tight">Ubah Pegawai</h2>
            </div>

            <BackButton :href="index.url()" />
        </div>

        <EmployeeForm
            :submit-url="update.url(employee.id)"
            method="put"
            submit-label="Perbarui"
            :initial-data="initialData"
            :profile-photo="profilePhoto"
            :employee-id="employee.id"
            :employee-name="employee.profile?.full_name"
            :genders="genders"
            :employment-statuses="employmentStatuses"
            :roles="roles"
        />
    </AdminLayout>
</template>
