<template>
  <el-dialog
    :showClose="getProps.showClose"
    :width="getProps.width"
    :draggable="getProps.draggable"
    :fullscreen="getFullscreen"
    v-bind="attrs"
    v-model="isModal"
    @close="onCloseModal"
  >
    <template #header>
      <div class="dialog-header">
        <div class="dialog-header-title">{{ getProps.title }}</div>
        <div class="dialog-header-action">
          <el-tooltip content="还原" placement="bottom" v-if="fullscreen">
            <el-link :underline="false" class="mr-4"
              ><el-icon class="el-input__icon" :size="18">
                <FullscreenExitOutlined role="full" @click="handleFullScreen" /> </el-icon
            ></el-link>
            >
          </el-tooltip>
          <el-tooltip v-else content="最大化" placement="bottom">
            <el-link :underline="false" class="mr-4"
              ><el-icon class="el-input__icon" :size="18"
                ><FullscreenOutlined role="close" @click="handleFullScreen" /></el-icon
            ></el-link>
          </el-tooltip>
          <el-tooltip content="关闭" placement="bottom">
            <el-link :underline="false" @click="onCloseModal"
              ><el-icon class="el-input__icon" :size="18"><CloseOutlined /></el-icon
            ></el-link>
          </el-tooltip>
        </div>
      </div>
    </template>
    <template #default>
      <slot name="default"></slot>
    </template>
    <template v-if="!getProps.hideFooter && !$slots.footer" #footer>
      <el-space>
        <slot name="prefixFooter"></slot>
        <el-button @click="closeModal" v-bind="getProps.cancelButProps">取消</el-button>
        <slot name="centerFooter"></slot>
        <el-button
          v-bind="getProps.confirmButProps"
          :loading="subLoading"
          type="primary"
          @click="handleSubmit"
          >{{ getProps.confirmButText }}
        </el-button>
        <slot name="suffixFooter"></slot>
      </el-space>
    </template>
    <template v-else #footer>
      <slot name="action"></slot>
    </template>
  </el-dialog>
</template>
<script lang="ts" setup>
  import { getCurrentInstance, ref, unref, computed, useAttrs, provide } from 'vue';
  import { basicProps } from './props';
  import { deepMerge } from '@/utils';
  import { ModalProps, ModalMethods } from './type';
  import { FullscreenExitOutlined, FullscreenOutlined, CloseOutlined } from '@vicons/antd';

  const attrs = useAttrs();
  const props = defineProps({ ...basicProps });

  const emit = defineEmits(['close', 'ok', 'register']);

  const propsRef = ref(null);

  const isModal = ref(false);
  const subLoading = ref(false);
  const fullscreen = ref(false);

  const getProps = computed(() => {
    return { ...props, ...(unref(propsRef) as any) };
  });

  async function setProps(modalProps: Partial<ModalProps>): Promise<void> {
    propsRef.value = deepMerge(unref(propsRef) || ({} as any), modalProps);
  }

  const getFullscreen = computed(() => {
    return fullscreen.value;
  });

  function setSubLoading(status: boolean) {
    subLoading.value = status;
  }

  function openModal() {
    isModal.value = true;
  }

  //切换全屏
  function handleFullScreen() {
    fullscreen.value = !fullscreen.value;
  }

  function closeModal() {
    isModal.value = false;
    subLoading.value = false;
    emit('close');
  }

  function onCloseModal() {
    isModal.value = false;
    emit('close');
  }

  function handleSubmit() {
    subLoading.value = true;
    emit('ok');
  }

  const modalMethods: ModalMethods = {
    setProps,
    openModal,
    closeModal,
    setSubLoading,
  };

  const instance = getCurrentInstance();
  if (instance) {
    emit('register', modalMethods);
  }

  provide('handleSubmit', handleSubmit);
  provide('closeModal', closeModal);
  provide('subLoading', subLoading.value);

  defineExpose({
    openModal,
    closeModal,
    setProps,
    setSubLoading,
    handleSubmit,
  });
</script>

<style lang="scss" scoped>
  .dialog-header {
    width: 100%;
    display: flex;
    justify-content: space-around;
    &-title {
      flex: 1;
      font-weight: 400;
      font-size: 16px;
    }
    &-action {
      color: var(--el-color-info);
    }
  }
</style>
