<script setup lang="ts">
import type { FormInstance } from 'antdv-next'
import apiAssetLicense from '@/api/modules/assetLicense'
import apiBaseCategory from '@/api/modules/baseCategory'
import apiBaseManufacturer from '@/api/modules/baseManufacturer'
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: '', license_key: '', total_seats: undefined as number | undefined,
  manufacturer_id: undefined as number | undefined, category_id: undefined as number | undefined,
  min_qty: undefined as number | undefined, licensed_to_name: '', licensed_to_email: '',
  supplier_id: undefined as number | undefined, order_number: '',
  purchase_cost: undefined as number | undefined, purchase_date: '',
  expiration_date: '', termination_date: '', notes: '',
})

const rules = {
  name: [{ required: true, message: '请输入软件名称', trigger: 'blur' }],
  license_key: [{ required: true, message: '请输入序列号/密钥', trigger: 'blur' }],
}

const categoryOptions = ref<{ value: number; label: string }[]>([])
const manufacturerOptions = ref<{ value: number; label: string }[]>([])
const supplierOptions = 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 })) })
  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
  apiAssetLicense.detail(form.value.id as number).then((res: any) => {
    loading.value = false
    const d = res.data
    Object.assign(form.value, {
      name: d.name, license_key: d.license_key, total_seats: d.total_seats,
      manufacturer_id: d.manufacturer_id, category_id: d.category_id,
      min_qty: d.min_qty, licensed_to_name: d.licensed_to_name, licensed_to_email: d.licensed_to_email,
      supplier_id: d.supplier_id, order_number: d.order_number,
      purchase_cost: d.purchase_cost, purchase_date: d.purchase_date,
      expiration_date: d.expiration_date, termination_date: d.termination_date, notes: d.notes,
    })
  }).catch(() => { loading.value = false })
}

defineExpose({
  submit() {
    return new Promise<void>((resolve) => {
      formRef.value?.validate().then(() => {
        apiAssetLicense.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="license_key">
      <AInput v-model:value="form.license_key" placeholder="请输入序列号/密钥" />
    </AFormItem>
    <AFormItem label="总授权数" name="total_seats">
      <AInputNumber v-model:value="form.total_seats" placeholder="请输入总授权数" class="w-full" />
    </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="category_id">
      <ASelect v-model:value="form.category_id" placeholder="请选择分类" allow-clear show-search :filter-option="(input: string, option: any) => option.label?.toLowerCase().includes(input.toLowerCase())" :options="categoryOptions" />
    </AFormItem>
    <AFormItem label="库存预警值" name="min_qty">
      <AInputNumber v-model:value="form.min_qty" placeholder="最少数量" class="w-full" />
    </AFormItem>
    <AFormItem label="许可人" name="licensed_to_name">
      <AInput v-model:value="form.licensed_to_name" placeholder="许可人名字" />
    </AFormItem>
    <AFormItem label="许可人邮箱" name="licensed_to_email">
      <AInput v-model:value="form.licensed_to_email" 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="order_number">
      <AInput v-model:value="form.order_number" placeholder="请输入订单号" />
    </AFormItem>
    <AFormItem label="采购价格" name="purchase_cost">
      <AInputNumber v-model:value="form.purchase_cost" placeholder="请输入采购价格" :precision="2" class="w-full" />
    </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="expiration_date">
      <ADatePicker v-model:value="form.expiration_date" value-format="YYYY-MM-DD" class="w-full" />
    </AFormItem>
    <AFormItem label="终止日期" name="termination_date">
      <ADatePicker v-model:value="form.termination_date" value-format="YYYY-MM-DD" class="w-full" />
    </AFormItem>
    <AFormItem label="备注" name="notes">
      <ATextarea v-model:value="form.notes" placeholder="请输入备注" :rows="3" />
    </AFormItem>
  </AForm>
</template>
