<!-- 搜索表单 -->
<template>
  <a-space :size="10" style="flex-wrap: wrap">
    <!--    <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-input-search
      allow-clear
      placeholder="请输入关键词"
      v-model:value="searchText"
      @pressEnter="search"
      @search="search"
    />
  </a-space>
</template>

<script lang="ts" setup>
  import {
    PlusOutlined,
    EditOutlined,
    SearchOutlined,
    DeleteOutlined,
    UpSquareOutlined,
    DownSquareOutlined
  } from '@ant-design/icons-vue';
  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('orderNo');
  // 搜索内容
  const searchText = ref('');
  // 日期范围选择
  const dateRange = ref<[string, string]>(['', '']);
  const listType = ref<number>(0);

  /* 搜索 */
  const search = () => {
    const [d1, d2] = dateRange.value ?? [];
    if (type.value == 'orderNo') {
      where.orderNo = searchText.value;
      where.userId = undefined;
    }
    if (type.value == 'userId') {
      where.userId = searchText.value;
      where.orderNo = undefined;
    }
    console.log(where);
    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);
    console.log(listType);
    // 全部订单
    if (listType == 0) {
      console.log('全部订单');
    }
    // 待发货
    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.orderStatus = 20;
    }
    emit('search', {
      ...where
    });
  };

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