<template>
  <div class="upload">
    <input
      type="file"
      name=""
      id=""
      accept=".jpg,.png,.jpeg,.pdf"
      class="hidden"
      ref="inputRef"
      @change="handleChange"
    />
    <div
      class="box text-center flex items-center justify-center h-full cursor-pointer"
      v-if="!modelValue"
      @click="inputRef?.click"
    >
      <div>
        <Icon
          icon-name="iconshangchuan"
          style="font-size: 50px; color: #c3c3c3"
          class="mx-auto"
        ></Icon>
        <div>上传作品</div>
      </div>
    </div>
    <div class="fileContainer relative overflow-hidden" v-if="modelValue">
      <Pdf
        v-if="getSuffixName(modelValue.name) === 'pdf'"
        :url="localUrl"
      ></Pdf>
      <img v-else :src="localUrl" alt="" class="w-full h-full object-cover" />
      <div
        class="reupload text-white flex items-center justify-center transform top-1/2 -translate-y-1/2 left-1/2 -translate-x-1/2 absolute cursor-pointer"
        @click="inputRef?.click"
      >
        <Icon1 icon-name="iconzhongxin" class="mr-2"></Icon1>
        重新上传
      </div>
    </div>
    <div style="margin-top: 29px; color: var(--text-gray)">
      请上传jpg、png、jpeg、pdf格式的作品登记委托书，需加盖公章，法定代表人签字。
    </div>
  </div>
</template>

<script setup lang="ts">
import Icon from "@/components/Icon/index.vue";
import { nextTick, onBeforeUnmount, onMounted, ref } from "vue";
import { iFile } from "./types";
import Pdf from "../../../components/Pdf/index.vue";
import { fileTypeChecked, getSuffixName } from "@/utils/file";
import { ElMessage, useFormItem } from "element-plus";
import Icon1 from "@/components/Icon/index.vue";
const inputRef = ref<HTMLInputElement>();

const props = defineProps<{
  modelValue: iFile | null;
}>();
const formItem = useFormItem();
const emit = defineEmits(["update:modelValue"]);

const localUrl = ref("");

const handleChange = (e: Event) => {
  const files = (e.target as HTMLInputElement).files;
  if (!files || files?.length === 0) return;
  const file = files[0];
  if (!fileTypeChecked(file, inputRef.value!.accept)) {
    ElMessage.error("请上传指定类型的文件");
    return;
  }
  emit("update:modelValue", {
    file: file,
    name: file.name,
    size: file.size,
    progress: 0,
    url: "",
  } as iFile);
  uploadFileToServer(file);

  nextTick(() => {
    formItem.formItem?.validate("blur");
  });
};

onMounted(() => {
  if (!props.modelValue) return;
  const url = URL.createObjectURL(props.modelValue?.file);
  localUrl.value = url;
});

const uploadFileToServer = (file: File) => {
  // 上传??????
  const url = URL.createObjectURL(file);
  localUrl.value = url;
};

onBeforeUnmount(() => {
  URL.revokeObjectURL(localUrl.value);
});
</script>

<style scoped lang="scss">
.box {
  width: 249px;
  height: 139px;
  background: #f7f7f7;
  border-radius: 10px;
  border: 1px dashed #e2e2e2;
  font-size: 14px;
  font-family: PingFangSC-Medium, PingFang SC;
  font-weight: 500;
  color: var(--sy-gray-text);
}
.fileContainer {
  width: 136px;
  height: 169px;
  background: #f7f7f7;
  box-shadow: 0px 2px 10px 0px rgba(186, 186, 186, 0.5),
    inset 0px 1px 3px 0px rgba(251, 251, 251, 0.5);
  border-radius: 4px;
  border: 1px solid #e2e2e2;
  .reupload {
    display: none;
    width: 87px;
    height: 30px;
    background: rgba(0, 0, 0, 0.6);
    border-radius: 4px;
    font-size: 12px;
    font-family: PingFangSC-Regular, PingFang SC;
    font-weight: 400;
  }
  &:hover {
    .reupload {
      display: flex;
    }
  }
}
</style>
