<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: 'SchoolDormSearch'
});

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

const emit = defineEmits<Emits>();

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

const model = defineModel<Api.Art.SchoolDormSearchParams>('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-dorm-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="campusId" class="pr-24px">
              <NInput v-model:value="model.campusId" placeholder="请输入校区ID" />
            </NFormItemGi>
            <NFormItemGi span="24 s:12 m:6" label="几人间(4/6/8...)" label-width="auto" path="roomSize" class="pr-24px">
              <NInput v-model:value="model.roomSize" placeholder="请输入几人间(4/6/8...)" />
            </NFormItemGi>
            <NFormItemGi
              span="24 s:12 m:6"
              label="是否上床下桌(0否1是)"
              label-width="auto"
              path="bunkBedDesk"
              class="pr-24px"
            >
              <NInput v-model:value="model.bunkBedDesk" placeholder="请输入是否上床下桌(0否1是)" />
            </NFormItemGi>
            <NFormItemGi
              span="24 s:12 m:6"
              label="是否独立卫浴(0否1是)"
              label-width="auto"
              path="privateBath"
              class="pr-24px"
            >
              <NInput v-model:value="model.privateBath" placeholder="请输入是否独立卫浴(0否1是)" />
            </NFormItemGi>
            <NFormItemGi
              span="24 s:12 m:6"
              label="宿舍标签(冗余文本：空调/热水/洗衣房...)"
              label-width="auto"
              path="tags"
              class="pr-24px"
            >
              <NInput v-model:value="model.tags" placeholder="请输入宿舍标签(冗余文本：空调/热水/洗衣房...)" />
            </NFormItemGi>
            <NFormItemGi span="24 s:12 m:6" label="补充说明" label-width="auto" path="description" class="pr-24px">
              <NInput v-model:value="model.description" 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>
