<!-- 搜索表单 -->
<template>
  <a-space :size="10" style="flex-wrap: wrap">
    <a-radio-group v-model:value="listType" @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="4">进行中</a-radio-button>
      <!-- <a-radio-button :value="5">已取消</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-select v-model:value="expire" style="width: 100px; " @change="search">
      <a-select-option value="0">到期时间</a-select-option>
      <a-select-option value="1">7天</a-select-option>
      <a-select-option value="2">3天</a-select-option>
      <a-select-option value="3">已逾期</a-select-option>
    </a-select>
    <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="userId">用户ID</a-select-option>
          <a-select-option value="equipmentCode">电池编号</a-select-option>
        </a-select>
      </template>
    </a-input-search>

    <a-button
      danger
      type="primary"
      class="ele-btn-icon"
      :disabled="selection.length === 0"
      @click="removeBatch"
    >
      <template #icon>
        <delete-outlined />
      </template>
      <span>批量删除</span>
    </a-button>
  </a-space>
</template>

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

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

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

  // 表单数据
  const { where, resetFields } = useSearch<OrderParam>({
    orderNo: undefined,
    userId: undefined,
    payStatus: undefined,
    deliveryStatus: undefined,
    orderStatus: undefined
  });
  // 下来选项
  const type = ref('keywords');
  // 搜索内容
  const searchText = ref('');
  const expire = ref('0');
  // 日期范围选择
  const dateRange = ref<[string, string]>(['', '']);
  const listType = ref<number>(0);

  /* 搜索 */
  const search = () => {
    const [d1, d2] = dateRange.value ?? [];

    if(expire.value == '1'){
      where.expireDay = 7
      where.isExpire = -1
    } else if(expire.value == '2'){
      where.expireDay = 3
      where.isExpire = -1
    } else if(expire.value == '3'){
      where.isExpire = 1
      where.expireDay = 0
    } else {
      where.isExpire = -1
      where.expireDay = 0

      if (type.value == 'orderNo') {
        where.orderNo = searchText.value;
        where.userId = undefined;
      }
      if (type.value == 'userId') {
        where.userId = searchText.value;
        where.orderNo = undefined;
      }
      if (type.value == 'merchantCode') {
        where.merchantCode = searchText.value;
        where.orderNo = undefined;
      }
      if(type.value == 'equipmentCode'){
        where.keywords = searchText.value;//直接通过模糊查询来查询电池编号
        where.orderNo = undefined;
      }
      if (type.value == 'keywords') {
        where.keywords = searchText.value;
      }
    }
    emit('search', {
      ...where,
      createTimeStart: d1 ? d1 + ' 00:00:00' : '',
      createTimeEnd: d2 ? d2 + ' 23:59:59' : ''
    });
  };

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

  const handleTabs = (e) => {
    resetFields();
    const listType = Number(e.target.value);
    // 全部订单
    if (listType == 0) {
      // console.log('全部订单');
    } else {
      where.isExpire = -1
      where.expireDay = 0
    }
    // 待发货
    // if (listType == 1) {
    //   console.log('待发货');
    //   where.payStatus = 20;
    //   where.deliveryStatus = 10;
    // }
    // 待收货
    if (listType == 2) {
      // console.log('待发货');
      where.payStatus = 20;
      where.deliveryStatus <= 20;
      where.receiptStatus = 10;
    }
    // 待付款
    if (listType == 3) {
      // console.log('待付款');
      where.payStatus = 10;
    }
    // 已完成
    if (listType == 4) {
      // console.log('已完成');
      where.payStatus = 20;
      where.orderStatus = 30;
    }
    // 已取消
    if (listType == 5) {
      // console.log('已取消');
      where.receiptStatus = 30;
    }
    emit('search', {
      ...where
    });
  };

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