<template>
  <el-form
    ref="form"
    :model="caseFile"
    label-width="90px"
    class="camera-from dispatch-file-from"
  >
    <el-form-item label="附件：" class="mandatory">
      <el-upload
        class="upload-demo"
        :multiple="false"
        :limit="1"
        :disabled="!!file"
        :before-upload="upload"
        :file-list="fileList"
        :http-request="httpsApi"
        :on-preview="previewFile"
        :accept="photoFormats"
        :before-remove="removeFile"
      >
        <el-button type="primary" :disabled="!!file">
          <el-icon><Upload /></el-icon>上传
        </el-button>
        <template v-slot:tip>
          <div class="el-upload__tip">注：可上传{{ size }}以内的{{ photoFormatDesc }}</div>
        </template>
        <template v-slot:file="{ file }">
          <div class="file" @click.stop="previewFile()">
            <div>
              <el-icon><Document /></el-icon>
              <span class="name">{{ file.name }}</span>
            </div>
            <el-icon v-if="!caseFile.id" @click.stop="removeFile()"><Close /></el-icon>
          </div>
        </template>
      </el-upload>
    </el-form-item>
    <el-form-item label="附件标题：" class="mandatory">
      <el-input
        v-model="caseFile.imgInfo"
        placeholder="请输入最多不能超过50字"
        maxlength="50"
        show-word-limit
      ></el-input>
    </el-form-item>
  </el-form>
</template>

<script setup lang="ts">
import {
  DrawFormatDesc,
  DrawFormats,
  photoFormats,
  photoFormatDesc,
  FileDrawType,
  OtherFormatDesc,
  OtherFormats,
} from "@/constant/caseFile";
import { uploadFile } from "@/store/system";
import { maxFileSize } from "@/constant/caseFile";
import { useUpload } from "@/hook/upload";
import { saveOrUpdate, CaseImg } from "@/store/case";
import { ElMessage } from "element-plus";
import { computed, ref, watch, watchEffect } from "vue";
import { QuiskExpose } from "@/helper/mount";

const props = defineProps<{
  caseId: number;
  data: CaseImg;
}>();

const caseFile = ref<CaseImg>({
  caseId: props.caseId,
  id: props.data?.id,
  imgUrl: props.data.imgUrl,
  imgInfo: props.data.imgInfo,
  sort: props.data?.sort || '',
} as any);

const { size, fileList, upload, removeFile, previewFile, file, accept } = useUpload({
  maxSize: maxFileSize,
  formats: photoFormats,
});

const formatDesc = computed(() =>
DrawFormatDesc
);
watch(props, newValue => {
  caseFile.value.id = newValue.data.id;
  caseFile.value.imgInfo = newValue.data.imgInfo;
  caseFile.value.imgUrl = newValue.data.imgUrl;
  caseFile.value.sort = newValue.data.sort;
  if(newValue.data.imgUrl){
    file.value = {
    name: newValue.data.imgInfo || '',
    url: newValue.data.imgUrl || '',
  }
  }
},{ immediate: true })
watchEffect(() => {
  if (file.value?.name) {
    caseFile.value.imgInfo = file.value?.name.substring(0, 50);
  }
});

const httpsApi = async ({file})=> {
  console.log('httpsApi', file)
  let fileUrl = await uploadFile(file);

  file.value = {
    name: file.name,
    url: fileUrl,
  }
  console.log('httpsApi', file, fileUrl)
}

defineExpose<QuiskExpose>({
  async submit() {
    if (!file.value) {
      ElMessage.error("请上传附件");
      throw "请上传附件";
    } else if (!caseFile.value.imgInfo.trim()) {
      ElMessage.error("附件标题不能为空！");
      throw "附件标题不能为空！";
    }
    console.log('defineExpose', caseFile.value, file.value)
    let imgUrl = file.value && file.value.value ? file.value.value?.url : file.value?.url
    await saveOrUpdate({ ...caseFile.value, imgUrl });
    return caseFile.value;
  },
});
</script>

<style scoped lang="scss">
.upload-demo {
  overflow: hidden;
}

.file {
  display: flex;
  justify-content: space-between;
  align-items: center;
  > div {
    display: flex;
    align-items: center;
  }

  .name {
    margin-left: 10px;
  }
}
</style>
