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

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

const emit = defineEmits<Emits>();

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

const model = defineModel<Api.Art.SchoolSearchParams>('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-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="学校编码（唯一标识，如国标代码）"
              label-width="auto"
              path="mainCode"
              class="pr-24px"
            >
              <NInput v-model:value="model.mainCode" placeholder="请输入学校编码（唯一标识，如国标代码）" />
            </NFormItemGi>
            <NFormItemGi span="24 s:12 m:6" label="学校主名称" label-width="auto" path="mainName" class="pr-24px">
              <NInput v-model:value="model.mainName" placeholder="请输入学校主名称（官方全称）" />
            </NFormItemGi>
            <NFormItemGi
              span="24 s:12 m:6"
              label="学校简称（备用）"
              label-width="auto"
              path="shortName"
              class="pr-24px"
            >
              <NInput v-model:value="model.shortName" placeholder="请输入学校简称（备用）" />
            </NFormItemGi>
            <NFormItemGi span="24 s:12 m:6" label="省份" label-width="auto" path="province" class="pr-24px">
              <NInput v-model:value="model.province" placeholder="请输入省份" />
            </NFormItemGi>
            <NFormItemGi span="24 s:12 m:6" label="城市" label-width="auto" path="city" class="pr-24px">
              <NInput v-model:value="model.city" placeholder="请输入城市" />
            </NFormItemGi>
            <NFormItemGi span="24 s:12 m:6" label="区县" label-width="auto" path="district" class="pr-24px">
              <NInput v-model:value="model.district" placeholder="请输入区县" />
            </NFormItemGi>
            <NFormItemGi
              span="24 s:12 m:6"
              label="大学类型：综合/工科/财经/艺术..."
              label-width="auto"
              path="universityType"
              class="pr-24px"
            >
              <NSelect
                v-model:value="model.universityType"
                placeholder="请选择大学类型：综合/工科/财经/艺术..."
                :options="[]"
                clearable
              />
            </NFormItemGi>
            <NFormItemGi
              span="24 s:12 m:6"
              label="学历层次：本科/专科"
              label-width="auto"
              path="educationLevel"
              class="pr-24px"
            >
              <NInput v-model:value="model.educationLevel" placeholder="请输入学历层次：本科/专科" />
            </NFormItemGi>
            <NFormItemGi
              span="24 s:12 m:6"
              label="办学性质：公办/民办/中外合作"
              label-width="auto"
              path="schoolNature"
              class="pr-24px"
            >
              <NInput v-model:value="model.schoolNature" placeholder="请输入办学性质：公办/民办/中外合作" />
            </NFormItemGi>
            <NFormItemGi
              span="24 s:12 m:6"
              label="主管部门：教育部/工信部/民委..."
              label-width="auto"
              path="supervisorDept"
              class="pr-24px"
            >
              <NInput v-model:value="model.supervisorDept" 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>
