import { h, ref } from 'vue';
import { cloneDeep } from 'lodash-es';
import { FormSchema } from '@/components/Form';
import { renderOptionTag, renderPopoverMemberSumma, MemberSumma } from '@/utils';
import { useDictStore } from '@/store/modules/dict';

const dict = useDictStore();

export class State {
  public id = 0; // id
  public lessonId = null; // 课程id
  public videoUrl = ''; // 视频url
  public name = ''; // 章节名
  public sortOrder = 0; // 排序号
  public parentId = 0; // 父章节id
  public level = 0; // 章节层级
  public revision = 0; // 乐观锁
  public createdBy = 0; // 创建人
  public createdBySumma?: null | MemberSumma = null; // 创建人摘要信息
  public createdTime = ''; // 创建时间
  public updatedBy = 0; // 更新人
  public updatedBySumma?: null | MemberSumma = null; // 更新人摘要信息
  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 schemas = ref<FormSchema[]>([
  {
    field: 'id',
    component: 'NInputNumber',
    label: 'id',
    componentProps: {
      placeholder: '请输入id',
      onUpdateValue: (e: any) => {
        console.log(e);
      },
    },
  },
]);

// 表格列
export const columns = [
  {
    title: 'id',
    key: 'id',
    align: 'left',
    width: -1,
  },
  {
    title: '课程id',
    key: 'lessonId',
    align: 'left',
    width: -1,
    render(row: State) {
      return renderOptionTag('lessonOption', row.lessonId);
    },
  },
  {
    title: '视频url',
    key: 'videoUrl',
    align: 'left',
    width: -1,
  },
  {
    title: '章节名',
    key: 'name',
    align: 'left',
    width: -1,
  },
  {
    title: '排序号',
    key: 'sortOrder',
    align: 'left',
    width: -1,
  },
  {
    title: '父章节id',
    key: 'parentId',
    align: 'left',
    width: -1,
  },
  {
    title: '章节层级',
    key: 'level',
    align: 'left',
    width: -1,
  },
  {
    title: '创建人',
    key: 'createdBy',
    align: 'left',
    width: -1,
    render(row: State) {
      return renderPopoverMemberSumma(row.createdBySumma);
    },
  },
  {
    title: '更新人',
    key: 'updatedBy',
    align: 'left',
    width: -1,
    render(row: State) {
      return renderPopoverMemberSumma(row.updatedBySumma);
    },
  },
];

// 加载字典数据选项
export function loadOptions() {
  dict.loadOptions(['lessonOption']);
}