<!-- 搜索表单 -->
<template>
  <div class="search">
    <a-space :size="10" style="flex-wrap: wrap">
      <!--      <SelectOrganization-->
      <!--        :placeholder="`按部门筛选`"-->
      <!--        v-model:value="where.organizationName"-->
      <!--        @done="chooseOrganization"-->
      <!--      />-->
      <SelectGrade
        :placeholder="`按等级筛选`"
        v-model:value="where.gradeName"
        @done="chooseGradeId"
      />
      <a-radio-group v-model:value="where.isAdmin" @change="search">
        <a-radio-button :value="1">管理员</a-radio-button>
      </a-radio-group>
      <a-range-picker
        v-model:value="dateRange"
        @change="search"
        value-format="YYYY-MM-DD"
        class="ele-fluid"
      />
      <!--      <a-radio-group v-model:value="where.categoryId" @change="search">-->
      <!--        <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-button class="ele-btn-icon" @click="handleExport">-->
      <!--        <template #icon>-->
      <!--          <download-outlined />-->
      <!--        </template>-->
      <!--        <span>导出</span>-->
      <!--      </a-button>-->
      <a-input-search
        allow-clear
        placeholder="姓名|手机号|用户ID"
        v-model:value="where.keywords"
        @pressEnter="search"
        @search="search"
        style="width: 360px"
      />
      <a-button @click="reset">重置</a-button>
      <!--      <span style="margin-left: 20px"-->
      <!--        >已签到 {{ signUsers }} / 已报餐 {{ postUsers }}</span-->
      <!--      >-->
    </a-space>
  </div>
</template>

<script lang="ts" setup>
  import useSearch from '@/utils/use-search';
  import { ref, watch } from 'vue';
  import { OrderGoods, OrderGoodsParam } from '@/api/order/goods/model';
  import { message } from 'ant-design-vue';
  import { Organization } from '@/api/system/organization/model';
  import { utils, writeFile } from 'xlsx';
  import dayjs from 'dayjs';
  import { Grade } from '@/api/user/grade/model';
  import { BookingUserParam } from '@/api/booking/bookingUser/model';

  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<BookingUserParam>({
    id: undefined,
    keywords: undefined,
    timeStart: undefined,
    timeEnd: undefined
  });
  // 下来选项
  // const categoryId = ref<number>(0);
  const post = ref<number>(0);
  const sign = ref<number>(0);
  const noSign = ref<number>(0);
  // 日期范围选择
  const dateRange = ref<[string, string]>(['', '']);

  /* 搜索 */
  const search = () => {
    const [d1, d2] = dateRange.value ?? [];
    where.timeStart = d1 ? dayjs(d1).valueOf() / 1000 : undefined;
    where.timeEnd = d2 ? dayjs(d2).valueOf() / 1000 : undefined;
    // where.createTimeStart = d1 ? d1 + ' 00:00:00' : undefined;
    // where.createTimeEnd = d2 ? d2 + ' 00:00:00' : undefined;
    emit('search', {
      ...where
    });
  };

  const chooseOrganization = (e: Organization) => {
    // where.organizationId = e.organizationId;
    // where.organizationName = e.organizationName;
    search();
  };

  const chooseGradeId = (e: Grade) => {
    where.gradeId = e.gradeId;
    where.gradeName = e.name;
    search();
  };

  /*  重置 */
  const reset = () => {
    dateRange.value = ['', ''];
    resetFields();
    post.value = 0;
    sign.value = 0;
    noSign.value = 0;
    search();
  };

  // 导出
  const handleExport = () => {
    if (!props.selection?.length) {
      message.error('请至少选择一条数据');
      return;
    }
    const array: (string | number)[][] = [
      [
        '部门',
        '编号',
        '姓名',
        '早餐报餐次数',
        '早餐签到次数',
        '午餐报餐次数',
        '午餐签到次数',
        '晚餐报餐次数',
        '晚餐签到次数',
        '消费金额'
      ]
    ];
    props.selection?.forEach((d: OrderGoods) => {
      array.push([
        `${d.organizationName}`,
        `${d.userId}`,
        `${d.nickname}`,
        `${d.breakfastPostUsers}`,
        `${d.breakfastSignUsers}`,
        `${d.lunchPostUsers}`,
        `${d.lunchSignUsers}`,
        `${d.dinnerPostUsers}`,
        `${d.dinnerSignUsers}`
      ]);
    });
    const sheetName = '报餐统计导出';
    const workbook = {
      SheetNames: [sheetName],
      Sheets: {}
    };
    const sheet = utils.aoa_to_sheet(array);
    workbook.Sheets[sheetName] = sheet;
    // 设置列宽
    sheet['!cols'] = [
      { wch: 10 },
      { wch: 10 },
      { wch: 10 },
      { wch: 10 },
      { wch: 20 },
      { wch: 40 },
      { wch: 10 }
    ];
    writeFile(workbook, '报餐统计导出.xlsx');
  };

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