<template>
  <sa-search-bar
    ref="searchBarRef"
    v-model="formData"
    label-width="100px"
    :showExpand="false"
    @reset="handleReset"
    @search="handleSearch"
  >
    <el-col v-bind="setSpan(6)">
      <el-form-item :label="$t('page.search.name')" prop="name">
        <el-input v-model="formData.name" :placeholder="$t('page.search.placeholderName')" clearable />
      </el-form-item>
    </el-col>
    <el-col v-bind="setSpan(6)">
      <el-form-item :label="$t('page.search.title')" prop="title">
        <el-input v-model="formData.title" :placeholder="$t('page.search.placeholderTitle')" clearable />
      </el-form-item>
    </el-col>
    <el-col v-bind="setSpan(6)">
      <el-form-item :label="$t('page.search.isDefault')" prop="is_default">
        <el-select v-model="formData.is_default" :placeholder="$t('page.search.placeholderIsDefault')" clearable>
          <el-option :label="$t('page.table.defaultYes')" :value="1" />
          <el-option :label="$t('page.table.defaultNo')" :value="0" />
        </el-select>
      </el-form-item>
    </el-col>
  </sa-search-bar>
</template>

<script setup lang="ts">
  type AnteConfigSearchForm = {
    name?: string
    title?: string
    /** 1=是 0=否 */
    is_default?: 0 | 1 | null
    [key: string]: unknown
  }

  interface Props {
    modelValue: AnteConfigSearchForm
  }
  interface Emits {
    (e: 'update:modelValue', value: AnteConfigSearchForm): void
    (e: 'search', params: AnteConfigSearchForm): void
    (e: 'reset'): void
  }
  const props = defineProps<Props>()
  const emit = defineEmits<Emits>()

  const searchBarRef = ref()
  const formData = computed<AnteConfigSearchForm>({
    get: () => props.modelValue,
    set: (val) => emit('update:modelValue', val)
  })

  function handleReset() {
    searchBarRef.value?.ref.resetFields()
    emit('reset')
  }

  function handleSearch() {
    emit('search', formData.value)
  }

  const setSpan = (span: number) => {
    return {
      span,
      xs: 24,
      sm: span >= 12 ? span : 12,
      md: span >= 8 ? span : 8,
      lg: span,
      xl: span
    }
  }
</script>
