<template>
  <el-dialog 
    title="历史开单记录详情" 
    :visible.sync="visible" 
    :close-on-click-modal="false"
    class="NCC-dialog NCC-dialog_center" 
    width="1000px"
    @close="handleClose">
    <div class="detail-content" v-loading="loading">
      <!-- 基本信息卡片 -->
      <el-card class="info-card" shadow="hover">
        <div slot="header" class="card-header">
          <i class="el-icon-tickets"></i>
          <span class="card-title">基本信息</span>
        </div>
        <el-row :gutter="20">
          <el-col :span="8">
            <div class="info-item"> 
              <label class="info-label">
                <i class="el-icon-document"></i>
                ID：
              </label>
              <span class="info-value highlight">{{ dataForm.id || '无' }}</span>
            </div>
          </el-col>
          <el-col :span="8">
            <div class="info-item">
              <label class="info-label">
                <i class="el-icon-tickets"></i>
                开单号：
              </label>
              <span class="info-value highlight">{{ dataForm.orderNo || '无' }}</span>
            </div>
          </el-col>
          <!-- <el-col :span="24">
            <div class="info-item">
              <label class="info-label">
                <i class="el-icon-picture"></i>
                图片名称：
              </label>
              <span class="info-value">{{ dataForm.imageName || '无' }}</span>
            </div>
          </el-col> -->
        </el-row>
      </el-card>

      <!-- 会员信息卡片 -->
      <el-card class="info-card" shadow="hover">
        <div slot="header" class="card-header">
          <i class="el-icon-user"></i>
          <span class="card-title">会员信息</span>
        </div>
        <el-row :gutter="20">
          <el-col :span="8">
            <div class="info-item">
              <label class="info-label">
                <i class="el-icon-user"></i>
                会员编号：
              </label>
              <span class="info-value">{{ dataForm.memberNo || '无' }}</span>
            </div>
          </el-col>
          <el-col :span="8">
            <div class="info-item">
              <label class="info-label">
                <i class="el-icon-phone"></i>
                会员手机号：
              </label>
              <span class="info-value">{{ dataForm.memberPhone || '无' }}</span>
            </div>
          </el-col>
          <el-col :span="8">
            <div class="info-item">
              <label class="info-label">
                <i class="el-icon-user-solid"></i>
                会员名称：
              </label>
              <span class="info-value">{{ dataForm.memberName || '无' }}</span>
            </div>
          </el-col>
        </el-row>
      </el-card>

      <!-- 图片信息卡片 -->
      <el-card class="info-card" shadow="hover" v-if="imageList.length > 0">
        <div slot="header" class="card-header">
          <i class="el-icon-picture"></i>
          <span class="card-title">图片信息</span>
          <span class="image-count-badge">共 {{ imageList.length }} 张</span>
        </div>
        <div class="image-gallery">
          <div 
            v-for="(imgUrl, index) in imageList" 
            :key="index"
            class="image-item"
          >
            <el-image
              :src="imgUrl"
              :preview-src-list="imageList"
              :initial-index="index"
              fit="cover"
              class="gallery-image"
            >
              <div slot="error" class="image-error">
                <i class="el-icon-picture-outline"></i>
                <span>图片加载失败</span>
              </div>
            </el-image>
          </div>
        </div>
      </el-card>

      <!-- 备注信息卡片 -->
      <el-card class="info-card" shadow="hover" v-if="dataForm.remarks">
        <div slot="header" class="card-header">
          <i class="el-icon-edit-outline"></i>
          <span class="card-title">备注信息</span>
        </div>
        <div class="info-item">
          <div class="remarks-content">{{ dataForm.remarks || '无' }}</div>
        </div>
      </el-card>

      <!-- 时间信息卡片 -->
      <el-card class="info-card" shadow="hover">
        <div slot="header" class="card-header">
          <i class="el-icon-time"></i>
          <span class="card-title">时间信息</span>
        </div>
        <el-row :gutter="20">
          <el-col :span="12">
            <div class="info-item">
              <label class="info-label">
                <i class="el-icon-time"></i>
                创建时间：
              </label>
              <span class="info-value">{{ formatDateTime(dataForm.createdAt) || '无' }}</span>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="info-item">
              <label class="info-label">
                <i class="el-icon-time"></i>
                更新时间：
              </label>
              <span class="info-value">{{ formatDateTime(dataForm.updatedAt) || '无' }}</span>
            </div>
          </el-col>
        </el-row>
      </el-card>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="handleClose">关闭</el-button>
    </div>
  </el-dialog>
</template>

<script>
import request from '@/utils/request'

export default {
  name: 'DetailDialog',
  data() {
    return {
      visible: false,
      loading: false,
      dataForm: {
        id: null,
        orderNo: '',
        imageName: '',
        memberNo: '',
        memberPhone: '',
        memberName: '',
        remarks: '',
        createdAt: null,
        updatedAt: null
      }
    }
  },
  computed: {
    // 图片列表（计算属性，避免重复计算）
    imageList() {
      return this.getImageList(this.dataForm.imageName)
    }
  },
  methods: {
    // 初始化
    init(id) {
      this.visible = true
      if (id) {
        this.getInfo(id)
      }
    },
    // 获取详情
    getInfo(id) {
      this.loading = true
      request({
        url: `/api/Extend/LqOrderRecords/${id}`,
        method: 'GET'
      }).then(res => {
        if (res.code === 200) {
          this.dataForm = res.data || {}
        } else {
          this.$message.error(res.msg || '获取详情失败')
        }
        this.loading = false
      }).catch(error => {
        console.error('获取历史开单记录详情失败:', error)
        this.$message.error('获取详情失败')
        this.loading = false
      })
    },
    // 关闭
    handleClose() {
      this.visible = false
      this.dataForm = {
        id: null,
        orderNo: '',
        imageName: '',
        memberNo: '',
        memberPhone: '',
        memberName: '',
        remarks: '',
        createdAt: null,
        updatedAt: null
      }
    },
    // 格式化日期时间
    formatDateTime(dateTime) {
      if (!dateTime) return '无'
      const date = new Date(dateTime)
      if (isNaN(date.getTime())) return '无'
      const year = date.getFullYear()
      const month = String(date.getMonth() + 1).padStart(2, '0')
      const day = String(date.getDate()).padStart(2, '0')
      const hours = String(date.getHours()).padStart(2, '0')
      const minutes = String(date.getMinutes()).padStart(2, '0')
      const seconds = String(date.getSeconds()).padStart(2, '0')
      return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`
    },
    // 解析图片URL字符串（分号分隔）
    getImageList(imageStr) {
      if (!imageStr || typeof imageStr !== 'string') return []
      // 按分号分割，过滤空字符串，去除首尾空格
      const urls = imageStr.split(';')
        .map(url => url.trim())
        .filter(url => url && url.length > 0)
      return urls
    }
  }
}
</script>

<style lang="scss" scoped>
.detail-content {
  padding: 10px 0;
//   max-height: 70vh;
//   overflow-y: auto;
}

.info-card {
  margin-bottom: 20px;
  border-radius: 8px;
  transition: all 0.3s;
  
  &:hover {
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  }
  
  &:last-child {
    margin-bottom: 0;
  }
  
  ::v-deep .el-card__header {
    padding: 16px 20px;
    background: linear-gradient(135deg, #f5f7fa 0%, #ffffff 100%);
    border-bottom: 1px solid #e4e7ed;
  }
  
  ::v-deep .el-card__body {
    padding: 20px;
  }
}

.card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  
  i {
    margin-right: 8px;
    color: #409EFF;
    font-size: 18px;
  }
  
  .card-title {
    font-size: 16px;
    font-weight: 600;
    color: #303133;
  }
  
  .image-count-badge {
    font-size: 12px;
    color: #909399;
    background: #f0f2f5;
    padding: 4px 8px;
    border-radius: 12px;
    font-weight: normal;
  }
}

.info-item {
  margin-bottom: 18px;
  display: flex;
  align-items: center;
  padding: 8px 0;
  
  &:last-child {
    margin-bottom: 0;
  }
  
  .info-label {
    // min-width: 120px;
    color: #606266;
    font-weight: 500;
    display: flex;
    align-items: center;
    // text-align: right;
    // margin-right: 12px;
    
    i {
      margin-right: 6px;
      color: #909399;
      font-size: 14px;
    }
  }
  
  .info-value {
    flex: 1;
    color: #303133;
    word-break: break-all;
    font-size: 14px;
    &.highlight {
      color: #409EFF;
      font-weight: 600;
    }
  }
}

.image-gallery {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 16px;
  padding: 10px 0;
  
  .image-item {
    position: relative;
    width: 100%;
    padding-top: 100%; // 保持1:1比例
    border-radius: 8px;
    overflow: hidden;
    border: 1px solid #e4e7ed;
    cursor: pointer;
    transition: all 0.3s;
    
    &:hover {
      border-color: #409EFF;
      box-shadow: 0 2px 8px rgba(64, 158, 255, 0.2);
      transform: translateY(-2px);
    }
    
    .gallery-image {
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      
      ::v-deep .el-image__inner {
        object-fit: cover;
      }
    }
    
    .image-error {
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      background: #f5f7fa;
      color: #909399;
      
      i {
        font-size: 32px;
        margin-bottom: 8px;
      }
      
      span {
        font-size: 12px;
      }
    }
  }
}

.remarks-content {
  padding: 12px 16px;
  background: #f5f7fa;
  border-radius: 6px;
  color: #303133;
  line-height: 1.6;
  word-break: break-all;
  white-space: pre-wrap;
  border-left: 3px solid #409EFF;
}

.dialog-footer {
  text-align: right;
  padding: 20px 0 0;
  border-top: 1px solid #e4e7ed;
  margin-top: 10px;
}

.dialog-footer .el-button {
  margin-left: 10px;
}

// 滚动条样式优化
.detail-content::-webkit-scrollbar {
  width: 6px;
}

.detail-content::-webkit-scrollbar-track {
  background: #f1f1f1;
  border-radius: 3px;
}

.detail-content::-webkit-scrollbar-thumb {
  background: #c1c1c1;
  border-radius: 3px;
  
  &:hover {
    background: #a8a8a8;
  }
}
</style>

