Initial commit — DLS lab-app Flutter project
This commit is contained in:
@@ -0,0 +1,581 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../../../core/theme/app_theme.dart';
|
||||
import '../../../models/connection.dart';
|
||||
import '../../../models/job.dart';
|
||||
import 'connection_stats_repository.dart';
|
||||
|
||||
class ConnectionDetailScreen extends StatefulWidget {
|
||||
const ConnectionDetailScreen({
|
||||
super.key,
|
||||
required this.connection,
|
||||
required this.labTenantId,
|
||||
});
|
||||
|
||||
final Connection connection;
|
||||
final String labTenantId;
|
||||
|
||||
@override
|
||||
State<ConnectionDetailScreen> createState() => _ConnectionDetailScreenState();
|
||||
}
|
||||
|
||||
class _ConnectionDetailScreenState extends State<ConnectionDetailScreen> {
|
||||
late Future<ConnectionStats> _future;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_load();
|
||||
}
|
||||
|
||||
void _load() {
|
||||
setState(() {
|
||||
_future = ConnectionStatsRepository.instance.fetchStats(
|
||||
labTenantId: widget.labTenantId,
|
||||
clinicTenantId: widget.connection.clinicTenantId,
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final conn = widget.connection;
|
||||
final clinicName = conn.clinicName ?? 'Klinik';
|
||||
|
||||
return Scaffold(
|
||||
backgroundColor: AppColors.background,
|
||||
body: CustomScrollView(
|
||||
slivers: [
|
||||
SliverAppBar(
|
||||
pinned: true,
|
||||
expandedHeight: 140,
|
||||
backgroundColor: AppColors.primary,
|
||||
foregroundColor: Colors.white,
|
||||
flexibleSpace: FlexibleSpaceBar(
|
||||
background: Container(
|
||||
decoration: const BoxDecoration(
|
||||
gradient: LinearGradient(
|
||||
begin: Alignment.topLeft,
|
||||
end: Alignment.bottomRight,
|
||||
colors: [Color(0xFF0F172A), AppColors.primary],
|
||||
),
|
||||
),
|
||||
child: SafeArea(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.fromLTRB(20, 48, 20, 16),
|
||||
child: Row(
|
||||
children: [
|
||||
Container(
|
||||
width: 52,
|
||||
height: 52,
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.white.withValues(alpha: 0.15),
|
||||
borderRadius: BorderRadius.circular(14),
|
||||
border: Border.all(color: Colors.white.withValues(alpha: 0.2)),
|
||||
),
|
||||
child: const Icon(Icons.local_hospital_outlined, color: Colors.white, size: 26),
|
||||
),
|
||||
const SizedBox(width: 14),
|
||||
Expanded(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Text(
|
||||
clinicName,
|
||||
style: const TextStyle(
|
||||
color: Colors.white,
|
||||
fontSize: 18,
|
||||
fontWeight: FontWeight.w700,
|
||||
),
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
const SizedBox(height: 3),
|
||||
_StatusBadge(status: conn.status),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
SliverToBoxAdapter(
|
||||
child: FutureBuilder<ConnectionStats>(
|
||||
future: _future,
|
||||
builder: (ctx, snap) {
|
||||
if (snap.connectionState == ConnectionState.waiting) {
|
||||
return const Padding(
|
||||
padding: EdgeInsets.symmetric(vertical: 80),
|
||||
child: Center(child: CircularProgressIndicator(color: AppColors.accent)),
|
||||
);
|
||||
}
|
||||
if (snap.hasError) {
|
||||
return Padding(
|
||||
padding: const EdgeInsets.all(32),
|
||||
child: Center(
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
const Icon(Icons.wifi_off_rounded, color: AppColors.cancelled, size: 40),
|
||||
const SizedBox(height: 12),
|
||||
Text('Hata: ${snap.error}', style: const TextStyle(color: AppColors.textSecondary), textAlign: TextAlign.center),
|
||||
const SizedBox(height: 12),
|
||||
FilledButton.icon(
|
||||
onPressed: _load,
|
||||
icon: const Icon(Icons.refresh_rounded, size: 16),
|
||||
label: const Text('Tekrar Dene'),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
final stats = snap.data!;
|
||||
return _StatsBody(stats: stats);
|
||||
},
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// ── Stats body ────────────────────────────────────────────────────────────────
|
||||
|
||||
class _StatsBody extends StatelessWidget {
|
||||
const _StatsBody({required this.stats});
|
||||
final ConnectionStats stats;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Padding(
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
// KPI row
|
||||
Row(
|
||||
children: [
|
||||
Expanded(child: _KpiCard(label: 'Toplam İş', value: '${stats.totalJobs}', icon: Icons.work_outline_rounded, color: AppColors.accent)),
|
||||
const SizedBox(width: 10),
|
||||
Expanded(child: _KpiCard(label: 'Aktif', value: '${stats.activeJobs}', icon: Icons.pending_outlined, color: AppColors.inProgress)),
|
||||
const SizedBox(width: 10),
|
||||
Expanded(child: _KpiCard(label: 'Teslim', value: '${stats.deliveredJobs}', icon: Icons.check_circle_outline, color: AppColors.success)),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 10),
|
||||
Row(
|
||||
children: [
|
||||
Expanded(child: _KpiCard(label: 'Bu Ay', value: '${stats.thisMonthJobs}', icon: Icons.calendar_month_outlined, color: AppColors.accent)),
|
||||
const SizedBox(width: 10),
|
||||
Expanded(child: _KpiCard(label: 'Geçen Ay', value: '${stats.lastMonthJobs}', icon: Icons.history_rounded, color: AppColors.textSecondary)),
|
||||
const SizedBox(width: 10),
|
||||
Expanded(child: _KpiCard(label: 'İptal', value: '${stats.cancelledJobs}', icon: Icons.cancel_outlined, color: AppColors.cancelled)),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
|
||||
// Revenue
|
||||
_SectionTitle('Finans'),
|
||||
const SizedBox(height: 8),
|
||||
_RevenueCard(stats: stats),
|
||||
const SizedBox(height: 16),
|
||||
|
||||
// Prosthetic type breakdown
|
||||
if (stats.byType.isNotEmpty) ...[
|
||||
_SectionTitle('Ürün Tipi Dağılımı'),
|
||||
const SizedBox(height: 8),
|
||||
_TypeBreakdownCard(byType: stats.byType, total: stats.totalJobs),
|
||||
const SizedBox(height: 16),
|
||||
],
|
||||
|
||||
// Monthly trend
|
||||
_SectionTitle('Aylık Karşılaştırma'),
|
||||
const SizedBox(height: 8),
|
||||
_MonthCompareCard(stats: stats),
|
||||
const SizedBox(height: 16),
|
||||
|
||||
// Recent jobs
|
||||
if (stats.recentJobs.isNotEmpty) ...[
|
||||
_SectionTitle('Son İşler'),
|
||||
const SizedBox(height: 8),
|
||||
_RecentJobsCard(jobs: stats.recentJobs),
|
||||
const SizedBox(height: 16),
|
||||
],
|
||||
|
||||
const SizedBox(height: 32),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// ── KPI card ──────────────────────────────────────────────────────────────────
|
||||
|
||||
class _KpiCard extends StatelessWidget {
|
||||
const _KpiCard({required this.label, required this.value, required this.icon, required this.color});
|
||||
final String label;
|
||||
final String value;
|
||||
final IconData icon;
|
||||
final Color color;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Container(
|
||||
padding: const EdgeInsets.all(14),
|
||||
decoration: BoxDecoration(
|
||||
color: AppColors.surface,
|
||||
borderRadius: BorderRadius.circular(14),
|
||||
border: Border.all(color: AppColors.border),
|
||||
boxShadow: [BoxShadow(color: Colors.black.withValues(alpha: 0.03), blurRadius: 6, offset: const Offset(0, 2))],
|
||||
),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Icon(icon, size: 18, color: color),
|
||||
const SizedBox(height: 8),
|
||||
Text(value, style: TextStyle(fontSize: 22, fontWeight: FontWeight.w800, color: color)),
|
||||
const SizedBox(height: 2),
|
||||
Text(label, style: const TextStyle(fontSize: 11, color: AppColors.textMuted, fontWeight: FontWeight.w500)),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// ── Revenue card ──────────────────────────────────────────────────────────────
|
||||
|
||||
class _RevenueCard extends StatelessWidget {
|
||||
const _RevenueCard({required this.stats});
|
||||
final ConnectionStats stats;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final collected = stats.totalRevenue - stats.pendingRevenue;
|
||||
return Container(
|
||||
padding: const EdgeInsets.all(16),
|
||||
decoration: BoxDecoration(
|
||||
color: AppColors.surface,
|
||||
borderRadius: BorderRadius.circular(14),
|
||||
border: Border.all(color: AppColors.border),
|
||||
),
|
||||
child: Column(
|
||||
children: [
|
||||
_RevRow(
|
||||
label: 'Toplam Gelir',
|
||||
value: _fmt(stats.totalRevenue),
|
||||
color: AppColors.textPrimary,
|
||||
bold: true,
|
||||
),
|
||||
const Divider(height: 20),
|
||||
_RevRow(label: 'Tahsil Edilen', value: _fmt(collected), color: AppColors.success),
|
||||
const SizedBox(height: 8),
|
||||
_RevRow(label: 'Bekleyen Alacak', value: _fmt(stats.pendingRevenue), color: AppColors.pending),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
String _fmt(double v) => '${v.toStringAsFixed(0)} TL';
|
||||
}
|
||||
|
||||
class _RevRow extends StatelessWidget {
|
||||
const _RevRow({required this.label, required this.value, required this.color, this.bold = false});
|
||||
final String label;
|
||||
final String value;
|
||||
final Color color;
|
||||
final bool bold;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final style = TextStyle(
|
||||
fontSize: bold ? 15 : 14,
|
||||
fontWeight: bold ? FontWeight.w700 : FontWeight.w500,
|
||||
color: color,
|
||||
);
|
||||
return Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
Text(label, style: style.copyWith(color: bold ? AppColors.textPrimary : AppColors.textSecondary)),
|
||||
Text(value, style: style),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// ── Type breakdown ────────────────────────────────────────────────────────────
|
||||
|
||||
class _TypeBreakdownCard extends StatelessWidget {
|
||||
const _TypeBreakdownCard({required this.byType, required this.total});
|
||||
final Map<String, int> byType;
|
||||
final int total;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final sorted = byType.entries.toList()..sort((a, b) => b.value.compareTo(a.value));
|
||||
|
||||
return Container(
|
||||
padding: const EdgeInsets.all(16),
|
||||
decoration: BoxDecoration(
|
||||
color: AppColors.surface,
|
||||
borderRadius: BorderRadius.circular(14),
|
||||
border: Border.all(color: AppColors.border),
|
||||
),
|
||||
child: Column(
|
||||
children: [
|
||||
for (int i = 0; i < sorted.length; i++) ...[
|
||||
if (i > 0) const SizedBox(height: 10),
|
||||
_TypeBar(
|
||||
label: _typeLabel(sorted[i].key),
|
||||
count: sorted[i].value,
|
||||
total: total,
|
||||
),
|
||||
],
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
String _typeLabel(String key) => switch (key) {
|
||||
'metal_porselen' => 'Metal Porselen',
|
||||
'zirkonyum' => 'Zirkonyum',
|
||||
'implant_ustu_zirkonyum'=> 'İmplant Üstü Zirkonyum',
|
||||
'gecici' => 'Geçici',
|
||||
'e_max' => 'E-Max',
|
||||
'tam_protez' => 'Tam Protez',
|
||||
'parsiyel' => 'Parsiyel Protez',
|
||||
_ => 'Diğer',
|
||||
};
|
||||
}
|
||||
|
||||
class _TypeBar extends StatelessWidget {
|
||||
const _TypeBar({required this.label, required this.count, required this.total});
|
||||
final String label;
|
||||
final int count;
|
||||
final int total;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final pct = total > 0 ? count / total : 0.0;
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
Text(label, style: const TextStyle(fontSize: 13, fontWeight: FontWeight.w500, color: AppColors.textPrimary)),
|
||||
Text('$count adet · ${(pct * 100).toStringAsFixed(0)}%',
|
||||
style: const TextStyle(fontSize: 12, color: AppColors.textMuted)),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 5),
|
||||
ClipRRect(
|
||||
borderRadius: BorderRadius.circular(4),
|
||||
child: LinearProgressIndicator(
|
||||
value: pct,
|
||||
minHeight: 7,
|
||||
backgroundColor: AppColors.border,
|
||||
valueColor: const AlwaysStoppedAnimation<Color>(AppColors.accent),
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// ── Monthly compare ───────────────────────────────────────────────────────────
|
||||
|
||||
class _MonthCompareCard extends StatelessWidget {
|
||||
const _MonthCompareCard({required this.stats});
|
||||
final ConnectionStats stats;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final thisMonth = stats.thisMonthJobs;
|
||||
final lastMonth = stats.lastMonthJobs;
|
||||
final maxBar = (thisMonth > lastMonth ? thisMonth : lastMonth).clamp(1, 999);
|
||||
final trend = lastMonth == 0
|
||||
? null
|
||||
: ((thisMonth - lastMonth) / lastMonth * 100).toStringAsFixed(0);
|
||||
final isUp = thisMonth >= lastMonth;
|
||||
|
||||
return Container(
|
||||
padding: const EdgeInsets.all(16),
|
||||
decoration: BoxDecoration(
|
||||
color: AppColors.surface,
|
||||
borderRadius: BorderRadius.circular(14),
|
||||
border: Border.all(color: AppColors.border),
|
||||
),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
if (trend != null) ...[
|
||||
Row(
|
||||
children: [
|
||||
Icon(
|
||||
isUp ? Icons.trending_up_rounded : Icons.trending_down_rounded,
|
||||
size: 18,
|
||||
color: isUp ? AppColors.success : AppColors.cancelled,
|
||||
),
|
||||
const SizedBox(width: 6),
|
||||
Text(
|
||||
isUp ? '+$trend% geçen aya göre' : '$trend% geçen aya göre',
|
||||
style: TextStyle(
|
||||
fontSize: 13,
|
||||
fontWeight: FontWeight.w600,
|
||||
color: isUp ? AppColors.success : AppColors.cancelled,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
],
|
||||
Row(
|
||||
crossAxisAlignment: CrossAxisAlignment.end,
|
||||
children: [
|
||||
_BarColumn(label: 'Geçen Ay', count: lastMonth, maxCount: maxBar, color: AppColors.border),
|
||||
const SizedBox(width: 16),
|
||||
_BarColumn(label: 'Bu Ay', count: thisMonth, maxCount: maxBar, color: AppColors.accent),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _BarColumn extends StatelessWidget {
|
||||
const _BarColumn({required this.label, required this.count, required this.maxCount, required this.color});
|
||||
final String label;
|
||||
final int count;
|
||||
final int maxCount;
|
||||
final Color color;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final height = maxCount > 0 ? (count / maxCount * 80).clamp(4.0, 80.0) : 4.0;
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.center,
|
||||
children: [
|
||||
Text('$count', style: TextStyle(fontSize: 16, fontWeight: FontWeight.w700, color: color == AppColors.border ? AppColors.textSecondary : AppColors.accent)),
|
||||
const SizedBox(height: 4),
|
||||
Container(
|
||||
width: 40,
|
||||
height: height,
|
||||
decoration: BoxDecoration(
|
||||
color: color,
|
||||
borderRadius: BorderRadius.circular(4),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 4),
|
||||
Text(label, style: const TextStyle(fontSize: 11, color: AppColors.textMuted)),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// ── Recent jobs ───────────────────────────────────────────────────────────────
|
||||
|
||||
class _RecentJobsCard extends StatelessWidget {
|
||||
const _RecentJobsCard({required this.jobs});
|
||||
final List<Job> jobs;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Container(
|
||||
decoration: BoxDecoration(
|
||||
color: AppColors.surface,
|
||||
borderRadius: BorderRadius.circular(14),
|
||||
border: Border.all(color: AppColors.border),
|
||||
),
|
||||
child: Column(
|
||||
children: [
|
||||
for (int i = 0; i < jobs.length; i++) ...[
|
||||
if (i > 0) const Divider(height: 1),
|
||||
_RecentJobRow(job: jobs[i]),
|
||||
],
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _RecentJobRow extends StatelessWidget {
|
||||
const _RecentJobRow({required this.job});
|
||||
final Job job;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final color = job.status == JobStatus.delivered ? AppColors.success : AppColors.inProgress;
|
||||
final bg = job.status == JobStatus.delivered ? AppColors.successBg : AppColors.inProgressBg;
|
||||
return Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 14, vertical: 11),
|
||||
child: Row(
|
||||
children: [
|
||||
Container(
|
||||
width: 36,
|
||||
height: 36,
|
||||
decoration: BoxDecoration(color: bg, borderRadius: BorderRadius.circular(10)),
|
||||
child: Center(child: Icon(Icons.assignment_outlined, size: 18, color: color)),
|
||||
),
|
||||
const SizedBox(width: 10),
|
||||
Expanded(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(job.patientCode, style: const TextStyle(fontSize: 14, fontWeight: FontWeight.w600, color: AppColors.textPrimary)),
|
||||
Text(job.prostheticType.label, style: const TextStyle(fontSize: 12, color: AppColors.textSecondary)),
|
||||
],
|
||||
),
|
||||
),
|
||||
Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 3),
|
||||
decoration: BoxDecoration(color: bg, borderRadius: BorderRadius.circular(6)),
|
||||
child: Text(job.status.label, style: TextStyle(fontSize: 11, fontWeight: FontWeight.w600, color: color)),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// ── Helpers ───────────────────────────────────────────────────────────────────
|
||||
|
||||
class _SectionTitle extends StatelessWidget {
|
||||
const _SectionTitle(this.text);
|
||||
final String text;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Text(text, style: const TextStyle(fontSize: 13, fontWeight: FontWeight.w700, color: AppColors.textMuted, letterSpacing: 0.5));
|
||||
}
|
||||
}
|
||||
|
||||
class _StatusBadge extends StatelessWidget {
|
||||
const _StatusBadge({required this.status});
|
||||
final ConnectionStatus status;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final (label, color) = switch (status) {
|
||||
ConnectionStatus.approved => ('Onaylı', AppColors.success),
|
||||
ConnectionStatus.pending => ('Bekliyor', AppColors.pending),
|
||||
ConnectionStatus.rejected => ('Reddedildi', AppColors.cancelled),
|
||||
};
|
||||
return Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 3),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.white.withValues(alpha: 0.15),
|
||||
borderRadius: BorderRadius.circular(6),
|
||||
),
|
||||
child: Text(label, style: TextStyle(fontSize: 11, fontWeight: FontWeight.w600, color: color)),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,124 @@
|
||||
import 'package:pocketbase/pocketbase.dart';
|
||||
import '../../../core/api/pocketbase_client.dart';
|
||||
import '../../../models/job.dart';
|
||||
|
||||
class ConnectionStats {
|
||||
const ConnectionStats({
|
||||
required this.totalJobs,
|
||||
required this.byStatus,
|
||||
required this.byType,
|
||||
required this.totalRevenue,
|
||||
required this.pendingRevenue,
|
||||
required this.thisMonthJobs,
|
||||
required this.lastMonthJobs,
|
||||
required this.revisionCount,
|
||||
required this.recentJobs,
|
||||
});
|
||||
|
||||
final int totalJobs;
|
||||
final Map<String, int> byStatus; // 'in_progress', 'sent', 'delivered', 'cancelled'
|
||||
final Map<String, int> byType; // prosthetic_type -> count
|
||||
final double totalRevenue;
|
||||
final double pendingRevenue;
|
||||
final int thisMonthJobs;
|
||||
final int lastMonthJobs;
|
||||
final int revisionCount;
|
||||
final List<Job> recentJobs;
|
||||
|
||||
int get deliveredJobs => byStatus['delivered'] ?? 0;
|
||||
int get activeJobs => (byStatus['in_progress'] ?? 0) + (byStatus['sent'] ?? 0);
|
||||
int get cancelledJobs => byStatus['cancelled'] ?? 0;
|
||||
double get revisionRate => totalJobs > 0 ? revisionCount / totalJobs * 100 : 0;
|
||||
double get completionRate => totalJobs > 0 ? deliveredJobs / totalJobs * 100 : 0;
|
||||
}
|
||||
|
||||
class ConnectionStatsRepository {
|
||||
ConnectionStatsRepository._();
|
||||
static final instance = ConnectionStatsRepository._();
|
||||
|
||||
PocketBase get _pb => PocketBaseClient.instance.pb;
|
||||
|
||||
Future<ConnectionStats> fetchStats({
|
||||
required String labTenantId,
|
||||
required String clinicTenantId,
|
||||
}) async {
|
||||
final now = DateTime.now();
|
||||
final thisMonthStart = DateTime(now.year, now.month, 1);
|
||||
final lastMonthStart = DateTime(now.year, now.month - 1, 1);
|
||||
final lastMonthEnd = thisMonthStart.subtract(const Duration(seconds: 1));
|
||||
|
||||
final filter = 'lab_tenant_id = "$labTenantId" && clinic_tenant_id = "$clinicTenantId"';
|
||||
|
||||
final results = await Future.wait([
|
||||
_pb.collection('jobs').getList(
|
||||
filter: filter,
|
||||
perPage: 500,
|
||||
expand: 'clinic_tenant_id,lab_tenant_id',
|
||||
),
|
||||
_pb.collection('finance_entries').getList(
|
||||
filter: 'tenant_id = "$labTenantId" && job_id.clinic_tenant_id = "$clinicTenantId"',
|
||||
perPage: 500,
|
||||
),
|
||||
]);
|
||||
|
||||
final jobsResult = results[0];
|
||||
final financeResult = results[1];
|
||||
|
||||
final allJobs = jobsResult.items
|
||||
.map((r) => Job.fromJson(r.toJson()))
|
||||
.toList();
|
||||
|
||||
// Status breakdown
|
||||
final byStatus = <String, int>{};
|
||||
final byType = <String, int>{};
|
||||
int thisMonthJobs = 0;
|
||||
int lastMonthJobs = 0;
|
||||
int revisionCount = 0;
|
||||
|
||||
for (final job in allJobs) {
|
||||
// Status
|
||||
final s = job.status.value;
|
||||
byStatus[s] = (byStatus[s] ?? 0) + 1;
|
||||
|
||||
// Type
|
||||
final t = job.prostheticType.value;
|
||||
byType[t] = (byType[t] ?? 0) + 1;
|
||||
|
||||
// Monthly
|
||||
final created = job.dateCreated;
|
||||
if (created.isAfter(thisMonthStart)) thisMonthJobs++;
|
||||
else if (created.isAfter(lastMonthStart) && created.isBefore(lastMonthEnd)) lastMonthJobs++;
|
||||
|
||||
// Revision
|
||||
if (job.status == JobStatus.inProgress && job.currentStep == null) revisionCount++;
|
||||
}
|
||||
|
||||
// Finance
|
||||
double totalRevenue = 0;
|
||||
double pendingRevenue = 0;
|
||||
for (final r in financeResult.items) {
|
||||
final j = r.toJson();
|
||||
final amount = (j['amount'] as num?)?.toDouble() ?? 0;
|
||||
totalRevenue += amount;
|
||||
if (j['status'] == 'pending') pendingRevenue += amount;
|
||||
}
|
||||
|
||||
// Recent jobs (last 5 delivered or sent)
|
||||
final recent = allJobs
|
||||
.where((j) => j.status == JobStatus.delivered || j.status == JobStatus.sent)
|
||||
.toList()
|
||||
..sort((a, b) => b.dateCreated.compareTo(a.dateCreated));
|
||||
|
||||
return ConnectionStats(
|
||||
totalJobs: allJobs.length,
|
||||
byStatus: byStatus,
|
||||
byType: byType,
|
||||
totalRevenue: totalRevenue,
|
||||
pendingRevenue: pendingRevenue,
|
||||
thisMonthJobs: thisMonthJobs,
|
||||
lastMonthJobs: lastMonthJobs,
|
||||
revisionCount: revisionCount,
|
||||
recentJobs: recent.take(5).toList(),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
import 'package:pocketbase/pocketbase.dart';
|
||||
import '../../../core/api/pocketbase_client.dart';
|
||||
import '../../../models/connection.dart';
|
||||
|
||||
class LabConnectionsRepository {
|
||||
LabConnectionsRepository._();
|
||||
static final instance = LabConnectionsRepository._();
|
||||
|
||||
PocketBase get _pb => PocketBaseClient.instance.pb;
|
||||
|
||||
Future<List<Connection>> listConnections(String labTenantId) async {
|
||||
final result = await _pb.collection('connections').getList(
|
||||
filter: 'lab_tenant_id = "$labTenantId"',
|
||||
expand: 'clinic_tenant_id,lab_tenant_id',
|
||||
perPage: 100,
|
||||
);
|
||||
return (result.items.map((r) => Connection.fromJson(r.toJson())).toList()
|
||||
..sort((a, b) => (b.dateCreated ?? '').compareTo(a.dateCreated ?? '')));
|
||||
}
|
||||
|
||||
Future<Connection> respondToRequest({
|
||||
required String connectionId,
|
||||
required bool approve,
|
||||
}) async {
|
||||
final record = await _pb.collection('connections').update(connectionId, body: {
|
||||
'status': approve ? 'approved' : 'rejected',
|
||||
});
|
||||
return Connection.fromJson(record.toJson());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,453 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import '../../../core/providers/auth_provider.dart';
|
||||
import '../../../core/theme/app_theme.dart';
|
||||
import '../../../core/widgets/gradient_app_bar.dart';
|
||||
import '../../../models/connection.dart';
|
||||
import 'connection_detail_screen.dart';
|
||||
import 'lab_connections_repository.dart';
|
||||
|
||||
class LabConnectionsScreen extends ConsumerStatefulWidget {
|
||||
const LabConnectionsScreen({super.key});
|
||||
|
||||
@override
|
||||
ConsumerState<LabConnectionsScreen> createState() =>
|
||||
_LabConnectionsScreenState();
|
||||
}
|
||||
|
||||
class _LabConnectionsScreenState extends ConsumerState<LabConnectionsScreen> {
|
||||
late Future<List<Connection>> _future;
|
||||
final _searchController = TextEditingController();
|
||||
String _searchQuery = '';
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_load();
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_searchController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
void _load() {
|
||||
final tenantId = ref.read(authProvider).activeTenant!.tenant.id;
|
||||
setState(() {
|
||||
_future = LabConnectionsRepository.instance.listConnections(tenantId);
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> _respond(String connectionId, bool approve) async {
|
||||
try {
|
||||
await LabConnectionsRepository.instance.respondToRequest(
|
||||
connectionId: connectionId,
|
||||
approve: approve,
|
||||
);
|
||||
_load();
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(
|
||||
approve ? 'Bağlantı onaylandı' : 'Bağlantı reddedildi'),
|
||||
),
|
||||
);
|
||||
}
|
||||
} catch (e) {
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text('Hata: $e')),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Color _statusColor(ConnectionStatus status) {
|
||||
return switch (status) {
|
||||
ConnectionStatus.pending => AppColors.pending,
|
||||
ConnectionStatus.approved => AppColors.success,
|
||||
ConnectionStatus.rejected => AppColors.cancelled,
|
||||
};
|
||||
}
|
||||
|
||||
Color _statusBg(ConnectionStatus status) {
|
||||
return switch (status) {
|
||||
ConnectionStatus.pending => AppColors.pendingBg,
|
||||
ConnectionStatus.approved => AppColors.successBg,
|
||||
ConnectionStatus.rejected => AppColors.cancelledBg,
|
||||
};
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
backgroundColor: AppColors.background,
|
||||
appBar: GradientAppBar(
|
||||
title: 'Bağlantılar',
|
||||
category: 'LABORATUVAR',
|
||||
searchController: _searchController,
|
||||
onSearchChanged: (v) => setState(() => _searchQuery = v),
|
||||
searchHint: 'Klinik adı ara...',
|
||||
),
|
||||
body: RefreshIndicator(
|
||||
color: AppColors.accent,
|
||||
onRefresh: () async => _load(),
|
||||
child: FutureBuilder<List<Connection>>(
|
||||
future: _future,
|
||||
builder: (ctx, snap) {
|
||||
if (snap.connectionState == ConnectionState.waiting) {
|
||||
return const Center(
|
||||
child: CircularProgressIndicator(color: AppColors.accent));
|
||||
}
|
||||
if (snap.hasError) {
|
||||
return Center(
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Container(
|
||||
width: 64,
|
||||
height: 64,
|
||||
decoration: BoxDecoration(
|
||||
color: AppColors.cancelledBg,
|
||||
borderRadius: BorderRadius.circular(16)),
|
||||
child: const Icon(Icons.wifi_off_rounded,
|
||||
color: AppColors.cancelled, size: 30),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
Text('Hata: ${snap.error}',
|
||||
style:
|
||||
const TextStyle(color: AppColors.textSecondary)),
|
||||
const SizedBox(height: 12),
|
||||
FilledButton.icon(
|
||||
onPressed: _load,
|
||||
icon: const Icon(Icons.refresh_rounded, size: 18),
|
||||
label: const Text('Tekrar Dene'),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
final allConnections = snap.data!;
|
||||
final q = _searchQuery.toLowerCase().trim();
|
||||
final connections = q.isEmpty
|
||||
? allConnections
|
||||
: allConnections.where((c) =>
|
||||
(c.clinicName ?? '').toLowerCase().contains(q)).toList();
|
||||
|
||||
if (allConnections.isEmpty) {
|
||||
return Center(
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Container(
|
||||
width: 72,
|
||||
height: 72,
|
||||
decoration: BoxDecoration(
|
||||
color: AppColors.inProgressBg,
|
||||
borderRadius: BorderRadius.circular(20)),
|
||||
child: const Icon(Icons.link_off,
|
||||
color: AppColors.inProgress, size: 32),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
const Text(
|
||||
'Henüz bağlantı yok',
|
||||
style: TextStyle(
|
||||
fontSize: 16,
|
||||
fontWeight: FontWeight.w600,
|
||||
color: AppColors.textPrimary),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
const Text(
|
||||
'Kliniklerden gelen istekler burada görünür.',
|
||||
style: TextStyle(
|
||||
color: AppColors.textSecondary, fontSize: 13),
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
final pending = connections
|
||||
.where((c) => c.status == ConnectionStatus.pending)
|
||||
.toList();
|
||||
final others = connections
|
||||
.where((c) => c.status != ConnectionStatus.pending)
|
||||
.toList();
|
||||
|
||||
return ListView(
|
||||
padding: const EdgeInsets.fromLTRB(16, 12, 16, 24),
|
||||
children: [
|
||||
if (pending.isNotEmpty) ...[
|
||||
_SectionHeader(
|
||||
label: 'Bekleyen İstekler',
|
||||
count: pending.length,
|
||||
countColor: AppColors.pending,
|
||||
countBg: AppColors.pendingBg,
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
...pending.map((c) => Padding(
|
||||
padding: const EdgeInsets.only(bottom: 10),
|
||||
child: _ConnectionCard(
|
||||
connection: c,
|
||||
statusColor: _statusColor(c.status),
|
||||
statusBg: _statusBg(c.status),
|
||||
onApprove: () => _respond(c.id, true),
|
||||
onReject: () => _respond(c.id, false),
|
||||
),
|
||||
)),
|
||||
const SizedBox(height: 8),
|
||||
],
|
||||
if (others.isNotEmpty) ...[
|
||||
_SectionHeader(
|
||||
label: 'Bağlantılar',
|
||||
count: others.length,
|
||||
countColor: AppColors.textSecondary,
|
||||
countBg: AppColors.surfaceVariant,
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
...others.map((c) {
|
||||
final tenantId = ref.read(authProvider).activeTenant!.tenant.id;
|
||||
return Padding(
|
||||
padding: const EdgeInsets.only(bottom: 10),
|
||||
child: _ConnectionCard(
|
||||
connection: c,
|
||||
statusColor: _statusColor(c.status),
|
||||
statusBg: _statusBg(c.status),
|
||||
onTap: c.status == ConnectionStatus.approved
|
||||
? () => Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (_) => ConnectionDetailScreen(
|
||||
connection: c,
|
||||
labTenantId: tenantId,
|
||||
),
|
||||
),
|
||||
)
|
||||
: null,
|
||||
),
|
||||
);
|
||||
}),
|
||||
],
|
||||
],
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _SectionHeader extends StatelessWidget {
|
||||
const _SectionHeader({
|
||||
required this.label,
|
||||
required this.count,
|
||||
required this.countColor,
|
||||
required this.countBg,
|
||||
});
|
||||
final String label;
|
||||
final int count;
|
||||
final Color countColor;
|
||||
final Color countBg;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Row(
|
||||
children: [
|
||||
Text(
|
||||
label,
|
||||
style: const TextStyle(
|
||||
fontSize: 13,
|
||||
fontWeight: FontWeight.w600,
|
||||
color: AppColors.accent,
|
||||
letterSpacing: 0.3,
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 2),
|
||||
decoration: BoxDecoration(
|
||||
color: countBg,
|
||||
borderRadius: BorderRadius.circular(10),
|
||||
),
|
||||
child: Text(
|
||||
'$count',
|
||||
style: TextStyle(
|
||||
color: countColor,
|
||||
fontSize: 12,
|
||||
fontWeight: FontWeight.w600,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _ConnectionCard extends StatefulWidget {
|
||||
const _ConnectionCard({
|
||||
required this.connection,
|
||||
required this.statusColor,
|
||||
required this.statusBg,
|
||||
this.onApprove,
|
||||
this.onReject,
|
||||
this.onTap,
|
||||
});
|
||||
final Connection connection;
|
||||
final Color statusColor;
|
||||
final Color statusBg;
|
||||
final VoidCallback? onApprove;
|
||||
final VoidCallback? onReject;
|
||||
final VoidCallback? onTap;
|
||||
|
||||
@override
|
||||
State<_ConnectionCard> createState() => _ConnectionCardState();
|
||||
}
|
||||
|
||||
class _ConnectionCardState extends State<_ConnectionCard> {
|
||||
bool _loading = false;
|
||||
|
||||
Future<void> _act(VoidCallback? cb) async {
|
||||
if (cb == null) return;
|
||||
setState(() => _loading = true);
|
||||
cb();
|
||||
await Future.delayed(const Duration(milliseconds: 500));
|
||||
if (mounted) setState(() => _loading = false);
|
||||
}
|
||||
|
||||
String _formatDate(String? raw) {
|
||||
if (raw == null) return '';
|
||||
try {
|
||||
final dt = DateTime.parse(raw);
|
||||
return '${dt.day.toString().padLeft(2, '0')}.${dt.month.toString().padLeft(2, '0')}.${dt.year}';
|
||||
} catch (_) {
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final c = widget.connection;
|
||||
final isPending = c.status == ConnectionStatus.pending;
|
||||
|
||||
return Material(
|
||||
color: AppColors.surface,
|
||||
borderRadius: BorderRadius.circular(14),
|
||||
child: InkWell(
|
||||
onTap: widget.onTap,
|
||||
borderRadius: BorderRadius.circular(14),
|
||||
child: Container(
|
||||
padding: const EdgeInsets.all(14),
|
||||
decoration: BoxDecoration(
|
||||
borderRadius: BorderRadius.circular(14),
|
||||
border: Border.all(color: AppColors.border),
|
||||
boxShadow: [
|
||||
BoxShadow(
|
||||
color: Colors.black.withValues(alpha: 0.04),
|
||||
blurRadius: 8,
|
||||
offset: const Offset(0, 2))
|
||||
],
|
||||
),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Row(
|
||||
children: [
|
||||
Container(
|
||||
width: 44,
|
||||
height: 44,
|
||||
decoration: BoxDecoration(
|
||||
color: widget.statusBg,
|
||||
borderRadius: BorderRadius.circular(12)),
|
||||
child: Icon(Icons.business_outlined,
|
||||
color: widget.statusColor, size: 22),
|
||||
),
|
||||
const SizedBox(width: 12),
|
||||
Expanded(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
c.clinicName ?? 'Klinik',
|
||||
style: const TextStyle(
|
||||
fontSize: 15,
|
||||
fontWeight: FontWeight.w600,
|
||||
color: AppColors.textPrimary),
|
||||
),
|
||||
if (c.dateCreated != null) ...[
|
||||
const SizedBox(height: 2),
|
||||
Text(
|
||||
_formatDate(c.dateCreated),
|
||||
style: const TextStyle(
|
||||
color: AppColors.textMuted, fontSize: 12),
|
||||
),
|
||||
],
|
||||
],
|
||||
),
|
||||
),
|
||||
Container(
|
||||
padding:
|
||||
const EdgeInsets.symmetric(horizontal: 10, vertical: 4),
|
||||
decoration: BoxDecoration(
|
||||
color: widget.statusBg,
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
),
|
||||
child: Text(
|
||||
c.status.label,
|
||||
style: TextStyle(
|
||||
color: widget.statusColor,
|
||||
fontWeight: FontWeight.w600,
|
||||
fontSize: 12,
|
||||
),
|
||||
),
|
||||
),
|
||||
if (widget.onTap != null) ...[
|
||||
const SizedBox(width: 4),
|
||||
const Icon(Icons.chevron_right_rounded,
|
||||
size: 18, color: AppColors.textMuted),
|
||||
],
|
||||
],
|
||||
),
|
||||
if (isPending) ...[
|
||||
const SizedBox(height: 12),
|
||||
Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: OutlinedButton(
|
||||
onPressed:
|
||||
_loading ? null : () => _act(widget.onReject),
|
||||
style: OutlinedButton.styleFrom(
|
||||
foregroundColor: AppColors.cancelled,
|
||||
side: const BorderSide(color: AppColors.cancelled)),
|
||||
child: const Text('Reddet'),
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
Expanded(
|
||||
child: FilledButton(
|
||||
onPressed:
|
||||
_loading ? null : () => _act(widget.onApprove),
|
||||
style: FilledButton.styleFrom(
|
||||
backgroundColor: AppColors.success),
|
||||
child: _loading
|
||||
? const SizedBox(
|
||||
width: 16,
|
||||
height: 16,
|
||||
child: CircularProgressIndicator(
|
||||
strokeWidth: 2, color: Colors.white),
|
||||
)
|
||||
: const Text('Onayla'),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user