<!-- 搜索表单 -->
<template>
  <a-space>
    <a-button type="primary" class="ele-btn-icon" @click="add">
      <template #icon>
        <PlusOutlined />
      </template>
      <span>添加</span>
    </a-button>
    <SelectUser
      :placeholder="`推荐人`"
      v-model:value="where.dealerName"
      @done="chooseDealerId"
    />
    <SelectUser
      :placeholder="`用户`"
      v-model:value="where.nickname"
      @done="chooseUserId"
    />
    <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 type { MerchantParam } from '@/api/merchant/model';
  import { watch } from 'vue';
  import SelectUser from '@/components/SelectUser/index.vue';
  import { UserReferee } from '@/api/user/referee/model';
  import { User } from '@/api/system/user/model';

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

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

  // 表单数据
  const { where, resetFields } = useSearch<UserReferee>({
    id: undefined,
    userId: undefined,
    dealerId: undefined,
    nickname: '',
    dealerName: ''
  });

  const chooseDealerId = (data: User) => {
    where.dealerName = data.nickname;
    where.dealerId = data.userId;
    emit('search', where);
  };

  const chooseUserId = (data: User) => {
    where.nickname = data.nickname;
    where.userId = data.userId;
    emit('search', where);
  };

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

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

  /*  重置 */
  const reset = () => {
    resetFields();
    emit('search', where);
  };

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