<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
    <el-form-item v-show="false" :label="t('ProductionPlan.Plan.baogongDialogPlanLabel')" prop="planId">
        <el-input v-model="formData.planId" :placeholder="t('ProductionPlan.Plan.baogongDialogPlanPlaceholder')" />
      </el-form-item>
      <el-form-item :label="t('ProductionPlan.Plan.baogongDialogQualifiedNumberLabel')" prop="num">
        <el-input-number v-model="formData.num" :placeholder="t('ProductionPlan.Plan.baogongDialogQualifiedNumberPlaceholder')" class="!w-240px"/>
      </el-form-item>
      <el-form-item :label="t('ProductionPlan.Plan.baogongDialogUnqualifiedNumberLabel')" prop="noPassNum">
        <el-input-number v-model="formData.noPassNum" :placeholder="t('ProductionPlan.Plan.baogongDialogUnqualifiedNumberPlaceholder')" class="!w-240px"/>
      </el-form-item>
      <el-form-item :label="t('ProductionPlan.Plan.baogongDialogReasonLabel')" prop="remark">
        <el-input v-model="formData.remark" :placeholder="t('ProductionPlan.Plan.baogongDialogReasonPlaceholder')"/>
      </el-form-item>
    </el-form>

    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">{{ t('ProductionPlan.Plan.baogongDialogSubmitButtonText') }}</el-button>
      <el-button @click="dialogVisible = false">{{ t('ProductionPlan.Plan.baogongDialogCancelButtonText') }}</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">

import { BaogongRecordApi, BaogongRecordVO } from '@/api/mes/baogongrecord'

/** 生产领料 表单 */
defineOptions({ name: 'Baogong' })

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


const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formData = ref({
  id: undefined,
  planId: undefined,
  num: undefined,
  baogongTime: undefined,
  noPassNum: undefined,
  remark: undefined,
})
const formRules = reactive({
  num: [{ required: true, message: t('ProductionPlan.Plan.validatorBaogongQualifiedRequired'), trigger: 'blur' }],
  noPassNum: [{ required: true, message: t('ProductionPlan.Plan.validatorBaogongUnqualifiedRequired'), trigger: 'blur' }],
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (planCode: string, planId: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('ProductionPlan.Plan.baogongDialogTitle', { code: planCode })
  resetForm()
  formLoading.value = true
  try {
   formData.value.planId = planId
  } 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 BaogongRecordVO
    await BaogongRecordApi.createBaogongRecord(data)
    message.success(t('ProductionPlan.Plan.baogongSuccessMessage'))
    dialogVisible.value = false
    emit('success')
  } finally {
    formLoading.value = false
  }
}



/** 重置表单 */
const resetForm = () => {
  formData.value = {
    num: undefined,
    planId: undefined,
  }
  formRef.value?.resetFields()
}

</script>
