<template>
  <el-form :inline="true" :model="searchForm" class="search-form">
    <el-form-item label="用户ID">
      <el-input v-model="searchForm.userId" placeholder="用户ID" 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-select v-model="searchForm.status" placeholder="全部" clearable style="width: 120px">
        <el-option
          v-for="(label, value) in OrderStatusLabels"
          :key="value"
          :label="label"
          :value="Number(value)"
        />
      </el-select>
    </el-form-item>
    <el-form-item label="账号类型">
      <el-select v-model="searchForm.accountType" placeholder="全部" clearable style="width: 100px">
        <el-option
          v-for="(label, value) in AccountTypeLabels"
          :key="value"
          :label="label"
          :value="Number(value)"
        />
      </el-select>
    </el-form-item>
    <el-form-item label="账号状态">
      <el-select v-model="searchForm.accountStatus" placeholder="全部" clearable style="width: 100px">
        <el-option
          v-for="(label, value) in AccountStatusLabels"
          :key="value"
          :label="label"
          :value="Number(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 {
  OrderStatusLabels,
  AccountTypeLabels,
  AccountStatusLabels,
  type ComprehensiveOrderListRequest
} from '@/api/business/order'

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

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

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

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

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