<template>
  <sa-search-bar
    ref="searchBarRef"
    v-model="formData"
    label-width="120px"
    :showExpand="false"
    @reset="handleReset"
    @search="handleSearch"
    @expand="handleExpand"
  >
    <el-col v-bind="setSpan(6)">
      <el-form-item :label="$t('page.search.dicePoints')" prop="grid_number_min">
        <div class="range-wrap">
          <el-input-number
            v-model="formData.grid_number_min"
            :placeholder="$t('table.searchBar.min')"
            controls-position="right"
            class="range-input"
          />
          <span class="range-sep">{{ $t('table.searchBar.rangeSeparator') }}</span>
          <el-input-number
            v-model="formData.grid_number_max"
            :placeholder="$t('table.searchBar.max')"
            controls-position="right"
            class="range-input"
          />
        </div>
      </el-form-item>
    </el-col>
    <el-col v-bind="setSpan(6)">
      <el-form-item :label="$t('page.search.displayText')" prop="ui_text">
        <el-input v-model="formData.ui_text" :placeholder="$t('page.search.fuzzyQuery')" clearable />
      </el-form-item>
    </el-col>
    <el-col v-bind="setSpan(6)">
      <el-form-item :label="$t('page.search.realEv')" prop="real_ev_min">
        <div class="range-wrap">
          <el-input-number
            v-model="formData.real_ev_min"
            :placeholder="$t('table.searchBar.min')"
            :precision="2"
            controls-position="right"
            class="range-input"
          />
          <span class="range-sep">{{ $t('table.searchBar.rangeSeparator') }}</span>
          <el-input-number
            v-model="formData.real_ev_max"
            :placeholder="$t('table.searchBar.max')"
            :precision="2"
            controls-position="right"
            class="range-input"
          />
        </div>
      </el-form-item>
    </el-col>
    <el-col v-bind="setSpan(6)">
      <el-form-item :label="$t('page.search.tier')" prop="tier">
        <el-select v-model="formData.tier" :placeholder="$t('table.searchBar.all')" clearable style="width: 100%">
          <el-option label="T1" value="T1" />
          <el-option label="T2" value="T2" />
          <el-option label="T3" value="T3" />
          <el-option label="T4" value="T4" />
          <el-option label="T5" value="T5" />
        </el-select>
      </el-form-item>
    </el-col>
  </sa-search-bar>
</template>

<script setup lang="ts">
  interface Props {
    modelValue: Record<string, any>
  }
  interface Emits {
    (e: 'update:modelValue', value: Record<string, any>): void
    (e: 'search', params: Record<string, any>): void
    (e: 'reset'): void
  }
  const props = defineProps<Props>()
  const emit = defineEmits<Emits>()
  // 展开/收起
  const isExpanded = ref<boolean>(false)

  // 表单数据双向绑定
  const searchBarRef = ref()
  const formData = computed({
    get: () => props.modelValue,
    set: (val) => emit('update:modelValue', val)
  })

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

  // 搜索
  async function handleSearch() {
    emit('search', formData.value)
  }

  // 展开/收起
  function handleExpand(expanded: boolean) {
    isExpanded.value = expanded
  }

  // 栅格占据的列数
  const setSpan = (span: number) => {
    return {
      span: span,
      xs: 24,
      sm: span >= 12 ? span : 12,
      md: span >= 8 ? span : 8,
      lg: span,
      xl: span
    }
  }
</script>

<style lang="scss" scoped>
  .range-wrap {
    display: flex;
    align-items: center;
    gap: 8px;
    min-width: 0;
  }

  .range-input {
    flex: 1;
    min-width: 0;
  }

  .range-sep {
    color: var(--el-text-color-secondary);
    flex-shrink: 0;
  }
</style>
