<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="玩家" prop="username">
        <el-input v-model="formData.username" placeholder="用户名模糊" clearable />
      </el-form-item>
    </el-col>
    <el-col v-bind="setSpan(6)">
      <el-form-item label="彩金池配置" prop="lottery_config_name">
        <el-input v-model="formData.lottery_config_name" placeholder="名称模糊" clearable />
      </el-form-item>
    </el-col>
    <el-col v-bind="setSpan(6)">
      <el-form-item label="抽奖类型" prop="lottery_type">
        <el-select v-model="formData.lottery_type" placeholder="全部" clearable style="width: 100%">
          <el-option label="付费" :value="0" />
          <el-option label="赠送" :value="1" />
        </el-select>
      </el-form-item>
    </el-col>
    <el-col v-bind="setSpan(6)">
      <el-form-item label="中奖" prop="is_win">
        <el-select v-model="formData.is_win" placeholder="全部" clearable style="width: 100%">
          <el-option label="无" :value="0" />
          <el-option label="中奖" :value="1" />
        </el-select>
      </el-form-item>
    </el-col>
    <el-col v-bind="setSpan(6)">
      <el-form-item label="赢取平台币" prop="win_coin_min">
        <div class="range-wrap">
          <el-input-number
            v-model="formData.win_coin_min"
            placeholder="最小"
            :precision="2"
            controls-position="right"
            class="range-input"
          />
          <span class="range-sep">至</span>
          <el-input-number
            v-model="formData.win_coin_max"
            placeholder="最大"
            :precision="2"
            controls-position="right"
            class="range-input"
          />
        </div>
      </el-form-item>
    </el-col>
    <el-col v-bind="setSpan(6)">
      <el-form-item label="奖励配置" prop="reward_ui_text">
        <el-input v-model="formData.reward_ui_text" placeholder="前端显示文本模糊" clearable />
      </el-form-item>
    </el-col>
    <el-col v-bind="setSpan(6)">
      <el-form-item label="中奖名(档位)" prop="reward_tier">
        <el-select v-model="formData.reward_tier" placeholder="全部" 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) => ({
    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>
