<template>
  <el-dialog
    v-model="dialogVisible"
    :close-on-click-modal="false"
    :title="parameter.title"
    :destroy-on-close="true"
    :width="parameter.width"
    :top="parameter.top"
    draggable
  >
    <ProFrom ref="proFormRef" :items-options="parameter.itemsOptions" :form-options="_options" :model="parameter.model">
      <template #modelAddress="{}">
        <FileUpload :file-size="4096" :file-type="['pt']" v-on:update:modelValue="setModelAddr"/>
      </template>
    </ProFrom>
    <template #footer>
      <span class="dialog-footer">
        <el-button type="primary" v-if="parameter.isEdit" :loading="butLoading" @click="handleSubmit">确认</el-button>
        <el-button @click="handleCancel">取消</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup lang="ts" name="FormDialog">
import {ref, ComputedRef, computed, reactive} from 'vue'
import ProFrom from '@/components/ProForm/index.vue'
import { ElMessage } from 'element-plus'
import FileUpload from '@/components/Upload/File.vue'

interface EmitEvent {
  (e: 'update'): void
}
const emits = defineEmits<EmitEvent>()

const videoUploadRef = ref<InstanceType<typeof FileUpload> | null>(null)

export interface FormParameterProps {
  title: string // 标题
  width?: number // 弹框宽度
  labelWidth?: number // label宽度
  api?: (params: any) => Promise<any> // 表单提交api
  isEdit?: boolean // 是否编辑
  top?: string // 离顶部距离
  formOptions?: ProForm.FormOptions // 表单配置
  itemsOptions: ProForm.ItemsOptions[] // 动态表单字段配置
  model?: Record<ProForm.FormItem['prop'], ProForm.FormItem['value']> // 表单数据对象
  getTableList?: () => void // 获取表格数据的Api
}

// dialog状态
const dialogVisible = ref(false)
const butLoading = ref(false)
// 父组件传过来的参数
const parameter = ref<FormParameterProps>({
  title: '',
  width: 500,
  top: '10vh',
  itemsOptions: [],
  formOptions: {},
  isEdit: true
})
const _options: ComputedRef<ProForm.FormOptions> = computed(() => {
  const form = {
    labelWidth: 120,
    hasFooter: false,
    disabled: false
  }
  return Object.assign(form, parameter.value.formOptions)
})

const modelAddr = ref('')
const setModelAddr = res => {
  console.log('modelAddr', res)
  modelAddr.value = res
}

const proFormRef = ref<InstanceType<typeof ProFrom> | null>(null)
// 表单提交校验
const handleSubmit = () => {
  const formEl = proFormRef.value?.proFormRef
  const formModel = proFormRef.value?.formModel
  formModel.modelAddress = modelAddr.value
  console.log('formModel', formModel)

  butLoading.value = true
  if (!formEl) return
  formEl.validate(valid => {
    if (valid) {
      parameter.value.api!({ ...formModel, ...parameter.value.model }).then(res => {
        if (res.code == 200) {
          proFormRef.value?.resetForm(formEl)
          ElMessage.success('操作成功')
          emits('update')
          dialogVisible.value = false
          parameter.value.getTableList && parameter.value.getTableList()
        } else {
          console.log('message', res.message)
        }
      })
      butLoading.value = false
    }
    butLoading.value = false
  })
}

// 取消按钮，重置表单，关闭弹框
const handleCancel = () => {
  console.log(parameter.value.model)
  const formEl = proFormRef.value?.proFormRef
  if (!formEl) return
  if (parameter.value.model?.url) {
    ElMessage.info('请先删除已经上传的图片')
    return
  }
  proFormRef.value?.resetForm(formEl)
  butLoading.value = false
  dialogVisible.value = false
}

// 接收父组件参数
const openDialog = (params: FormParameterProps) => {
  parameter.value = { ...parameter.value, ...params }
  _options.value.disabled = !parameter.value.isEdit
  butLoading.value = false
  dialogVisible.value = true
}

defineExpose({
  openDialog
})
</script>

<style scoped>
.upload-video-box {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 200px;
  margin-top: 20px;
  border: 1px dashed #cccccc;
  :deep(.upload-file-uploader) {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    height: 100%;
    font-size: 18px;
    text-align: center;
    cursor: pointer;
  }
}
</style>
