<!-- 搜索表单 -->
<template>
  <a-space>
    <a-button type="primary" class="ele-btn-icon" @click="add">
      <template #icon>
        <PlusOutlined />
      </template>
      <span>添加</span>
    </a-button>
    <a-button
      danger
      type="primary"
      class="ele-btn-icon"
      :disabled="selection.length === 0"
      @click="removeBatch"
    >
      <template #icon>
        <DeleteOutlined />
      </template>
      <span>批量删除</span>
    </a-button>
    <a-button
      danger
      type="primary"
      class="ele-btn-icon"
      :disabled="selection.length === 0"
      @click="banBatch"
    >
      <template #icon>
        <DeleteOutlined />
      </template>
      <span>下架</span>
    </a-button>
    <SelectUser
      placeholder="请选择用户"
      v-model:value="where.nickname"
      @done="chooseUserId"
    />
    <DictSelect
      dict-code="region"
      v-model:value="where.region"
      style="width: 180px"
      placeholder="请选择所在区域"
      @change="onChange"
    />
    <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 { watch } from 'vue';
  import type { HouseInfoParam } from '@/api/house/info/model';
  import { User } from '@/api/system/user/model';
  import DictSelect from '@/components/DictSelect/index.vue';

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

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

  // 表单数据
  const { where, resetFields } = useSearch<HouseInfoParam>({
    userId: undefined,
    nickname: '',
    region: undefined,
    keywords: undefined
  });

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

  const onChange = (text) => {
    where.region = text;
    search();
  };

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

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

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

  const banBatch = () => {
    emit('ban')
  }

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

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