<template>
  <div class="upload-warp picture-card">
    <div v-if="!base64Image" class="flex col uplaod-btn" @click="openImage">
      <el-icon>
        <Plus />
      </el-icon>
      <div class="add-text">添加</div>
    </div>
    <div v-if="base64Image" class="image-out">
      <img class="el-upload-list__item-thumbnail" :src="'file:///'+base64Image" alt="" />
      <span class="el-upload-list__item-actions">
        <span  @click.stop="handlePictureCardPreview(base64Image)">
          <el-icon><zoom-in /></el-icon>
        </span>
        <span v-if="!disabled" class="mar-left-10"  @click.stop="handleRemove()">
          <el-icon>
            <Delete />
          </el-icon>
        </span>
      </span>
    </div>

    <el-dialog v-model="dialogVisible" v-if="dialogVisible">
      <img class="w-full" :src="dialogImageUrl" alt="Preview Image" />
    </el-dialog>
  </div>
</template>
<script lang="ts" setup>
import { ref, defineEmits } from 'vue'
import { Delete, Download, Plus, ZoomIn } from '@element-plus/icons-vue'
import type { UploadFile } from 'element-plus'
import { imagesUpload } from '@/utils/appconfig'
import { ElMessage } from 'element-plus'
import * as  userApi from '@/apis/user'

import client from "@/stores/modules/client";
import icpList from '@/utils/ipc'
const clientStore = client();
// 定义自定义事件
const emit = defineEmits(['input']);

interface Props {
  value: string
  hasDel?: boolean
  hasSize?: boolean
  width?: number
  height?: number
  fileSize?: number
  styleWidth?: string
  accept?: string[]
}
const props = withDefaults(defineProps<Props>(), {
  value: '',
  hasDel: true,
  hasSize: true,
  width: 0,
  height: 0,
  fileSize: imagesUpload.fileSize,
  styleWidth: '160',
  accept: () => imagesUpload.accept
})

const dialogImageUrl = ref('')

const dialogVisible = ref(false)

const disabled = ref(false)

const handleRemove = () => {
  filePath.value = ''
  base64Image.value = ''
  emit('input' , '' )
}
const handlePictureCardPreview = (base64Image) => {
  dialogImageUrl.value = base64Image!
  dialogVisible.value = true
}

const handleDownload = (file: UploadFile) => {
  console.log(file)
}

const base64Image = ref(props.value)
const filePath = ref('')
function openImage() {

  clientStore.ipc.removeAllListeners(icpList.utils.openImage);
  clientStore.ipc.send(icpList.utils.openImage);
  clientStore.ipc.on(icpList.utils.openImage, async (event, result) => {
 //   filePath.value = result.filePath
 //   base64Image.value = result.filePath
    clientStore.ipc.removeAllListeners(icpList.utils.openImage);
    emit('input' ,result.filePath)
  })
}
</script>


<style lang="scss" scoped>
// .upload-warp {
//   ::v-deep {
//     .el-upload--picture-card {
//       background-color: #fff;

//       .el-icon,
//       .add-text {
//         color: #2957FF;
//       }
//     }
//   }
// }

.uplaod-btn {
  width: 88px;
  height: 88px;
  cursor: pointer;
  background: #fff;
  border-radius: 6px;
  border: 1px dashed #B3B4B5;

  .el-icon,
  .add-text {
    color: #2957FF;
    font-size: 14px;
  }

  &:hover {
    border: 1px dashed #2957FF;
  }
}

.image-out {
  width: 88px;
  height: 88px;
  position: relative;
  border: 1px solid var(--el-border-color);
  border-radius: 6px;
  overflow: hidden;
  cursor: pointer;

  .el-upload-list__item-thumbnail {
    width: 100%;
    height: 100%;
    object-fit: contain;
  }
  .el-upload-list__item-actions{
    position: absolute;
    width: 100%;
    height: 30px;
    left: 0;
    bottom:0;
    cursor: default;
    display: inline-flex;
    justify-content: center;
    align-items: center;
    color: #fff;
    opacity: 0;
    font-size: 20px;
    background-color: var(--el-overlay-color-lighter);
    transition: opacity var(--el-transition-duration);
    span{
        display: none;
        cursor: pointer;
      }
    // .el-upload-list__item-preview,
    // .el-upload-list__item-delete{

    // }
  }

  &:hover .el-upload-list__item-actions{
    opacity: 1;
    span{
      display: inline-flex;
      cursor: pointer;
      width: 20px;
    }
  }
}
</style>
