import type { FormSchemaGetter } from '#/adapter/form';
import type { VxeGridProps } from '#/adapter/vxe-table';

import {
  assetTypeList,
  getAssetTypeDetail,
  addAssetType,
  updateAssetType,
  deleteAssetType,
} from '#/api/system/asset/asset';

export const queryFormSchema: FormSchemaGetter = () => [
  {
    component: 'Input',
    fieldName: 'typeName',
    label: '资产类型',
    componentProps: {
      placeholder: '请输入资产类型',
    },
  },
];

export const tableColumns: VxeGridProps['columns'] = [
  {
    type: 'checkbox',
    width: 80,
  },
  {
    field: 'typeName',
    title: '资产类型',
    minWidth: 120,
  },
  {
    field: 'assetCount',
    title: '资产数量',
    minWidth: 100,
  },
  {
    field: 'description',
    title: '资产描述',
    minWidth: 200,
  },
  {
    field: 'action',
    fixed: 'right',
    slots: { default: 'action' },
    title: '操作',
    width: 150,
  },
];

export const drawerFormSchema: FormSchemaGetter = () => [
  {
    component: 'Input',
    dependencies: {
      show: () => false,
      triggerFields: [''],
    },
    fieldName: 'id',
  },
  {
    component: 'Input',
    fieldName: 'typeName',
    label: '资产类型',
    componentProps: {
      placeholder: '请输入资产类型',
      maxlength: 100,
    },
    rules: 'required',
  },
  {
    component: 'Input',
    fieldName: 'description',
    label: '类型描述',
    componentProps: {
      placeholder: '请输入类型描述',
      maxlength: 200,
      type: 'textarea',
      rows: 3,
      showWordLimit: true,
    },
  },
];

// 接口调用函数
export const assetTypeApi = {
  // 获取资产类型列表
  getList: async (params: any) => {
    const resp = await assetTypeList(params);
    console.log('资产类型列表响应:', resp);
    return { items: resp?.rows || [], total: resp?.total || 0 };
  },
  
  // 获取资产类型详情
  getDetail: async (id: number) => {
    const resp = await getAssetTypeDetail(id.toString());
    console.log('资产类型详情响应:', resp);
    return resp || {};
  },
  
  // 新增资产类型
  create: async (data: any) => {
    const resp = await addAssetType(data);
    return resp;
  },
  
  // 更新资产类型
  update: async (data: any) => {
    const resp = await updateAssetType(data);
    return resp;
  },
  
  // 删除资产类型
  delete: async (id: number) => {
    const resp = await deleteAssetType([id.toString()]);
    return resp;
  },
  
  // 批量删除资产类型
  deleteBatch: async (ids: number[]) => {
    const stringIds = ids.map(id => id.toString());
    const resp = await deleteAssetType(stringIds);
    return resp;
  },
};
