<script setup lang="ts">
import type { ExperimentRecord } from '../types'
import { ElMessageBox } from 'element-plus'
import ImageViewer from '@/components/ImageViewer.vue'
import { uploadExperimentPicture } from '../api'

interface Props {
  competition_id: string
}
const props = defineProps<Props>()

const emit = defineEmits<{
  (e: 'update'): void
}>()

const detail = $ref(inject('detail') as ExperimentRecord)

const isEmpty = $computed(() => {
  return !props.competition_id || !detail
})

let imageViewerVisible = $ref<boolean>(false)
let imageViewerIndex = $ref<number>(0)
// 查看
function handlePreview(index: number) {
  imageViewerVisible = true
  imageViewerIndex = index
}
// 删除
function handleRemove(index: number) {
  ElMessageBox.confirm('删除之后无法恢复，确认删除该截图吗？', '提示').then(() => {
    const pictures = detail.pictures.filter((item, i) => i !== index)
    uploadExperimentPicture({ competition_id: props.competition_id, pictures: JSON.stringify(pictures) }).then(() => {
      emit('update')
    })
  })
}
</script>

<template>
  <el-empty description="暂无数据" v-if="isEmpty" />
  <template v-else>
    <h3>实验过程</h3>
    <ul class="picture-list">
      <li v-for="(item, index) in detail.pictures" :key="item.url">
        <img :src="item.url" />
        <p>截图时间：{{ item.upload_time }}</p>
        <div class="cover">
          <div class="cover-inner">
            <el-button type="primary" plain round @click="handlePreview(index)">查看</el-button>
            <el-button type="primary" plain round @click="handleRemove(index)">删除</el-button>
          </div>
        </div>
      </li>
    </ul>
    <ImageViewer v-model="imageViewerVisible" :index="imageViewerIndex" :items="detail.pictures"></ImageViewer>
  </template>
</template>

<style lang="scss" scoped>
.result-score {
  margin-bottom: 40px;
  text-align: center;
  h2 {
    font-size: 18px;
    font-weight: 400;
    line-height: 30px;
    color: #333333;
    text-align: center;
  }
  .t1 {
    margin: 20px auto 10px;
    padding-top: 64px;
    width: 80px;
    height: 110px;
    font-size: 24px;
    font-weight: bold;
    line-height: 1;
    color: #ffffff;
    background: url(@/assets/images/score_bg.png) no-repeat;
    background-size: contain;
    box-sizing: border-box;
  }
  .t2 {
    font-size: 14px;
    line-height: 24px;
    color: #666666;
  }
}
h3 {
  font-size: 16px;
  font-weight: 400;
  line-height: 27px;
  color: #333333;
}
.picture-list {
  li {
    position: relative;
    height: 200px;
    margin: 20px 0;
    img {
      width: 100%;
      height: 100%;
      object-fit: cover;
    }
    p {
      position: absolute;
      left: 0;
      right: 0;
      bottom: 0;
      padding: 0 10px;
      font-size: 14px;
      color: #fff;
      line-height: 30px;
      text-align: right;
      background-color: rgba(0, 0, 0, 0.5);
    }
    &:hover {
      .cover {
        display: block;
      }
    }
  }
  .cover {
    display: none;
    position: absolute;
    left: 0;
    right: 0;
    top: 0;
    bottom: 0;
    background-color: rgba(0, 0, 0, 0.5);
  }
  .cover-inner {
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    .el-button {
      width: 100px;
      margin: 10px 0;
    }
  }
}
</style>
