<script setup lang="ts">
import { computed } from 'vue';
import type { SearchField } from '@/components/custom/table-search-fields.vue';
import TableSearchFields from '@/components/custom/table-search-fields.vue';

defineOptions({ name: 'PerformanceSearch' });

interface Option {
  label: string;
  value: string | number;
}

interface Props {
  teamMode?: boolean;
  subordinateOptions?: Option[];
  deptOptions?: Option[];
  statusOptions?: Option[];
}

const props = withDefaults(defineProps<Props>(), {
  teamMode: false,
  subordinateOptions: () => [],
  deptOptions: () => [],
  statusOptions: () => []
});

const model = defineModel<Api.Performance.Sheet.SearchParams>('model', { required: true });

const emit = defineEmits<{
  reset: [];
  search: [];
}>();

const baseFields = computed<SearchField[]>(() => [
  {
    key: 'periodMonthRange',
    label: '绩效月份',
    type: 'dateRange',
    dateRangeType: 'monthrange',
    valueFormat: 'YYYY-MM-DD',
    placeholder: '选择月份区间'
  },
  { key: 'statusCode', label: '状态', type: 'select', placeholder: '请选择状态', options: props.statusOptions }
]);

const teamFields = computed<SearchField[]>(() => [
  baseFields.value[0],
  {
    key: 'employeeId',
    label: '下属',
    type: 'select',
    placeholder: '请选择下属',
    options: props.subordinateOptions,
    filterable: true
  },
  { key: 'employeeDeptId', label: '部门', type: 'select', placeholder: '请选择部门', options: props.deptOptions },
  { key: 'managerName', label: '直属上级', type: 'input', placeholder: '请输入直属上级' },
  baseFields.value[1]
]);

const fields = computed<SearchField[]>(() => (props.teamMode ? teamFields.value : baseFields.value));
</script>

<template>
  <TableSearchFields v-model="model" :fields="fields" :columns="4" @reset="emit('reset')" @search="emit('search')" />
</template>
