<!-- 搜索表单 -->
<template>
  <div class="search">
    <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>-->
      <!--          <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-input-search
        allow-clear
        placeholder="请输入手机号|用户ID"
        v-model:value="where.keywords"
        @pressEnter="search"
        @search="search"
        style="width: 220px"
      />
      <a-button @click="reset">重置</a-button>
      <a-button type="primary" class="ele-btn-icon" @click="handleExport">
        <template #icon>
          <download-outlined />
        </template>
        <span>导出</span>
      </a-button>
    </a-space>
  </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;
    (e: 'export'): 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 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 = () => {
    emit('done');
  };

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

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

  count();
</script>
