<!-- 搜索表单 -->
<template>
  <a-space :size="10" style="flex-wrap: wrap">
    <!--    <a-button type="primary" class="ele-btn-icon" @click="add">-->
    <!--      <template #icon>-->
    <!--        <PlusOutlined />-->
    <!--      </template>-->
    <!--      <span>发布菜品</span>-->
    <!--    </a-button>-->
    <!--    <a-button-->
    <!--      danger-->
    <!--      type="primary"-->
    <!--      class="ele-btn-icon"-->
    <!--      :disabled="selection.length === 0"-->
    <!--      @click="removeBatch"-->
    <!--    >-->
    <!--      <template #icon>-->
    <!--        <DeleteOutlined />-->
    <!--      </template>-->
    <!--      <span>批量删除</span>-->
    <!--    </a-button>-->
    <a-date-picker
      placeholder="预定日期"
      value-format="YYYY-MM-DD"
      v-model:value="deliveryTime"
      @change="search"
    />
    <a-radio-group v-model:value="categoryId" @change="handleTabs">
      <a-radio-button :value="25">早餐</a-radio-button>
      <a-radio-button :value="26">午餐</a-radio-button>
      <a-radio-button :value="27">晚餐</a-radio-button>
    </a-radio-group>
    <a-radio-group v-model:value="deliveryStatus" @change="onDeliveryStatus">
      <a-radio-button :value="20">已签到</a-radio-button>
      <a-radio-button :value="10">未签到</a-radio-button>
    </a-radio-group>
    <a-radio-group v-model:value="gear" @change="onGear">
      <a-radio-button :value="10">食堂档口</a-radio-button>
      <a-radio-button :value="20">物品档口</a-radio-button>
    </a-radio-group>
    <!--    <a-button @click="onRepairData">修复数据</a-button>-->
    <a-input-search
      allow-clear
      placeholder="请输入订单号"
      v-model:value="searchText"
      @pressEnter="search"
      @search="search"
    />
    <span style="margin-left: 20px" v-if="deliveryTime"
      >已签到 {{ signUsers }} / 已报餐 {{ postUsers }}</span
    >
  </a-space>
</template>

<script lang="ts" setup>
  import { PlusOutlined, DeleteOutlined } from '@ant-design/icons-vue';
  import useSearch from '@/utils/use-search';
  import { ref, watch } from 'vue';
  import { OrderGoodsParam } from '@/api/order/goods/model';
  import { countOrderGoods, repairData } from '@/api/apps/statistics';
  import { Dayjs } from 'dayjs';
  import { message } from 'ant-design-vue';

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

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

  // 表单数据
  const { where, resetFields } = useSearch<OrderGoodsParam>({
    categoryId: undefined,
    deliveryTime: undefined
  });
  // 下来选项
  const type = ref('keywords');
  const categoryId = ref<number>(25);
  const signUsers = ref<number>(0);
  const postUsers = ref<number>(0);
  const deliveryStatus = ref<number>(0);
  const gear = ref<number>(0);
  // 搜索内容
  const searchText = ref('');
  // 预定日期
  const deliveryTime = ref<Dayjs>();
  // 日期范围选择
  const dateRange = ref<[string, string]>(['', '']);

  /* 搜索 */
  const search = () => {
    const [d1, d2] = dateRange.value ?? [];
    if (type.value == 'keywords') {
      where.keywords = searchText.value;
    }
    where.deliveryTime = deliveryTime.value
      ? deliveryTime.value + ' 00:00:00'
      : '';
    where.categoryId = categoryId.value;
    where.payStatus = 20;
    if (deliveryStatus.value > 0) {
      where.deliveryStatus = deliveryStatus.value;
    }
    if (gear.value > 0) {
      where.gear = gear.value;
    }
    emit('search', {
      ...where,
      createTimeStart: d1 ? d1 + ' 00:00:00' : '',
      createTimeEnd: d2 ? d2 + ' 23:59:59' : ''
    });
    count();
  };

  const handleTabs = (e) => {
    const index = Number(e.target.value);
    resetFields();
    categoryId.value = index;
    search();
  };

  const onDeliveryStatus = (e) => {
    const index = Number(e.target.value);
    resetFields();
    deliveryStatus.value = index;
    search();
  };

  const onGear = (e) => {
    const index = Number(e.target.value);
    resetFields();
    gear.value = index;
    search();
  };

  // 发布菜品
  const add = () => {
    emit('add');
  };

  const count = () => {
    where.deliveryTime = deliveryTime.value
      ? deliveryTime.value + ' 00:00:00'
      : '';
    where.payStatus = 20;
    countOrderGoods(where).then((res) => {
      console.log(res);
      if (categoryId.value == 25) {
        signUsers.value = res?.breakfastSignUsers;
        postUsers.value = res?.breakfastPostUsers;
      }
      if (categoryId.value == 26) {
        signUsers.value = res?.lunchSignUsers;
        postUsers.value = res?.lunchPostUsers;
      }
      if (categoryId.value == 27) {
        signUsers.value = res?.dinnerSignUsers;
        postUsers.value = res?.dinnerPostUsers;
      }
    });
  };

  const onRepairData = () => {
    where.deliveryTime = deliveryTime.value
      ? deliveryTime.value + ' 00:00:00'
      : '';
    // where.payStatus = 10;
    repairData({}).then(() => {
      // message.success(res.message);
    });
  };

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

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