<script setup lang="ts">
import type { FormInstance } from 'antdv-next'
import apiBizFailure from '@/api/modules/bizFailure'

export interface Props { id?: number | string }
const props = withDefaults(defineProps<Props>(), { id: '' })

const loading = ref(false)
const formRef = useTemplateRef<FormInstance>('formRef')
const form = ref({
  id: props.id as number | string,
  target_type: undefined as string | undefined, target_id: undefined as number | undefined,
  failure_name: '', failure_description: '', failure_date: '',
  status: 'open', reported_by: undefined as number | undefined,
  resolved_by: undefined as number | undefined, notes: '',
})

const rules = {
  target_type: [{ required: true, message: '请选择关联类型', trigger: 'change' }],
  target_id: [{ required: true, message: '请输入关联目标', trigger: 'blur' }],
  failure_name: [{ required: true, message: '请输入故障名称', trigger: 'blur' }],
}

onMounted(() => { if (form.value.id !== '') getInfo() })

function getInfo() {
  loading.value = true
  apiBizFailure.detail(form.value.id as number).then((res: any) => {
    loading.value = false
    const d = res.data
    Object.assign(form.value, {
      id: d.id, target_type: d.target_type, target_id: d.target_id,
      failure_name: d.failure_name, failure_description: d.failure_description,
      failure_date: d.failure_date, status: d.status,
      reported_by: d.reported_by, resolved_by: d.resolved_by, notes: d.notes,
    })
  }).catch(() => { loading.value = false })
}

defineExpose({
  submit() {
    return new Promise<void>((resolve) => {
      formRef.value?.validate().then(() => {
        apiBizFailure.save(form.value).then(() => { faToast.success(form.value.id ? '编辑成功' : '新增成功'); resolve() })
      }).catch(() => {})
    })
  },
})
</script>

<template>
  <div v-if="loading" class="flex-center py-12"><ASpin /></div>
  <AForm v-else ref="formRef" :model="form" :rules="rules" :label-col="{ span: 5 }">
    <AFormItem label="关联类型" name="target_type">
      <ASelect v-model:value="form.target_type" placeholder="请选择" :options="[
        { value: 'device', label: '设备' }, { value: 'accessory', label: '配件' },
      ]" />
    </AFormItem>
    <AFormItem label="关联目标ID" name="target_id"><AInputNumber v-model:value="form.target_id" placeholder="设备/配件ID" class="w-full" /></AFormItem>
    <AFormItem label="故障名称" name="failure_name"><AInput v-model:value="form.failure_name" placeholder="请输入故障名称" /></AFormItem>
    <AFormItem label="故障描述" name="failure_description"><ATextarea v-model:value="form.failure_description" placeholder="请输入故障描述" :rows="3" /></AFormItem>
    <AFormItem label="故障日期" name="failure_date"><ADatePicker v-model:value="form.failure_date" show-time value-format="YYYY-MM-DD HH:mm:ss" class="w-full" /></AFormItem>
    <AFormItem label="状态" name="status">
      <ASelect v-model:value="form.status" :options="[
        { value: 'open', label: '待处理' }, { value: 'in_progress', label: '处理中' },
        { value: 'resolved', label: '已修复' }, { value: 'closed', label: '已关闭' },
      ]" />
    </AFormItem>
    <AFormItem label="报告人ID" name="reported_by"><AInputNumber v-model:value="form.reported_by" placeholder="报告人ID" class="w-full" /></AFormItem>
    <AFormItem label="修复人ID" name="resolved_by"><AInputNumber v-model:value="form.resolved_by" placeholder="修复人ID" class="w-full" /></AFormItem>
    <AFormItem label="备注" name="notes"><ATextarea v-model:value="form.notes" placeholder="请输入备注" :rows="2" /></AFormItem>
  </AForm>
</template>
