<!-- 搜索表单 -->
<template>
  <div class="search">
    <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>
      <SelectOrganization
        :placeholder="`请选择部门`"
        v-model:value="where.organizationName"
        @done="chooseOrganization"
      />
      <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-radio-group v-model:value="where.deliveryStatus" @change="search">-->
      <!--        <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="where.gear" @change="search">-->
      <!--        <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="where.keywords"
        @pressEnter="search"
        @search="search"
        style="width: 220px"
      />
      <a-button @click="reset">重置</a-button>
      <!--      <span style="margin-left: 20px"-->
      <!--        >已签到 {{ signUsers }} / 已报餐 {{ postUsers }}</span-->
      <!--      >-->
      <a-button type="primary" class="ele-btn-icon" @click="handleExport">
        <template #icon>
          <download-outlined />
        </template>
        <span>导出</span>
      </a-button>
    </a-space>
    <!--    <a-spin :spinning="loading">-->
    <!--      <a-alert-->
    <!--        :message="`报餐统计：已报餐 ${post} / 已签到 ${sign} / 未签到 ${noSign}`"-->
    <!--        type="info"-->
    <!--        style="margin-top: 8px; max-width: 602px"-->
    <!--      />-->
    <!--    </a-spin>-->
  </div>
</template>

<script lang="ts" setup>
  import {
    PlusOutlined,
    DeleteOutlined,
    DownloadOutlined
  } from '@ant-design/icons-vue';
  import useSearch from '@/utils/use-search';
  import { ref, watch } from 'vue';
  import { countOrderGoods } from '@/api/apps/statistics';
  import { message } from 'ant-design-vue';
  import { utils, writeFile } from 'xlsx';
  import { Organization } from '@/api/system/organization/model';
  import { BcExportParam } from '@/api/apps/bc/export/model';
  import { RechargeOrder } from '@/api/user/recharge/export/model';

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

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

  // 表单数据
  const { where, resetFields } = useSearch<BcExportParam>({
    exportId: undefined,
    keywords: undefined,
    organizationName: '',
    organizationId: undefined,
    createTimeStart: undefined,
    createTimeEnd: undefined
  });
  // 下来选项
  // const categoryId = ref<number>(0);
  const post = ref<number>(0);
  const sign = ref<number>(0);
  const noSign = ref<number>(0);
  // 请求状态
  const loading = ref(true);
  // const deliveryStatus = ref<number>(0);
  // const gear = ref<number>(0);
  // 预定日期
  // const deliveryTime = ref<Dayjs>();
  // 日期范围选择
  const dateRange = ref<[string, string]>(['', '']);

  /* 搜索 */
  const search = () => {
    const [d1, d2] = dateRange.value ?? [];
    where.createTimeStart = d1 ? d1 + ' 00:00:00' : undefined;
    where.createTimeEnd = d2 ? d2 + ' 00:00:00' : undefined;
    emit('search', {
      ...where
    });
    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 chooseOrganization = (e: Organization) => {
    where.organizationName = e.organizationName;
    where.organizationId = e.organizationId;
    search();
  };

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

  const count = () => {
    if (where.deliveryTimeStart == undefined) {
      console.log('sss>>>');
      loading.value = false;
      return false;
    }
    loading.value = true;
    countOrderGoods(where)
      .then((data) => {
        console.log('data>>>', data);
        if (data) {
          post.value = data.post;
          sign.value = data.sign;
          noSign.value = data.noSign;
        } else {
          post.value = 0;
          sign.value = 0;
          noSign.value = 0;
        }
        loading.value = false;
      })
      .catch((err) => {
        message.error(err.message);
        loading.value = false;
      });
  };

  // 导出
  const handleExport = () => {
    if (!props.selection?.length) {
      emit('done');
      return;
    }
    const array: (string | number)[][] = [
      [
        '部门',
        '编号',
        '姓名',
        '充值金额',
        '充值时间',
        '添加人',
        '备注',
        '充值方式'
      ]
    ];
    props.selection?.forEach((d: RechargeOrder) => {
      array.push([
        `${d.organizationName}`,
        `${d.userId}`,
        `${d.nickname}`,
        `${d.payPrice}`,
        `${d.createTime}`,
        `财务充值`,
        `${d.comments}`,
        `管理员充值`
      ]);
    });
    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');
  };

  // 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,
    () => {}
  );

  count();
</script>
