<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible"  width="800px">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="auto"
      v-loading="formLoading"
    >
      <el-row>
        <el-col :span="12">
          <el-form-item :label="t('ProductionPlan.FormingRecord.detailDialogStartTimeLabel')" prop="startTime">
            <el-date-picker
              v-model="formData.startTime"
              type="date"
              value-format="x"
              :placeholder="t('ProductionPlan.FormingRecord.detailDialogStartTimePlaceholder')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="t('ProductionPlan.FormingRecord.detailDialogEndTimeLabel')" prop="endTime">
            <el-date-picker
              v-model="formData.endTime"
              type="date"
              value-format="x"
              :placeholder="t('ProductionPlan.FormingRecord.detailDialogEndTimePlaceholder')"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item :label="t('ProductionPlan.FormingRecord.detailDialogXijiangLabel')" prop="xijiang">
            <el-input v-model="formData.xijiang" :placeholder="t('ProductionPlan.FormingRecord.detailDialogXijiangPlaceholder')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="t('ProductionPlan.FormingRecord.detailDialogTuoshuiLabel')" prop="tuoshui">
            <el-input v-model="formData.tuoshui" :placeholder="t('ProductionPlan.FormingRecord.detailDialogTuoshuiPlaceholder')" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item :label="t('ProductionPlan.FormingRecord.detailDialogXimuLabel')" prop="ximu">
            <el-input v-model="formData.ximu" :placeholder="t('ProductionPlan.FormingRecord.detailDialogXimuPlaceholder')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="t('ProductionPlan.FormingRecord.detailDialogProductLabel')" prop="productId">
            <el-select v-model="formData.productId"  clearable filterable :placeholder="t('ProductionPlan.FormingRecord.detailDialogProductPlaceholder')" >
              <el-option
                v-for="item in productList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="12">
          <el-form-item :label="t('ProductionPlan.FormingRecord.detailDialogAppearanceLabel')" prop="appearance">
            <el-input v-model="formData.appearance" :placeholder="t('ProductionPlan.FormingRecord.detailDialogAppearancePlaceholder')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="t('ProductionPlan.FormingRecord.detailDialogProductWeightLabel')" prop="productWeight">
            <el-input v-model="formData.productWeight" :placeholder="t('ProductionPlan.FormingRecord.detailDialogProductWeightPlaceholder')" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item :label="t('ProductionPlan.FormingRecord.detailDialogRemarkLabel')" prop="remark">
        <el-input v-model="formData.remark" :placeholder="t('ProductionPlan.FormingRecord.detailDialogRemarkPlaceholder')" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">{{ t('ProductionPlan.FormingRecord.dialogSubmitButtonText') }}</el-button>
      <el-button @click="dialogVisible = false">{{ t('ProductionPlan.FormingRecord.dialogCancelButtonText') }}</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { getBoolDictOptions, DICT_TYPE } from '@/utils/dict'
import { RecordTemplateApi } from '@/api/mes/record'
import {ProductVO} from "@/api/erp/product/product";
import {PlanApi} from "@/api/mes/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,
  recordId: undefined,
  startTime: undefined,
  endTime: undefined,
  xijiang: undefined,
  tuoshui: undefined,
  ximu: undefined,
  productId: undefined,
  appearance: undefined,
  productWeight: undefined,
  remark: undefined,
  isEnable: undefined,
})
const formRules = reactive({
  recordId: [{ required: true, message: t('ProductionPlan.FormingRecord.detailValidatorRecordIdRequired'), trigger: 'blur' }],
  xijiang: [{ required: true, message: t('ProductionPlan.FormingRecord.detailValidatorXijiangRequired'), trigger: 'blur' }],
  tuoshui: [{ required: true, message: t('ProductionPlan.FormingRecord.detailValidatorTuoshuiRequired'), trigger: 'blur' }],
  ximu: [{ required: true, message: t('ProductionPlan.FormingRecord.detailValidatorXimuRequired'), trigger: 'blur' }],
  productId: [{ required: true, message: t('ProductionPlan.FormingRecord.detailValidatorProductRequired'), trigger: 'blur' }],
  appearance: [{ required: true, message: t('ProductionPlan.FormingRecord.detailValidatorAppearanceRequired'), trigger: 'blur' }],
  productWeight: [{ required: true, message: t('ProductionPlan.FormingRecord.detailValidatorProductWeightRequired'), trigger: 'blur' }],
})
const formRef = ref() // 表单 Ref
const productList = ref<ProductVO[]>([]) // 产品列表
/** 打开弹窗 */
const open = async (type: string, id?: number, recordId: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  formData.value.recordId = recordId
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await RecordTemplateApi.getRecordChengxing(id)
    } finally {
      formLoading.value = false
    }
  }
  productList.value = await PlanApi.getStartAndFinishPlanProduct()
  if(productList.value.length ==1)
    formData.value.productId = productList.value[0].id
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

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

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    recordId: undefined,
    startTime: undefined,
    endTime: undefined,
    xijiang: undefined,
    tuoshui: undefined,
    ximu: undefined,
    productId: undefined,
    appearance: undefined,
    productWeight: undefined,
    remark: undefined,
    isEnable: undefined,
  }
  formRef.value?.resetFields()
}
</script>
