<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="planNo">
        <el-input v-model="formData.planNo" placeholder="请输入计划单号" />
      </el-form-item>
      <el-form-item label="项目id" prop="projectId">
        <el-input v-model="formData.projectId" placeholder="请输入项目id" />
      </el-form-item>
      <el-form-item label="项目负责人" prop="projectOwner">
        <el-input v-model="formData.projectOwner" placeholder="请输入项目负责人" />
      </el-form-item>
      <el-form-item label="是否需要工艺:1表示需要，0表示不需要" prop="hasCraft">
        <el-radio-group v-model="formData.hasCraft">
          <el-radio
            v-for="dict in getBoolDictOptions(DICT_TYPE.HELI_COMMON_IS_OR_NOT)"
            :key="dict.value"
            :label="dict.value"
          >
            {{ dict.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="工艺负责人" prop="craftOwner">
        <el-input v-model="formData.craftOwner" placeholder="请输入工艺负责人" />
      </el-form-item>
      <el-form-item label="工艺开始日期" prop="craftStartDate">
        <el-date-picker
          v-model="formData.craftStartDate"
          type="date"
          value-format="x"
          placeholder="选择工艺开始日期"
        />
      </el-form-item>
      <el-form-item label="工艺结束日期" prop="craftEndDate">
        <el-date-picker
          v-model="formData.craftEndDate"
          type="date"
          value-format="x"
          placeholder="选择工艺结束日期"
        />
      </el-form-item>
      <el-form-item label="工艺流程数组信息[{'id':1,'sdate':'','edate':'','owner':'','description':''}]" prop="craftContent">
        <Editor v-model="formData.craftContent" height="150px" />
      </el-form-item>
      <el-form-item label="编辑人" prop="editor">
        <el-input v-model="formData.editor" placeholder="请输入编辑人" />
      </el-form-item>
      <el-form-item label="编辑日期" prop="editorDate">
        <el-date-picker
          v-model="formData.editorDate"
          type="date"
          value-format="x"
          placeholder="选择编辑日期"
        />
      </el-form-item>
      <el-form-item label="审核人" prop="auditor">
        <el-input v-model="formData.auditor" placeholder="请输入审核人" />
      </el-form-item>
      <el-form-item label="审核日期" prop="auditDate">
        <el-date-picker
          v-model="formData.auditDate"
          type="date"
          value-format="x"
          placeholder="选择审核日期"
        />
      </el-form-item>
      <el-form-item label="批准人" prop="approver">
        <el-input v-model="formData.approver" placeholder="请输入批准人" />
      </el-form-item>
      <el-form-item label="批准日期" prop="approveDate">
        <el-date-picker
          v-model="formData.approveDate"
          type="date"
          value-format="x"
          placeholder="选择批准日期"
        />
      </el-form-item>
      <el-form-item label="备注" prop="description">
        <el-input v-model="formData.description" type="textarea" placeholder="请输入备注" />
      </el-form-item>
      <el-form-item label="状态,1未开始，2生产中，3已完成，4已终止 ，默认是1" prop="status">
        <el-radio-group v-model="formData.status">
          <el-radio
            v-for="dict in getIntDictOptions(DICT_TYPE.HELI_PROJECT_PLAN_STATUS)"
            :key="dict.value"
            :label="dict.value"
          >
            {{ dict.label }}
          </el-radio>
        </el-radio-group>
      </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 { getIntDictOptions, getBoolDictOptions, DICT_TYPE } from '@/utils/dict'
import * as PlanApi from '@/api/heli/plan'

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,
  planNo: undefined,
  projectId: undefined,
  projectOwner: undefined,
  hasCraft: undefined,
  craftOwner: undefined,
  craftStartDate: undefined,
  craftEndDate: undefined,
  craftContent: undefined,
  editor: undefined,
  editorDate: undefined,
  auditor: undefined,
  auditDate: undefined,
  approver: undefined,
  approveDate: undefined,
  description: undefined,
  status: undefined,
})
const formRules = reactive({
  planNo: [{ required: true, message: '计划单号不能为空', trigger: 'blur' }],
  projectId: [{ required: true, message: '项目id不能为空', trigger: 'blur' }],
})
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 PlanApi.getPlan(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 PlanApi.PlanVO
    if (formType.value === 'create') {
      await PlanApi.createPlan(data)
      message.success(t('common.createSuccess'))
    } else {
      await PlanApi.updatePlan(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    planNo: undefined,
    projectId: undefined,
    projectOwner: undefined,
    hasCraft: undefined,
    craftOwner: undefined,
    craftStartDate: undefined,
    craftEndDate: undefined,
    craftContent: undefined,
    editor: undefined,
    editorDate: undefined,
    auditor: undefined,
    auditDate: undefined,
    approver: undefined,
    approveDate: undefined,
    description: undefined,
    status: undefined,
  }
  formRef.value?.resetFields()
}
</script>