<template>
  <sa-search-bar
    ref="searchBarRef"
    v-model="formData"
    label-width="100px"
    :showExpand="false"
    @reset="handleReset"
    @search="handleSearch"
    @expand="handleExpand"
  >
    <el-col v-bind="setSpan(5)">
      <el-form-item label="类型" prop="type">
        <el-select v-model="formData.type" placeholder="全部" clearable style="width: 100%">
          <el-option label="充值" :value="0" />
          <el-option label="提现" :value="1" />
          <el-option label="购买抽奖次数" :value="2" />
          <el-option label="管理员加点" :value="3" />
          <el-option label="管理员扣点" :value="4" />
        </el-select>
      </el-form-item>
    </el-col>
    <el-col v-bind="setSpan(5)">
      <el-form-item label="用户(用户名)" prop="username">
        <el-input v-model="formData.username" placeholder="按用户名搜索" clearable />
      </el-form-item>
    </el-col>
    <el-col v-bind="setSpan(8)">
      <el-form-item label="平台币" prop="coin_min">
        <div class="coin-range-wrap">
          <el-input-number
            v-model="formData.coin_min"
            placeholder="最小"
            :precision="2"
            controls-position="right"
            class="coin-range-input"
          />
          <span class="coin-range-sep">至</span>
          <el-input-number
            v-model="formData.coin_max"
            placeholder="最大"
            :precision="2"
            controls-position="right"
            class="coin-range-input"
          />
        </div>
      </el-form-item>
    </el-col>
    <el-col v-bind="setSpan(8)">
      <el-form-item label="创建时间" prop="create_time">
        <el-date-picker
          v-model="formData.create_time"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
          value-format="YYYY-MM-DD HH:mm:ss"
          style="width: 100%"
        />
      </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, // 平板：大于等于12保持，否则用半宽
      md: span >= 8 ? span : 8, // 中等屏幕：大于等于8保持，否则用三分之一宽
      lg: span,
      xl: span
    }
  }
</script>

<style lang="scss" scoped>
  .coin-range-wrap {
    display: flex;
    align-items: center;
    gap: 8px;
    min-width: 0; /* 允许在栅格内收缩，避免挤压右侧按钮 */
  }

  .coin-range-input {
    flex: 1;
    min-width: 0; /* 数字框可收缩 */
  }

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