<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="提醒人" prop="click">
        <el-input v-model="formData.click" placeholder="请输入提醒人" />
      </el-form-item>
      <el-form-item label="提醒人id" prop="clickid">
        <el-input v-model="formData.clickid" placeholder="请输入提醒人id" />
      </el-form-item>
      <el-form-item label="事项名称" prop="thingname">
        <el-input v-model="formData.thingname" placeholder="请输入事项名称" />
      </el-form-item>
      <el-form-item label="提醒项目说明" prop="things">
        <el-input v-model="formData.things" placeholder="请输入提醒项目说明" />
      </el-form-item>
      <el-form-item label="级别" prop="level">
        <el-input v-model="formData.level" placeholder="请输入级别" />
      </el-form-item>
      <el-form-item label="事项生成时间" prop="showname">
        <el-date-picker
          v-model="formData.showname"
          type="date"
          value-format="x"
          placeholder="选择事项生成时间"
        />
      </el-form-item>
      <el-form-item label="待办或者关注" prop="dborgz">
        <el-input v-model="formData.dborgz" placeholder="请输入待办或者关注" />
      </el-form-item>
      <el-form-item label="是否处理点击" prop="yesorno">
        <el-input v-model="formData.yesorno" placeholder="请输入是否处理点击" />
      </el-form-item>
      <el-form-item label="备注1" prop="attr1">
        <el-input v-model="formData.attr1" placeholder="请输入备注1" />
      </el-form-item>
      <el-form-item label="备注2" prop="attr2">
        <el-input v-model="formData.attr2" placeholder="请输入备注2" />
      </el-form-item>
      <el-form-item label="备注3" prop="attr3">
        <el-input v-model="formData.attr3" placeholder="请输入备注3" />
      </el-form-item>
      <el-form-item label="备注4" prop="attr4">
        <el-input v-model="formData.attr4" placeholder="请输入备注4" />
      </el-form-item>
      <el-form-item label="备注5" prop="attr5">
        <el-input v-model="formData.attr5" placeholder="请输入备注5" />
      </el-form-item>
      <el-form-item label="提醒周期" prop="clicktime">
        <el-date-picker
          v-model="formData.clicktime"
          type="date"
          value-format="x"
          placeholder="选择提醒周期"
        />
      </el-form-item>
      <el-form-item label="当前提醒时间" prop="nowtime">
        <el-date-picker
          v-model="formData.nowtime"
          type="date"
          value-format="x"
          placeholder="选择当前提醒时间"
        />
      </el-form-item>
      <el-form-item label="备注6" prop="attr6">
        <el-date-picker
          v-model="formData.attr6"
          type="date"
          value-format="x"
          placeholder="选择备注6"
        />
      </el-form-item>
      <el-form-item label="备注7" prop="attr7">
        <el-date-picker
          v-model="formData.attr7"
          type="date"
          value-format="x"
          placeholder="选择备注7"
        />
      </el-form-item>
      <el-form-item label="备注8" prop="attr8">
        <el-date-picker
          v-model="formData.attr8"
          type="date"
          value-format="x"
          placeholder="选择备注8"
        />
      </el-form-item>
      <el-form-item label="备注9" prop="attr9">
        <el-input v-model="formData.attr9" placeholder="请输入备注9" />
      </el-form-item>
      <el-form-item label="备注10" prop="attr10">
        <el-date-picker
          v-model="formData.attr10"
          type="date"
          value-format="x"
          placeholder="选择备注10"
        />
      </el-form-item>
      <el-form-item label="备注11" prop="attr11">
        <el-input v-model="formData.attr11" placeholder="请输入备注11" />
      </el-form-item>
      <el-form-item label="备注12" prop="attr12">
        <el-input v-model="formData.attr12" placeholder="请输入备注12" />
      </el-form-item>
      <el-form-item label="备注13" prop="attr13">
        <el-input v-model="formData.attr13" placeholder="请输入备注13" />
      </el-form-item>
      <el-form-item label="备注14" prop="attr14">
        <el-input v-model="formData.attr14" placeholder="请输入备注14" />
      </el-form-item>
      <el-form-item label="备注15" prop="attr15">
        <el-input v-model="formData.attr15" placeholder="请输入备注15" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import * as bdgzsomthingApi from '@/api/heli/bdgzsomthing'

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  click: undefined,
  clickid: undefined,
  thingname: undefined,
  things: undefined,
  level: undefined,
  showname: undefined,
  dborgz: undefined,
  yesorno: undefined,
  attr1: undefined,
  attr2: undefined,
  attr3: undefined,
  attr4: undefined,
  attr5: undefined,
  clicktime: undefined,
  nowtime: undefined,
  attr6: undefined,
  attr7: undefined,
  attr8: undefined,
  attr9: undefined,
  attr10: undefined,
  attr11: undefined,
  attr12: undefined,
  attr13: undefined,
  attr14: undefined,
  attr15: undefined,
})
const formRules = reactive({
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await bdgzsomthingApi.getbdgzsomthing(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as bdgzsomthingApi.bdgzsomthingVO
    if (formType.value === 'create') {
      await bdgzsomthingApi.createbdgzsomthing(data)
      message.success(t('common.createSuccess'))
    } else {
      await bdgzsomthingApi.updatebdgzsomthing(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    click: undefined,
    clickid: undefined,
    thingname: undefined,
    things: undefined,
    level: undefined,
    showname: undefined,
    dborgz: undefined,
    yesorno: undefined,
    attr1: undefined,
    attr2: undefined,
    attr3: undefined,
    attr4: undefined,
    attr5: undefined,
    clicktime: undefined,
    nowtime: undefined,
    attr6: undefined,
    attr7: undefined,
    attr8: undefined,
    attr9: undefined,
    attr10: undefined,
    attr11: undefined,
    attr12: undefined,
    attr13: undefined,
    attr14: undefined,
    attr15: undefined,
  }
  formRef.value?.resetFields()
}
</script>