<template>
  <Teleport to="body">
    <div v-if="src" class="modal a-show" aria-hidden="false">
      <div class="modal-backdrop" @click="close" />
      <div class="modal-dialog" role="dialog" aria-modal="true" tabindex="-1">
        <header class="modal-header">
          <h4 class="modal-title">Photo</h4>
          <button class="btn-icon modal-close" type="button" aria-label="Close" @click="close">✕</button>
        </header>
        <div class="modal-panel lightbox-panel">
          <div class="modal-body">
            <img
              v-if="!imgError"
              :src="src"
              class="lightbox-img"
              @error="imgError = true"
            />
            <div v-else class="lightbox-error">
              <i class="ph ph-image-broken"></i>
              <span>Failed to load image</span>
            </div>
          </div>
        </div>
      </div>
    </div>
  </Teleport>
</template>

<script setup>
import { ref, watch } from 'vue'
import { useLightbox } from '@/composables/useLightbox.js'

const { src, close } = useLightbox()
const imgError = ref(false)

watch(src, (val) => {
  document.body.style.overflow = val ? 'hidden' : ''
  imgError.value = false
})
</script>

<style>
.lightbox-panel {
  max-width: 90vw;
  min-height: unset;
  width: fit-content;
  cursor: default;
}

body .modal .lightbox-panel .modal-body {
  max-height: 90vh;
  padding: 0;
}

.lightbox-img {
  display: block;
  width: 100%;
  max-height: 90vh;
  object-fit: cover;
}

.lightbox-error {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 32px;
  color: #787C99;
  font-size: 13px;
}
.lightbox-error i {
  font-size: 32px;
}
</style>
