<template>
  <bz-page-wrapper
    title="BzLoading 示例"
    content="可通过注册全局指令: setupGlobDirectives,局部指令: loadingDirective 通过v-loading 方式使用"
    v-loading="loadingRef"
  >
    <div ref="wrapEl">
      <a-alert message="组件方式" />
      <bz-button class="my-16 mr-16" type="primary" @click="openCompFullLoading">
        全屏 Loading
      </bz-button>
      <bz-button class="my-16" type="primary" @click="openCompAbsolute"> 容器内 Loading </bz-button>
      <bz-loading
        :loading="loading"
        :absolute="absolute"
        :theme="theme"
        :background="background"
        :tip="tip"
      />

      <a-alert message="函数方式" />

      <bz-button class="my-16 mr-16" type="primary" @click="openFnFullLoading">
        全屏 Loading
      </bz-button>
      <bz-button class="my-16" type="primary" @click="openFnWrapLoading">
        容器内 Loading
      </bz-button>

      <a-alert message="指令方式" />
      <bz-button class="my-16 mr-16" type="primary" @click="openDirectiveLoading">
        打开指令Loading
      </bz-button>
    </div>
  </bz-page-wrapper>
</template>

<script lang="ts">
  import { defineComponent, ref, toRefs, reactive } from 'vue';
  import { Alert } from 'ant-design-vue';
  import { loadingDirective, useLoading } from '/@/index';
  export default defineComponent({
    components: { [Alert.name]: Alert },
    directives: {
      loading: loadingDirective,
    },
    setup() {
      const wrapEl = ref<ElRef>(null);

      const loadingRef = ref(false);
      const compState = reactive({
        absolute: false,
        loading: false,
        theme: 'dark',
        background: 'rgba(111,111,111,.7)',
        tip: '加载中...',
      });
      const [openFullLoading, closeFullLoading] = useLoading({
        tip: '加载中...',
      });

      const [openWrapLoading, closeWrapLoading] = useLoading({
        target: wrapEl,
        props: {
          tip: '加载中...',
          absolute: true,
        },
      });

      function openLoading(absolute: boolean) {
        compState.absolute = absolute;
        compState.loading = true;
        setTimeout(() => {
          compState.loading = false;
        }, 2000);
      }

      function openCompFullLoading() {
        openLoading(false);
      }

      function openCompAbsolute() {
        openLoading(true);
      }

      function openFnFullLoading() {
        openFullLoading();

        setTimeout(() => {
          closeFullLoading();
        }, 2000);
      }

      function openFnWrapLoading() {
        openWrapLoading();

        setTimeout(() => {
          closeWrapLoading();
        }, 2000);
      }

      function openDirectiveLoading() {
        loadingRef.value = true;
        setTimeout(() => {
          loadingRef.value = false;
        }, 2000);
      }

      return {
        openCompFullLoading,
        openFnFullLoading,
        openFnWrapLoading,
        openCompAbsolute,
        wrapEl,
        loadingRef,
        openDirectiveLoading,
        ...toRefs(compState),
      };
    },
  });
</script>
