<template>
  <el-form
    ref="form"
    :model="caseFile"
    label-width="90px"
    class="camera-from dispatch-file-from jm-file-upload"
  >
    <el-form-item label="分类：" class="mandatory">
      <el-select
      v-model="caseFile.dictId"
      placeholder="请选择分类"
      size="large"
      style="width: 240px"
    >
      <el-option
        v-for="item in options"
        :key="item.id"
        :label="item.dictName"
        :value="item.id"
      />
    </el-select>
      <!-- <el-cascader
        v-model="caseFile.type"
        :options="fileOptions"
        :props="{
          checkStrictly: true,
        }"
        clearable
      /> -->
    </el-form-item>
    <el-form-item label="文件：" class="mandatory uploadFile" style="margin-left: 12px">
      <el-upload
        class="upload-demo"
        :multiple="false"
        drag
        :limit="1"
        :disabled="!!file"
        :before-upload="upload"
        :file-list="fileList"
        :http-request="()=>{}"
        :on-success="handleSuccess"
        :on-preview="previewFile"
        :accept="accept"
        :before-remove="removeFile"
      >
        <div type="primary" :disabled="!!file">
          <div>点击或拖拽文件上传</div>
          <div class="">{{ size }}以内的{{ formatDesc }}</div>
        </div>
        <template v-slot:file="{ file }: { file: UploadFile }">
          <div class="file" @click.stop="previewFile()">
            <div>
              <el-icon><Document /></el-icon>
              <span class="name">{{ file.name }}</span>
            </div>
            <el-icon @click.stop="removeFile()"><Close /></el-icon>
          </div>
        </template>
      </el-upload>
    </el-form-item>
  </el-form>
</template>

<script setup lang="ts">
import {
  DrawFormatDesc,
  DrawFormats,
  FileDrawType,
  OtherFormatDesc,
  OtherFormats,
  fileOptions
} from "@/constant/caseFile";
import { maxFileSize } from "@/constant/caseFile";
import { useUpload } from "@/hook/upload";
import { CaseFile, addCaseFile } from "@/store/caseFile";
import { getDictFileLists, uploadNewFile } from "@/store/case";
import { ElMessage, UploadFile } from "element-plus";
import { computed, ref, watchEffect, onMounted } from "vue";
import { addCaseScenes } from "./quisk";
import { QuiskExpose } from "@/helper/mount";

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

const caseFile = ref({
  caseId: props.caseId,
  filesTypeId: props.fileType,
  filesTitle: "",
  type: '',
  dictId: ''
});
const options = ref([])
onMounted(async () => {
  const res = await getDictFileLists(1);
  console.log('onMounted', res);
  options.value = res;
});

const { size, fileList, upload, removeFile, previewFile, file, accept } = useUpload({
  maxSize: 2000 * 1024 * 1024,
  formats: [".jpg", ".png", ".jpeg", ".mp4", ".wav", ".mp3", ".shp"],
});

const formatDesc = computed(() =>
  'jpg、png、jpeg、mp4、wav、mp3、shp上传'
);

// 上传请求
const handleSuccess = (option) => {
  console.log(option);
}

const handleAdd = async () => {
  await addCaseScenes();
};
watchEffect(() => {
  if (file.value?.name) {
    caseFile.value.filesTitle = file.value?.name.substring(0, 50);
  }
});

defineExpose<QuiskExpose>({
  async submit() {
    if (!file.value) {
      ElMessage.error("请上传附件");
      throw "请上传附件";
    } else if (!caseFile.value.filesTitle.trim()) {
      ElMessage.error("附件标题不能为空！");
      throw "附件标题不能为空！";
    }

    await uploadNewFile({ dictId: caseFile.value.dictId, file: file.value });
    return 
  },
});
</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;
  }
}
.jm-file-upload {
  .mtk {
    position: absolute;
    right: 0;
    top: 0;
  }
}
</style>
