<template>
  <div class="app">
    <!-- <div v-if="visible" class="modal-overlay" @click="closeBoxCard">
      <div class="modal-content" @click.stop> -->
        <!-- 跑马灯容器 -->
        <div class="carousel-container">
          <el-carousel    v-if="!loading" height="500px" arrow="always">
            <el-carousel-item  v-for="(image, index) in carouselImages" :key="index">
              <!-- 添加双击事件和放大容器 -->
              <el-image 
              class="carousel-image"
                :src="image.url" 
                :fit="cover"
                @dblclick="showEnlargedImage(image.url)"
              />
              <div class="text-overlay">{{ image.little }}</div>
            </el-carousel-item>
          </el-carousel>
          <div v-else class="loading-text">加载中...</div>
        </div>

        <!-- 放大图片模态框 -->
        <div v-if="showEnlarged" class="enlarged-modal" @click="showEnlarged = false">
          <img :src="enlargedImageUrl" class="enlarged-image" />
        </div>

        <div class="box-card">
          
          <div class="card-body">
            <div class="info-container">
              <div v-for="(item, index) in stockInfo" :key="index" class="info-item" :class="{ 'status-error': item.label === '盘点结果' && item.value === '盘点异常' }">
                <strong>{{ item.label }}:</strong>
                <span>{{ item.value }}</span>
              </div>
            </div>
          </div>
        </div>
      </div>
    <!-- </div>
  </div> -->
</template>

<script>
import { ref, onMounted } from 'vue';
import { StockApi } from '@/api/logistics/stock';

export default {
  props: {
    stockId: {
      type: [String, Number],
      default: null
    }
  },
  setup(props, { emit }) {
    const loading = ref(true);
    const carouselImages = ref([]);
    const stockInfo = ref([]);
    const showEnlarged = ref(false);
    const enlargedImageUrl = ref('');

    // 获取库存详情
    const fetchStockInfo = async () => {
      try {
        const response = await StockApi.getStockStatus({
          stockId: props.stockId
        });
        let statusString = "盘点正确";
        const scanData = response.scan || [];
        // 检查所有扫描项是否一致
        // const hasInconsistent = scanData.some(item => 
        //   item.wmsCode !== item.code
        // );
        
        // if (hasInconsistent) {
        //   statusString = "盘点异常";
        // }       
        // 处理轮播图数据
        carouselImages.value = response.images || [];
        // 新增：将时间戳转换为日期字符串
        const formatDate = (timestamp) => {
          const date = new Date(timestamp);
          return date.toLocaleDateString() + ' ' + date.toLocaleTimeString();
        };
        
        // 处理盘点信息
        stockInfo.value = [
          { 
            label: '创建时间', 
            value: formatDate(response.createTime) // 使用转换后的日期字符串
          },
          { 
            label: '批次号', 
            value: response.lotnum || '-' 
          },
          { 
            label: '盘点结果', 
            value: (response.statusString) 
          },
          // { 
          //   label: '托盘编码', 
          //   value: response.trayCode || '-' 
          // }
        ];
        console.log(response.scan);
        // 将列表信息转换为stockInfo信息
        for(const item of response.scan){
          //wms值
          stockInfo.value.push({
            label: '上位'+item.remark,
            value: item.wmsCode
          });
          //扫描值
          stockInfo.value.push({
            label: '扫描'+item.remark,
            value: item.code
          });

          //判断
          stockInfo.value.push({
            label: '扫描结果',
            value: item.code===item.wmsCode?'正确':'异常'
          });
        }
        console.log(stockInfo);
 
      } catch (error) {
        console.error('获取库存信息失败:', error);
      } finally {
        loading.value = false;
      }
    };

    onMounted(() => {
      console.log(props.stockId);
      if (props.stockId) {
        fetchStockInfo();
      }
    });

    const showEnlargedImage = (url) => {
      enlargedImageUrl.value = url;
      showEnlarged.value = true;
    };

    const closeBoxCard = () => {
      emit('close-box-card');
    };

    return {
      loading,
      carouselImages,
      stockInfo,
      showEnlarged,
      enlargedImageUrl,
      showEnlargedImage,
      closeBoxCard
    };
  }
};
</script>

<style scoped>
/* 新增放大图片样式 */
.enlarged-modal {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.9);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 999;
  cursor: pointer;
}

.enlarged-image {
  max-width: 90%;
  max-height: 90%;
  object-fit: contain;
}

/* 原有样式保持不变 */
.app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
}

button {
  padding: 10px 20px;
  font-size: 16px;
  cursor: pointer;
}

.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
}

.modal-content {
  background-color: #fff;
  padding: 20px;
  border-radius: 8px;
  width: 80%;
  max-width: 600px;
}
.status-error {
  color: red;
}
<style scoped>
.box-card {
  z-index: 1; /* 确保在父容器之上 */
  background-color: #fff;
  padding: 20px;
  border-radius: 8px;
  width: 100%;
  max-width: 600px;
  position: relative;
  overflow: hidden;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}
.carousel-image {
  width: 100%;
  height: 100%;
  object-fit: cover; /* 图片填充容器，并保持比例 */
  object-position: center; /* 确保图片居中 */
}

.close-btn {
  background-color: transparent;
  border: none;
  font-size: 18px;
  cursor: pointer;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.card-body {
  font-size: 12px;
}

.info-container {
  display: flex;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 10px;
}

.info-item {
  flex: 1 1 calc(25% - 01px);
  background-color: #f9f9f9;
  padding: 10px;
  border-radius: 4px;
  box-sizing: border-box;
  text-align: center;
}

.carousel-container {
  position: relative; /* 或 absolute、fixed、sticky，根据你的布局需求选择 */
  /* z-index: 9999;  */
  width: 100%;
  max-width: 888px;
  padding: 2px; 
  margin: 0 auto; 
}

.carousel-image {
  width: 100%;
  height: auto;
  display: block;
  cursor: pointer; 
  box-sizing: border-box; 
}

.text-overlay {
  position: absolute;
  top: 20px;
  left: 10px;
  color: white;
  font-size: 30px;
}
</style>
