<!-- 搜索表单 -->
<template>
  <a-space :size="10" style="flex-wrap: wrap">
    <a-radio-group v-model:value="applyStatus" @change="handleApplyStatus">
      <a-radio-button :value="0">全部</a-radio-button>
      <a-radio-button :value="10">待审核</a-radio-button>
      <a-radio-button :value="20">审核通过</a-radio-button>
      <a-radio-button :value="30">审核拒绝</a-radio-button>
    </a-radio-group>
    <a-range-picker
      v-model:value="dateRange"
      value-format="YYYY-MM-DD"
      class="ele-fluid"
    />
    <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="keywords">模糊搜索</a-select-option>
          <a-select-option value="withdrawCode">单号</a-select-option>
          <a-select-option value="nickname">用户名</a-select-option>
        </a-select>
      </template>
    </a-input-search>
  </a-space>
</template>

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

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

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

  // 表单数据
  const { where, resetFields } = useSearch<WithdrawParam>({

  });
  // 下来选项
  const type = ref('keywords');
  // 搜索内容
  const searchText = ref('');
  // 日期范围选择
  const dateRange = ref<[string, string]>(['', '']);
  const applyStatus = ref<number>(0);

  /* 搜索 */
  const search = () => {
    const [d1, d2] = dateRange.value ?? [];
    if (type.value == 'withdrawCode') {
      where.withdrawCode = searchText.value;
    }
    if (type.value == 'nickname') {
      where.nickname = searchText.value;
    }
    if (type.value == 'keywords') {
      where.keywords = searchText.value;
    }
    emit('search', {
      ...where,
      beginDate: d1,
      endDate: d2
    });
  };

  const handleApplyStatus = (e) => {
    resetFields();
    const applyStatus = Number(e.target.value);
    if(applyStatus > 0){
      where.applyStatus = applyStatus;
    }

    emit('search', {
      ...where
    });
  };

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