<template>
<!--  <Dialog v-model="dialogVisible" title="标本图片批量导入" width="500">-->
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
    >
      <el-form-item label="照片组名称" prop="groupName">
        <el-input v-model="formData.groupName" placeholder="请输入照片组名称" />
      </el-form-item>
      <el-form-item label="照片组时间" prop="groupDate">
        <el-date-picker
          v-model="formData.groupDate"
          type="date"
          value-format="x"
          placeholder="选择照片组时间"
        />
      </el-form-item>

      <el-form-item label="照片组简介" prop="groupDescription">
<!--        <Editor v-model="formData.groupDescription" height="150px" />-->
        <el-input
          v-model="formData.groupDescription"
          :rows="3"
          type="textarea"
          placeholder="请输入照片组简介"
        />
      </el-form-item>
      <el-form-item label="照片组上传" prop="uploadPhotos">
<!--        + '&groupDate=' + formData.groupDate      /-->
          <el-upload
            ref="uploadRef"
            v-model:file-list="fileList"
            :action="importUrl + '?updateSupport=' + updateSupport + '&groupName=' + formData.groupName + '&groupDescription=' + formData.groupDescription + '&groupDate=' + formData.groupDate "
            :auto-upload="false"
            :disabled="formLoading"
            :headers="uploadHeaders"
            :limit="1"
            :on-error="submitFormError"
            :on-success="submitFormSuccess"
            accept=".zip"
            drag
          >
            <Icon icon="ep:upload" />
            <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
            <template #tip>
              <div class="el-upload__tip text-center">
<!--                <div class="el-upload__tip">-->
<!--                  <el-checkbox v-model="updateSupport" />-->
<!--                  是否更新已经存在的用户数据-->
<!--                </div>-->
                <span>仅允许导入zip格式文件。</span>
              </div>
            </template>
          </el-upload>
      </el-form-item>
    </el-form>
    <template #footer >
      <el-button :disabled="formLoading" type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>


<script lang="ts" setup>
import { useUpload } from "@/components/UploadFile/src/useUpload";
import { getAccessToken } from "@/utils/auth";
import {ref, reactive, defineExpose, defineEmits} from 'vue'
const importUrl =
  import.meta.env.VITE_BASE_URL + import.meta.env.VITE_API_URL + '/photosmanage/photo-group/import-package'
defineOptions({ name: 'imageImportForm' })
const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中
const uploadRef = ref()
const message = useMessage() // 消息弹窗
const updateSupport = ref(0) // 是否更新已经存在的用户数据
const fileList = ref([]) // 文件列表

const formData = ref({
  id: undefined,
  groupName: undefined,
  groupDate: undefined,
  groupDescription: undefined,
  uploadPhotos: undefined
})
const formRules = reactive({
  groupName: [{ required: true, message: '照片组名称不能为空', trigger: 'blur' }],

})
const formRef = ref() // 表单 Ref
/** 打开弹窗 */
const open = () => {
  dialogVisible.value = true
  updateSupport.value = 0
  fileList.value = []
  resetForm()
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗
//const {  httpRequest } = useUpload()

/** 图片上传成功 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitFormSuccess = (response: any) => {
  console.log('tj',response)
  if (response.code !== 0) {
    message.error(response.msg)
    formLoading.value = false
    return
  }
  formLoading.value = false
  dialogVisible.value = false
  // 发送操作成功的事件
  emit('success')
}

/** 上传错误提示 */
const submitFormError = (): void => {
  message.error('上传失败，请您重新上传！')
  formLoading.value = false
}
const uploadHeaders = ref() // 上传 Header 头

/** 提交表单 */

const submitForm = async () => {
  if (fileList.value.length == 0) {
    message.error('请上传文件')
    return
  }
  uploadHeaders.value = {
    Authorization: 'Bearer ' + getAccessToken(),
  }
  formLoading.value = true
  uploadRef.value!.submit()
  // 发送操作成功的事件
  emit('success')
}

/** 重置表单 */
const resetForm = async (): Promise<void> => {
  formData.value = {
    id: undefined,
    groupName: undefined,
    groupDate: undefined,
    groupDescription: undefined,
    uploadPhotos: undefined
  }
  // 重置上传状态和文件
  formLoading.value = false
  await nextTick()
  uploadRef.value?.clearFiles()
}
</script>
