import 'package:zhiying_base_widget/pages/login_page/invite/bloc/bloc.dart';
import 'package:zhiying_base_widget/pages/login_page/invite/model/login_invite_user.dart';
import 'package:zhiying_base_widget/pages/login_page/model/login_model.dart';
import 'package:zhiying_base_widget/pages/login_page/model/login_user.dart';
import 'package:zhiying_comm/util/empty_util.dart';
import 'package:zhiying_comm/util/net_util.dart';
import 'package:zhiying_comm/util/global_config.dart';
import '../../login_util.dart';

class LoginInviteRepostitory {
  /// 获取数据，如果缓存有，则获取缓存的数据
  Future<LoginModel> fetchData() async {
    var result = await LoginUtil.getLoginModel();
    if (!EmptyUtil.isEmpty(result)) {
      return result;
    }
    return null;
  }

  /// 获取邀请人信息
  Future<LoginInviteUser> fetchInviteUserInfo(LoginInviteQueryEvent event) async {
    var result = await NetUtil.post('/api/v1/user/invite/${event.num}');
    if (NetUtil.isSuccess(result) && !EmptyUtil.isEmpty(result[GlobalConfig.HTTP_RESPONSE_KEY_DATA])) {
      LoginInviteUser model = LoginInviteUser.fromJson(result[GlobalConfig.HTTP_RESPONSE_KEY_DATA]);
      return model;
    } else {
      return null;
    }
  }

  /// 提交
  Future<LoginUser> submitInvite(LoginInviteSubmitEvent event) async {
    var result = await NetUtil.post('/api/v1/user/invite/ack', params: {'mobile': event.mobile, 'superior_user_id': event.num});
    if (NetUtil.isSuccess(result) && !EmptyUtil.isEmpty(result[GlobalConfig.HTTP_RESPONSE_KEY_DATA])) {
      LoginUser model = LoginUser.fromJson(result[GlobalConfig.HTTP_RESPONSE_KEY_DATA]);
      return model;
    }
    return null;
  }
}
