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

const dict = useDictStore();

export class State {
  public id = 0; // id
  public phone = ''; // 手机号
  public nickname = ''; // 昵称
  public email = ''; // 邮箱
  public avatar = ''; // 头像URL
  public introduce = ''; // 个人简介
  public password = ''; // 密码
  public salt = ''; // 密码盐
  public realname = ''; // 真实姓名
  public gender = 0; // 性别
  public birthday = ''; // 生日
  public school = ''; // 学校
  public grade = ''; // 学历
  public major = ''; // 专业
  public status = 1; // 用户状态
  public verificationToken = ''; // 邮箱验证令牌
  public emailVerified = 0; // 邮箱验证状态
  public createdAt = ''; // 注册时间
  public lastLogin = ''; // 最后登录时间
  public loginCount = 0; // 累计登录次数

  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 = {
  email: {
    required: false,
    trigger: ['blur', 'input'],
    type: 'string',
    validator: validate.email,
  },
};

// 表格搜索表单
export const schemas = ref<FormSchema[]>([
  {
    field: 'id',
    component: 'NInputNumber',
    label: 'id',
    componentProps: {
      placeholder: '请输入id',
      onUpdateValue: (e: any) => {
        console.log(e);
      },
    },
  },
  {
    field: 'status',
    component: 'NSelect',
    label: '用户状态',
    defaultValue: null,
    componentProps: {
      placeholder: '请选择用户状态',
      options: dict.getOption('sys_normal_disable'),
      onUpdateValue: (e: any) => {
        console.log(e);
      },
    },
  },
  {
    field: 'createdAt',
    component: 'NDatePicker',
    label: '注册时间',
    componentProps: {
      type: 'datetimerange',
      clearable: true,
      shortcuts: defRangeShortcuts(),
      onUpdateValue: (e: any) => {
        console.log(e);
      },
    },
  },
]);

// 表格列
export const columns = [
  {
    title: 'id',
    key: 'id',
    align: 'left',
    width: -1,
  },
  {
    title: '手机号',
    key: 'phone',
    align: 'left',
    width: -1,
  },
  {
    title: '昵称',
    key: 'nickname',
    align: 'left',
    width: -1,
  },
  {
    title: '邮箱',
    key: 'email',
    align: 'left',
    width: -1,
  },
  {
    title: '头像URL',
    key: 'avatar',
    align: 'left',
    width: -1,
    render(row: State) {
      return renderImage(row.avatar);
    },
  },
  {
    title: '密码',
    key: 'password',
    align: 'left',
    width: -1,
  },
  {
    title: '密码盐',
    key: 'salt',
    align: 'left',
    width: -1,
  },
  {
    title: '真实姓名',
    key: 'realname',
    align: 'left',
    width: -1,
  },
  {
    title: '性别',
    key: 'gender',
    align: 'left',
    width: -1,
  },
  {
    title: '生日',
    key: 'birthday',
    align: 'left',
    width: -1,
  },
  {
    title: '学校',
    key: 'school',
    align: 'left',
    width: -1,
  },
  {
    title: '学历',
    key: 'grade',
    align: 'left',
    width: -1,
  },
  {
    title: '专业',
    key: 'major',
    align: 'left',
    width: -1,
  },
  {
    title: '用户状态',
    key: 'status',
    align: 'left',
    width: -1,
    render(row: State) {
      return renderOptionTag('sys_normal_disable', row.status);
    },
  },
  {
    title: '邮箱验证令牌',
    key: 'verificationToken',
    align: 'left',
    width: -1,
  },
  {
    title: '邮箱验证状态',
    key: 'emailVerified',
    align: 'left',
    width: -1,
  },
  {
    title: '注册时间',
    key: 'createdAt',
    align: 'left',
    width: -1,
  },
  {
    title: '最后登录时间',
    key: 'lastLogin',
    align: 'left',
    width: -1,
  },
  {
    title: '累计登录次数',
    key: 'loginCount',
    align: 'left',
    width: -1,
  },
];

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