<template>
  <BasicModal
    v-bind="$attrs"
  <%_ if (enableTranslation) { _%>
    :title="t('component.excel.exportModalTitle')"
  <%_ } else { _%>
    title="t$t('component.excel.exportModalTitle')$t$"
  <%_ } _%>
    @ok="handleOk"
    @register="registerModal"
  >
    <BasicForm
      :labelWidth="100"
      :schemas="schemas"
      :showActionButtonGroup="false"
      @register="registerForm"
    />
  </BasicModal>
</template>
<script lang="ts">
  import type { ExportModalResult } from './typing';
  import { defineComponent } from 'vue';
  import { BasicModal, useModalInner } from '@/components/Modal';
  import { BasicForm, FormSchema, useForm } from '@/components/Form/index';
  <%_ if (enableTranslation) { _%>
  import { useI18n } from '@/hooks/web/useI18n';

  const { t } = useI18n();
  <%_ } _%>

  const schemas: FormSchema[] = [
    {
      field: 'filename',
      component: 'Input',
      label: <% if (enableTranslation) { %>t('component.excel.fileName')<% } else { %>'t$t('component.excel.fileName')$t$'<% } %>,
      rules: [{ required: true }],
    },
    {
      field: 'bookType',
      component: 'Select',
      label: <% if (enableTranslation) { %>t('component.excel.fileType')<% } else { %>'t$t('component.excel.fileType')$t$'<% } %>,
      defaultValue: 'xlsx',
      rules: [{ required: true }],
      componentProps: {
        options: [
          {
            label: 'xlsx',
            value: 'xlsx',
            key: 'xlsx',
          },
          {
            label: 'html',
            value: 'html',
            key: 'html',
          },
          {
            label: 'csv',
            value: 'csv',
            key: 'csv',
          },
          {
            label: 'txt',
            value: 'txt',
            key: 'txt',
          },
        ],
      },
    },
  ];
  export default defineComponent({
    components: { BasicModal, BasicForm },
    emits: ['success', 'register'],
    setup(_, { emit }) {
      const [registerForm, { validateFields }] = useForm();
      const [registerModal, { closeModal }] = useModalInner();

      async function handleOk() {
        const res = (await validateFields()) as ExportModalResult;
        const { filename, bookType } = res;
        emit('success', {
          filename: `${filename.split('.').shift()}.${bookType}`,
          bookType,
        });
        closeModal();
      }

      return {
        <%_ if (enableTranslation) { _%>
        t,
        <%_ } _%>
        schemas,
        handleOk,
        registerForm,
        registerModal,
      };
    },
  });
</script>
