<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="outsourceNo">
        <el-input v-model="formData.outsourceNo" placeholder="请输入委外验收单号，唯一" />
      </el-form-item>
      <el-form-item label="采购id" prop="purchaseId">
        <el-input v-model="formData.purchaseId" placeholder="请输入采购id" />
      </el-form-item>
      <el-form-item label="供应商id" prop="supplierId">
        <el-input v-model="formData.supplierId" placeholder="请输入供应商id" />
      </el-form-item>
      <el-form-item label="采购物类型，1物料，2加工件" prop="goodsType">
        <el-radio-group v-model="formData.goodsType">
          <el-radio
            v-for="dict in getBoolDictOptions(DICT_TYPE.HELI_PROJECT_PURCHASE_GOODS_TYPE)"
            :key="dict.value"
            :label="dict.value"
          >
            {{ dict.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="是否自动入库 0 否 1 是" prop="autoStockIn">
        <el-radio-group v-model="formData.autoStockIn">
          <el-radio label="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="是否自动出库 0 否 1 是" prop="autoStockOut">
        <el-radio-group v-model="formData.autoStockOut">
          <el-radio label="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="送检人" prop="deliver">
        <el-input v-model="formData.deliver" placeholder="请输入送检人" />
      </el-form-item>
      <el-form-item label="收检人" prop="receiver">
        <el-input v-model="formData.receiver" placeholder="请输入收检人" />
      </el-form-item>
      <el-form-item label="到货日期" prop="arrivalDate">
        <el-date-picker
          v-model="formData.arrivalDate"
          type="date"
          value-format="x"
          placeholder="选择到货日期"
        />
      </el-form-item>
      <el-form-item label="检验日期" prop="inspectionDate">
        <el-date-picker
          v-model="formData.inspectionDate"
          type="date"
          value-format="x"
          placeholder="选择检验日期"
        />
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="formData.remark" placeholder="请输入备注" />
      </el-form-item>
      <el-form-item label="单据状态（1已保存/2已提交）" prop="status">
        <el-radio-group v-model="formData.status">
          <el-radio
            v-for="dict in getBoolDictOptions(DICT_TYPE.HELI_OUTSOURCE_STOCK_STATUS)"
            :key="dict.value"
            :label="dict.value"
          >
            {{ dict.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="提交人" prop="submitter">
        <el-input v-model="formData.submitter" placeholder="请输入提交人" />
      </el-form-item>
      <el-form-item label="提交时间" prop="submitTime">
        <el-date-picker
          v-model="formData.submitTime"
          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 { getBoolDictOptions, DICT_TYPE } from '@/utils/dict'
import * as OutsourceStockApi from '@/api/heli/outsourcestock'

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,
  outsourceNo: undefined,
  purchaseId: undefined,
  supplierId: undefined,
  goodsType: undefined,
  autoStockIn: undefined,
  autoStockOut: undefined,
  deliver: undefined,
  receiver: undefined,
  arrivalDate: undefined,
  inspectionDate: undefined,
  remark: undefined,
  status: undefined,
  submitter: undefined,
  submitTime: undefined,
})
const formRules = reactive({
  outsourceNo: [{ required: true, message: '委外验收单号，唯一不能为空', trigger: 'blur' }],
  purchaseId: [{ required: true, message: '采购id不能为空', trigger: 'blur' }],
  goodsType: [{ required: true, message: '采购物类型，1物料，2加工件不能为空', trigger: 'blur' }],
  autoStockIn: [{ required: true, message: '是否自动入库 0 否 1 是不能为空', trigger: 'blur' }],
  autoStockOut: [{ required: true, message: '是否自动出库 0 否 1 是不能为空', trigger: 'blur' }],
  receiver: [{ required: true, message: '收检人不能为空', trigger: 'blur' }],
  arrivalDate: [{ required: true, message: '到货日期不能为空', trigger: 'blur' }],
  inspectionDate: [{ required: true, message: '检验日期不能为空', trigger: 'blur' }],
  status: [{ required: true, message: '单据状态（1已保存/2已提交）不能为空', trigger: 'blur' }],
})
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 OutsourceStockApi.getOutsourceStock(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 OutsourceStockApi.OutsourceStockVO
    if (formType.value === 'create') {
      await OutsourceStockApi.createOutsourceStock(data)
      message.success(t('common.createSuccess'))
    } else {
      await OutsourceStockApi.updateOutsourceStock(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    outsourceNo: undefined,
    purchaseId: undefined,
    supplierId: undefined,
    goodsType: undefined,
    autoStockIn: undefined,
    autoStockOut: undefined,
    deliver: undefined,
    receiver: undefined,
    arrivalDate: undefined,
    inspectionDate: undefined,
    remark: undefined,
    status: undefined,
    submitter: undefined,
    submitTime: undefined,
  }
  formRef.value?.resetFields()
}
</script>