<!-- 搜索表单 -->
<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-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="4">已完成</a-radio-button>
      <a-radio-button :value="5">待付款</a-radio-button>
      <a-radio-button :value="6">临时报餐</a-radio-button>
    </a-radio-group>
    <a-date-picker
      placeholder="预定日期"
      value-format="YYYY-MM-DD"
      v-model:value="where.deliveryTime"
      @change="search"
    />
    <a-select
      v-model:value="where.week"
      style="width: 120px"
      placeholder="请选择星期"
      allow-clear
      @change="handleWeek"
    >
      <a-select-option :value="1">星期一</a-select-option>
      <a-select-option :value="2">星期二</a-select-option>
      <a-select-option :value="3">星期三</a-select-option>
      <a-select-option :value="4">星期四</a-select-option>
      <a-select-option :value="5">星期五</a-select-option>
      <a-select-option :value="6">星期六</a-select-option>
      <a-select-option :value="0">星期日</a-select-option>
    </a-select>
    <a-input-search
      allow-clear
      placeholder="请输入关键词"
      v-model:value="where.keywords"
      @pressEnter="search"
      @search="search"
    />
  </a-space>
</template>

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

  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,
    week: undefined,
    deliveryStatus: undefined,
    orderStatus: undefined
  });
  // 日期范围选择
  const dateRange = ref<[string, string]>(['', '']);
  const listType = ref<number>(0);

  /* 搜索 */
  const search = () => {
    const [d1, d2] = dateRange.value ?? [];
    emit('search', {
      ...where,
      createTimeStart: d1 ? d1 + ' 00:00:00' : '',
      createTimeEnd: d2 ? d2 + ' 23:59:59' : '',
      deliveryTime: where.deliveryTime ? where.deliveryTime + ' 00:00:00' : ''
    });
  };

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

  const handleTabs = (e) => {
    resetFields();
    const listType = Number(e.target.value);

    // 全部订单
    if (listType == 0) {
      assignObject(where, {});
      console.log('全部');
    }
    // 待发货
    if (listType == 1) {
      console.log('已下单');
      where.payStatus = 20;
      where.orderStatus = 10;
    }
    // 待收货
    // if (listType == 2) {
    //   console.log('待发货');
    //   where.payStatus = 20;
    //   where.deliveryStatus = 20;
    //   where.receiptStatus = 10;
    // }
    // 待付款
    // if (listType == 3) {
    //   console.log('待付款');
    //   where.orderStatus = 20;
    // }
    // 已完成
    if (listType == 4) {
      console.log('已完成');
      where.payStatus = 20;
      where.deliveryStatus = 30;
    }
    // 已取消
    if (listType == 5) {
      where.payStatus = 10;
    }
    // 已取消
    if (listType == 6) {
      console.log('临时报餐');
      where.isTemporary = 1;
    }
    console.log(where);
    emit('search', {
      ...where
    });
  };

  const handleWeek = (index) => {
    where.week = index;
    search();
  };

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