<script setup lang="ts">
import type { FormInstance } from 'antdv-next'
import apiAssetOffering from '@/api/modules/assetOffering'
import apiBaseSupplier from '@/api/modules/baseSupplier'

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, name: '', supplier_id: undefined as number | undefined,
  service_number: '', start_date: '', end_date: '', cost: undefined as number | undefined,
  notes: '', offering_status: 'active',
})

const rules = { name: [{ required: true, message: '请输入服务名称', trigger: 'blur' }] }
const supplierOptions = ref<{ value: number; label: string }[]>([])

onMounted(() => {
  apiBaseSupplier.list().then((res: any) => { supplierOptions.value = res.data.map((i: any) => ({ value: i.id, label: i.name })) })
  if (form.value.id !== '') getInfo()
})

function getInfo() {
  loading.value = true
  apiAssetOffering.detail(form.value.id as number).then((res: any) => {
    loading.value = false
    const d = res.data
    Object.assign(form.value, {
      name: d.name, supplier_id: d.supplier_id, service_number: d.service_number,
      start_date: d.start_date, end_date: d.end_date, cost: d.cost,
      notes: d.notes, offering_status: d.offering_status,
    })
  }).catch(() => { loading.value = false })
}

defineExpose({
  submit() {
    return new Promise<void>((resolve) => {
      formRef.value?.validate().then(() => {
        apiAssetOffering.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="name"><AInput v-model:value="form.name" placeholder="请输入服务名称" /></AFormItem>
    <AFormItem label="供应商" name="supplier_id">
      <ASelect v-model:value="form.supplier_id" placeholder="请选择供应商" allow-clear show-search :filter-option="(input: string, option: any) => option.label?.toLowerCase().includes(input.toLowerCase())" :options="supplierOptions" />
    </AFormItem>
    <AFormItem label="服务单号" name="service_number"><AInput v-model:value="form.service_number" placeholder="请输入服务单号/合同号" /></AFormItem>
    <AFormItem label="服务状态" name="offering_status">
      <ASelect v-model:value="form.offering_status" :options="[
        { value: 'active', label: '有效' }, { value: 'expired', label: '过期' }, { value: 'cancelled', label: '取消' },
      ]" />
    </AFormItem>
    <AFormItem label="开始日期" name="start_date"><ADatePicker v-model:value="form.start_date" value-format="YYYY-MM-DD" class="w-full" /></AFormItem>
    <AFormItem label="结束日期" name="end_date"><ADatePicker v-model:value="form.end_date" value-format="YYYY-MM-DD" class="w-full" /></AFormItem>
    <AFormItem label="费用" name="cost"><AInputNumber v-model:value="form.cost" placeholder="请输入费用" :precision="2" class="w-full" /></AFormItem>
    <AFormItem label="备注" name="notes"><ATextarea v-model:value="form.notes" placeholder="请输入备注" :rows="3" /></AFormItem>
  </AForm>
</template>
