<script setup lang="ts">
import { $t } from '@/locales';

defineOptions({
  name: 'SearchForm',
  inheritAttrs: false
});

interface Emits {
  (e: 'reset'): void;
  (e: 'resetInterlayer'): void;
  (e: 'search'): void;
}

const emit = defineEmits<Emits>();

const model = defineModel<any>('model');

// 重置后，再触发搜索
function reset() {
  emit('reset'); // 触发重置事件：这个用于最外层的hook的resetSearchParams
  emit('resetInterlayer'); // 触发中间层的清除，用于中间层定义的数据清除
  emit('search');
}

function search() {
  emit('search');
}
</script>

<template>
  <NCard :bordered="false" size="small" class="card-wrapper" v-bind="$attrs">
    <NCollapse :default-expanded-names="['search']">
      <NCollapseItem :title="$t('common.search')" name="search">
        <NForm :model="model" label-placement="left" :label-width="80">
          <NGrid responsive="screen" item-responsive>
            <slot name="formItem"></slot>
            <NFormItemGi span="24 s:12 m:4">
              <NSpace class="w-full" justify="start">
                <NButton @click="reset">
                  <template #icon>
                    <icon-ic-round-refresh class="text-icon" />
                  </template>
                  {{ $t('common.reset') }}
                </NButton>
                <NButton type="primary" ghost @click="search">
                  <template #icon>
                    <icon-ic-round-search class="text-icon" />
                  </template>
                  {{ $t('common.search') }}
                </NButton>
              </NSpace>
            </NFormItemGi>
          </NGrid>
        </NForm>
      </NCollapseItem>
    </NCollapse>
  </NCard>
</template>

<style scoped></style>
