<template>
  <div class="img-content">
    <div v-if="mode === 'edit'" class="pic">
      <el-icon :size="100"><Picture /></el-icon>
    </div>
    <div
      v-else
      ref="PasteContentRef"
      class="paste-content"
      :class="{ active: isFocus }"
      :contenteditable="contenteditable"
      @click="clickFun"
      @paste="pasteFun"
    >
      <template v-if="fileList.length > 0">
        <div v-for="(item, index) in fileList" :key="index" class="img-item">
          <div class="img">
            <img :src="item.src" />
          </div>
          <div class="tip">
            <el-input
              v-model="item.title"
              placeholder="请输入图例名称"
              clearable
              @input="inputFun"
            />
          </div>
          <div class="del-btn" @click.stop="delFun(index)">
            <el-icon :size="22"><DeleteFilled /></el-icon>
          </div>
        </div>
      </template>
      <div v-else class="no-data">单击此处粘贴图片...</div>
    </div>
    <div v-if="mode === 'edit'" class="setting-btn" @click="diaShow = true">
      <el-icon :size="22"><Setting /></el-icon>
    </div>
    <Dialog v-model="diaShow" diaTitle="图片设置" :width="400" @close="closeFun">
      <div class="content">
        <div class="title-key">
          <div class="label">图片key</div>
          <el-input v-model="titleKey" placeholder="请输入" clearable />
        </div>
      </div>
      <template #footer>
        <div>
          <el-button type="primary" @click="closeFun">确定</el-button>
        </div>
      </template>
    </Dialog>
  </div>
</template>

<script setup lang="ts">
import { ref, watch, onMounted, onBeforeUnmount, nextTick } from 'vue';
import { Picture, DeleteFilled } from '@element-plus/icons-vue';
import Dialog from '@/components/common/dialog/index.vue';
import { cloneDeep } from 'lodash-es';

const emit = defineEmits(['update:keyValue', 'update:value']);

interface Props {
  keyValue: any;
  value: any;
  mode: string;
  disabled?: boolean;
}

const props = withDefaults(defineProps<Props>(), {
  keyValue: '',
  value: [],
  mode: '',
  disabled: false,
});

const diaShow = ref(false);
const titleKey = ref(props.keyValue);
const fileList = ref<any>([]);
const isFocus = ref(false);
const PasteContentRef = ref();
const contenteditable = ref(false);

watch(
  () => titleKey.value,
  (val: any) => {
    emit('update:keyValue', val);
  },
  { deep: true }
);

watch(
  () => props.value,
  (val: any) => {
    fileList.value = cloneDeep(val) || [];
  },
  { deep: true, immediate: true }
);

onMounted(() => {
  document.addEventListener('click', focusFun);
});

onBeforeUnmount(() => {
  document.removeEventListener('click', focusFun);
});

const clickFun = () => {
  if (props.disabled) {
    return;
  }
  isFocus.value = true;
};

const focusFun = (event: any) => {
  if (props.disabled) {
    return;
  }
  if (PasteContentRef.value && !PasteContentRef.value.contains(event.target)) {
    isFocus.value = false;
  }
};

const delFun = (index: any) => {
  contenteditable.value = true;
  nextTick(() => {
    PasteContentRef.value.click();
    PasteContentRef.value.focus();
    fileList.value.splice(index, 1);
    contenteditable.value = false;
    emit('update:value', fileList.value);
  });
};

const pasteFun = (event: any) => {
  if (props.disabled) {
    return;
  }
  event.preventDefault();
  const win: any = window;
  const clipboardData = event.clipboardData || win.clipboardData;
  const items = clipboardData.items;
  if (!items) return;
  for (let i = 0; i < items.length; i++) {
    const item = items[i];
    if (item.kind === 'file' && item.type.startsWith('image/')) {
      const file = item.getAsFile();
      const reader = new FileReader();
      reader.onload = () => {
        const base64 = reader.result;
        fileList.value.push({
          src: base64,
        });
        emit('update:value', fileList.value);
      };
      reader.readAsDataURL(file);
      break;
    }
  }
};

const inputFun = () => {
  emit('update:value', fileList.value);
};

const closeFun = () => {
  diaShow.value = false;
};
</script>

<style lang="scss" scoped>
.img-content {
  position: relative;
  margin-bottom: 10px;
  display: flex;
  align-items: center;
  justify-content: flex-start;
  &:hover {
    .setting-btn {
      opacity: 1;
    }
  }
  .pic {
    width: 100%;
    height: 200px;
    border-radius: 4px;
    background-color: var(--el-bg-color);
    color: var(--el-border-color);
    display: flex;
    align-items: center;
    justify-content: center;
  }
  .paste-content {
    padding: 20px;
    width: 100%;
    min-height: 200px;
    background-color: var(--el-bg-color);
    border-radius: 4px;
    border: dashed 4px var(--el-bg-color);
    &.active {
      border-color: var(--el-color-primary);
    }
    .img-item {
      position: relative;
      width: 100%;
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      margin-bottom: 20px;
      &:hover {
        .del-btn {
          opacity: 1;
        }
      }
      .img {
        display: flex;
        align-items: center;
        justify-content: center;
        width: 100%;
        margin-bottom: 10px;
        img {
          max-width: 100%;
        }
      }
      .tip {
        width: 300px;
      }
      .del-btn {
        opacity: 0;
        position: absolute;
        top: 0;
        right: 0;
        color: var(--el-color-danger);
        cursor: pointer;
      }
    }
    .no-data {
      color: var(--el-text-color-placeholder);
    }
  }
  .setting-btn {
    opacity: 0;
    position: absolute;
    top: 40px;
    left: -29px;
    width: 30px;
    height: 30px;
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--el-color-info);
    background-color: var(--el-bg-color);
    border-radius: 4px 0 0 4px;
    border: solid 1px var(--el-border-color);
    border-right: 0;
    z-index: 1;
    cursor: pointer;
  }
}

.content {
  .title-key {
    display: flex;
    align-items: center;
    width: 300px;
    margin-bottom: 10px;
    .label {
      width: 80px;
    }
  }
}
</style>
