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

defineOptions({
  name: 'RankSearch',
})

const emit = defineEmits<Emits>()

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

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

const model = defineModel<Api.Rank.UserSearchParams>('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 publishStatusOptions = [
  { label: '待审核', value: '2' }, // Mapping to '2' (Warning)
  { label: '已发布', value: '1' }, // Mapping to '1' (Success)
]
</script>

<template>
  <NCard :bordered="false" size="small" class="card-wrapper">
    <NCollapse>
      <NCollapseItem title="筛选查询" name="rank-search">
        <NForm ref="formRef" :model="model" label-placement="left" :label-width="100">
          <NGrid responsive="screen" item-responsive>
            <NFormItemGi span="24 s:12 m:8" label="比赛活动名称" path="competitionActivityName" class="pr-24px">
              <NInput v-model:value="model.competitionActivityName" placeholder="请输入比赛活动名称" />
            </NFormItemGi>
            <NFormItemGi span="24 s:12 m:8" label="发布状态" path="publishStatus" class="pr-24px">
              <NSelect
                v-model:value="model.publishStatus"
                placeholder="请选择"
                :options="publishStatusOptions"
                clearable
              />
            </NFormItemGi>
            <NFormItemGi span="24 s:12 m:8" 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>
