<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="产品" 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-form-item label="单位" prop="unitId">
        <el-select
          v-model="formData.unitId"
          clearable
          filterable
          placeholder="请选择产品"
        >
          <el-option
            v-for="item in unitList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
       <el-form-item label="数量" prop="number">
         <el-input-number
           v-model="formData.number"
           :min="0"
           class="!w-1/1"
           placeholder="请输入数量"
         />
      </el-form-item>
      <el-form-item label="每包/个" prop="packageSize">
        <el-input-number
          v-model="formData.packageSize"
          :min="0"
          class="!w-1/1"
          placeholder="请输入打包要求(每包/个)"
        />
      </el-form-item>
      <el-form-item label="项目名称" prop="projectName">
        <el-input v-model="formData.projectName" placeholder="请输入项目名称" />
      </el-form-item>
      <el-form-item label="技术要求" prop="techRequirements">
        <el-input v-model="formData.techRequirements" placeholder="请输入技术要求" />
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="formData.remark" placeholder="请输入备注" />
      </el-form-item>
      <el-form-item label="打包数量/包" prop="packageNumber">
        <el-input-number
          v-model="formData.packageNumber"
          :min="0"
          class="!w-1/1"
          placeholder="请输入数量"
        />
      </el-form-item>
      <el-form-item label="条码" prop="barCode">
        <el-input v-model="formData.barCode" placeholder="请输入条码" />
      </el-form-item>
      <el-form-item label="附件" prop="attachment">
        <UploadFile v-model="formData.attachment" :limit="1"/>
      </el-form-item>

      <el-form-item label="工厂完成" prop="finishDate">
        <el-date-picker
          v-model="formData.finishDate"
          type="date"
          value-format="x"
          placeholder="选择工厂完成日期"
        />
      </el-form-item>
      <el-form-item label="装柜日期" prop="boxingDate">
        <el-date-picker
          v-model="formData.boxingDate"
          type="date"
          value-format="x"
          placeholder="选择装柜日期"
        />
      </el-form-item>
      <el-form-item label="到达日期" prop="arriveDate">
        <el-date-picker
          v-model="formData.arriveDate"
          type="date"
          value-format="x"
          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 { TaskApi } from '@/api/mes/task'
import { ProductUnitApi, ProductUnitVO } from '@/api/erp/product/unit'
import {ProductApi, ProductVO} from "@/api/erp/product/product";

const unitList = ref<ProductUnitVO[]>([]) // 产品单位列表
const productList = ref<ProductVO[]>([]) // 产品列表
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,
  productId: undefined,
  unitId: undefined,
  taskId: undefined,
  number: undefined,
  packageSize: undefined,
  projectName: undefined,
  techRequirements: undefined,
  remark: undefined,
  packageNumber: undefined,
  finishDate: undefined,
  boxingDate: undefined,
  arriveDate: undefined,
})
const formRules = reactive({
  productId: [{ required: true, message: '产品ID不能为空', trigger: 'blur' }],
  unitId: [{ required: true, message: '单位ID不能为空', trigger: 'blur' }],
  taskId: [{ required: true, message: 'task ID不能为空', trigger: 'blur' }],
  number: [{ required: true, message: '数量不能为空', trigger: 'blur' }],
  packageSize: [{ required: true, message: '打包要求(每包/个)不能为空', trigger: 'blur' }]
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number, taskId: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  formData.value.taskId = taskId
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await TaskApi.getTaskDetail(id)
    } finally {
      formLoading.value = false
    }
  }
  // 产品单位
  unitList.value = await ProductUnitApi.getProductUnitSimpleList()
  productList.value = await ProductApi.getMesProductSimpleList()
}
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 TaskApi.createTaskDetail(data)
      message.success(t('common.createSuccess'))
    } else {
      await TaskApi.updateTaskDetail(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    productId: undefined,
    unitId: undefined,
    taskId: undefined,
    number: undefined,
    packageSize: undefined,
    projectName: undefined,
    techRequirements: undefined,
    remark: undefined
  }
  formRef.value?.resetFields()
}
</script>
