<!-- 搜索表单 -->
<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"
      v-role="'admin'"
    >
      <template #icon>
        <delete-outlined />
      </template>
      <span>删除</span>
    </a-button>
    <a-input-search
      allow-clear
      placeholder="请输入搜索关键词"
      v-model:value="searchText"
      @pressEnter="search"
      @search="search"
    >
      <template #addonBefore>
        <a-select v-model:value="type" style="width: 100px; margin: -5px -12px">
          <a-select-option value="projectName">项目名称</a-select-option>
          <a-select-option value="projectCode">项目标识</a-select-option>
        </a-select>
      </template>
    </a-input-search>
    <a-button type="link"> 高级搜索 </a-button>
  </a-space>
</template>

<script lang="ts" setup>
  import {
    PlusOutlined,
    SearchOutlined,
    DeleteOutlined
  } from '@ant-design/icons-vue';
  import useSearch from '@/utils/use-search';
  import type { ProjectParam } from '@/api/oa/project/model';
  import { ref } from 'vue';

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

  // 表单数据
  const { where } = useSearch<ProjectParam>({
    projectName: '',
    projectCode: ''
  });

  // 下来选项
  const type = ref('projectName');
  // 搜索内容
  const searchText = ref('');

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

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

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