<template>
  <a-upload
    v-model:file-list="avatarFile"
    name="file"
    accept=".png,.jpg,.jpeg"
    :show-upload-list="false"
    action="https://v4-test.4dkankan.com/takelook/upload/file"
    :max-count="1"
    class="uploader"
    :headers="{
      authorization: 'authorization-text'
    }"
    :disabled="avatarFile.length > 0"
    @change="handleAvatarChange"
  >
    <div
      class="add-item-icon scene-sign"
      v-if="avatarFile.length > 0 && avatarFile[0].response"
    >
      <a-image class="avatar" :src="avatarFile[0].response.data" alt="avatar" />
      <span class="delete-scene" @click="deleteAvatar(avatarFile[0])">
        <close-outlined class="delete-scene-icon" />
      </span>
    </div>
    <div class="add-item-icon" v-else>
      <a-button shape="circle" class="button" type="primary">
        <plus-outlined class="add-room-icon" />
      </a-button>
    </div>
  </a-upload>
</template>
<script lang="ts" setup>
import { ref } from 'vue'
import { message, type UploadChangeParam } from 'ant-design-vue'
import type { FileItem } from './album-list.vue'

import { watchEffect } from 'vue'
const emit = defineEmits(['sync'])
const avatarFile = ref<any[]>([])
const props = defineProps<{ data: string | undefined }>()

watchEffect(() => {
  if (props.data?.length) {
    const tempData = {} as FileItem
    tempData.uid = `data-0`
    tempData.response = {
      data: props.data,
      ok: 0
    }
    if (!avatarFile.value?.length) {
      console.log('mapper', tempData)
      avatarFile.value = [tempData]
    }
  } else {
    avatarFile.value = []
  }
})

const handleAvatarChange = (info: UploadChangeParam) => {
  if (info.file.status === 'done') {
    const { code, data } = info.file.response
    if (code === 0) {
      emit('sync', data || '')
    }
  } else if (info.file.status === 'error') {
    message.error(`${info.file.name} file upload failed.`)
  }
}
const deleteAvatar = (item: any) => {
  const index = avatarFile.value.findIndex(i => i.uid === item.uid)
  if (index > -1) {
    avatarFile.value.splice(index, 1)
  }
  emit('sync', '')
}
</script>

<style lang="scss" scoped>
.add-item-icon {
  width: 120px;
  height: 120px;
  display: flex;
  justify-content: center;
  align-items: center;
  border: 1px solid #ebedf0;
  overflow: hidden;
  .avatar {
    max-width: 100%;
    // border-radius: 50%;
  }
  .button {
    background: linear-gradient(144deg, #00aefb 0%, #0076f6 100%);
    width: 40px;
    height: 40px;
  }
}
.scene-sign {
  border-radius: 4px;
  overflow: hidden;
  position: relative;
  z-index: 10;
  img {
    width: 100%;
    height: 100%;
    display: block;
    object-fit: cover;
  }
  .title {
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    height: 24px;
    background: rgba(0, 0, 0, 0.5);
    padding: 5px;
    font-size: 12px;
    color: #fff;
    margin: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .delete-scene {
    z-index: 2;
    position: absolute;
    right: 0;
    top: 0;
    width: 52px;
    height: 52px;
    background-color: rgba(0, 0, 0, 0.5);
    color: #fa5555;
    font-size: 14px;
    border-radius: 50%;
    display: flex;
    align-items: flex-end;
    transform: translate(100%, -100%);
    transition: all 0.3s ease;
    opacity: 0;
    cursor: pointer;

    .delete-scene-icon {
      padding: 10px;
    }
  }

  &:hover .delete-scene {
    transform: translate(50%, -50%);
    opacity: 1;
  }
  .status-cover {
    z-index: 1;
    position: absolute;
    left: 0;
    top: 0;
    right: 0;
    bottom: 0;
    background-color: rgba(0, 0, 0, 0.5);
    display: flex;
    align-items: center;
    justify-content: center;

    p {
      color: #fff;
    }
  }
}
</style>
