<!-- 搜索表单 -->
<template>
  <a-space :size="10" style="flex-wrap: wrap">
    <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"
      v-if="selection.length > 0"
      @click="removeBatch"
    >
      <template #icon>
        <DeleteOutlined />
      </template>
      <span>批量删除</span>
    </a-button>
    <!--    <a-button @click="batchMove" v-if="selection.length > 0">-->
    <!--      <template #icon>-->
    <!--        <UserSwitchOutlined />-->
    <!--      </template>-->
    <!--      批量转移-->
    <!--    </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="customerName">客户名称</a-select-option>
          <a-select-option value="customerCode">客户标识</a-select-option>
        </a-select>
      </template>
    </a-input-search>
    <UserSelect
      v-if="tabValue === 'all'"
      v-model:value="where.userId"
      :placeholder="`按用户筛选`"
      @select="search"
      @clear="onClear"
    />
  </a-space>
</template>

<script lang="ts" setup>
  import {
    PlusOutlined,
    DeleteOutlined
  } from '@ant-design/icons-vue';
  import useSearch from '@/utils/use-search';
  import type { CustomerParam } from '@/api/oa/customer/model';
  import { ref, watch } from 'vue';
  import { assignObject } from 'ele-admin-pro';
  import UserSelect from '@/components/UserSelect/index.vue';

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

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

  // 表单数据
  const { where } = useSearch<CustomerParam>({
    customerName: '',
    customerCode: '',
    nickname: '',
    userId: undefined
  });
  // 下来选项
  const type = ref('customerName');
  // 搜索内容
  const searchText = ref('');

  /* 搜索 */
  const search = (item?: CustomerParam) => {
    assignObject(where, {});
    console.log(item);
    if (type.value == 'customerName') {
      where.customerName = searchText.value;
    }
    if (type.value == 'customerCode') {
      where.customerCode = searchText.value;
    }
    if (item?.userId) {
      where.userId = item.userId;
    }
    emit('search', where);
  };

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

  // 转移
  // const batchMove = () => {
  //   emit('batchMove');
  // };

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

  const onClear = () => {
    where.userId = undefined;
    search();
  };

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