<template>
  <el-form :inline="true" :model="searchForm" class="search-form">
    <el-form-item label="用户ID">
      <el-input v-model="searchForm.userId" placeholder="用户ID/UID" clearable @keyup.enter="handleSearch" style="width: 120px" />
    </el-form-item>
    <el-form-item label="手机号">
      <el-input v-model="searchForm.mobile" placeholder="手机号" clearable @keyup.enter="handleSearch" style="width: 140px" />
    </el-form-item>
    <el-form-item label="订单号">
      <el-input v-model="searchForm.orderNum" placeholder="订单号" clearable @keyup.enter="handleSearch" style="width: 180px" />
    </el-form-item>
    <el-form-item label="下单时间">
      <el-date-picker
        v-model="dateRange"
        type="daterange"
        range-separator="至"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
        value-format="YYYY-MM-DD"
        style="width: 240px"
      />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="handleSearch">
        <el-icon><Search /></el-icon>查询
      </el-button>
      <el-button @click="handleReset">
        <el-icon><Refresh /></el-icon>重置
      </el-button>
    </el-form-item>
  </el-form>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import { Search, Refresh } from '@element-plus/icons-vue'
import type { OrderListRequest } from '@/api/business/order'

const emit = defineEmits<{
  (e: 'search', params: Partial<OrderListRequest>): void
  (e: 'reset'): void
}>()

const searchForm = reactive({
  userId: '',
  mobile: '',
  orderNum: ''
})

const dateRange = ref<[string, string] | null>(null)

const handleSearch = () => {
  const params: Partial<OrderListRequest> = {}
  
  if (searchForm.userId) {
    const parsedUserId = parseInt(searchForm.userId)
    if (!isNaN(parsedUserId)) {
      params.userId = parsedUserId
    }
  }
  if (searchForm.mobile) {
    params.mobile = searchForm.mobile
  }
  if (searchForm.orderNum) {
    params.orderNum = searchForm.orderNum
  }
  if (dateRange.value && dateRange.value.length === 2) {
    params.startDate = dateRange.value[0]
    params.endDate = dateRange.value[1]
  }
  
  emit('search', params)
}

const handleReset = () => {
  searchForm.userId = ''
  searchForm.mobile = ''
  searchForm.orderNum = ''
  dateRange.value = null
  emit('reset')
}
</script>

<style scoped>
.search-form {
  margin-bottom: 16px;
}
</style>
