<template>
  <left-skeleton @save="save" @deleteFunc="deleteFunc">
    <el-input v-model="inputText" placeholder="请输入在线地址或在下方上传" />
    <el-upload
      drag
      :action="config.base.origin + '/h5template/upload'"
      accept="image/*"
      :limit="1"
      :on-success="onSuccess"
      :headers="{ Authorization: token }"
    >
      <el-icon class="el-icon--upload"><upload-filled /></el-icon>
      <div class="el-upload__text">拖拽图片到此处或<em>点击上传图片</em></div>
    </el-upload>
    <img :src="inputText" style="width: 100%" />
  </left-skeleton>
</template>

<script setup>
import config from '../../../../config/index';
import { defineProps, ref, inject } from 'vue';
import leftSkeleton from '../H5Editor/leftSkeleton.vue';

const inputText = ref('');
const saveComponents = inject('saveComponents');
const deleteItemComponents = inject('deleteItemComponents');

const props = defineProps({
  item: Object,
  index: String,
});

const item = JSON.parse(JSON.stringify(props.item));
const token = localStorage.getItem('token');

inputText.value = item.item.src;

const onSuccess = res => {
  if (res.code != 0) return;
  inputText.value = res.data.url;
};

const save = () => {
  item.item.src = inputText.value;
  saveComponents(item);
};

const deleteFunc = () => {
  /**
   * 1.删除本地
   * 2.删除远程
   */
  deleteItemComponents(props.index);
};

</script>

<style scoped>
.el-upload {
  margin: 1em 0;
}
</style>
