246 lines
8.4 KiB
Dart
246 lines
8.4 KiB
Dart
import 'package:auto_size_text/auto_size_text.dart';
|
|
import 'package:baligh_dashboard/screens/admin_screen.dart';
|
|
import 'package:baligh_dashboard/widgets/custom_button.dart';
|
|
import 'package:baligh_dashboard/widgets/dashboard_tile.dart';
|
|
import 'package:cloud_firestore/cloud_firestore.dart';
|
|
import 'package:flutter/material.dart';
|
|
|
|
class DashboardScreen extends StatefulWidget {
|
|
const DashboardScreen({super.key, required this.adminRole});
|
|
|
|
final String adminRole;
|
|
|
|
@override
|
|
State<DashboardScreen> createState() => _DashboardScreenState();
|
|
}
|
|
|
|
class _DashboardScreenState extends State<DashboardScreen> {
|
|
bool isLoading = false;
|
|
String? error;
|
|
|
|
List<Map<String, dynamic>> reports = [];
|
|
|
|
@override
|
|
void initState() {
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
getInitialData();
|
|
});
|
|
super.initState();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Directionality(
|
|
textDirection: TextDirection.rtl,
|
|
child: Scaffold(
|
|
body: SafeArea(
|
|
child: Padding(
|
|
padding: const EdgeInsets.symmetric(horizontal: 15),
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.center,
|
|
children: [
|
|
const SizedBox(
|
|
height: 50,
|
|
),
|
|
AutoSizeText(
|
|
"البلاغات",
|
|
style: Theme.of(context).textTheme.titleLarge,
|
|
minFontSize: 12,
|
|
),
|
|
if (isLoading) ...[
|
|
const SizedBox(
|
|
height: 50,
|
|
),
|
|
Text("جار التحميل",
|
|
style: Theme.of(context).textTheme.bodyLarge),
|
|
] else if (error != null) ...[
|
|
const SizedBox(
|
|
height: 50,
|
|
),
|
|
Text(error!, style: Theme.of(context).textTheme.bodyLarge),
|
|
] else ...[
|
|
if (widget.adminRole == "admin") ...[
|
|
const SizedBox(
|
|
height: 10,
|
|
),
|
|
SizedBox(
|
|
width: 200,
|
|
child: CustomButton(
|
|
label: "اضافة مشرف",
|
|
isElevated: true,
|
|
onPressed: () {
|
|
Navigator.push(
|
|
context,
|
|
MaterialPageRoute(
|
|
builder: (context) => const AdminScreen()));
|
|
},
|
|
),
|
|
),
|
|
],
|
|
const SizedBox(
|
|
height: 30,
|
|
),
|
|
Row(
|
|
children: [
|
|
Expanded(
|
|
child: AutoSizeText(
|
|
"اسم صاحب البلاغ",
|
|
style: Theme.of(context).textTheme.bodyMedium,
|
|
maxLines: 3,
|
|
minFontSize: 8,
|
|
),
|
|
),
|
|
const SizedBox(
|
|
width: 5,
|
|
),
|
|
Expanded(
|
|
child: AutoSizeText(
|
|
"عنوان صاحب البلاغ",
|
|
style: Theme.of(context).textTheme.bodyMedium,
|
|
maxLines: 3,
|
|
minFontSize: 8,
|
|
),
|
|
),
|
|
const SizedBox(
|
|
width: 5,
|
|
),
|
|
Expanded(
|
|
child: AutoSizeText(
|
|
"المدرسة",
|
|
style: Theme.of(context).textTheme.bodyMedium,
|
|
maxLines: 3,
|
|
minFontSize: 8,
|
|
),
|
|
),
|
|
const SizedBox(
|
|
width: 5,
|
|
),
|
|
Expanded(
|
|
child: AutoSizeText(
|
|
"نوع البلاغ",
|
|
style: Theme.of(context).textTheme.bodyMedium,
|
|
maxLines: 3,
|
|
minFontSize: 8,
|
|
),
|
|
),
|
|
const SizedBox(
|
|
width: 5,
|
|
),
|
|
Expanded(
|
|
child: AutoSizeText(
|
|
"الحالة",
|
|
style: Theme.of(context).textTheme.bodyMedium,
|
|
maxLines: 3,
|
|
minFontSize: 8,
|
|
),
|
|
),
|
|
],
|
|
),
|
|
const SizedBox(
|
|
height: 10,
|
|
),
|
|
Expanded(
|
|
child: ListView.separated(
|
|
shrinkWrap: true,
|
|
itemCount: reports.length,
|
|
separatorBuilder: (context, index) => const SizedBox(
|
|
height: 10,
|
|
),
|
|
itemBuilder: (context, index) => DashboardTile(
|
|
id: reports[index]["id"] ?? "",
|
|
name: reports[index]["name"] ?? "",
|
|
age: reports[index]["age"] ?? "",
|
|
phone: reports[index]["phone"] ?? "",
|
|
address: reports[index]["address"] ?? "",
|
|
school: reports[index]["schoolName"] ?? "",
|
|
nationalIdNumber:
|
|
reports[index]["nationalIdNumber"] ?? "",
|
|
description: reports[index]["description"] ?? "",
|
|
attachments: reports[index]["attachments"] ?? "",
|
|
type: reports[index]["type"] ?? "",
|
|
createdAt: reports[index]["createdAt"] ?? "",
|
|
status: reports[index]["status"] ?? ""),
|
|
),
|
|
)
|
|
]
|
|
],
|
|
),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
Future<void> getInitialData() async {
|
|
try {
|
|
setState(() {
|
|
isLoading = true;
|
|
});
|
|
|
|
var temp = await getReportsWithUsers();
|
|
|
|
setState(() {
|
|
isLoading = false;
|
|
reports = temp;
|
|
});
|
|
} catch (e, s) {
|
|
setState(() {
|
|
isLoading = false;
|
|
error = "حدث خطأ ما";
|
|
});
|
|
}
|
|
}
|
|
|
|
Future<List<Map<String, dynamic>>> getReportsWithUsers() async {
|
|
QuerySnapshot reportsSnapshot;
|
|
|
|
if (widget.adminRole == "admin") {
|
|
reportsSnapshot = await FirebaseFirestore.instance
|
|
.collection('reports')
|
|
.orderBy('createdAt', descending: true)
|
|
.get();
|
|
} else {
|
|
reportsSnapshot = await FirebaseFirestore.instance
|
|
.collection('reports')
|
|
.where('type', isEqualTo: widget.adminRole)
|
|
.orderBy('createdAt', descending: true)
|
|
.get();
|
|
}
|
|
|
|
List<Map<String, dynamic>> reportsWithUsers = [];
|
|
Set<DocumentReference> userRefs = {};
|
|
|
|
// Collect unique user references
|
|
for (var reportDoc in reportsSnapshot.docs) {
|
|
var reportData = reportDoc.data() as Map<String, dynamic>;
|
|
if (reportData['userRef'] == null) continue;
|
|
userRefs.add(reportData['userRef'] as DocumentReference);
|
|
reportsWithUsers.add({
|
|
'id': reportDoc.id,
|
|
'type': reportData["type"],
|
|
'description': reportData["description"],
|
|
'attachments': reportData["attachments"],
|
|
'status': reportData["status"],
|
|
'createdAt': reportData["createdAt"],
|
|
'userId': reportData["userId"],
|
|
});
|
|
}
|
|
|
|
// Fetch all user data in one go
|
|
var userDocs = await Future.wait(userRefs.map((ref) => ref.get()));
|
|
|
|
for (var el in userDocs) {
|
|
int index = reportsWithUsers.indexWhere((r) => r["userId"] == el["uid"]);
|
|
reportsWithUsers[index]["name"] = el["name"];
|
|
reportsWithUsers[index]["phone"] = el["phone"];
|
|
reportsWithUsers[index]["age"] = el["age"];
|
|
reportsWithUsers[index]["schoolName"] = el["schoolName"];
|
|
reportsWithUsers[index]["stage"] = el["stage"];
|
|
reportsWithUsers[index]["address"] = el["address"];
|
|
reportsWithUsers[index]["nationalIdNumber"] = el["nationalIdNumber"];
|
|
}
|
|
|
|
return reportsWithUsers;
|
|
}
|
|
}
|