<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible"  width="800px">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-row>
        <el-col :span="12">
          <el-form-item label="开机时间" prop="startTime">
            <el-date-picker
              v-model="formData.startTime"
              type="date"
              value-format="x"
              placeholder="选择开机时间"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="停机时间" prop="endTime">
            <el-date-picker
              v-model="formData.endTime"
              type="date"
              value-format="x"
              placeholder="选择停机时间"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="碎浆时间" prop="suijiangTime">
            <el-date-picker
              v-model="formData.suijiangTime"
              type="date"
              value-format="x"
              placeholder="选择碎浆时间"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="碎浆时间" prop="suijiangCostTime">
            <el-date-picker
              v-model="formData.suijiangCostTime"
              type="date"
              value-format="x"
              placeholder="选择碎浆时间"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="原料配比id" prop="ratioId">
            <el-input v-model="formData.ratioId" placeholder="请输入原料配比id" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="原料配比" prop="ratio">
            <el-input v-model="formData.ratio" placeholder="请输入原料配比" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="用水量kg" prop="waterUsage">
            <el-input v-model="formData.waterUsage" placeholder="请输入用水量kg" />
          </el-form-item>

        </el-col>
        <el-col :span="12">
          <el-form-item label="放浆浓度%" prop="densityOut">
            <el-input v-model="formData.densityOut" placeholder="请输入放浆浓度%" />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="12">
          <el-form-item label="加药量kg" prop="addItemUsage">
            <el-input v-model="formData.addItemUsage" placeholder="请输入加药量kg" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="出渣率%" prop="slagYield">
            <el-input v-model="formData.slagYield" placeholder="请输入出渣率%" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="浓度%" prop="density">
            <el-input v-model="formData.density" placeholder="请输入浓度%" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="备注" prop="remark">
            <el-input v-model="formData.remark" placeholder="请输入备注" />
          </el-form-item>
        </el-col>
      </el-row>


    </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 { RecordTemplateApi } from '@/api/mes/record'
import { getBoolDictOptions, DICT_TYPE } from '@/utils/dict'
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,
  suijiangTime: undefined,
  suijiangCostTime: undefined,
  ratioId: undefined,
  ratio: undefined,
  waterUsage: undefined,
  densityOut: undefined,
  addItemUsage: undefined,
  slagYield: undefined,
  density: undefined,
  remark: undefined,
  isEnable: undefined,
})
const formRules = reactive({
  recordId: [{ required: true, message: '组织id不能为空', trigger: 'blur' }],
  startTime: [{ required: true, message: '开机时间不能为空', trigger: 'blur' }],
  endTime: [{ required: true, message: '停机时间不能为空', trigger: 'blur' }],
  suijiangTime: [{ required: true, message: '碎浆时间不能为空', trigger: 'blur' }],
  isEnable: [{ required: true, message: '是否启用不能为空', trigger: 'blur' }],
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
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.getRecordSuijiang(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
    if (formType.value === 'create') {
      await RecordTemplateApi.createRecordSuijiang(data)
      message.success(t('common.createSuccess'))
    } else {
      await RecordTemplateApi.updateRecordSuijiang(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,
    suijiangTime: undefined,
    suijiangCostTime: undefined,
    ratioId: undefined,
    ratio: undefined,
    waterUsage: undefined,
    densityOut: undefined,
    addItemUsage: undefined,
    slagYield: undefined,
    density: undefined,
    remark: undefined,
    isEnable: undefined,
  }
  formRef.value?.resetFields()
}
</script>
