<script setup lang="ts">
import { onMounted, ref, toRaw, watch } from 'vue';
import { jsonClone } from '@sa/utils';
import { fetchGetSchoolCollegeList } from '@/service/api/art/school-college';
import { fetchGetSchoolList } from '@/service/api/art/school';
import { useNaiveForm } from '@/hooks/common/form';
import { $t } from '@/locales';

defineOptions({
  name: 'SchoolMajorSearch'
});

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

interface Props {
  fixedSchoolId?: CommonType.IdType | null;
}

const props = withDefaults(defineProps<Props>(), {
  fixedSchoolId: null
});

const emit = defineEmits<Emits>();

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

const model = defineModel<Api.Art.SchoolMajorSearchParams>('model', { required: true });
const schoolOptions = ref<CommonType.Option<CommonType.IdType>[]>([]);
const collegeOptions = ref<CommonType.Option<CommonType.IdType>[]>([]);

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

async function getSchoolOptions() {
  const { data, error } = await fetchGetSchoolList({
    pageNum: 1,
    pageSize: 1000,
    params: {}
  });
  if (!error) {
    schoolOptions.value = data.rows.map(item => ({
      value: item.schoolId,
      label: `${item.mainName || item.shortName || '未命名学校'} (${item.schoolId})`
    }));
  } else {
    schoolOptions.value = [];
  }
}

async function getCollegeOptions(schoolId?: CommonType.IdType | null) {
  if (!schoolId) {
    collegeOptions.value = [];
    return;
  }
  const { data, error } = await fetchGetSchoolCollegeList({
    pageNum: 1,
    pageSize: 1000,
    schoolId,
    params: {}
  });
  if (!error) {
    collegeOptions.value = data.rows.map(item => ({
      value: item.collegeId,
      label: item.collegeName
    }));
  } else {
    collegeOptions.value = [];
  }
}

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

async function reset() {
  await restoreValidation();
  resetModel();
  getCollegeOptions(model.value.schoolId);
  emit('search');
}

async function search() {
  await validate();
  emit('search');
}

onMounted(() => {
  getSchoolOptions();
  getCollegeOptions(model.value.schoolId);
});

watch(
  () => model.value.schoolId,
  (schoolId, oldSchoolId) => {
    if (schoolId !== oldSchoolId) {
      model.value.collegeId = null;
      getCollegeOptions(schoolId);
    }
  }
);
</script>

<template>
  <NCard :bordered="false" size="small" class="school-search-card card-wrapper">
    <NCollapse>
      <NCollapseItem :title="$t('common.search')" name="art-school-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="schoolId" class="pr-24px">
              <NSelect
                v-model:value="model.schoolId"
                :options="schoolOptions"
                :disabled="props.fixedSchoolId !== null"
                placeholder="请选择院校"
                clearable
                filterable
              />
            </NFormItemGi>
            <NFormItemGi span="24 s:12 m:6" label="学院ID" label-width="auto" path="collegeId" class="pr-24px">
              <NSelect
                v-model:value="model.collegeId"
                :options="collegeOptions"
                placeholder="请选择学院"
                clearable
                filterable
              />
            </NFormItemGi>
            <NFormItemGi span="24 s:12 m:6" label="专业编码(可选)" label-width="auto" path="majorCode" class="pr-24px">
              <NInput v-model:value="model.majorCode" placeholder="请输入专业编码(可选)" />
            </NFormItemGi>
            <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="学制(3/4/5)" label-width="auto" path="durationYears" class="pr-24px">
              <NInput v-model:value="model.durationYears" placeholder="请输入学制(3/4/5)" />
            </NFormItemGi>
            <NFormItemGi
              span="24 s:12 m:6"
              label="专业类别：工学/理学/艺术学..."
              label-width="auto"
              path="majorCategory"
              class="pr-24px"
            >
              <NInput v-model:value="model.majorCategory" placeholder="请输入专业类别：工学/理学/艺术学..." />
            </NFormItemGi>
            <NFormItemGi
              span="24 s:12 m:6"
              label="学位类型：工学学士/理学学士/艺术学学士..."
              label-width="auto"
              path="degreeType"
              class="pr-24px"
            >
              <NSelect
                v-model:value="model.degreeType"
                placeholder="请选择学位类型：工学学士/理学学士/艺术学学士..."
                :options="[]"
                clearable
              />
            </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>
