<!-- 搜索表单 -->
<template>
  <a-space :size="10" style="flex-wrap: wrap">
    <DictSelect
      dict-code="ICType"
      :placeholder="`IC卡类型`"
      style="width: 120px"
      v-model:value="where.typeName"
      @done="onType"
    />
    <a-input-search
      allow-clear
      placeholder="请输入关键词"
      v-model:value="where.keywords"
      @pressEnter="search"
      @search="search"
    >
      <template #addonBefore>
        <a-select v-model:value="type" style="width: 100px; margin: -5px -12px">
          <a-select-option value="userId">用户ID</a-select-option>
          <a-select-option value="username">真实姓名</a-select-option>
          <a-select-option value="phone">手机号</a-select-option>
          <a-select-option value="cardNum">IC卡数字</a-select-option>
        </a-select>
      </template>
    </a-input-search>
    <a-button @click="reset">重置</a-button>
  </a-space>
</template>

<script lang="ts" setup>
  import { ref, watch } from 'vue';
  import useSearch from '@/utils/use-search';
  import { UserCardParam } from '@/api/booking/userCard/model';

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

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

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

  // 表单数据
  const { where, resetFields } = useSearch<UserCardParam>({
    type: undefined,
    userId: undefined,
    phone: undefined,
    keywords: undefined
  });

  const search = () => {
    if (type.value == 'userId') {
      where.userId = Number(where.keywords);
      where.keywords = undefined;
      emit('search', where);
    }
    if (type.value == 'username') {
      where.username = where.keywords;
      where.keywords = undefined;
      emit('search', where);
    }
    if (type.value == 'phone') {
      where.phone = where.keywords;
      where.keywords = undefined;
      emit('search', where);
    }
    if (type.value == 'cardNum') {
      where.cardNum = where.keywords;
      where.keywords = undefined;
      emit('search', where);
    }
    // emit('search', where);
  };

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

  const onType = (item: any) => {
    where.type = item.key;
    emit('search', where);
  };

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