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

const dict = useDictStore();

export class State {
  public id = 0; // id
  public name = ''; // 课程名
  public cover = ''; // 封面图
  public categoryId = null; // 所属分类
  public video = ''; // 介绍视频
  public school = ''; // 所属学校
  public description = ''; // 课程概述
  public target = ''; // 授课目标
  public outline = ''; // 课程大纲
  public prerequisite = ''; // 预备知识
  public reference = ''; // 参考资料
  public arrangement = ''; // 学时安排
  public startTime = ''; // 开课时间
  public endTime = ''; // 结课时间
  public revision = 0; // 乐观锁
  public question = ''; // 常见问题
  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 schemas = ref<FormSchema[]>([
  {
    field: 'name',
    component: 'NInput',
    label: '课程名',
    componentProps: {
      placeholder: '请输入课程名',
      onUpdateValue: (e: any) => {
        console.log(e);
      },
    },
  },
  {
    field: 'school',
    component: 'NInput',
    label: '所属学校',
    componentProps: {
      placeholder: '请输入所属学校',
      onUpdateValue: (e: any) => {
        console.log(e);
      },
    },
  },
]);

// 表格列
export const columns = [
  {
    title: 'id',
    key: 'id',
    align: 'left',
    width: -1,
  },
  {
    title: '课程名',
    key: 'name',
    align: 'left',
    width: -1,
  },
  {
    title: '封面图',
    key: 'cover',
    align: 'left',
    width: -1,
    render(row: State) {
      return renderImage(row.cover);
    },
  },
  {
    title: '所属分类',
    key: 'categoryId',
    align: 'left',
    width: -1,
    render(row: State) {
      return renderOptionTag('lessonCategoryOption', row.categoryId);
    },
  },
  {
    title: '介绍视频',
    key: 'video',
    align: 'left',
    width: -1,
    render(row: State) {
      return renderFile(row.video);
    },
  },
  {
    title: '所属学校',
    key: 'school',
    align: 'left',
    width: -1,
  },
  {
    title: '开课时间',
    key: 'startTime',
    align: 'left',
    width: -1,
  },
  {
    title: '结课时间',
    key: 'endTime',
    align: 'left',
    width: -1,
  },
];

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