<!--
 Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved.

 https://www.mall4j.com/

 未经允许，不可做商业用途！

 版权所有，侵权必究！
-->
<template>
  <div class="img-upload-components">
    <div class="plugin-images">
      <div
        tabindex="0"
        class="el-upload el-upload-text"
        @click="elxImgboxHandle"
      >
        <el-image
          v-if="modelValue"
          fit="contain"
          :src="addDomain(modelValue)"
          class="pic"
        >
          <template #error>
            <img
              class="img-slot"
              src="~@/assets/images/def.png"
            >
          </template>
        </el-image>
        <el-icon
          v-else
          class="pic-uploader-icon"
        >
          <Plus />
        </el-icon>
      </div>
    </div>
    <!-- 弹窗, 新增图片 -->
    <img-box
      v-if="elxImgboxVisible"
      ref="elxImgboxRef"
      @refresh-pic="refreshPic"
    />
    <el-dialog
      v-model="visible"
      :append-to-body="visible"
    >
      <img-show :src="modelValue" />
    </el-dialog>
  </div>
</template>

<script setup>
const { proxy } = getCurrentInstance()

defineProps({
  modelValue: {
    default: '',
    type: String
  },
  disabled: {
    default: false,
    type: Boolean
  }
})

const emit = defineEmits(['update:modelValue'])

const Data = reactive({
  elxImgboxVisible: false,
  visible: false
})

const { visible, elxImgboxVisible } = toRefs(Data)

const elxImgboxRef = ref(null)
// 打开图片选择窗
const elxImgboxHandle = () => {
  if (Data.disabled) {
    openImg()
    return false
  }
  Data.elxImgboxVisible = true
  proxy.$nextTick(() => {
    elxImgboxRef.value.init(1)
  })
}

const openImg = () => {
  Data.visible = true
}

const refreshPic = (imagePath) => {
  emit('update:modelValue', imagePath)
}
</script>

<style lang="scss" scoped>
.img-upload-components {
  max-height: 120px;

  .plugin-images {
  display: inline-block;
  width: auto;
  .el-upload {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 120px;
    height: 120px;
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    box-sizing: border-box;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    .pic-uploader-icon {
      font-size: 28px;
      color: #8c939d;
      width: 120px;
      height: 120px;
      line-height: 120px;
      text-align: center;
    }
    .pic {
      width: 100%;
      max-width: 118px;
      height: 100%;
      max-height: 118px;
      display: block;
    }
    .el-upload:hover {
      border-color: #409eff;
    }
  }
}
}

</style>
