<!-- 搜索表单 -->
<template>
  <a-space :size="10" style="flex-wrap: wrap">
    <SelectMerchantDown
      v-if="!getMerchantId()"
      :placeholder="`选择场馆`"
      class="input-item"
      v-model:value="where.merchantId"
      @change="search"
    />
    <a-date-picker
      placeholder="按天筛选"
      value-format="YYYY-MM-DD"
      v-model:value="where.dateTime"
      @change="search"
    />
    <a-button @click="reset">刷新</a-button>
    <!--    <a-radio-group v-model:value="where.keywords" @change="search">-->
    <!--      <template v-for="(item, index) in next7day" :key="index">-->
    <!--        <a-radio-button value="0"-->
    <!--          >{{ item.date }} {{ getWeek(item.week) }}</a-radio-button-->
    <!--        >-->
    <!--      </template>-->
    <!--    </a-radio-group>-->
  </a-space>
  <ele-modal
    :width="500"
    :visible="showQrcode"
    :maskClosable="false"
    title="使用微信扫一扫完成支付"
    :body-style="{ paddingBottom: '28px' }"
    @cancel="closeQrcode"
    @ok="closeQrcode"
  >
    <div class="qrcode">
      <ele-qr-code-svg v-if="text" :value="text" :size="200" />
      <div class="ele-text-secondary">使用微信扫一扫完成支付</div>
    </div>
  </ele-modal>
</template>

<script lang="ts" setup>
  import { ref, watch } from 'vue';
  import useSearch from '@/utils/use-search';
  import { OrderParam } from '@/api/shop/order/model';
  import { getNativeCode } from '@/api/system/payment';
  import { getNext7day, getServerTime } from '@/api/layout';
  import { getMerchantId } from '@/utils/common';

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

  interface serverTime {
    today?: string;
  }

  interface dateItem {
    date?: string;
    week?: string;
  }

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

  // 表单数据
  const { where, resetFields } = useSearch<OrderParam>({
    dateTime: '',
    isStatus: 0,
    merchantCode: undefined,
    week: 0,
    merchantId: getMerchantId()
  });

  /* 搜索 */
  const search = () => {
    emit('search', where);
  };

  /*  重置 */
  const reset = () => {
    emit('search', where);
  };

  // 二维码内容
  const text = ref('');
  const showQrcode = ref(false);
  const next7day = ref<dateItem[]>([]);
  const serverTime = ref<serverTime>({});

  const closeQrcode = () => {
    showQrcode.value = !showQrcode.value;
  };

  const getCode = () => {
    getNativeCode({}).then((data) => {
      text.value = String(data);
      showQrcode.value = true;
    });
  };

  const reload = () => {
    getNext7day().then((res) => {
      next7day.value = res;
      where.week = res[0].week;
    });
    getServerTime().then((res) => {
      serverTime.value = res;
      where.dateTime = res.today;
      where.merchantId = getMerchantId();
      // where.merchantCode = '37';
      where.isStatus = 0;
      emit('search', where);
    });
  };

  reload();

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

<style lang="less" scoped>
  .qrcode {
    display: flex;
    justify-content: center;
    flex-direction: column;
    align-items: center;
    padding: 40px 0;
  }
</style>
