<template>
  <Col v-bind="actionColOpt" v-if="showActionButtonGroup">
    <div style="width: 100%" :style="{ textAlign: actionColOpt.style.textAlign }">
      <Form.Item>
        <slot name="resetBefore"></slot>
        <Button
          type="default"
          class="mr-2"
          v-bind="getResetBtnOptions"
          @click="resetAction"
          v-if="showResetButton"
        >
          {{ getResetBtnOptions.text }}
        </Button>
        <slot name="submitBefore"></slot>

        <Button
          type="primary"
          class="mr-2"
          v-bind="getSubmitBtnOptions"
          @click="submitAction"
          v-if="showSubmitButton"
        >
          {{ getSubmitBtnOptions.text }}
        </Button>

        <slot name="advanceBefore"></slot>
        <Button
          type="link"
          size="small"
          @click="toggleAdvanced"
          v-if="showAdvancedButton && !hideAdvanceBtn"
        >
          <%_ if (enableTranslation) { _%>
          {{ isAdvanced ? t('component.form.putAway') : t('component.form.unfold') }}
          <%_ } else { _%>
          {{ isAdvanced ? 't$t('component.form.putAway')$t$' : 't$t('component.form.unfold')$t$' }}
          <%_ } _%>
          <BasicArrow class="ml-1" :expand="!isAdvanced" up />
        </Button>
        <slot name="advanceAfter"></slot>
      </Form.Item>
    </div>
  </Col>
</template>
<script lang="ts" setup>
  import type { ColEx } from '../types';
  import { computed, PropType } from 'vue';
  import { Form, Col } from 'ant-design-vue';
  import { Button, ButtonProps } from '@/components/Button';
  import { BasicArrow } from '@/components/Basic';
  import { useFormContext } from '../hooks/useFormContext';
  <%_ if (enableTranslation) { _%>
  import { useI18n } from '@/hooks/web/useI18nOut';
  <%_ } _%>
  import { propTypes } from '@/utils/propTypes';

  type ButtonOptions = Partial<ButtonProps> & { text: string };

  defineOptions({ name: 'BasicFormAction' });

  const props = defineProps({
    showActionButtonGroup: propTypes.bool.def(true),
    showResetButton: propTypes.bool.def(true),
    showSubmitButton: propTypes.bool.def(true),
    showAdvancedButton: propTypes.bool.def(true),
    resetButtonOptions: {
      type: Object as PropType<ButtonOptions>,
      default: () => ({}),
    },
    submitButtonOptions: {
      type: Object as PropType<ButtonOptions>,
      default: () => ({}),
    },
    actionColOptions: {
      type: Object as PropType<Partial<ColEx>>,
      default: () => ({}),
    },
    actionSpan: propTypes.number.def(6),
    isAdvanced: propTypes.bool,
    hideAdvanceBtn: propTypes.bool,
    layout: propTypes.oneOf(['horizontal', 'vertical', 'inline']).def('horizontal'),
  });

  const emit = defineEmits(['toggle-advanced']);

  <%_ if (enableTranslation) { _%>
  const { t } = useI18n();
  <%_ } _%>
  const { resetAction, submitAction } = useFormContext();

  const actionColOpt = computed(() => {
    const { showAdvancedButton, actionSpan: span, actionColOptions } = props;
    const actionSpan = 24 - span;
    const advancedSpanObj = showAdvancedButton
            ? { span: actionSpan < 6 ? 24 : actionSpan }
            : {};
    const actionColOpt: Partial<ColEx> = {
      style: { textAlign: 'right' },
      ...advancedSpanObj,
      ...actionColOptions,
    };
    if (props.layout !== 'inline') {
      actionColOpt['span'] = showAdvancedButton ? 6 : 4;
    }
    return actionColOpt;
  });
  const getResetBtnOptions = computed((): ButtonOptions => {
    return Object.assign(
      {
        text: <% if (enableTranslation) { %>t('common.resetText')<% } else { %>'t$t('common.resetText')$t$'<% } %>,
        preIcon: 'ic:baseline-restart-alt',
      },
      props.resetButtonOptions,
    );
  });

  const getSubmitBtnOptions = computed(() => {
    return Object.assign(
      {
        text: <% if (enableTranslation) { %>t('common.queryText')<% } else { %>'t$t('common.queryText')$t$'<% } %>,
        preIcon: 'ant-design:search-outlined',
      },
      props.submitButtonOptions,
    );
  });

  function toggleAdvanced() {
    emit('toggle-advanced');
  }
</script>
<style>
  .ml-1 {
    margin-left: 0.25rem;
  }
</style>
