<template>
  <div class="image-report-info">
    <div>
      <template v-if="data">
        <div
          v-for="(item) in visibleItems"
          :key="item.field"
          :class="['description-item', { 'with-divider': item.isLine }]"
        >
          <template v-if="data[item.field] !== null && data[item.field] !== undefined">
            <!-- 子标题独占一行 -->
            <template v-if="item.subTitle">
              <!-- <hr style="background-color: #E5E9F0; margin: 12px 0"> -->
              <div class="sub-title">{{ item.subTitle }}</div>
            </template>

            <!-- 标签与内容同行（自动换行） -->
            <div class="content-row">
              <div :class="['label-wrapper', { 'with-avatar': item.showAvatar }]">
                <a-avatar
                  v-if="item.showAvatar"
                  :size="item.avatar.size"
                  :icon="item.avatar.icon"
                  :style="{ background: item.avatar.background }"
                />
              </div>
              <div class="content-value">
                <template v-if="item.fieldType === 'text' || !item.fieldType">
                  <span v-if="item.label" class="label-text">{{ item.label }}：</span>  <span :style="item.style ? item.style : {}">{{ data[item.field] }}</span>
                </template>
                <template v-else-if="item.fieldType === 'images'">
                  <div class="image-preview-container">
                    <div
                      v-for="(img, index) in data[item.field]"
                      :key="index"
                      class="thumbnail-item"
                      @click="openPreview(item.field,index)"
                    >
                      <img :src="img" class="thumbnail-image" :style="item.style ? item.style : {}" alt="预览图">
                    </div>
                  </div>
                </template>
              </div>
            </div>
          </template>
        </div>
      </template>
    </div>
    <ImagePreview
      :visible="previewVisible"
      :imgList="previewImgList"
      :initialIndex="previewIndex"
      @close="previewVisible = false"
    />
  </div>
</template>

<script>

import { getConfigByName, runLogic } from '@vue2-client/services/api/common'
import ImagePreview from './ImagePreview.vue'

export default {
  name: 'XImageReport',
  components: {
    ImagePreview
  },
  data () {
    return {
      // 隐藏配置 当数据结果 = -1^ 该数据字段会被delete
      data: null,
      config: null,
      previewVisible: false, // 控制预览显示
      previewImgList: [], // 预览图片数组
      previewIndex: 0 // 当前预览索引
    }
  },
  props: {
    queryParamsName: {
      type: String,
      default: ''
    },
    parameter: {
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  computed: {
    // 获取应该显示的标签
    visibleItems () {
      if (!this.config?.items) return []
      return this.config.items
    }
  },
  created () {
    this.getData(this.queryParamsName, {})
  },
  methods: {
    async getData (data, parameterData) {
      this.data = null
      getConfigByName(data, 'af-his', res => {
        this.config = res
        console.log(this.config)
        const parameter = { ...res.parameter, ...this.parameter, ...parameterData }
        if (res.logicName) {
          console.log('runlogic', res.logicName)
          runLogic(res.logicName, parameter, 'af-his').then(result => {
            this.data = result
          })
        }
      })
    },
     // 打开预览
    openPreview (field, index) {
      this.previewImgList = this.data[field]
      this.previewIndex = index
      this.previewVisible = true
    }
  },
  watch: {
    queryParamsName: {
      handler (newValue) {
        console.log(newValue)
        this.getData(newValue, {})
      },
      deep: true
    },
    parameter: {
      handler (newValue, oldValue) {
        console.log('监听事件触发:', { old: oldValue, new: newValue })
        // 当 parameter 变化时重新获取数据
        this.getData(this.queryParamsName, {})
      },
      deep: true // 深度监听对象内部变化
    }
  }
}
</script>

<style scoped>
.image-report-info {
  background: #fff;
  padding: 12px;
  border-radius: 4px;
  width: 100%;
}
/* 子标题样式 */
.sub-title {
  font-size: 16px;
  margin-bottom: 8px;
  display: block; /* 确保独占一行 */
}

/* 标签与内容同行布局 */
.content-row {
  display: flex;
  align-items: flex-start; /* 顶部对齐 */
  flex-wrap: wrap; /* 允许换行 */
  margin-bottom: 4px;
}

.label-wrapper {
  display: flex;
  align-items: center;
  flex-shrink: 0; /* 防止标签被压缩 */
}

.content-value {
  flex: 1;
  min-width: 60%; /* 保证内容区域最小宽度 */
  word-break: break-word; /* 长文本自动换行 */
}

/* 图片横向排列（自动换行） */
.image-preview-container {
  display: flex;
  flex-wrap: wrap; /* 关键：宽度不足时自动换行 */
  gap: 8px; /* 图片间距 */
  margin-top: 8px;
}

.thumbnail-item {
  flex-shrink: 0;
}

.thumbnail-image {
  width: 80px;
  height: 80px;
  object-fit: cover;
  border-radius: 4px;
  cursor: pointer;
}

.label-text {
  font-weight: bold;
}

</style>
