<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="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-col :span="12">
          <el-form-item label="外观" prop="appearance">
            <el-input v-model="formData.appearance" 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="waterproof">
            <el-input v-model="formData.waterproof" placeholder="请输入防水" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="合格品量" prop="qualifiedNumber">
            <el-input v-model="formData.qualifiedNumber" placeholder="请输入合格品量" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="废品量" prop="unqualifiedNumber">
            <el-input v-model="formData.unqualifiedNumber" placeholder="请输入废品量" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="总产量" prop="totalNumber">
            <el-input v-model="formData.totalNumber" placeholder="请输入总产量" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="合格率%" prop="qualifiedRate">
            <el-input v-model="formData.qualifiedRate" placeholder="请输入合格率%" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="废品原因" prop="unqualifiedReason">
            <el-input v-model="formData.unqualifiedReason" 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 { 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,
  productId: undefined,
  appearance: undefined,
  productWeight: undefined,
  waterproof: undefined,
  qualifiedNumber: undefined,
  unqualifiedNumber: undefined,
  totalNumber: undefined,
  qualifiedRate: undefined,
  unqualifiedReason: undefined,
  remark: undefined,
  isEnable: undefined
})
const formRules = reactive({
  recordId: [{ required: true, message: '记录id不能为空', trigger: 'blur' }],
  qualifiedNumber: [{ required: true, message: '合格品量不能为空', trigger: 'blur' }],
  unqualifiedNumber: [{ required: true, message: '废品量不能为空', trigger: 'blur' }],
  totalNumber: [{ 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.getRecordPinjian(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.createRecordPinjian(data)
      message.success(t('common.createSuccess'))
    } else {
      await RecordTemplateApi.updateRecordPinjian(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    recordId: undefined,
    productId: undefined,
    appearance: undefined,
    productWeight: undefined,
    waterproof: undefined,
    qualifiedNumber: undefined,
    unqualifiedNumber: undefined,
    totalNumber: undefined,
    qualifiedRate: undefined,
    unqualifiedReason: undefined,
    remark: undefined,
    isEnable: undefined
  }
  formRef.value?.resetFields()
}
</script>
