<template>
  <div class="web-detail-container">

    <div class="content-container">
      <div class="left-panel">
        <DynamicDetail v-model="detailData" :config-list="detailConfig" />
      </div>
      <!-- <div class="right-panel" v-if="isEdit">
        <ContinueReport ref="continueReport" @refresh="getDisasterDetail" />
      </div> -->
    </div>
  </div>
</template>

<script setup>
import { onMounted, ref, computed } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { ElMessage } from 'element-plus'
import { ArrowLeft, Picture, VideoCamera } from '@element-plus/icons-vue'
import ContinueReport from './IceDisasterContinueReportPC.vue'
import { request } from '@shared/utils/request'
import FileUpload from '@/component/FileUpload/FileUpload.vue'
import detailConfig from './detailConfig'
import DynamicDetail from '@/component/DynamicDetail/DynamicDetail.vue'

const router = useRouter()
const route = useRoute()

// 详情数据
const detailData = ref({
  event: null,
  report: [],
  fileList: [],
  lossList: [],
  occurLocation: '',
  occurTime: '',
  roadConditionType: '',
  routeNo: ''
})

// 事件状态
const eventStatus = ref(0)

// 是否为编辑
const isEdit = computed(() => {
  return route.query.mode === 'edit'
})

const continueReport = ref(null)

// 所有填报记录（首报 + 续报）
const allReports = computed(() => {
  const reports =
    detailData.value.report?.map((item, index) => {
      if (index === detailData.value.report.length - 1) {
        item.title = '首报'
      } else {
        item.title = '续报' + (detailData.value.report.length - 1 - index)
      }
      return item
    }) || []
  return reports
  // return reports.reverse()
})

// 是否有填报数据
const hasReportData = computed(() => {
  return allReports.value.length > 0
})


// 获取灾毁详情
const getDisasterDetail = async () => {
  const id = route.query.id
  if (!id) {
    ElMessage.warning('缺少灾毁ID')
    return
  }

  try {
    const result = await request({
      url: `/snow-ops-platform/event/getById?id=${route.query.id}`,
      method: 'get',
    })

    if (result?.data) {
      const data = result.data
      console.log('🚀 ~ getDisasterDetail ~ data:', data)
      detailData.value = {
        event: data.event || null,
        report: data.report || [],
        fileList: data.fileList || [],
        lossList: data.lossList || [],
        occurLocation: data.occurLocation || '',
        occurTime: data.occurTime || '',
        roadConditionType: data.roadConditionType || '',
        routeNo: data.routeNo || ''
      }
      eventStatus.value = data.eventStatus || 0

      if (isEdit.value) {
        const newFormData = {
          ...data,
          lossList: null,
          report: {},
          fileList: null
        }
        continueReport.value?.initFormData(newFormData)
      }
    } else {
      ElMessage.warning(result.message || '获取详情失败')
    }
  } catch (error) {
    console.error('获取灾毁详情失败:', error)
    ElMessage.error('获取详情失败，请稍后重试')
  }
}

// 点击返回
const handleClickBack = () => {
  router.push('/disasterManagement')
}

onMounted(() => {
  getDisasterDetail()
})
</script>

<style scoped lang="scss">
.web-detail-container {
  padding: 20px;
  background: #f5f7fa;
  height: 100%;
  overflow: auto;
  padding-bottom: 100px;
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
  padding: 0 20px;

  .header-left {
    display: flex;
    align-items: center;
    gap: 16px;

    .page-title {
      margin: 0;
      font-size: 20px;
      font-weight: 600;
      color: #303133;
    }
  }
}

.info-card {
  margin-bottom: 20px;
  border-radius: 8px;

  :deep(.el-card__header) {
    padding: 16px 20px;
    border-bottom: 1px solid #ebeef5;
    background: #fafafa;
  }

  .card-header {
    .card-title {
      font-size: 16px;
      font-weight: 600;
      color: #303133;
    }
  }
}

.info-row {
  margin-bottom: 16px;

  &:last-child {
    margin-bottom: 0;
  }
}

.info-item {
  display: flex;
  align-items: flex-start;
  line-height: 1.5;

  & + .info-item {
    margin-top: 10px;
  }

  &.margin {
    margin-top: 10px;
  }

  .info-label {
    white-space: nowrap;
    flex-shrink: 0;
    color: #909399;
    font-size: 14px;
  }

  .info-value {
    flex: 1;
    color: #606266;
    font-size: 14px;
    word-break: break-all;
  }
}

.report-section {
  margin-bottom: 32px;
  padding-bottom: 24px;
  border-bottom: 1px solid #ebeef5;

  &:last-child {
    border-bottom: none;
    margin-bottom: 0;
    padding-bottom: 0;
  }
}

.report-header {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  margin-bottom: 20px;
  padding-bottom: 12px;
  border-bottom: 1px dashed #dcdfe6;

  .report-title {
    font-size: 16px;
    font-weight: 600;
    color: #409eff;
  }

  .report-meta {
    font-size: 13px;
    color: #909399;
  }
}

.attachment-wrapper {
  margin-top: 12px;
  padding-top: 8px;
  border-top: 1px dashed #ebeef5;

  .attachment-item {
    .attachment-list {
      display: flex;
      flex-wrap: wrap;
      gap: 16px;

      .attachment-link {
        display: inline-flex;
        align-items: center;
        gap: 6px;
        padding: 6px 12px;
        background: #f5f7fa;
        border-radius: 4px;
        cursor: pointer;

        &:hover {
          background: #ecf5ff;
          color: #409eff;
        }

        .file-name {
          font-size: 13px;
          max-width: 200px;
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
        }
      }
    }
  }
}

.footer-buttons {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  display: flex;
  justify-content: center;
  padding: 16px 24px;
  background: #fff;
  box-shadow: 0 -2px 12px rgba(0, 0, 0, 0.08);
  z-index: 100;

  .footer-btn {
    width: 200px;
    height: 44px;
    font-size: 16px;
    border-radius: 22px;
  }
}
.content-container {
  display: flex;
  .left-panel {
    flex: 1;
    margin-right: 10px;
  }
  .right-panel {
    width: 300px;
  }
}
.content-wrapper {
  display: flex;
  flex-direction: column;
}
.basic-info-wrapper {
  display: flex;
}
.detal-info-wrapper {
  margin-top: 10px;
  border-top: 1px solid #efefef;
  padding-top: 10px;
}
.info-list {
  flex: 1;
  overflow: hidden;
}
.file-list {
}
</style>
