<script setup lang="ts">
import type { CourseType, ExperimentRecord } from '../types'
import { HomeFilled } from '@element-plus/icons-vue'
import { useGetCourseList } from '../composables/useGetCourseList'
import { upload } from '@/utils/upload'
import { getExperimentRecord, uploadExperimentPicture, submitExperimentRecord } from '../api'
import dayjs from 'dayjs'

const Book = defineAsyncComponent(() => import('../components/Book.vue'))
const Video = defineAsyncComponent(() => import('../components/Video.vue'))
const Discuss = defineAsyncComponent(() => import('../components/Discuss.vue'))
const Result = defineAsyncComponent(() => import('../components/Result.vue'))
const ReportDialog = defineAsyncComponent(() => import('../components/ReportDialog.vue'))

const route = useRoute()

// 左侧
const form = reactive<{ course_id: string; experiment_id: string }>({
  course_id: (route.query.course_id as string) || '',
  experiment_id: (route.query.experiment_id as string) || ''
})
// 课程列表
const { courses } = useGetCourseList()
// 实验列表
const experimentList = $computed(() => {
  const course = courses.value.find((item: CourseType) => item.id === form.course_id)
  return course?.experiments || []
})
let detail = $ref<ExperimentRecord>()
provide('detail', $$(detail))

function fetchInfo() {
  if (!form.experiment_id) return
  getExperimentRecord({ experiment_id: form.experiment_id }).then(res => {
    detail = Array.isArray(res.data.data) ? undefined : res.data.data
  })
}
watchEffect(() => {
  fetchInfo()
})

// 右侧
const LAB_URL = import.meta.env.VITE_LAB_URL
let iframeKey = $ref(Date.now())
// 返回首页
function handleBackHome() {
  iframeKey = Date.now()
}

const reportDialogVisible = $ref(false)
// 是否已经提交
const submitted = $computed(() => {
  return detail ? detail.status !== 0 : false
})
// 是否禁用
const disabled = $computed(() => {
  return submitted || !form.experiment_id
})
const iframeRef = $ref<HTMLIFrameElement>()

let screenshotLoading = $ref(false)
let screenshotTimestamp = $ref(0)
// 截图
function handleCapture() {
  const iframeWindow = iframeRef?.contentWindow
  if (!iframeWindow) return
  screenshotTimestamp = Date.now()
  screenshotLoading = true
  // 发送截图消息
  iframeWindow?.postMessage({ action: 'screenshot', timestamp: screenshotTimestamp }, '*')
}
// 截图之后
function handleCaptureCallback(event: MessageEvent) {
  const { data } = event
  if (data.action === 'screenshot' && data.timestamp === screenshotTimestamp) {
    upload(data.blob).then(url => {
      uploadPicture(url)
    })
  }
}
onMounted(() => {
  window.addEventListener('message', handleCaptureCallback, false)
})
onUnmounted(() => {
  window.removeEventListener('message', handleCaptureCallback, false)
})

// 上传截图
function uploadPicture(url: string) {
  const pictures = detail?.pictures || []
  pictures.unshift({ url, name: 'screenshot.png', upload_time: dayjs().format('YYYY-MM-DD HH:mm:ss') })
  uploadExperimentPicture({ experiment_id: form.experiment_id, pictures: JSON.stringify(pictures) }).then(() => {
    fetchInfo()
    screenshotLoading = false
  })
}
// 提交实验
function handleSubmit() {
  submitExperimentRecord({ experiment_id: form.experiment_id }).then(() => {
    fetchInfo()
  })
}
</script>

<template>
  <section class="lab">
    <div class="lab-left">
      <el-form :model="form" label-suffix=":" hide-required-asterisk>
        <el-form-item label="请选择课程">
          <el-select v-model="form.course_id" style="width: 100%">
            <el-option v-for="item in courses" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="请选择实验" prop="experiment_id">
          <el-select v-model="form.experiment_id" style="width: 100%">
            <el-option v-for="item in experimentList" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <el-tabs type="border-card" stretch>
        <el-tab-pane label="实训指导" lazy>
          <Book :experiment_id="form.experiment_id"></Book>
        </el-tab-pane>
        <el-tab-pane label="操作视频" lazy>
          <Video :experiment_id="form.experiment_id"></Video>
        </el-tab-pane>
        <el-tab-pane label="讨论交流" lazy>
          <Discuss :experiment_id="form.experiment_id"></Discuss>
        </el-tab-pane>
        <el-tab-pane label="过程与结果" lazy>
          <Result :experiment_id="form.experiment_id"></Result>
        </el-tab-pane>
      </el-tabs>
    </div>
    <div class="lab-right">
      <AppCard>
        <el-row justify="space-between">
          <el-button type="primary" :icon="HomeFilled" :disabled="submitted" @click="handleBackHome"
            >返回首页</el-button
          >
          <div>
            <el-button type="primary" :disabled="disabled" @click="handleSubmit">提交</el-button>
            <el-button type="primary" :disabled="disabled" @click="reportDialogVisible = true">上传报告</el-button>
            <el-button type="primary" :disabled="disabled" :loading="screenshotLoading" @click="handleCapture"
              >截图</el-button
            >
          </div>
        </el-row>
      </AppCard>
      <div class="lab-box">
        <el-empty description="您已经提交该实验，不能再进行操作，切换其他实验再做操作吧。" v-if="submitted" />
        <iframe :src="LAB_URL" :key="iframeKey" frameborder="0" class="iframe" ref="iframeRef" v-else></iframe>
      </div>
    </div>
  </section>
  <!-- 上传报告 -->
  <ReportDialog
    v-model="reportDialogVisible"
    :experiment_id="form.experiment_id"
    @update="fetchInfo"
    v-if="reportDialogVisible && form.experiment_id"
  ></ReportDialog>
</template>

<style lang="scss" scoped>
.lab {
  display: flex;
  height: calc(100vh - 110px);
}
.lab-left {
  display: flex;
  flex-direction: column;
  width: 400px;
  padding: 20px;
  background-color: #e1e4eb;
  border-radius: 6px;
  box-sizing: border-box;
  .el-tabs {
    flex: 1;
    border: 0;
    overflow: hidden;
  }
  :deep(.el-tabs__header) {
    background-color: #e1e4eb;
  }
  :deep(.el-tabs__item) {
    padding: 0 14px !important;
    border: 0;
    border-radius: 6px 6px 0px 0px;
  }
  :deep(.el-tabs__content) {
    height: calc(100% - 40px);
    box-sizing: border-box;
  }
  :deep(.el-tab-pane) {
    height: 100%;
    overflow-y: auto;
  }
}
.lab-right {
  margin-left: 20px;
  flex: 1;
  display: flex;
  flex-direction: column;
  height: 100%;
}
.lab-box {
  flex: 1;
  width: 100%;
  margin-top: 20px;
}
.iframe {
  width: 100%;
  height: 100%;
}
</style>
