<script setup lang="ts">
import type { FormInstance } from 'antdv-next'
import apiAssetConsumable from '@/api/modules/assetConsumable'
import apiBaseCategory from '@/api/modules/baseCategory'
import apiBaseManufacturer from '@/api/modules/baseManufacturer'
import apiBaseLocation from '@/api/modules/baseLocation'

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: '', item_no: '',
  category_id: undefined as number | undefined, manufacturer_id: undefined as number | undefined,
  order_number: '', purchase_date: '', purchase_cost: undefined as number | undefined,
  quantity: 0, min_quantity: undefined as number | undefined, location_id: undefined as number | undefined,
})

const rules = {
  name: [{ required: true, message: '请输入耗材名称', trigger: 'blur' }],
  category_id: [{ required: true, message: '请选择分类', trigger: 'change' }],
}

const categoryOptions = ref<{ value: number; label: string }[]>([])
const manufacturerOptions = ref<{ value: number; label: string }[]>([])
const locationOptions = ref<{ value: number; label: string }[]>([])

onMounted(() => {
  apiBaseCategory.list().then((res: any) => { categoryOptions.value = res.data.map((i: any) => ({ value: i.id, label: i.name })) })
  apiBaseManufacturer.list().then((res: any) => { manufacturerOptions.value = res.data.map((i: any) => ({ value: i.id, label: i.name })) })
  apiBaseLocation.list().then((res: any) => { locationOptions.value = res.data.map((i: any) => ({ value: i.id, label: i.name })) })
  if (form.value.id !== '') getInfo()
})

function getInfo() {
  loading.value = true
  apiAssetConsumable.detail(form.value.id as number).then((res: any) => {
    loading.value = false
    const d = res.data
    Object.assign(form.value, {
      name: d.name, item_no: d.item_no, category_id: d.category_id,
      manufacturer_id: d.manufacturer_id, order_number: d.order_number,
      purchase_date: d.purchase_date, purchase_cost: d.purchase_cost,
      quantity: d.quantity, min_quantity: d.min_quantity, location_id: d.location_id,
    })
  }).catch(() => { loading.value = false })
}

defineExpose({
  submit() {
    return new Promise<void>((resolve) => {
      formRef.value?.validate().then(() => {
        apiAssetConsumable.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="category_id">
      <ASelect v-model:value="form.category_id" placeholder="请选择分类" show-search :filter-option="(input: string, option: any) => option.label?.toLowerCase().includes(input.toLowerCase())" :options="categoryOptions" />
    </AFormItem>
    <AFormItem label="物品编号" name="item_no"><AInput v-model:value="form.item_no" placeholder="请输入物品编号" /></AFormItem>
    <AFormItem label="厂商" name="manufacturer_id">
      <ASelect v-model:value="form.manufacturer_id" placeholder="请选择厂商" allow-clear show-search :filter-option="(input: string, option: any) => option.label?.toLowerCase().includes(input.toLowerCase())" :options="manufacturerOptions" />
    </AFormItem>
    <AFormItem label="存放位置" name="location_id">
      <ASelect v-model:value="form.location_id" placeholder="请选择位置" allow-clear show-search :filter-option="(input: string, option: any) => option.label?.toLowerCase().includes(input.toLowerCase())" :options="locationOptions" />
    </AFormItem>
    <AFormItem label="当前数量" name="quantity"><AInputNumber v-model:value="form.quantity" class="w-full" /></AFormItem>
    <AFormItem label="最小库存预警" name="min_quantity"><AInputNumber v-model:value="form.min_quantity" class="w-full" /></AFormItem>
    <AFormItem label="订单号" name="order_number"><AInput v-model:value="form.order_number" placeholder="请输入订单号" /></AFormItem>
    <AFormItem label="购买日期" name="purchase_date"><ADatePicker v-model:value="form.purchase_date" value-format="YYYY-MM-DD" class="w-full" /></AFormItem>
    <AFormItem label="购买成本" name="purchase_cost"><AInputNumber v-model:value="form.purchase_cost" placeholder="请输入购买成本" :precision="2" class="w-full" /></AFormItem>
  </AForm>
</template>
