<template>
  <div class="img-content">
    <Toper
      title="图片"
      :btns="['edit', 'del']"
      :disabled="mode !== 'edit'"
      @edit="diaShow = true"
      @del="removeFun"
    />
    <template v-if="mode === 'edit'">
      <div v-for="(item, index) in Array(paramsData.colNum)" :key="index" class="pic">
        <div class="title-item">{{ titleKey || '请输入图片key' }}</div>
        <el-icon :size="100"><Picture /></el-icon>
        <div class="title-item">{{ paramsData.title }}</div>
      </div>
    </template>
    <div
      v-else
      ref="PasteContentRef"
      class="paste-content"
      :class="{ active: isFocus }"
      :contenteditable="contenteditable"
      @click="clickFun"
      @paste="pasteFun"
    >
      <template v-if="fileList.length > 0">
        <el-row :gutter="20">
          <el-col v-for="(item, index) in fileList" :key="index" :span="24 / paramsData.colNum">
            <div class="img-item">
              <div class="img">
                <img :src="item.src" />
              </div>
              <div class="tip">
                <el-input
                  class="img-tip-inp"
                  v-model="item.title"
                  :placeholder="params.placeholder || '请输入图例名称'"
                  clearable
                  @input="inputFun"
                />
              </div>
              <div class="del-btn" @click.stop="delFun(index)">
                <el-icon :size="22"><DeleteFilled /></el-icon>
              </div>
            </div>
          </el-col>
        </el-row>
      </template>
      <div v-else class="preview">
        <div class="no-data">单击此处粘贴图片...</div>
        <div class="placeholder">{{ paramsData.title }}</div>
      </div>
    </div>
    <Dialog v-model="diaShow" diaTitle="图片设置" :width="400" @close="closeFun">
      <div class="content">
        <el-form label-width="auto">
          <el-form-item label="图片key">
            <el-input v-model="titleKey" placeholder="请输入" clearable />
          </el-form-item>
          <el-form-item label="默认名称">
            <el-input v-model="paramsData.title" placeholder="请输入" clearable />
          </el-form-item>
          <el-form-item label="输入提示">
            <el-input v-model="paramsData.placeholder" placeholder="请输入" clearable />
          </el-form-item>
          <el-form-item label="展示列数">
            <el-input-number
              v-model="paramsData.colNum"
              :min="1"
              :max="4"
              :step="1"
              step-strictly
            />
          </el-form-item>
        </el-form>
      </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 Toper from './toper.vue';
import { cloneDeep } from 'lodash-es';

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

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

const props = withDefaults(defineProps<Props>(), {
  keyValue: '',
  value: [],
  params: {},
  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);
const paramsData = ref(cloneDeep(props.params));
if (!paramsData.value.colNum) {
  paramsData.value.colNum = 1;
}

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

watch(
  () => paramsData.value,
  (val: any) => {
    emit('update:params', 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 removeFun = () => {
  emit('del');
};

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;
  }
  const dom = event.target;
  const tagName = dom.tagName;
  if (tagName.toLowerCase() !== 'input') {
    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,
          title: props.params.title,
        });
        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">
.img-content {
  .img-tip-inp {
    .el-input__inner {
      text-align: center;
    }
  }
}
</style>

<style lang="scss" scoped>
.img-content {
  position: relative;
  margin-bottom: 10px;
  display: flex;
  align-items: center;
  justify-content: flex-start;
  border-radius: 4px;
  padding: 20px 10px 10px;
  border: solid 1px var(--el-border-color);
  .pic {
    width: 100%;
    height: 200px;
    border-radius: 4px;
    background-color: var(--el-bg-color);
    color: var(--el-text-color-placeholder);
    display: flex;
    align-items: center;
    justify-content: center;
    align-content: center;
    flex-wrap: wrap;
    .title-item {
      width: 100%;
      text-align: 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: 100%;
        max-width: 300px;
      }
      .del-btn {
        opacity: 0;
        position: absolute;
        top: 0;
        right: 0;
        color: var(--el-color-danger);
        cursor: pointer;
      }
    }
    .preview {
      width: 100%;
      height: 148px;
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      align-content: space-between;
      .no-data {
        color: var(--el-text-color-placeholder);
      }
      .placeholder {
        text-align: center;
        color: var(--el-text-color-primary);
      }
    }
  }
}
</style>
