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

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

const emit = defineEmits<Emits>();

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

const model = defineModel<Api.Art.MajorSearchParams>('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="card-wrapper">
    <NCollapse>
      <NCollapseItem :title="$t('common.search')" name="art-major-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="majorName" class="pr-24px">
              <NInput v-model:value="model.majorName" placeholder="请输入专业名称" />
            </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="majorIcon" class="pr-24px">
              <NInput v-model:value="model.majorIcon" placeholder="请输入专业图标" />
            </NFormItemGi>
            <NFormItemGi span="24 s:12 m:6" label="学制(年)" label-width="auto" path="schoolingYears" class="pr-24px">
              <NInput v-model:value="model.schoolingYears" placeholder="请输入学制(年)" />
            </NFormItemGi>
            <NFormItemGi
              span="24 s:12 m:6"
              label="所属一级学科"
              label-width="auto"
              path="disciplinePrimary"
              class="pr-24px"
            >
              <NInput v-model:value="model.disciplinePrimary" placeholder="请输入所属一级学科" />
            </NFormItemGi>
            <NFormItemGi
              span="24 s:12 m:6"
              label="所属二级学科"
              label-width="auto"
              path="disciplineSecondary"
              class="pr-24px"
            >
              <NInput v-model:value="model.disciplineSecondary" placeholder="请输入所属二级学科" />
            </NFormItemGi>
            <NFormItemGi
              span="24 s:12 m:6"
              label="授予学士学位"
              label-width="auto"
              path="degreeAwarded"
              class="pr-24px"
            >
              <NInput v-model:value="model.degreeAwarded" placeholder="请输入授予学士学位" />
            </NFormItemGi>
            <NFormItemGi span="24 s:12 m:6" label="专业概括" label-width="auto" path="summary" class="pr-24px">
              <NInput v-model:value="model.summary" placeholder="请输入专业概括" />
            </NFormItemGi>
            <NFormItemGi
              span="24 s:12 m:6"
              label="培养方向"
              label-width="auto"
              path="trainingDirection"
              class="pr-24px"
            >
              <NInput v-model:value="model.trainingDirection" placeholder="请输入培养方向" />
            </NFormItemGi>
            <NFormItemGi span="24 s:12 m:6" label="主要课程" label-width="auto" path="coreCourses" class="pr-24px">
              <NInput v-model:value="model.coreCourses" 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>
