<!--  -->
<template>
  <div
    class="pic-box"
    :class="{ show: viewer }"
    :style="metasHeight ? `height:${metasHeight}px;` : ''"
  >
    <div>
      <div
        v-if="currentIndex != 0"
        class="ctrl-btn left-btn"
        @click.stop="changeImage('pre')"
      >
        <kk-icon type="left" />
      </div>
      <div
        v-if="currentIndex < data.length - 1"
        class="ctrl-btn right-btn"
        @click.stop="changeImage('next')"
      >
        <kk-icon type="right" />
      </div>
    </div>
    <div class="over-box">
      <div
        class="image-list"
        :style="`transform:translateX(${-100 * currentIndex}%);`"
      >
        <div
          v-for="(i, index) in data"
          :key="index"
          class="image-item"
          :style="`transform:translateX(${
            100 * index
          }%);background-image:url(${changeUrl(i.src)});`"
        />
      </div>
      <kk-icon class="loading-icon" type="_loading_" />
      <!-- <div class="del-btn">
                <kk-icon type="del"></kk-icon>
            </div> -->
    </div>
    <div class="continue">
      <span class="pic-num">
        <span class="cur">{{ currentIndex + 1 }}</span>
        <span><span>&nbsp;</span>/<span>&nbsp;</span></span>
        <span>{{ data.length }}</span>
      </span>
    </div>
    <!-- 移动端缩放 -->
  </div>
</template>

<script lang="ts">
import { defineComponent, inject, ref } from 'vue'
import { buildProps } from '@kankan-components/utils'
import { KkIcon } from '@kankan-components/components'
import type { PropType } from 'vue'

const currentIndex = ref(0)

const props = buildProps({
  metasHeight: {
    type: Number,
    default: null,
  },
  data: {
    type: Array as PropType<SourceType[]>,
    default: () => [],
  },
  viewer: {
    type: Boolean,
    default: false,
  },
  scale: {
    type: Boolean,
    default: false,
  },
})

export default defineComponent({
  name: 'MetaImage',
  components: {
    'kk-icon': KkIcon,
  },
  props,
  setup() {
    const __sdk: any = inject('__sdk')
    function changeUrl(name: string, now?: string) {
      if (name.includes('http')) {
        return name
      } else {
        if (
          (typeof name === 'string' && name.slice(0, 4) == 'blob') ||
          (typeof name === 'string' && name.slice(0, 10) == 'data:image')
        ) {
          return name
        } else {
          return __sdk.resource.getUserResourceURL(name, false, now)
        }
      }
    }
    const changeImage = (type: 'pre' | 'next') => {
      if (type == 'pre') {
        currentIndex.value--
      } else {
        currentIndex.value++
      }
    }

    return {
      changeUrl,
      changeImage,
      currentIndex,
    }
  },
})
</script>
<style lang="scss" scoped>
.showPicBox {
  width: 100%;
  height: 100%;
  position: fixed;
  z-index: 10000;
  background: rgb(24, 22, 22);
  top: 0;
  left: 0;
  .close {
    position: absolute;
    top: 10px;
    right: 10px;
    width: 20px;
    height: 20px;
    z-index: 100;
    color: #fff;
    .iconfont {
      font-size: 20px;
    }
  }
  .loading {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
  }
  .imgbox {
    width: 100%;
    height: 100%;
    background-repeat: no-repeat;
    background-size: contain;
    background-position: center center;
    #eleImg {
      // position: absolute;

      // top: 50%;
      // left: 50%;
      // transform: translate(-50%, -50%);
      margin: 0 auto;
      display: block;
      &.s {
        height: 100%;
        width: auto;
      }
      &.h {
        height: auto;
        width: 100%;
      }
    }
  }
}
.del-btn {
  width: 24px;
  height: 24px;
  background: rgba(0, 0, 0, 0.6);
  border-radius: 50%;
  position: absolute;
  cursor: pointer;
  top: 10px;
  right: 10px;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: center;
}
.loading-icon {
  color: var(--editor-main-color);
  animation: rotate 2s infinite linear;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  font-size: 30px;
}
@keyframes rotate {
  0% {
    transform: translate(-50%, -50%) rotate(0deg);
  }
  100% {
    transform: translate(-50%, -50%) rotate(360deg);
  }
}
.pic-box {
  width: 100%;
  height: 100%;
  position: absolute;
  border-radius: 4px;
  border: 1px solid rgba(255, 255, 255, 0.2);
  top: 0;
  left: 0;
  z-index: 10;

  .over-box {
    width: 100%;
    height: 100%;
    overflow: hidden;
  }
  .continue {
    width: 100%;
    height: 32px;
    background: linear-gradient(180deg, rgba(0, 0, 0, 0.1) 0%, #000000 200%);
    border-radius: 0px 0px 4px 4px;
    position: absolute;
    bottom: 0;
    left: 0;

    .ui-input {
      width: 100%;
    }
    .continue-tips {
      font-size: 12px;
      margin-right: 5px;
    }
    .edit-pic-num {
      // position: absolute;
      // right: 10px;
      font-size: 12px;
      .cur {
        color: var(--editor-main-color);
      }
    }
    .pic-num {
      position: absolute;
      right: 10px;
      top: 50%;
      transform: translateY(-50%);
      font-size: 12px;
      .cur {
        color: var(--editor-main-color);
      }
    }
  }

  .ctrl-btn {
    width: 32px;
    height: 32px;
    background: rgba(0, 0, 0, 0.2);
    border-radius: 50%;
    position: absolute;
    cursor: pointer;
    top: 50%;
    transform: translateY(-50%);
    z-index: 10;
    display: flex;
    align-items: center;
    justify-content: center;
    .iconfont {
      font-size: 14px;
    }
    &.left-btn {
      left: 5px;
    }
    &.right-btn {
      right: 5px;
    }
  }
  .image-list {
    width: 100%;
    height: 100%;
    position: relative;
    transition: all 0.3s linear;
    .image-item {
      width: 100%;
      height: 100%;
      // background: red;
      position: absolute;
      transform: translateX(0);
      text-align: center;
      background-repeat: no-repeat;
      background-size: contain;
      background-position: center;
      img {
        height: 100%;
        width: auto;
      }
    }
  }
  &.show {
    .ctrl-btn {
      width: 40px;
      height: 80px;
      background: rgba(0, 0, 0, 0.6);
      .iconfont {
        font-size: 20px;
      }
      &.left-btn {
        left: 0px;
        border-radius: 0 40px 40px 0;
        .icon {
          margin-right: 5px;
        }
      }
      &.right-btn {
        right: 0px;
        border-radius: 40px 0 0 40px;
        .icon {
          margin-left: 8px;
        }
      }
    }
    .continue {
      width: 76px;
      height: 36px;
      background: rgba(0, 0, 0, 0.6);
      border-radius: 20px;
      position: absolute;
      bottom: -5%;
      left: 50%;
      transform: translateX(-50%);

      .pic-num {
        width: 76px;
        height: 36px;
        display: inline-block;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 20px;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        span {
          display: flex;
          align-items: center;
          justify-content: center;
        }
      }
    }
  }
}

[is-mobile] {
  .pic-box {
    &.show {
      .ctrl-btn {
        width: 40px;
        height: 80px;
        background: rgba(0, 0, 0, 0.6);
        .iconfont {
          font-size: 20px;
        }
        &.left-btn {
          left: 0px;
          border-radius: 0 40px 40px 0;
          .icon {
            margin-right: 5px;
          }
        }
        &.right-btn {
          right: 0px;
          border-radius: 40px 0 0 40px;
          .icon {
            margin-left: 8px;
          }
        }
      }
      .continue {
        width: 76px;
        height: 36px;
        background: rgba(0, 0, 0, 0.6);
        border-radius: 20px;
        position: absolute;
        bottom: -6%;
        left: 50%;
        transform: translateX(-50%);

        .pic-num {
          width: 76px;
          height: 36px;
          display: inline-block;
          display: flex;
          align-items: center;
          justify-content: center;
          font-size: 20px;
          top: 50%;
          left: 50%;
          transform: translate(-50%, -50%);
          span {
            display: flex;
            align-items: center;
            justify-content: center;
          }
        }
      }
    }
  }
}
</style>
