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

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: '', location_id: undefined as number | undefined,
  category_id: undefined as number | undefined, status: 'planning', manager_id: undefined as number | undefined,
  start_date: '', end_date: '', note: '',
})

const rules = {
  name: [{ required: true, message: '请输入任务名称', trigger: 'blur' }],
  manager_id: [{ required: true, message: '请输入负责人ID', trigger: 'blur' }],
}

// Items table
const itemsLoading = ref(false)
const items = ref<any[]>([])

const itemsColumns = [
  { title: '资产类型', dataIndex: 'item_type', key: 'item_type' },
  { title: '资产ID', dataIndex: 'item_id', key: 'item_id' },
  { title: '系统数量', dataIndex: 'system_quantity', key: 'system_quantity' },
  { title: '实际数量', dataIndex: 'actual_quantity', key: 'actual_quantity' },
  { title: '状态', dataIndex: 'status', key: 'status' },
  { title: '盘点人', dataIndex: 'checker_name', key: 'checker_name' },
]

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

function getInfo() {
  loading.value = true
  apiBizStocktake.detail(form.value.id as number).then((res: any) => {
    loading.value = false
    const d = res.data
    Object.assign(form.value, {
      id: d.id, name: d.name, location_id: d.location_id, category_id: d.category_id,
      status: d.status, manager_id: d.manager_id, start_date: d.start_date,
      end_date: d.end_date, note: d.note,
    })
    // Load items
    itemsLoading.value = true
    apiBizStocktake.items(d.id).then((r: any) => { items.value = r.data || []; itemsLoading.value = false })
  }).catch(() => { loading.value = false })
}

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

<template>
  <div v-if="loading" class="flex-center py-12"><ASpin /></div>
  <div v-else>
    <AForm ref="formRef" :model="form" :rules="rules" :label-col="{ span: 5 }">
      <AFormItem label="任务名称" name="name"><AInput v-model:value="form.name" placeholder="请输入任务名称" /></AFormItem>
      <AFormItem label="负责人ID" name="manager_id"><AInputNumber v-model:value="form.manager_id" placeholder="负责人ID" class="w-full" /></AFormItem>
      <AFormItem label="盘点位置" name="location_id"><AInputNumber v-model:value="form.location_id" placeholder="位置ID(可选)" class="w-full" /></AFormItem>
      <AFormItem label="盘点分类" name="category_id"><AInputNumber v-model:value="form.category_id" placeholder="分类ID(可选)" class="w-full" /></AFormItem>
      <AFormItem label="状态" name="status">
        <ASelect v-model:value="form.status" :options="[
          { value: 'planning', label: '计划中' }, { value: 'executing', label: '执行中' }, { value: 'completed', label: '已完成' },
        ]" />
      </AFormItem>
      <AFormItem label="开始日期" name="start_date"><ADatePicker v-model:value="form.start_date" show-time value-format="YYYY-MM-DD HH:mm:ss" class="w-full" /></AFormItem>
      <AFormItem label="结束日期" name="end_date"><ADatePicker v-model:value="form.end_date" show-time value-format="YYYY-MM-DD HH:mm:ss" class="w-full" /></AFormItem>
      <AFormItem label="备注" name="note"><ATextarea v-model:value="form.note" placeholder="请输入备注" :rows="2" /></AFormItem>
    </AForm>
    <!-- Items list for existing stocktake -->
    <div v-if="form.id" class="mt-6">
      <div class="text-sm font-medium mb-2">盘点明细</div>
      <ATable :loading="itemsLoading" :columns="itemsColumns" :data-source="items" :pagination="false" size="small" row-key="id" />
    </div>
  </div>
</template>
