import 'dart:convert';

import 'package:firebase_database/firebase_database.dart';
import 'package:mobile_pos/model/user_role_model.dart';

import '../currency.dart';

class UserRoleRepo {
  Future<List<UserRoleModel>> getAllUserRole() async {
    List<UserRoleModel> allUser = [];
    FirebaseDatabase.instance.ref(constUserId).child('User Role').orderByKey().get().then((value) {
      for (var element in value.children) {
        try {
          var data = UserRoleModel.fromJson(jsonDecode(jsonEncode(element.value)));
          allUser.add(data);
        } catch (e) {
          print(e);
        }
      }
    });
    var userRoleRef = FirebaseDatabase.instance.ref(constUserId).child('User Role');
    userRoleRef.keepSynced(true);
    return allUser;
  }

  Future<List<UserRoleModel>> getAllUserRoleFromAdmin() async {
    List<UserRoleModel> allUser = [];

    FirebaseDatabase.instance
        .ref('Admin Panel')
        .child('User Role')
        .orderByKey()
        .get()
        .then((value) {
      for (var element in value.children) {
        var data = UserRoleModel.fromJson(jsonDecode(jsonEncode(element.value)));
        data.id = element.key;
        allUser.add(data);
      }
    });

    var userRoleRef = FirebaseDatabase.instance.ref('Admin Panel').child('User Role');
    userRoleRef.keepSynced(true);

    return allUser;
  }
}
