import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';

/** 资产分类选项 */
export const AssetCategoryOptions = [
  { label: '易耗品', value: 1 },
  { label: '办公资产', value: 2 },
  { label: '固定资产', value: 3 },
];

/** 资产分类名称 */
export function getCategoryName(category?: number) {
  return (
    AssetCategoryOptions.find((item) => item.value === category)?.label ?? '-'
  );
}

/** 新增/修改表单 */
export function useFormSchema(formType: 'create' | 'edit' | 'detail') {
  const disabled = formType === 'detail';
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'assetNo',
      label: '资产编号',
      component: 'Input',
      componentProps: {
        placeholder: '留空则由系统自动生成',
        maxlength: 32,
        disabled,
      },
    },
    {
      fieldName: 'name',
      label: '资产名称',
      component: 'Input',
      componentProps: {
        placeholder: '如：激光打印机 / A4 复印纸',
        maxlength: 128,
        disabled,
      },
      rules: 'required',
    },
    {
      fieldName: 'category',
      label: '资产分类',
      component: 'Select',
      componentProps: {
        options: AssetCategoryOptions,
        placeholder: '请选择资产分类',
        allowClear: true,
        disabled,
      },
      rules: 'required',
    },
    {
      fieldName: 'spec',
      label: '规格型号',
      component: 'Input',
      componentProps: {
        placeholder: '请输入规格型号',
        maxlength: 128,
        disabled,
      },
    },
    {
      fieldName: 'unit',
      label: '计量单位',
      component: 'Input',
      componentProps: {
        placeholder: '如：台 / 个 / 箱',
        maxlength: 16,
        disabled,
      },
    },
    {
      fieldName: 'quantity',
      label: '库存数量',
      component: 'InputNumber',
      componentProps: {
        placeholder: '请输入当前库存数量',
        min: 0,
        precision: 0,
        class: '!w-full',
        disabled,
      },
      rules: 'required',
    },
    {
      fieldName: 'unitPrice',
      label: '单价（元）',
      component: 'InputNumber',
      componentProps: {
        placeholder: '请输入单价',
        min: 0,
        precision: 2,
        class: '!w-full',
        disabled,
      },
    },
    {
      fieldName: 'purchaseDate',
      label: '购入日期',
      component: 'DatePicker',
      componentProps: {
        placeholder: '请选择购入日期',
        valueFormat: 'YYYY-MM-DD',
        style: { width: '100%' },
        disabled,
      },
    },
    {
      fieldName: 'custodianName',
      label: '保管人（管理人）',
      component: 'Input',
      componentProps: {
        placeholder: '请输入资产保管人姓名',
        maxlength: 64,
        disabled,
      },
    },
    {
      fieldName: 'location',
      label: '存放地点',
      component: 'Input',
      componentProps: {
        placeholder: '请输入存放地点',
        maxlength: 128,
        disabled,
      },
    },
    {
      fieldName: 'remark',
      label: '备注',
      component: 'Textarea',
      componentProps: {
        placeholder: '请输入备注',
        autoSize: { minRows: 2, maxRows: 3 },
        disabled,
      },
      formItemClass: 'col-span-2',
    },
  ] as VbenFormSchema[];
}

/** 列表搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'assetNo',
      label: '资产编号',
      component: 'Input',
      componentProps: { placeholder: '请输入资产编号', allowClear: true },
    },
    {
      fieldName: 'name',
      label: '资产名称',
      component: 'Input',
      componentProps: { placeholder: '请输入资产名称', allowClear: true },
    },
    {
      fieldName: 'category',
      label: '资产分类',
      component: 'Select',
      componentProps: {
        options: AssetCategoryOptions,
        placeholder: '请选择资产分类',
        allowClear: true,
      },
    },
    {
      fieldName: 'custodianName',
      label: '保管人',
      component: 'Input',
      componentProps: { placeholder: '请输入保管人', allowClear: true },
    },
  ];
}

/** 列表列 */
export function useGridColumns(): VxeTableGridOptions['columns'] {
  return [
    { field: 'assetNo', title: '资产编号', width: 180, fixed: 'left' },
    { field: 'name', title: '资产名称', minWidth: 150 },
    {
      field: 'category',
      title: '资产分类',
      width: 110,
      slots: { default: 'category' },
    },
    { field: 'spec', title: '规格型号', minWidth: 140 },
    { field: 'unit', title: '单位', width: 70 },
    { field: 'quantity', title: '库存数量', width: 100 },
    { field: 'unitPrice', title: '单价（元）', width: 110 },
    { field: 'totalAmount', title: '总价值（元）', width: 120 },
    { field: 'custodianName', title: '保管人', width: 110 },
    { field: 'location', title: '存放地点', minWidth: 140 },
    {
      title: '操作',
      width: 180,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}
