<template>
  <a-drawer :title="state.info.file_name" placement="right" :closable="true" v-model:visible="visible" :mask="true"
    getContainer="#g-container" :wrap-style="{ position: 'absolute' }" wrapClassName="drawer-element-wrapper"
    @close="closeDrawer" width="600">
    <div class="info">
      <div>
        <ClockCircleOutlined /> {{ state.info.picture_time }}
      </div>
      <div>
        {{ state.info.size > 0 ? (state.info.size / 1024 / 1024).toFixed(1) + 'M' : '--' }}
      </div>
      <div>
        <div v-if="state.info.image_width && state.info.image_height">
          {{ state.info.image_width }}*{{ state.info.image_height }}
        </div>
        <div v-else>
          --
        </div>
      </div>
      <div>
        <span style="margin-right: 10px;">
          {{ state.info.latitude }}° N
        </span>
        <span>
          {{ state.info.longitude }}° E
        </span>
      </div>
    </div>
    <div class="image">
      <div class="image-background" v-if="state.imgLoading">
        <a-spin tip="加载中..." />
      </div>
      <Panoramic :src="state.info.url" v-else-if="state.info.media_type === 3" />
      <a-image width="100%" height="100%" style="object-fit: cover;" :src="state.info.url" :preview="false" v-else />
    </div>
    <div class="previewList">
      <div class="previewList-item">
        <img :src="state.info.thumbnail_url" />
      </div>
    </div>
  </a-drawer>
</template>

<script lang="ts" setup>
import { reactive, watch } from 'vue';
import { ClockCircleOutlined } from '@ant-design/icons-vue';
import Panoramic from '/@/components/panoramic/index.vue';
import { getWorkspaceId } from '/@/utils';
import { apis } from '/@/api/custom';

interface Props {
  visible: boolean,
  fileId: string,
  closeDrawer: () => void,
};

const props = withDefaults(defineProps<Props>(), {

});

const state = reactive({
  imgLoading: false,// 图片加载
  info: {
    url: '',
    thumbnail_url: '',// 缩略图
    media_type: 0,
    file_name: '',
    image_width: '',// 照片分辨率-宽度
    image_height: '',// 照片分辨率-高度
    size: 0,
    picture_time: '',// 拍摄时间
    longitude: '',// 经度
    latitude: '',// 纬度
  },
})

watch(() => props.fileId, async (newValue) => {
  if (props.visible && newValue) {
    const fileId = newValue.replace(/^resource__/, '');
    try {
      const res = await apis.fetchFileDetail(getWorkspaceId(), fileId);
      state.info = res.data;
      state.imgLoading = true;
      const img = new Image();
      img.src = state.info.url;
      img.onload = () => {
        state.imgLoading = false;
      };
    } catch (e) {
      console.error(e);
    }
  }
});
</script>

<style lang="scss" scoped>
.info {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
}

.image {
  width: 100%;
  height: 500px;

  &-background {
    width: 100%;
    height: 100%;
    padding: 100px;
    display: flex;
    justify-content: center;
    align-items: center;
  }
}

.previewList {
  width: 100%;
  height: 70px;
  display: flex;
  justify-content: center;
  margin-top: 24px;

  &-item {
    width: 80px;
    height: 60px;
    border: 2px solid $primary;
    border-radius: 4px;
    margin-bottom: 10px;
    cursor: pointer;

    img {
      width: 100%;
      height: 100%;
      object-fit: cover;
    }
  }
}
</style>