<script setup lang="ts">
import { jsonClone } from '@sa/utils'
import { toRaw } from 'vue'
import { enableStatusOptions } from '@/constants/business'
import { useNaiveForm } from '@/hooks/common/form'

defineOptions({
  name: 'TemplateSearch',
})

const emit = defineEmits<Emits>()

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

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

const model = defineModel<Api.Template.TemplateSearchParams>('model', { required: true })

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

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

async function reset() {
  await restoreValidation()
  resetModel()
}

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

const competitionOptions = [
  { label: '第九届阅读之星大赛', value: '1' },
  { label: '科普阅读大赛', value: '2' },
]

const sizeOptions = [
  { label: '210mmx297mm', value: '1' },
  { label: 'A3', value: '2' },
]
</script>

<template>
  <NCard :bordered="false" size="small" class="card-wrapper">
    <NCollapse>
      <NCollapseItem title="基础信息" name="template-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="模板名称" path="templateName" class="pr-24px">
              <NInput v-model:value="model.templateName" placeholder="请输入名称" />
            </NFormItemGi>
            <NFormItemGi span="24 s:12 m:6" label="关联比赛" path="competitionId" class="pr-24px">
              <NSelect
                v-model:value="model.competitionId" placeholder="请选择" :options="competitionOptions as any"
                clearable
              />
            </NFormItemGi>
            <NFormItemGi span="24 s:12 m:6" label="状态" path="status" class="pr-24px">
              <NSelect v-model:value="model.status" placeholder="请选择" :options="enableStatusOptions as any" clearable />
            </NFormItemGi>
            <NFormItemGi span="24 s:12 m:6" label="尺寸" path="size" class="pr-24px">
              <NSelect v-model:value="model.size" placeholder="请选择" :options="sizeOptions" clearable />
            </NFormItemGi>
            <NFormItemGi span="24" class="pr-24px">
              <NSpace class="w-full" justify="end">
                <NButton @click="reset">
                  <template #icon>
                    <icon-ic-round-refresh class="text-icon" />
                  </template>
                  重置
                </NButton>
                <NButton type="primary" ghost @click="search">
                  <template #icon>
                    <icon-ic-round-search class="text-icon" />
                  </template>
                  搜索
                </NButton>
              </NSpace>
            </NFormItemGi>
          </NGrid>
        </NForm>
      </NCollapseItem>
    </NCollapse>
  </NCard>
</template>

<style scoped></style>
