<!-- 搜索表单 -->
<template>
  <a-space :size="10" style="flex-wrap: wrap">
    <a-radio-group v-model:value="sceneType" @change="handleTabs">
      <a-radio-button :value="0">全部</a-radio-button>
      <a-radio-button :value="1">资产收益</a-radio-button>
      <a-radio-button :value="2">服务费收益</a-radio-button>
      <a-radio-button :value="3">推广收益</a-radio-button>
      <a-radio-button :value="4">门店业绩提成</a-radio-button>
      <a-radio-button :value="5">站点业绩提成</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="orderNo">订单号</a-select-option>
          <a-select-option value="merchantCode">门店编号</a-select-option>
          <a-select-option value="orderUserName">用户名</a-select-option>
          <a-select-option value="beneficiaryName">收益人</a-select-option><!--收益人信息存储在comments中 -->
        </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 { ProfitParam } from '@/api/profit/model';

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

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

  // 表单数据
  const { where, resetFields } = useSearch<ProfitParam>({
    scene: 0

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

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

  const handleTabs = (e) => {
    resetFields();
    const sceneType = Number(e.target.value);
    if(sceneType > 0){
      where.scene = sceneType;
    }

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

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