import { defineComponent, onMounted, onUnmounted, reactive, ref } from 'vue';
import { useUserStore } from '/src/store/modules/users';
import styles from './index.module.less';
import { state } from '/src/state';
import { NButton, NModal, NSpace, NSpin } from 'naive-ui';
import { exitFullscreen } from '/src/utils';
import { useRouter } from 'vue-router';
export default defineComponent({
  name: 'notation-a',
  setup() {
    const router = useRouter();
    const show = ref(false);
    const previewModal = ref(false);
    const previewParams = ref({} as any);
    const removeVisiable = ref(false);
    const userStore = useUserStore();
    const Authorization = userStore.getToken || '';
    const iframeRef = ref();
    // console.log(Authorization);
    const notationOpenCreate = sessionStorage.getItem('notation-open-create');
    const openCreateUrl = notationOpenCreate == '1' ? '&addShow=1' : '';
    sessionStorage.removeItem('notation-open-create');
    const data = reactive({
      src: `${
        /(192|localhost)/.test(location.origin)
          ? // ?
            // 'https://test.lexiaoya.cn'
            'http://localhost:3050'
          : location.origin
      }/notation/?t=${Date.now()}#/create?v=${Date.now()}&Authorization=${Authorization}${openCreateUrl}`
      //   src: `http://localhost:3050/#/create?Authorization=${Authorization}`
    });
    const fscreen = () => {
      const el = document.documentElement as any;
      //进入全屏
      (el.requestFullscreen && el.requestFullscreen()) ||
        (el.mozRequestFullScreen && el.mozRequestFullScreen()) ||
        (el.webkitRequestFullscreen && el.webkitRequestFullscreen()) ||
        (el.msRequestFullscreen && el.msRequestFullscreen());
    };
    const handleOpen = (e: MessageEvent) => {
      console.log(e.data, 'data');
      if (e.data.api === 'notation_open') {
        if (state.application) {
          show.value = true;
          previewModal.value = true;
          previewParams.value = {
            url: e.data.url
          };
          fscreen();
        } else {
          window.open(e.data.url);
        }
      } else if (e.data.api === 'notation_exit') {
        console.log('进来');

        removeVisiable.value = true;
      }
    };
    onMounted(() => {
      window.addEventListener('message', handleOpen);
    });
    onUnmounted(() => {
      window.removeEventListener('message', handleOpen);
    });
    return () => (
      <div class={styles.wrap}>
        <iframe ref={iframeRef} src={data.src} key={Date.now()}></iframe>

        <NModal
          v-model:show={previewModal.value}
          class={styles.previewWindow}
          showIcon={false}
          displayDirective="show">
          <NSpin show={show.value} style="--n-opacity-spinning: 1;">
            <iframe
              class={styles.previewIframe}
              onLoad={() => {
                show.value = false;
              }}
              frameborder="0"
              src={previewParams.value.url}></iframe>
          </NSpin>
        </NModal>

        <NModal
          v-model:show={removeVisiable.value}
          preset="card"
          class={['modalTitle', styles.removeVisiable]}
          title={'退出制谱'}>
          <div class={styles.studentRemove}>
            <p>是否退出制谱功能？</p>

            <NSpace class={styles.btnGroupModal} justify="center">
              <NButton
                round
                type="primary"
                onClick={() => {
                  previewModal.value = false;
                  previewParams.value.url = '';
                  removeVisiable.value = false;
                  // 给制谱发消息
                  iframeRef.value.contentWindow.postMessage(
                    {
                      api: 'reload'
                    },
                    '*'
                  );
                  if (state.application) {
                    document.exitFullscreen
                      ? document.exitFullscreen()
                      : document.mozCancelFullScreen
                      ? document.mozCancelFullScreen()
                      : document.webkitExitFullscreen
                      ? document.webkitExitFullscreen()
                      : '';
                  }
                }}>
                确定
              </NButton>
              <NButton round onClick={() => (removeVisiable.value = false)}>
                取消
              </NButton>
            </NSpace>
          </div>
        </NModal>
      </div>
    );
  }
});
