import { DataModel } from '@imengyu/js-request-transform';
import { AppServerRequestModule } from '../RequestModules';


export class LoginResult extends DataModel<LoginResult> {
  constructor() {
    super(LoginResult, "登录结果");
    this._convertTable = {
      userInfo: { clientSide: 'object', clientSideChildDataModel: UserInfo },
    };
    this._nameMapperServer = {
      'userinfo': 'userInfo',
      'mainBodyUserInfo': 'userInfo',
    }
    this._afterSolveServer = () => {
      if (this.mainBodyUserInfo) {
        this.userInfo.token = this.mainBodyUserInfo.token;
      }
    };
  }
  userInfo !:UserInfo;
  mainBodyUserInfo?:UserInfo;
}
export class UserInfo extends DataModel<UserInfo> {
  constructor() {
    super(UserInfo, "用户信息");
    this.setNameMapperCase('Camel', 'Snake');
    this._convertTable = {
      id: { clientSide: 'number', serverSide: 'number', clientSideRequired: true },
    }
  }

  expiresIn = 0;
  id = 0;
  userId = 0;
  mobile = '';
  nickname = '';
  avatar = '';
  username = '';
  token = '';
}
export class UserApi extends AppServerRequestModule<DataModel> {

  constructor() {
    super();
  }
  async loginAdmin(data?: {
    account: string,
    password: string,
  }) {
    const form = new FormData();
    form.append('account', data?.account || '');
    form.append('password', data?.password || '');
    return (await this.post('/user/adminLogin', form, '登录', undefined, LoginResult)).data as LoginResult;
  }
  async getUserInfo(main_body_user_id: number) {
    return (await this.post('/content/main_body_user/getMainBodyUser', {
      main_body_user_id,
    }, '获取用户信息', undefined, UserInfo)).data as UserInfo;
  }
  async refresh() {
    return (await this.post('/content/main_body_user/refreshUser', {
    }, '刷新用户', undefined, LoginResult)).data as LoginResult;
  }
}

export default new UserApi();
