<!-- 搜索表单 -->
<template>
  <a-space :size="10" style="flex-wrap: wrap">
    <a-button
      type="primary"
      class="ele-btn-icon"
      @click="add"
    >
      <template #icon>
        <plus-outlined />
      </template>
      <span>新建</span>
    </a-button>
    <a-button
      danger
      type="primary"
      class="ele-btn-icon"
      @click="removeBatch"
      :disabled="props.selection.length === 0"
    >
      <template #icon>
        <delete-outlined />
      </template>
      <span>批量删除</span>
    </a-button>
    <a-input-search
      allow-clear
      v-model:value="searchText"
      placeholder="请输入关键词"
      @search="search"
      @pressEnter="search"
    >
      <template #addonBefore>
        <a-select v-model:value="type" style="width: 100px; margin: -5px -12px">
          <a-select-option value="title">文章标题</a-select-option>
          <a-select-option value="articleId">文章ID</a-select-option>
          <a-select-option value="userId">用户ID</a-select-option>
        </a-select>
      </template>
    </a-input-search>
  </a-space>
</template>

<script lang="ts" setup>
  import {
    PlusOutlined,
    SearchOutlined,
    DeleteOutlined
  } from '@ant-design/icons-vue';
  import useSearch from '@/utils/use-search';
  import type { ArticleParam } from '@/api/cms/article/model';
  import { ref, watch } from 'vue';
  import { assignObject } from 'ele-admin-pro';

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

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

  // 表单数据
  const { where } = useSearch<ArticleParam>({
    title: ''
  });

  // 搜索内容
  const searchText = ref('');
  const type = ref('title');
  /* 搜索 */
  const search = () => {
    assignObject(where, {});
    if (type.value == 'title') {
      where.title = searchText.value;
    }
    if (type.value == 'articleId') {
      where.articleId = Number(searchText.value);
    }
    if (type.value == 'userId') {
      where.userId = Number(searchText.value);
    }
    emit('search', where);
  };

  /*  添加 */
  const add = () => {
    emit('add');
  };

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

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