<template>
  <view class="photo-image">
    <view
      class="photo-image__tabs"
      v-if="disableShow"
    >
      <view
        class="photo-image__tab"
        v-for="(pest, index) in pestList"
        :key="index"
        :class="{ active: activeIndexList.includes(index) }"
        @click="changeTab(index)"
      >
        <text class="tab-text">{{ pest.pest_name }}({{ pest.pest_num }})</text>
      </view>
    </view>
    <view class="photo-image__content" v-if="images.length">
      <view class="photo-image__grid">
        <view class="photo-image__item" v-for="(item, index) in images" :key="index" @click="handleClick(item)">
          <view class="photo-image__item-img-container">
            <image
              class="photo-image__item-img"
              :src="item.addr"
              mode="aspectFill"
            />
          </view>
          <view class="photo-image__item-time">
            {{ formatDate(item.addtime) }}
          </view>
        </view>
      </view>
    </view>
    <view v-else>
      <u-empty text="暂无图片"></u-empty>
    </view>
  </view>
</template>

<script>
export default {
  props:{
    disableShow:{
      type: Boolean,
      default: true
    },
    pestList:{
      type: Array,
      default: () => []
    },
    images:{
      type: Array,
      default: () => []
    },
    deviceInfo:{
      type: Object,
      default: () => ({})
    },
    currentYear:{
      type: String | Number,
      default: ''
    },
  },
  data() {
    return {
      activeIndexList:[],
      activePest:[],
      activeTab: 0,
      tabs: ['全部', '稻飞虱', '水龟虫', '蝼蛄', '系统','全部', '稻飞虱', '水龟虫', '蝼蛄', '系统'],
    };
  },
  methods: {
    changeTab(index) {
      if(this.activeIndexList.includes(index)){
        this.activePest = this.activePest.filter(item => item !== this.pestList[index])
        this.activeIndexList = this.activeIndexList.filter(item => item !== index)
      }else{
        this.activeIndexList.push(index)
        this.activePest.push(this.pestList[index])
      }
      this.$emit('changeTab', this.activePest)
    },
    // 格式化时间
    formatDate(dateString) {
      const date = new Date(dateString*1000);
      const year = date.getFullYear();
      const month = String(date.getMonth() + 1).padStart(2, '0');
      const day = String(date.getDate()).padStart(2, '0');
      const hour = String(date.getHours()).padStart(2, '0');
      const minute = String(date.getMinutes()).padStart(2, '0');
      const second = String(date.getSeconds()).padStart(2, '0');
      return `${year}-${month}-${day} ${hour}:${minute}:${second}`;
    },
    handleClick(item) {
      uni.navigateTo({
        url: '/pages/bzy/devicePhoto?device_id=' + item?.device_id + '&addtime=' + item?.addtime + '&img_id=' + item?.id + '&id=' + this.deviceInfo.id + '&currentYear=' + this.currentYear
      })
    }
  },
};
</script>

<style lang="scss" scoped>
.photo-image {
  width: 100%;
  box-sizing: border-box;

  &__header {
    margin-bottom: 32rpx;
    white-space: nowrap;

    &::-webkit-scrollbar {
      display: none;
    }
  }

  &__tabs {
    box-sizing: border-box;
    width: 100%;
    overflow-x: auto;
    overflow-y: hidden;
    white-space: nowrap;
    // 去掉滚动条
    -ms-overflow-style: none;
    scrollbar-width: none;
  }

  .photo-image__content{
    padding: 30rpx;
    background: #ffffff;
    border-radius: 16rpx;
    .photo-image__grid{
      display: grid;
      grid-template-columns: repeat(3, 1fr);
      gap: 20rpx;
    }
  }
  &__tab {
    position: relative;
    display: inline-block;
    box-sizing: border-box;
    margin-right: 16rpx;
    background: #ffffff;
    color: #999999;
    font-family: "Source Han Sans CN VF";
    font-size: 28rpx;
    padding: 10rpx 32rpx;
    border-radius: 8rpx;
    &:last-child {
      margin-right: 0;
    }

    .tab-text {
      margin-right: 8rpx;
    }

    .tab-check {
      color: #4CAF50;
      font-weight: bold;
    }

    &.active {
      color: #0bbc58;
    }

    .tab-underline {
      position: absolute;
      bottom: 0;
      left: 0;
      right: 0;
      height: 4rpx;
      background: #4CAF50;
      border-radius: 2rpx;
    }
  }
  &__item {
    position: relative;
    width: 100%;
    padding: 0rpx;
    box-sizing: border-box;
    display: inline-block;
    overflow: hidden;
    border:2rpx solid #e4e7ed;
    border-radius: 16rpx;
    &-img-container {
      width: 100%;
      padding-bottom: 100%;
      position: relative;
      border-radius: 16rpx;
      overflow: hidden;
      background-color: #f5f5f5;
    }

    &-img {
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
    }

    &-time {
      position: absolute;
      bottom: 0rpx;
      width: 100%;
      text-align: center;
      padding: 0;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
      margin-top: 12rpx;
      font-size: 22rpx;
      color: #fff;
      text-align: center;
      background-color: rgba(0, 0, 0, 0.5);
    }
  }
}
</style>