<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.sendNum" placeholder="发货单号" clearable @keyup.enter="handleSearch" style="width: 180px" />
    </el-form-item>
    <el-form-item label="状态">
      <el-select v-model="searchForm.status" placeholder="全部状态" clearable style="width: 120px">
        <el-option
          v-for="item in statusOptions"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        />
      </el-select>
    </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 { ShippingStatus, type ShippingOrderListRequest } from '@/api/business/order'

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

const searchForm = reactive({
  userId: '',
  mobile: '',
  sendNum: '',
  status: undefined as number | undefined
})

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

// 状态选项
const statusOptions = [
  { value: ShippingStatus.Pending, label: '待发货' },
  { value: ShippingStatus.Shipped, label: '已发货' },
  { value: ShippingStatus.Received, label: '已签收' },
  { value: ShippingStatus.Cancelled, label: '已取消' }
]

const handleSearch = () => {
  const params: Partial<ShippingOrderListRequest> = {}
  
  if (searchForm.userId) {
    params.userId = parseInt(searchForm.userId)
  }
  if (searchForm.mobile) {
    params.mobile = searchForm.mobile
  }
  if (searchForm.sendNum) {
    params.sendNum = searchForm.sendNum
  }
  if (searchForm.status !== undefined) {
    params.status = searchForm.status
  }
  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.sendNum = ''
  searchForm.status = undefined
  dateRange.value = null
  emit('reset')
}
</script>

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