<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="开机时间(S)" prop="machineStartTime">
            <el-input v-model="formData.machineStartTime" placeholder="开机时间" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="停机时间(S)" prop="machineEndTime">
            <el-input v-model="formData.machineEndTime" placeholder="请输入停机时间" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="加湿时间(S)" prop="wetTime">
            <el-input v-model="formData.wetTime" placeholder="请输入加湿时间" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="产品" prop="productId">
            <el-select v-model="formData.productId"  clearable filterable placeholder="请选择" >
              <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="产品进入水分" prop="weightIn">
            <el-input v-model="formData.weightIn" placeholder="请输入产品进入水分" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="产品取出水分" prop="weightOut">
            <el-input v-model="formData.weightOut" placeholder="请输入产品取出水分" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="单重(g)" prop="productWeight">
            <el-input v-model="formData.productWeight" placeholder="请输入单重(g)" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="产量" prop="productNumber">
            <el-input v-model="formData.productNumber" placeholder="请输入产量" />
          </el-form-item>
        </el-col>
      </el-row>

      <el-form-item label="备注" prop="remark">
        <el-input v-model="formData.remark" placeholder="请输入备注" />
      </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 { 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,
  machineStartTime: undefined,
  machineEndTime: undefined,
  wetTime: undefined,
  productId: undefined,
  weightIn: undefined,
  weightOut: undefined,
  productNumber: undefined,
  productWeight: 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' }],
  machineStartTime: [{ required: true, message: '开机时间(S)不能为空', trigger: 'blur' }],
  machineEndTime: [{ required: true, message: '停机时间(S)不能为空', trigger: 'blur' }],
  wetTime: [{ required: true, message: '加湿时间(S)不能为空', trigger: 'blur' }],
  productId: [{ required: true, message: '产品id不能为空', trigger: 'blur' }],
  productNumber: [{ required: true, message: '产量不能为空', trigger: 'blur' }],
  isEnable: [{ required: true, message: '是否启用不能为空', 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.getRecordJiashi(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.createRecordJiashi(data)
      message.success(t('common.createSuccess'))
    } else {
      await RecordTemplateApi.updateRecordJiashi(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,
    machineStartTime: undefined,
    machineEndTime: undefined,
    wetTime: undefined,
    productId: undefined,
    weightIn: undefined,
    weightOut: undefined,
    productNumber: undefined,
    productWeight: undefined,
    remark: undefined,
    isEnable: undefined
  }
  formRef.value?.resetFields()
}
</script>
