<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.player')" prop="username">
        <el-input v-model="formData.username" :placeholder="$t('page.search.usernameFuzzy')" clearable />
      </el-form-item>
    </el-col>
    <el-col v-bind="setSpan(6)">
      <el-form-item :label="$t('page.search.lotteryPoolConfig')" prop="lottery_config_name">
        <el-input v-model="formData.lottery_config_name" :placeholder="$t('page.search.nameFuzzy')" clearable />
      </el-form-item>
    </el-col>
    <el-col v-bind="setSpan(6)">
      <el-form-item :label="$t('page.search.drawType')" prop="lottery_type">
        <el-select v-model="formData.lottery_type" :placeholder="$t('table.searchBar.all')" clearable style="width: 100%">
          <el-option :label="$t('page.search.paid')" :value="0" />
          <el-option :label="$t('page.search.free')" :value="1" />
        </el-select>
      </el-form-item>
    </el-col>
    <el-col v-bind="setSpan(6)">
      <el-form-item :label="$t('page.search.isBigWin')" prop="is_win">
        <el-select v-model="formData.is_win" :placeholder="$t('table.searchBar.all')" clearable style="width: 100%">
          <el-option :label="$t('page.search.noBigWin')" :value="0" />
          <el-option :label="$t('page.search.bigWin')" :value="1" />
        </el-select>
      </el-form-item>
    </el-col>
    <el-col v-bind="setSpan(6)">
      <el-form-item :label="$t('page.search.direction')" prop="direction">
        <el-select v-model="formData.direction" :placeholder="$t('table.searchBar.all')" clearable style="width: 100%">
          <el-option :label="$t('page.search.clockwise')" :value="0" />
          <el-option :label="$t('page.search.anticlockwise')" :value="1" />
        </el-select>
      </el-form-item>
    </el-col>
    <el-col v-bind="setSpan(6)">
      <el-form-item :label="$t('page.search.winCoin')" prop="win_coin_min">
        <div class="range-wrap">
          <el-input-number
            v-model="formData.win_coin_min"
            :placeholder="$t('table.searchBar.min')"
            :precision="0"
            controls-position="right"
            class="range-input"
          />
          <span class="range-sep">{{ $t('table.searchBar.rangeSeparator') }}</span>
          <el-input-number
            v-model="formData.win_coin_max"
            :placeholder="$t('table.searchBar.max')"
            :precision="0"
            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.rollNumber')" prop="roll_number_min">
        <div class="range-wrap">
          <el-input-number
            v-model="formData.roll_number_min"
            :placeholder="$t('table.searchBar.min')"
            :min="5"
            :max="30"
            :precision="0"
            controls-position="right"
            class="range-input"
          />
          <span class="range-sep">{{ $t('table.searchBar.rangeSeparator') }}</span>
          <el-input-number
            v-model="formData.roll_number_max"
            :placeholder="$t('table.searchBar.max')"
            :min="5"
            :max="30"
            :precision="0"
            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.rewardConfig')" prop="reward_ui_text">
        <el-input v-model="formData.reward_ui_text" :placeholder="$t('page.search.uiTextFuzzy')" clearable />
      </el-form-item>
    </el-col>
    <el-col v-bind="setSpan(6)">
      <el-form-item :label="$t('page.search.rewardTier')" prop="reward_tier">
        <el-select v-model="formData.reward_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-option label="BIGWIN" value="BIGWIN" />
        </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) => ({
    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>
