<template>
  <div id="outerContainer" class="app" :class="{ dark: themeMode === 'Dark' }">
    <Header />
    <div class="content">
      <LeftPanel />
      <DocumentContainer />
      <CompareDocumentContainer />
      <DocumentEditorContainer v-if="toolMode === 'document'" :Sortable="Sortable" />
      <RightPanel />
      <RightPanelPageMode />
      <StampPanel />
      <LinkPanel />
      <ContentEditorPanel />
    </div>
    <div class="mask" :class="{ active: downloading || downloadError !== '' }">
      <div v-show="downloading" class="loading"><Loading /></div>
      <div v-show="downloadError !== ''" class="error-dialog">
        <div class="close-btn">
          <Button @click="closeMask">
            <Close />
          </Button>
        </div>
        <div class="fail-logo"><Failed /></div>
        <div class="text">{{ downloadError }} failed</div>
        <div class="confirm-btn" @click="closeMask">OK</div>
      </div>
    </div>
    <div class="mask active" v-show="activeSignCreatePanel"><SignCreatePanel /></div>
  </div>
  <div id="printContainer"></div>
  <Message />
</template>
<script setup>
  import initDocument from '@/helpers/initDocument'
  import { ref, computed, provide, getCurrentInstance } from 'vue'
  import { useViewerStore } from '@/stores/modules/viewer'
  import { Sortable, MultiDrag } from 'sortablejs'
  import core from '@/core'
  
  Sortable.mount(new MultiDrag())

  const useViewer = useViewerStore()
  const themeMode = computed(() => useViewer.getThemeMode)
  const downloading = computed(() => useViewer.getDownloading)
  const downloadError = computed(() => useViewer.getDownloadError)
  const activeSignCreatePanel = computed(() => useViewer.isElementOpen('signCreatePanel'))
  const popoverChanged = computed(() => useViewer.getPopoverChanged)
  const toolMode = computed(() => useViewer.getToolMode)

  const instance = getCurrentInstance().appContext.app.config.globalProperties

  provide('themeMode', themeMode)
  initDocument({ $t: instance.$t })
  window.instance.changeFile = () => {
    const openFileInput = document.getElementById("fileInput")
    openFileInput.click()

    if (activeSignCreatePanel.value) {
      useViewer.toggleElement('signCreatePanel')
      useViewer.setActiceToolMode('view')
    }
    if (!popoverChanged.value) useViewer.setPopoverChanged(true)
    if (toolMode.value === 'document') core.saveDocumentEdit('cancel')
  }

  const closeMask = () => {
    useViewer.setDownloadError('')
  }
</script>

<style lang="scss">
  .dark {
    .loading {
      color: #FFF;
    }
    .form-container span {
      color: #FFF;
    }
    .findbar {
      background-color: #343A40;
    }
    .thumbnail .box-thumbnail {
      color: #FFF;
    }
    .print-dialog .print-container {
      background-color: #000;
      color: #FFF;
    }
    .outline-view, .layer-view {
      .treeItem {
        .title {
          &:hover {
            background-color: rgba(255, 255, 255, 0.1);
          }
          &.selected {
            background-color: rgba(255, 255, 255, 0.2);
          }
          .treeItemToggler {
            color: #FFF;
          }
        }
        .title a, > a {
          color: #FFF;
        }
        &:hover > a {
          background-color: rgba(255, 255, 255, 0.1);
        }
        &.selected > a {
          background-color: rgba(255, 255, 255, 0.2);
        }
      }
    }
    &#outerContainer .drop-down {
      background-color: #343A40;
      color: #FFF;
    }
    &#outerContainer .drop-item {
      &:hover {
        background-color: rgba(255, 255, 255, 0.1);
      }
      &.active {
        background-color: rgba(255, 255, 255, 0.2);
      }
    }
    .measure-pop {
      background-color: #000;
      div {
        color: #FFF;
      }
    }
  }
 .app {
    display: flex;
    flex-direction: column;
    position: relative;
    width: 100%;
    height: 100%;
    margin: 0;
    padding: 0;
    -webkit-font-smoothing: antialiased;
    overflow: hidden;
  }
  .content {
    display: flex;
    flex-direction: row;
    flex: 1;
    overflow: auto;
  }
  .divider {
    width: 1px;
    height: 20px;
    margin-right: 8px;
    background-color: var(--c-divider);
    flex-shrink: 0;
  }
  .mask {
    display: none;
    position: fixed;
    z-index: 999;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    justify-content: center;
    align-items: center;
    background-color: rgba(0, 0, 0, 0.3);
    text-align: center;
    .loading svg {
      animation: spin .8s linear infinite;
    }
    .close-btn .button {
      margin-left: auto;
      margin-right: 0;
    }
    &.active {
      display: flex;
    }
    .error-dialog {
      width: 100%;
      max-width: 284px;
      color: #232A40;
      padding: 16px 16px 40px;
      background: #FFFFFF;
      box-shadow: 0px 4px 32px rgba(129, 149, 200, 0.32);
      border-radius: 4px;
      .fail-logo svg {
        width: 60px;
        height: 60px;
        margin-top: 12px;
        margin-bottom: 16px;
      }
      .text {
        font-size: 16px;
        line-height: 20px;
        color: #232A40;
      }
      .confirm-btn {
        width: 100%;
        max-width: 220px;
        margin: 30px auto 0;
        padding: 12px 0;
        color: #FFFFFF;
        background-color: #1460F3;
        border-radius: 6px;
        display: flex;
        align-items: center;
        font-weight: 700;
        font-size: 14px;
        line-height: 16px;
        justify-content: center;
      }
    }
  }
</style>
