<template>
  <el-drawer
    title="事件详情"
    :visible.sync="drawerVisible"
    direction="rtl"
    size="600px"
    :before-close="handleClose"
    @opened="handleOpened"
  >
    <div v-if="loading" class="drawer-loading">
      <i class="el-icon-loading"></i>
      <span>加载中...</span>
    </div>

    <div v-else-if="eventData" class="event-drawer-content">
      <div class="drawer-header">
        <div class="event-title">
          <span class="event-name">{{ eventData.title || '未命名事件' }}</span>
          <el-tag :type="getStatusTagType(eventData.status)" size="medium">
            {{ getStatusLabel(eventData.status) }}
          </el-tag>
          <el-tag v-if="eventData.importantFlag === 1" type="danger" size="mini">
            重点
          </el-tag>
        </div>
        <div class="event-code">{{ eventData.eventNo || '-' }}</div>
      </div>

      <el-divider content-position="left">基本信息</el-divider>
      <el-descriptions :column="2" border>
        <el-descriptions-item label="事件类型">{{ eventData.type || '-' }}</el-descriptions-item>
        <el-descriptions-item label="事件级别">{{ eventData.level || '-' }}</el-descriptions-item>
        <el-descriptions-item label="来源">{{ getSourceLabel(eventData.source) }}</el-descriptions-item>
        <el-descriptions-item label="发生时间">{{ formatDateTime(eventData.occurredAt) }}</el-descriptions-item>
      </el-descriptions>

      <el-divider content-position="left">位置</el-divider>
      <el-descriptions :column="1" border>
        <el-descriptions-item label="地点">{{ eventData.location || '-' }}</el-descriptions-item>
        <el-descriptions-item v-if="hasLocation()" label="坐标">
          {{ eventData.longitude }}, {{ eventData.latitude }}
        </el-descriptions-item>
      </el-descriptions>

      <div v-if="hasLocation() && mapReady" class="drawer-map-section">
        <el-divider content-position="left">位置地图</el-divider>
        <div class="map-container">
          <Amap
            container-id="event-detail-map"
            width="100%"
            height="260px"
            :center="[eventData.longitude, eventData.latitude]"
            :zoom="15"
            :markers="[
              {
                lat: eventData.latitude,
                lng: eventData.longitude,
                title: eventData.title || eventData.eventNo || '事件'
              }
            ]"
          />
        </div>
      </div>

      <el-divider content-position="left">事件摘要</el-divider>
      <div class="remark-section">
        {{ eventData.summary || '暂无摘要' }}
      </div>

      <el-divider content-position="left">补充信息</el-divider>
      <el-descriptions :column="2" border>
        <el-descriptions-item label="关联警情">{{ eventData.alertId || '-' }}</el-descriptions-item>
        <el-descriptions-item label="是否重点">{{ eventData.importantFlag === 1 ? '是' : '否' }}</el-descriptions-item>
        <el-descriptions-item v-if="eventData.importantFlag === 1" label="标记人">{{ eventData.importantBy || '-' }}</el-descriptions-item>
        <el-descriptions-item v-if="eventData.importantFlag === 1" label="标记时间">{{ formatDateTime(eventData.importantTime) }}</el-descriptions-item>
        <el-descriptions-item label="最新更新时间">{{ formatDateTime(eventData.updateTime) }}</el-descriptions-item>
      </el-descriptions>
    </div>

    <div v-else class="drawer-error">
      <el-alert
        title="加载失败"
        type="error"
        :closable="false"
        show-icon
      >
        <template #default>
          <p>无法加载事件详情，请稍后重试</p>
          <el-button size="mini" @click="loadEventDetail">重试</el-button>
        </template>
      </el-alert>
    </div>
  </el-drawer>
</template>

<script>
import Amap from '@/components/Amap'
import { getEvent } from '@/api/emergency/event'
import { formatDateTime } from '@/views/duty/alert/utils/dateHelpers'

export default {
  name: 'EventDetailDrawer',
  components: {
    Amap
  },
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    eventId: {
      type: [Number, String],
      default: null
    }
  },
  data() {
    return {
      drawerVisible: false,
      loading: false,
      eventData: null,
      mapReady: false
    }
  },
  methods: {
    async loadEventDetail() {
      if (!this.eventId) return
      this.loading = true
      this.mapReady = false
      try {
        const res = await getEvent(this.eventId)
        if (res && res.code === 200) {
          this.eventData = res.data || null
        } else {
          this.eventData = null
        }
      } catch (error) {
        console.error('加载事件详情失败:', error)
        this.eventData = null
      } finally {
        this.loading = false
      }
    },
    handleClose(done) {
      this.reset()
      done()
    },
    handleOpened() {
      this.$nextTick(() => {
        setTimeout(() => {
          this.mapReady = true
        }, 100)
      })
    },
    reset() {
      this.eventData = null
      this.mapReady = false
    },
    hasLocation() {
      return this.eventData && this.eventData.longitude && this.eventData.latitude
    },
    getStatusLabel(status) {
      const map = {
        draft: '草稿',
        pending: '待处理',
        processing: '处理中',
        completed: '已完成',
        archived: '已归档'
      }
      return map[status] || status || '-'
    },
    getStatusTagType(status) {
      const map = {
        draft: 'info',
        pending: 'warning',
        processing: 'success',
        completed: 'default',
        archived: 'info'
      }
      return map[status] || 'info'
    },
    getSourceLabel(source) {
      const map = {
        manual: '人工录入',
        'alert_convert': '警情转入',
        alert: '接警转入',
        other: '其他'
      }
      return map[source] || source || '-'
    },
    formatDateTime(value) {
      return formatDateTime(value)
    }
  },
  watch: {
    visible(newVal) {
      this.drawerVisible = newVal
      if (newVal && this.eventId) {
        this.loadEventDetail()
      }
    },
    eventId(newVal) {
      if (newVal && this.drawerVisible) {
        this.loadEventDetail()
      }
    },
    drawerVisible(newVal) {
      if (!newVal) {
        this.$emit('update:visible', false)
        this.reset()
      }
    }
  }
}
</script>

<style scoped lang="scss">
.drawer-loading {
  text-align: center;
  padding: 40px 20px;
  color: #909399;
}

.drawer-loading i {
  font-size: 24px;
  margin-right: 8px;
  animation: rotating 2s linear infinite;
}

@keyframes rotating {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

.event-drawer-content {
  padding: 20px;
}

.drawer-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
  padding-bottom: 12px;
  border-bottom: 1px solid #ebeef5;
}

.event-title {
  display: flex;
  align-items: center;
  gap: 10px;
}

.event-name {
  font-size: 18px;
  font-weight: 600;
  color: #303133;
}

.event-code {
  font-size: 13px;
  color: #909399;
}

.drawer-map-section {
  margin-top: 16px;
}

.map-container {
  border-radius: 4px;
  overflow: hidden;
}

.remark-section {
  padding: 12px;
  background: #f5f7fa;
  border-radius: 4px;
  line-height: 1.6;
  color: #606266;
  white-space: pre-wrap;
  word-break: break-word;
}

.drawer-error {
  padding: 20px;
}
</style>