import { h, ref } from 'vue';
import { cloneDeep } from 'lodash-es';
import { FormSchema } from '@/components/Form';

export class State {
  public id = 0; // id
  public name = ''; // 分类名
  public sortOrder = 0; // 排序
  public revision = 0; // 乐观锁
  public createdBy = 0; // 创建人
  public createdTime = ''; // 创建时间
  public updatedBy = 0; // 更新人
  public updatedTime = ''; // 更新时间

  constructor(state?: Partial<State>) {
    if (state) {
      Object.assign(this, state);
    }
  }
}

export function newState(state: State | Record<string, any> | null): State {
  if (state !== null) {
    if (state instanceof State) {
      return cloneDeep(state);
    }
    return new State(state);
  }
  return new State();
}

// 表单验证规则
export const rules = {
  name: {
    required: true,
    trigger: ['blur', 'input'],
    type: 'string',
    message: '请输入分类名',
  },
};

// 表格搜索表单
export const schemas = ref<FormSchema[]>([
  {
    field: 'name',
    component: 'NInput',
    label: '分类名',
    componentProps: {
      placeholder: '请输入分类名',
      onUpdateValue: (e: any) => {
        console.log(e);
      },
    },
  },
]);

// 表格列
export const columns = [
  {
    title: '分类名',
    key: 'name',
    align: 'left',
    width: -1,
  },
  {
    title: '排序',
    key: 'sortOrder',
    align: 'left',
    width: -1,
  },
];