<template>
  <div class="editor-header">
    <div class="left">
      <Popover trigger="click" placement="bottom-start" v-model:value="mainMenuVisible">
        <template #content>
          <FileInput
            accept="application/vnd.openxmlformats-officedocument.presentationml.presentation"
            @change="
              files => {
                spinType = 'import'
                importPPTXFile(files)
                mainMenuVisible = false
              }
            "
          >
            <PopoverMenuItem>导入pptx</PopoverMenuItem>
          </FileInput>
          <PopoverMenuItem @click="handleExport">导出pptx</PopoverMenuItem>
          <PopoverMenuItem
            @click="
              () => {
                resetSlides()
                mainMenuVisible = false
              }
            "
            >重置幻灯片</PopoverMenuItem
          >
          <PopoverMenuItem
            @click="
              () => {
                mainMenuVisible = false
                hotkeyDrawerVisible = true
              }
            "
            >快捷操作</PopoverMenuItem
          >
        </template>
        <div class="menuCon" :class="{ menuVisible: mainMenuVisible }">
          <img class="icon" src="./imgs/file.png" />
        </div>
      </Popover>
      <div class="line"></div>
      <div class="title">
        <div class="title-text">{{ title }}</div>
      </div>
    </div>

    <div class="right">
      <div class="cancelBtn" @click="handleClose">退出</div>
      <div class="saveBtn" @click="handleSave">保存课件</div>
    </div>

    <Drawer :width="320" v-model:visible="hotkeyDrawerVisible" placement="right">
      <HotkeyDoc />
      <template v-slot:title>快捷操作</template>
    </Drawer>
    <FullscreenSpin :loading="fullscreenSpinData.loading" :progress="fullscreenSpinData.progress" :tip="fullscreenSpinData.tip" />
  </div>
</template>

<script lang="ts" setup>
import { storeToRefs } from "pinia"
import { useSlidesStore } from "@/store"
import useImport from "@/hooks/useImport"
import useExport from "@/hooks/useExport"
import useSlideHandler from "@/hooks/useSlideHandler"

import HotkeyDoc from "./HotkeyDoc.vue"
import FileInput from "@/components/FileInput.vue"
import FullscreenSpin from "@/components/FullscreenSpin.vue"
import Drawer from "@/components/Drawer.vue"
import Popover from "@/components/Popover.vue"
import PopoverMenuItem from "@/components/PopoverMenuItem.vue"
import { ref, computed } from "vue"
import { ElMessageBox } from "element-plus"
import usePptWork from "@/store/pptWork"

const slidesStore = useSlidesStore()
const { title } = storeToRefs(slidesStore)
const { resetSlides } = useSlideHandler()

const mainMenuVisible = ref(false)
const hotkeyDrawerVisible = ref(false)

const spinType = ref<"import" | "export">("import")
/* 导入 */
const { importPPTXFile, importing, importProgress } = useImport()
/* 导出 */
const { exportPPTX, exporting, exportProgress } = useExport()
function handleExport() {
  mainMenuVisible.value = false
  ElMessageBox.confirm("导出pptx可能会丢失一些内容，确认导出？", "提示", {
    confirmButtonText: "确认",
    cancelButtonText: "取消",
    type: "warning"
  })
    .then(() => {
      spinType.value = "export"
      exportPPTX()
    })
    .catch(() => {})
}
const fullscreenSpinData = computed(() => {
  if (spinType.value === "export") {
    return {
      loading: exporting.value,
      progress: exportProgress.value,
      tip: "正在导出中，请稍等…"
    }
  } else {
    return {
      loading: importing.value,
      progress: importProgress.value,
      tip: "正在导入中，请稍等…"
    }
  }
})

const usePptWorkHook = usePptWork()
/* 保存课件 */
function handleSave() {
  usePptWorkHook.updatePPT()
}
/* 关闭页面 */
function handleClose() {
  window.close()
  window.parent.postMessage(
    {
      api: "notation_exit",
    },
    "*"
  );
}
</script>

<style lang="scss" scoped>
.editor-header {
  background-color: #fff;
  user-select: none;
  border-bottom: 1px solid $borderColor;
  display: flex;
  justify-content: space-between;
  padding: 0 24px;
}
.left,
.right {
  display: flex;
  justify-content: center;
  align-items: center;
}
.menuCon {
  display: flex;
  justify-content: center;
  align-items: center;
  cursor: pointer;
  margin-right: 8px;
  width: 34px;
  height: 34px;
  border-radius: 6px;
  margin-left: -8px;
  &:hover,
  &.menuVisible {
    background: rgba(34, 71, 133, 0.08);
  }
  & > img {
    width: 20px;
    height: 20px;
  }
}
.line {
  width: 1px;
  height: calc(100% - 26px);
  background-color: $borderColor;
}
.title {
  margin-left: 16px;
  .title-text {
    font-weight: 600;
    font-size: 15px;
    color: #131415;
  }
}
.cancelBtn {
  width: 88px;
  height: 32px;
  background: #f1f2f6;
  border-radius: 6px;
  line-height: 32px;
  font-weight: 600;
  font-size: 14px;
  color: #1e2022;
  text-align: center;
  cursor: pointer;
  &:hover {
    opacity: 0.8;
  }
}
.saveBtn {
  margin-left: 16px;
  width: 88px;
  height: 32px;
  background: linear-gradient(312deg, #1b7af8 0%, #3cbbff 100%);
  border-radius: 6px;
  line-height: 32px;
  font-weight: 600;
  font-size: 14px;
  color: #ffffff;
  text-align: center;
  cursor: pointer;
  &:hover {
    opacity: 0.8;
  }
}
</style>
