<template>
  <el-dropdown v-pdpath="'edit'">
    <span class="oper-span">
      编辑
      <el-icon class="el-icon--right">
        <arrow-down />
      </el-icon>
    </span>
    <template #dropdown>
      <el-dropdown-menu>
        <!-- :disabled="!operateIsPermissionByPath('edit', menu.key)" -->
        <el-dropdown-item v-for="menu in menus" :key="menu.key" @click="menu.onClick()">
          {{ menu.label }}
        </el-dropdown-item>
      </el-dropdown-menu>
    </template>
  </el-dropdown>
</template>

<script setup lang="ts">
import { computed } from "vue";
import { getFuseCodeLink, checkScenesOpen, MenuItem, getSWKKSyncLink } from "./help";
import { showCaseScenes, addCaseScenes, shareCase, downloadCase } from "./quisk";
import { RouteName, router } from "@/router";
import { copyCase, getCaseSceneList } from "@/store/case";
import { alert } from "@/helper/message";

const props = defineProps<{
  caseId: number;
  title: string;
  prevMenu?: MenuItem[];
  lastMenu?: MenuItem[];
}>();
const menus = computed(() => {
  if (!props.caseId) {
    return [];
  }
  const caseId = props.caseId;
  const fuseLink = getFuseCodeLink(caseId);

  return [
    ...(props.prevMenu || []).map((item) => ({
      ...item,
      onClick: () => item.onClick(caseId),
    })),
    {
      key: "scene",
      disabled: true,
      label: "场景管理",
      onClick: () =>
        showCaseScenes({
          caseId,
          onAddScenes: () => addCaseScenes({ caseId }),
        }),
    },
    {
      key: "fuse",
      label: "多元融合",
      onClick: () => checkScenesOpen(caseId, `${fuseLink}#fuseEdit/merge`),
    },
    {
      key: "view",
      label: "视图提取",
      onClick: () => checkScenesOpen(caseId, `${fuseLink}#sceneEdit/view`),
    },
    {
      key: "record",
      label: "屏幕录制",
      onClick: () => checkScenesOpen(caseId, `${fuseLink}#sceneEdit/record`),
    },
    {
      key: "file",
      label: "卷宗管理",
      onClick: () => {
        window.open(
          router.resolve({ name: RouteName.caseFile, params: { caseId: caseId } }).href
        );
      },
    },
    {
      key: "sync",
      label: "带看",
      onClick: async () => {
        const href = await getSWKKSyncLink(caseId);
        window.open(href);
      },
    },
    {
      key: "share",
      label: "分享",
      onClick: async () => {
        const scenes = await getCaseSceneList(caseId);
        if (!scenes.length) {
          alert("当前案件下无场景，请先添加场景。");
        } else {
          shareCase({ caseId: caseId });
        }
      },
    },
    {
      key: "fj",
      label: "分镜配置",
      onClick: async () => {
        window.open(`mirror.html?caseId=${caseId}`);
      },
    },
    {
      key: "download",
      label: "下载",
      onClick: async () => {
        const scenes = await getCaseSceneList(caseId);
        if (!scenes.length) {
          alert("当前案件下无场景，请先添加场景。");
        } else {
          downloadCase({ caseId, title: props.title });
        }
      },
    },
    ...(props.lastMenu || []).map((item) => ({
      ...item,
      onClick: () => item.onClick(caseId),
    })),
  ];
});
</script>
