<template>
  <view class="item-container" @click="$emit('click')">
    <view class="item-box">
      <view class="item-box-left">
        <image 
        class="img-item"
        :src="actualCover"
        mode="scaleToFill"
        @error="onImgError"
      ></image>
      </view>
      <view class="item-box-right">
        <view class="item-title line-clamp-2">{{ data.title || data.name  || '暂无标题' }}</view>
        <text class="item-author">{{ data.author || '佚名' }}</text>
        <text class="item-publish">{{ data.publisher || '暂无出版社数据' }}</text>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  name: "book-list-item",
  props: {
    data: {
      type: Object,
      required: true
    }
  },
  computed: {
    actualCover() {
      // 优先级：base64Cover > cover > imgCover
      
      // base64Cover 是本地 base64 图片，直接使用，不需要检查
      if (this.data.base64Cover) {
        return this.data.base64Cover;
      }
      
      // cover 和 imgCover 需要检查有效性
      const cover = this.data.cover || this.data.imgCover;
      
      // 如果有封面链接，检查是否是无效域名
      if (cover && this.isValidCoverLink(cover)) {
        return cover;
      }
      
      // 返回默认图片
      return '/static/images/default-book.png';
    }
  },
  methods: {
    /**
     * 检查封面链接是否有效
     * @param {string} coverlink - 封面链接
     * @returns {boolean} - 是否有效
     */
    isValidCoverLink(coverlink) {
      if (!coverlink || typeof coverlink !== 'string') {
        return false;
      }
      
      // 检查是否包含 http
      if (coverlink.indexOf('http') === -1) {
        return false;
      }
      
      // 已知无法访问的域名列表
      const blockedDomains = [
        'doubanio.com',
        'douban.com'
      ];
      
      // 检查是否包含被阻止的域名
      for (const domain of blockedDomains) {
        if (coverlink.includes(domain)) {
          return false;
        }
      }
      
      return true;
    },
    /**
     * 图片加载失败处理
     */
    onImgError(e) {
      e.target.src = "/static/images/default-book.png";
    }
  }
};
</script>

<style lang="scss" scoped>
.item-container {
  padding-bottom: 10px;
  .item-box {
    display: flex;
    justify-content: flex-start;
    align-items: flex-start;
    padding: 10px;
    background-color: #fff;
    border-radius: 6px;
    border-bottom: 1px solid #f4f4f4;
    .item-box-left {
      margin-right: 8px;
      .img-item{
        width: 64px;
        height: 90px;
        margin: 0 12px 0 0;
        border-radius: 5px;
      }
    }
    .item-box-right {
      display: flex;
      flex-direction: column;
      justify-content: flex-start;
      flex: 1;
      .item-title {
        font-size: 15px;
        font-weight: bold;
        color: #000;
        padding-bottom: 10px;
      }
      .item-author,
      .item-publish {
        font-size: 12px;
        color: #888;
        padding-bottom: 10px;
      }
      .hot-text {
        font-size: 12px;
        color: #ff4444;
      }
      .item-desc {
        padding-top: 5px;
        font-size: 13px;
        color: #666;
        line-height: 1.4;
      }
      .item-bottom-box {
        margin-top: 8px;
        display: flex;
        justify-content: space-between;
        align-items: center;
      }
    }
  }
}
</style>
