<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="入库id" prop="storageId">
        <el-input v-model="formData.storageId" placeholder="请输入入库id" />
      </el-form-item>
      <el-form-item label="组织部门id" prop="deptId">
        <el-input v-model="formData.deptId" placeholder="请输入组织部门id" />
      </el-form-item>
      <el-form-item label="工厂(SAP) 专业公司" prop="factory">
        <el-input v-model="formData.factory" placeholder="请输入工厂(SAP) 专业公司" />
      </el-form-item>
      <el-form-item label="库存地点 - SAP 部门 项目部级" prop="projectDepartment">
        <el-input v-model="formData.projectDepartment" placeholder="请输入库存地点 - SAP 部门 项目部级" />
      </el-form-item>
      <el-form-item label="成本中心(SAP) 库存地点 小队" prop="costCenter">
        <el-input v-model="formData.costCenter" placeholder="请输入成本中心(SAP) 库存地点 小队" />
      </el-form-item>
      <el-form-item label="物料编码" prop="materialCode">
        <el-input v-model="formData.materialCode" placeholder="请输入物料编码" />
      </el-form-item>
      <el-form-item label="物料名称" prop="materialName">
        <el-input v-model="formData.materialName" placeholder="请输入物料名称" />
      </el-form-item>
      <el-form-item label="物料组" prop="materialGroupName">
        <el-input v-model="formData.materialGroupName" placeholder="请输入物料组" />
      </el-form-item>
      <el-form-item label="物料组id" prop="materialGroupId">
        <el-input v-model="formData.materialGroupId" placeholder="请输入物料组id" />
      </el-form-item>
      <el-form-item label="出库数量" prop="quantity">
        <el-input v-model="formData.quantity" placeholder="请输入出库数量" />
      </el-form-item>
      <el-form-item label="单价" prop="unitPrice">
        <el-input v-model="formData.unitPrice" placeholder="请输入单价" />
      </el-form-item>
      <el-form-item label="单位" prop="unit">
        <el-input v-model="formData.unit" placeholder="请输入单位" />
      </el-form-item>
      <el-form-item label="使用方向 对应BOM节点" prop="bomNodeId">
        <el-input v-model="formData.bomNodeId" placeholder="请输入使用方向 对应BOM节点" />
      </el-form-item>
      <el-form-item label="使用类型(1维修 2保养 3临时)" prop="type">
        <el-select v-model="formData.type" placeholder="请选择使用类型(1维修 2保养 3临时)">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="出库时间" prop="deliveryTime">
        <el-date-picker
          v-model="formData.deliveryTime"
          type="date"
          value-format="x"
          placeholder="选择出库时间"
        />
      </el-form-item>
      <el-form-item label="出库原因" prop="reason">
        <el-input v-model="formData.reason" placeholder="请输入出库原因" />
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input v-model="formData.sort" placeholder="请输入排序" />
      </el-form-item>
      <el-form-item label="状态 0启用  1停用" prop="status">
        <el-radio-group v-model="formData.status">
          <el-radio value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="formData.remark" placeholder="请输入备注" />
      </el-form-item>
      <el-form-item label="流程实例id" prop="processInstanceId">
        <el-input v-model="formData.processInstanceId" placeholder="请输入流程实例id" />
      </el-form-item>
      <el-form-item label="审批状态 未提交、审批中、审批通过、审批不通过、已取消" prop="auditStatus">
        <el-radio-group v-model="formData.auditStatus">
          <el-radio value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </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 { IotOutboundApi, IotOutboundVO } from '@/api/pms/iotoutbound'

/** PMS 出库 表单 */
defineOptions({ name: 'IotOutboundForm' })

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,
  storageId: undefined,
  deptId: undefined,
  factory: undefined,
  projectDepartment: undefined,
  costCenter: undefined,
  materialCode: undefined,
  materialName: undefined,
  materialGroupName: undefined,
  materialGroupId: undefined,
  quantity: undefined,
  unitPrice: undefined,
  unit: undefined,
  bomNodeId: undefined,
  type: undefined,
  deliveryTime: undefined,
  reason: undefined,
  sort: undefined,
  status: undefined,
  remark: undefined,
  processInstanceId: undefined,
  auditStatus: undefined,
})
const formRules = reactive({
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await IotOutboundApi.getIotOutbound(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 as unknown as IotOutboundVO
    if (formType.value === 'create') {
      await IotOutboundApi.createIotOutbound(data)
      message.success(t('common.createSuccess'))
    } else {
      await IotOutboundApi.updateIotOutbound(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    storageId: undefined,
    deptId: undefined,
    factory: undefined,
    projectDepartment: undefined,
    costCenter: undefined,
    materialCode: undefined,
    materialName: undefined,
    materialGroupName: undefined,
    materialGroupId: undefined,
    quantity: undefined,
    unitPrice: undefined,
    unit: undefined,
    bomNodeId: undefined,
    type: undefined,
    deliveryTime: undefined,
    reason: undefined,
    sort: undefined,
    status: undefined,
    remark: undefined,
    processInstanceId: undefined,
    auditStatus: undefined,
  }
  formRef.value?.resetFields()
}
</script>