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

import { getAssetItemSimpleList } from '#/api/asset/item';

import { AssetCategoryOptions } from '../item/data';

/** 归还状态选项 */
export const ReturnStatusOptions = [
  { 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';
  const editDisabled = formType !== 'create';
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: { triggerFields: [''], show: () => false },
    },
    {
      fieldName: 'assetId',
      label: '领用资产',
      component: 'ApiSelect',
      componentProps: {
        api: getAssetItemSimpleList,
        labelField: 'name',
        valueField: 'id',
        placeholder: '请选择领用资产',
        allowClear: true,
        showSearch: true,
        disabled: disabled || editDisabled,
      },
      rules: 'required',
    },
    {
      fieldName: 'recipientName',
      label: '领用人',
      component: 'Input',
      componentProps: {
        placeholder: '请输入领用人姓名',
        maxlength: 64,
        disabled,
      },
      rules: 'required',
    },
    {
      fieldName: 'requisitionTime',
      label: '领用时间',
      component: 'DatePicker',
      componentProps: {
        showTime: true,
        valueFormat: 'YYYY-MM-DD HH:mm:ss',
        placeholder: '请选择领用时间',
        style: { width: '100%' },
        disabled: disabled || editDisabled,
      },
      rules: 'required',
    },
    {
      fieldName: 'quantity',
      label: '领用数量',
      component: 'InputNumber',
      componentProps: {
        min: 1,
        precision: 0,
        placeholder: '请输入领用数量',
        class: '!w-full',
        disabled: disabled || editDisabled,
      },
      rules: 'required',
    },
    {
      fieldName: 'expectedReturnDate',
      label: '预计归还日期',
      component: 'DatePicker',
      componentProps: {
        valueFormat: 'YYYY-MM-DD',
        placeholder: '请选择预计归还日期',
        style: { width: '100%' },
        disabled,
      },
    },
    {
      fieldName: 'purpose',
      label: '用途',
      component: 'Textarea',
      componentProps: {
        placeholder: '请输入领用用途',
        autoSize: { minRows: 2, maxRows: 3 },
        disabled,
      },
      formItemClass: 'col-span-2',
    },
    {
      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: 'no',
      label: '领用单号',
      component: 'Input',
      componentProps: { placeholder: '请输入领用单号', allowClear: true },
    },
    {
      fieldName: 'assetName',
      label: '资产名称',
      component: 'Input',
      componentProps: { placeholder: '请输入资产名称', allowClear: true },
    },
    {
      fieldName: 'recipientName',
      label: '领用人',
      component: 'Input',
      componentProps: { placeholder: '请输入领用人', allowClear: true },
    },
    {
      fieldName: 'category',
      label: '资产分类',
      component: 'Select',
      componentProps: {
        options: AssetCategoryOptions,
        placeholder: '请选择资产分类',
        allowClear: true,
      },
    },
    {
      fieldName: 'returnStatus',
      label: '归还状态',
      component: 'Select',
      componentProps: {
        options: ReturnStatusOptions,
        placeholder: '请选择归还状态',
        allowClear: true,
      },
    },
  ];
}

/** 列表列 */
export function useGridColumns(): VxeTableGridOptions['columns'] {
  return [
    { field: 'no', title: '领用单号', width: 175, fixed: 'left' },
    { field: 'assetNo', title: '资产编号', width: 175 },
    { field: 'assetName', title: '资产名称', minWidth: 140 },
    {
      field: 'category',
      title: '资产分类',
      width: 105,
      slots: { default: 'category' },
    },
    { field: 'custodianName', title: '保管人', width: 100 },
    { field: 'recipientName', title: '领用人', width: 100 },
    {
      field: 'requisitionTime',
      title: '领用时间',
      width: 165,
      formatter: 'formatDateTime',
    },
    { field: 'quantity', title: '领用数量', width: 95 },
    { field: 'returnedQuantity', title: '已归还', width: 90 },
    { field: 'remainingQuantity', title: '未归还', width: 90 },
    {
      field: 'expectedReturnDate',
      title: '预计归还',
      width: 115,
      formatter: 'formatDate',
    },
    { field: 'purpose', title: '用途', minWidth: 150, showOverflow: 'tooltip' },
    {
      field: 'returnStatus',
      title: '归还状态',
      width: 100,
      slots: { default: 'returnStatus' },
    },
    {
      title: '操作',
      width: 200,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}
