<!--
 * @Author: chris
 * @Date: 2025-08-08 16:17:37
 * @LastEditors: chris
 * @LastEditTime: 2025-08-11 15:34:00
-->
<template>
  <el-form :model="queryParams" ref="queryRef" :inline="true" label-width="68px">
    <el-form-item label="果园名称" prop="orchardName">
      <el-input v-model="queryParams.orchardName" placeholder="请输入果园名称" clearable style="width: 240px" @keyup.enter="handleQuery" />
    </el-form-item>
    <el-form-item label="负责人" prop="manager">
      <el-input v-model="queryParams.manager" placeholder="请输入负责人" clearable style="width: 240px" @keyup.enter="handleQuery" />
    </el-form-item>
    <el-form-item label="状态" prop="status">
      <el-select v-model="queryParams.status" placeholder="果园状态" clearable style="width: 240px">
        <el-option v-for="dict in orchardStatus" :key="dict.value" :label="dict.label" :value="dict.value" />
      </el-select>
    </el-form-item>
    <el-form-item label="创建时间" style="width: 308px">
      <el-date-picker 
        v-model="dateRangeLocal"
        value-format="YYYY-MM-DD"
        type="daterange"
        range-separator="至"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
        @change="handleDateChange"
      ></el-date-picker>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
      <el-button icon="Refresh" @click="resetQuery">重置</el-button>
    </el-form-item>
  </el-form>
</template>

<script setup>
import { ref } from 'vue'
import { useDict } from '@/utils/dict'

// 定义props
const props = defineProps({
  queryParams: { type: Object, required: true },
  dateRange: { type: Array, required: true }
})

// 定义事件
const emits = defineEmits(['query', 'reset', 'update:dateRange'])

// 字典数据
const { orchard_status: orchardStatus } = useDict('orchard_status')

// 创建本地变量存储日期范围
const dateRangeLocal = ref([...props.dateRange])

// 监听props.dateRange变化，同步到本地变量
watch(
  () => props.dateRange,
  (newVal) => {
    dateRangeLocal.value = [...newVal]
  },
  { deep: true }
)

// 日期变化时通知父组件
function handleDateChange(value) {
  emits('update:dateRange', value)
}

// 搜索按钮操作
function handleQuery() {
  emits('query')
}

// 重置按钮操作
function resetQuery() {
  // 重置日期范围
  dateRangeLocal.value = []
  emits('update:dateRange', [])
  emits('reset')
}
</script>