<!-- 搜索表单 -->
<template>
  <a-space>
    <regions-select
      v-model:value="city"
      valueField="label"
      placeholder="所在地区"
      type="provinceCity"
      class="ele-fluid"
      @change="onProvinceCity"
    />
    <regions-select
      v-model:value="cityMate"
      valueField="label"
      placeholder="择偶区域"
      type="provinceCity"
      class="ele-fluid"
      @change="onProvinceCityMate"
    />
    <a-input-search
      allow-clear
      placeholder="请输入搜索关键词"
      v-model:value="where.keywords"
      @pressEnter="search"
      @search="search"
      style="width: 240px"
    />
    <a-button @click="reset">重置</a-button>
  </a-space>
</template>

<script lang="ts" setup>
  import { PlusOutlined, DeleteOutlined } from '@ant-design/icons-vue';
  import useSearch from '@/utils/use-search';
  import { ref, watch } from "vue";
  import { Grade } from '@/api/user/grade/model';
  import type { UserParam } from '@/api/system/user/model';

  const emit = defineEmits<{
    (e: 'search', where?: UserParam): void;
    (e: 'add'): void;
    (e: 'remove'): void;
  }>();

  const props = defineProps<{
    // 勾选的项目
    selection?: [];
  }>();

  // 省市区
  const city = ref<string[]>([]);
  const cityMate = ref<string[]>([]);

  // 表单数据
  const { where, resetFields } = useSearch<UserParam>({
    userId: undefined,
    gradeId: undefined,
    gradeName: undefined,
    nickname: undefined,
    username: undefined,
    phone: undefined,
    realName: undefined,
    showProfile: true,
    city: undefined,
    cityMate: undefined,
    keywords: undefined
  });

  const onProvinceCity = (data) => {
    where.city = data[1];
    search();
  };

  const onProvinceCityMate = (data) => {
    where.cityMate = data[1];
    search();
  };

  const chooseGradeId = (data: Grade) => {
    where.gradeId = data.gradeId;
    where.gradeName = data.name;
    search();
  };

  /* 搜索 */
  const search = () => {
    emit('search', where);
  };

  // 新增
  const add = () => {
    emit('add');
  };

  // 批量删除
  const removeBatch = () => {
    emit('remove');
  };

  /*  重置 */
  const reset = () => {
    resetFields();
    search();
  };

  // 监听字典id变化
  watch(
    () => props.selection,
    () => {}
  );
</script>
