<template>
  <div class="table-actions">
    <template v-if="bimDone">
      <a @click="updateBimName">修改</a>
      <a @click="syncScene"> {{ store.current!.panos ? '修改' : '' }}同步 </a>
    </template>
    <template v-if="done">
      <a @click="queryScene">查看</a>
      <!-- <a>分享</a> -->
    </template>
    <a v-if="lastStep" class="warn" @click="deleteScene"> 删除 </a>
  </div>
</template>

<script setup lang="ts">
import { useProject, BinType, BimStatus, SceneStatus } from '@/store'
import { Modal } from 'ant-design-vue'
import { computed } from 'vue'
import { renderModal } from '@/helper'
import { projectManage } from '@/env'
import UploadBim from './update.vue'
import type { ProjectScene } from '@/store'

const props = defineProps<{ scene: ProjectScene }>()
const store = useProject()
const bimDone = computed(
  () => props.scene.type === BinType && props.scene.status === BimStatus.done
)
const sceneDone = computed(
  () =>
    props.scene.type !== BinType && props.scene.status === SceneStatus.SUCCESS
)
const done = computed(() => sceneDone.value || bimDone.value)
const lastStep = computed(
  () =>
    props.scene.type !== BinType ||
    [BimStatus.done, BimStatus.error].includes(props.scene.status)
)

const deleteScene = () => {
  Modal.confirm({
    content: '确定要删除此场景？',
    title: '系统提示',
    width: '400px',
    okText: '删除',
    icon: null,
    cancelText: '取消',
    onOk: async () => {
      if (props.scene.type === BinType) {
        await store.deleteBim()
      } else {
        await store.deleteScene(props.scene.num)
      }
    }
  })
}

const queryScene = () => {
  const base = `${projectManage}?projectId=${store.current!.projectId}`
  if ('num' in props.scene) {
    window.open(`${base}&m=${props.scene.num}`)
  } else {
    window.open(`${base}&bim`)
  }
}

const syncScene = () => {
  window.open(`${projectManage}?projectId=${store.current!.projectId}&adjust`)
}

const updateBimName = () => {
  const bim = store.current!.bimData!
  renderModal(UploadBim, {
    bim: {
      bimName: bim.bimName,
      bimPath: bim.bimOssFilePath || bim.bimLocalFilePath
    },
    async onSave(data) {
      await store.updateBimName(data.bimName)
    }
  })
}
</script>
