<!-- Copyright (C) 2012-2025 Zammad Foundation, https://zammad-foundation.org/ -->

<script setup lang="ts">
import { computed, reactive } from 'vue'

import Form from '#shared/components/Form/Form.vue'
import type { FormSubmitData } from '#shared/components/Form/types.ts'
import { useForm } from '#shared/components/Form/useForm.ts'
import type { TicketArticleTimeAccountingFormData } from '#shared/entities/ticket/types.ts'
import { EnumFormUpdaterId } from '#shared/graphql/types.ts'
import { i18n } from '#shared/i18n.ts'
import { useApplicationStore } from '#shared/stores/application.ts'

import CommonFlyout from '#desktop/components/CommonFlyout/CommonFlyout.vue'
import type { ActionFooterOptions } from '#desktop/components/CommonFlyout/types.ts'
import { closeFlyout } from '#desktop/components/CommonFlyout/useFlyout.ts'

const emit = defineEmits<{
  'account-time': [TicketArticleTimeAccountingFormData]
  skip: []
}>()

const { form } = useForm()

const submitForm = (
  formData: FormSubmitData<TicketArticleTimeAccountingFormData>,
) => {
  emit('account-time', formData)
  closeFlyout('ticket-time-accounting')
}

const onClose = (isCancel?: boolean) => {
  if (!isCancel) return
  emit('skip')
}

const { config } = useApplicationStore()

const timeAccountingUnit = computed(() => {
  switch (config.time_accounting_unit) {
    case 'hour':
      return __('hour(s)')
    case 'quarter':
      return __('quarter-hour(s)')
    case 'minute':
      return __('minute(s)')
    case 'custom': {
      if (config.time_accounting_unit_custom)
        return config.time_accounting_unit_custom
      return null
    }
    default:
      return null
  }
})

const formSchema = [
  {
    isLayout: true,
    component: 'FormGroup',
    props: {
      class: '@container/form-group',
    },
    children: [
      {
        id: 'timeUnit',
        name: 'time_unit',
        label: __('Accounted Time'),
        type: 'text',
        required: true,
        placeholder: __('Enter the time you want to record'),
        validation: 'number',
        ...(timeAccountingUnit.value
          ? {
              sectionsSchema: {
                suffix: {
                  // FIXME: Not working.
                  // if: '$timeAccountingUnit',
                  // children: '$timeAccountingUnit',
                  $el: 'span',
                  children: i18n.t(timeAccountingUnit.value || ''),
                  attrs: {
                    class:
                      'py-2.5 px-2.5 outline outline-1 -outline-offset-1 outline-blue-200 dark:outline-gray-700 bg-neutral-50 dark:bg-gray-500 rounded-e-md text-gray-100 dark:text-neutral-400',
                  },
                },
              },
            }
          : {}),
      },
      {
        if: '$timeAccountingTypes === true',
        id: 'accountedTimeTypeId',
        name: 'accounted_time_type_id',
        label: __('Activity Type'),
        type: 'select',
        props: {
          clearable: true,
        },
      },
    ],
  },
]

const timeAccountingTypes = computed(() => config.time_accounting_types)

const schemaData = reactive({
  // timeAccountingUnit,
  timeAccountingTypes,
})

const footerActionOptions = computed<ActionFooterOptions>(() => ({
  actionLabel: __('Account Time'),
  actionButton: { variant: 'submit', type: 'submit' },
  cancelLabel: __('Skip'),
  form: form.value,
}))
</script>

<template>
  <CommonFlyout
    :header-title="__('Time Accounting')"
    :footer-action-options="footerActionOptions"
    header-icon="stopwatch"
    name="ticket-time-accounting"
    no-close-on-action
    @close="onClose"
  >
    <div class="flex flex-col gap-3">
      <Form
        id="form-ticket-time-accounting"
        ref="form"
        :schema="formSchema"
        :schema-data="schemaData"
        :form-updater-id="
          EnumFormUpdaterId.FormUpdaterUpdaterTicketTimeAccounting
        "
        form-updater-initial-only
        @submit="
          submitForm(
            $event as FormSubmitData<TicketArticleTimeAccountingFormData>,
          )
        "
      />
    </div>
  </CommonFlyout>
</template>
