<!-- 搜索表单 -->
<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"
      :disabled="selection.length === 0"
      @click="removeBatch"
    >
      <template #icon>
        <delete-outlined />
      </template>
      <span>批量删除</span>
    </a-button>
    <a-input-search
      allow-clear
      placeholder="请输入关键词"
      v-model:value="searchText"
      @pressEnter="search"
      @search="search"
    />
    <a-button @click="reload">刷新</a-button>
  </a-space>
</template>

<script lang="ts" setup>
  import useSearch from '@/utils/use-search';
  import type { CustomerParam } from '@/api/oa/customer/model';
  import { ref, watch } from 'vue';
  import { TaskParam } from '@/api/oa/task/model';
  import { assignObject } from 'ele-admin-pro';
  import {
    PlusOutlined,
    EditOutlined,
    SearchOutlined,
    DeleteOutlined,
    UpSquareOutlined,
    DownSquareOutlined
  } from '@ant-design/icons-vue';
  import { useRouter } from 'vue-router';

  const props = withDefaults(
    defineProps<{
      // 选中的角色
      selection?: [];
    }>(),
    {}
  );

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

  // 表单数据
  const { where } = useSearch<TaskParam>({
    taskId: undefined,
    name: undefined,
    userId: undefined,
    nickname: undefined
  });
  // 下来选项
  const type = ref('keywords');
  // 搜索内容
  const searchText = ref('');
  const { push } = useRouter();

  /* 搜索 */
  const search = () => {
    assignObject(where, {});
    if (type.value == 'keywords') {
      where.keywords = searchText.value;
    }
    emit('search', where);
  };

  // 新增
  const add = () => {
    push('/user/task/add');
  };

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

  const reload = () => {
    // 刷新当前路由
    emit('search', where);
  };

  watch(
    () => props.selection,
    () => {}
  );
</script>
