<script setup lang="ts">
import { toRaw } from 'vue';
import { jsonClone } from '@sa/utils';
import { useNaiveForm } from '@/hooks/common/form';
import { $t } from '@/locales';

defineOptions({
  name: 'SchoolCampusSearch'
});

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

const emit = defineEmits<Emits>();

const { formRef, validate, restoreValidation } = useNaiveForm();

const model = defineModel<Api.Art.SchoolCampusSearchParams>('model', { required: true });

const defaultModel = jsonClone(toRaw(model.value));

function resetModel() {
  Object.assign(model.value, defaultModel);
}

async function reset() {
  await restoreValidation();
  resetModel();
  emit('search');
}

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

<template>
  <NCard :bordered="false" size="small" class="school-search-card card-wrapper">
    <NCollapse>
      <NCollapseItem :title="$t('common.search')" name="art-school-campus-search">
        <NForm ref="formRef" :model="model" label-placement="left" :label-width="80">
          <NGrid responsive="screen" item-responsive>
            <NFormItemGi span="24 s:12 m:6" label="学校ID" label-width="auto" path="schoolId" class="pr-24px">
              <NInput v-model:value="model.schoolId" placeholder="请输入学校ID" />
            </NFormItemGi>
            <NFormItemGi span="24 s:12 m:6" label="校区名称" label-width="auto" path="campusName" class="pr-24px">
              <NInput v-model:value="model.campusName" placeholder="请输入校区名称" />
            </NFormItemGi>
            <NFormItemGi span="24 s:12 m:6" label="校区位置(文本)" label-width="auto" path="location" class="pr-24px">
              <NInput v-model:value="model.location" placeholder="请输入校区位置(文本)" />
            </NFormItemGi>
            <NFormItemGi span="24 s:12 m:6" label="校区地址" label-width="auto" path="address" class="pr-24px">
              <NInput v-model:value="model.address" placeholder="请输入校区地址" />
            </NFormItemGi>
            <NFormItemGi span="24 s:12 m:6" label="经度" label-width="auto" path="lng" class="pr-24px">
              <NInput v-model:value="model.lng" placeholder="请输入经度" />
            </NFormItemGi>
            <NFormItemGi span="24 s:12 m:6" label="纬度" label-width="auto" path="lat" class="pr-24px">
              <NInput v-model:value="model.lat" placeholder="请输入纬度" />
            </NFormItemGi>
            <NFormItemGi span="24 s:12 m:6" label="校区介绍" label-width="auto" path="introduction" class="pr-24px">
              <NInput v-model:value="model.introduction" placeholder="请输入校区介绍" />
            </NFormItemGi>
            <NFormItemGi :show-feedback="false" span="24" class="pr-24px">
              <NSpace class="w-full" justify="end">
                <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>
