import type {
  OnActionClickFn,
  VbenFormSchema,
  VxeTableGridOptions,
} from '#/adapter';

import { DepartmentApi } from '#/api';

export const useSearchSchema = (): VbenFormSchema[] => {
  return [
    {
      component: 'Input',
      fieldName: 'name',
      label: '名称',
    },
    {
      component: 'Input',
      fieldName: 'code',
      label: '编码',
    },
  ];
};

export function useColumns(
  onActionClick?: OnActionClickFn<DepartmentApi.RecordItem>,
): VxeTableGridOptions<DepartmentApi.RecordItem>['columns'] {
  return [
    { title: '序号', type: 'seq', width: 50 },
    { align: 'left', field: 'name', title: '名称', width: 200, treeNode: true },
    {
      align: 'left',
      field: 'code',
      title: '编码',
      width: 150,
    },
    { align: 'left', field: 'remark', title: '备注' },
    {
      field: 'status',
      title: '状态',
      width: 82,
      cellRender: { name: 'CellTag' },
    },
    {
      align: 'right',
      cellRender: {
        attrs: {
          nameField: 'name',
          nameTitle: '组织',
          onClick: onActionClick,
        },
        name: 'CellAction',
        options: [
          {
            code: 'edit',
            auth: ['department:edit'],
          },
          {
            code: 'delete',
            auth: ['department:delete'],
          },
        ],
      },
      field: 'operation',
      fixed: 'right',
      headerAlign: 'center',
      showOverflow: false,
      title: '操作',
      width: 100,
    },
  ];
}
export const useSchema = (): VbenFormSchema[] => {
  return [
    {
      component: 'ApiTreeSelect',
      componentProps: {
        placeholder: '请输入上级',
        api: {
          url: DepartmentApi.getTree,
        },
        showSearch: true,
        fieldNames: { label: 'name', value: 'id' },
      },
      fieldName: 'parentId',
      label: '上级',
      rules: 'required',
    },
    {
      component: 'Input',
      componentProps: {
        placeholder: '请输入编号',
      },
      fieldName: 'code',
      label: '编号',
      rules: 'required',
    },
    {
      component: 'Input',
      componentProps: {
        placeholder: '请输入名称',
      },
      fieldName: 'name',
      label: '名称',
      rules: 'required',
    },
    {
      component: 'InputNumber',
      componentProps: {
        placeholder: '请输入排序',
      },
      fieldName: 'sort',
      label: '排序',
      rules: 'required',
    },
    {
      component: 'Input',
      componentProps: {
        placeholder: '请输入',
      },
      fieldName: 'remark',
      label: '备注',
    },
  ];
};
