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

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

const emit = defineEmits<Emits>();

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

const model = defineModel<Api.Art.SchoolMediaSearchParams>('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-media-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="业务类型：school/campus/college/major/dorm"
              label-width="auto"
              path="bizType"
              class="pr-24px"
            >
              <NSelect
                v-model:value="model.bizType"
                placeholder="请选择业务类型：school/campus/college/major/dorm"
                :options="[]"
                clearable
              />
            </NFormItemGi>
            <NFormItemGi span="24 s:12 m:6" label="业务主键ID" label-width="auto" path="bizId" class="pr-24px">
              <NInput v-model:value="model.bizId" placeholder="请输入业务主键ID" />
            </NFormItemGi>
            <NFormItemGi
              span="24 s:12 m:6"
              label="媒体类型：1-图片 2-视频"
              label-width="auto"
              path="mediaType"
              class="pr-24px"
            >
              <NSelect
                v-model:value="model.mediaType"
                placeholder="请选择媒体类型：1-图片 2-视频"
                :options="[]"
                clearable
              />
            </NFormItemGi>
            <NFormItemGi span="24 s:12 m:6" label="资源URL" label-width="auto" path="url" class="pr-24px">
              <NInput v-model:value="model.url" placeholder="请输入资源URL" />
            </NFormItemGi>
            <NFormItemGi
              span="24 s:12 m:6"
              label="封面URL(视频可用)"
              label-width="auto"
              path="coverUrl"
              class="pr-24px"
            >
              <NInput v-model:value="model.coverUrl" placeholder="请输入封面URL(视频可用)" />
            </NFormItemGi>
            <NFormItemGi span="24 s:12 m:6" label="排序" label-width="auto" path="sortNo" class="pr-24px">
              <NInput v-model:value="model.sortNo" 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>
