import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:intl/intl.dart' hide TextDirection;
import 'package:sefer_admin1/controller/functions/launch.dart';

import '../../../controller/admin/static_controller.dart';
import '../../widgets/mycircular.dart';

class DailyNotesView extends StatelessWidget {
  const DailyNotesView({super.key});

  @override
  Widget build(BuildContext context) {
    // نستخدم نفس الكونترولر للوصول لدالة جلب الملاحظات
    final controller = Get.find<StaticController>();

    // عند فتح الصفحة، نجلب ملاحظات اليوم الحالي
    WidgetsBinding.instance.addPostFrameCallback((_) {
      controller.fetchDailyNotes(DateTime.now());
    });

    return Scaffold(
      backgroundColor: const Color(0xFFF0F2F5),
      appBar: AppBar(
        title: Text(
          'سجل المكالمات اليومي',
          style: const TextStyle(
            color: Color(0xFF1A1A1A),
            fontWeight: FontWeight.w800,
            fontSize: 20,
          ),
        ),
        centerTitle: true,
        backgroundColor: Colors.white,
        elevation: 0,
        iconTheme: const IconThemeData(color: Colors.black87),
      ),
      body: GetBuilder<StaticController>(
        builder: (controller) {
          if (controller.isLoadingNotes) {
            return const Center(child: MyCircularProgressIndicator());
          }

          if (controller.dailyNotesList.isEmpty) {
            return Center(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Icon(Icons.note_alt_outlined,
                      size: 80, color: Colors.grey.shade300),
                  const SizedBox(height: 10),
                  Text("لا توجد سجلات لهذا اليوم",
                      style: TextStyle(color: Colors.grey.shade600)),
                ],
              ),
            );
          }

          return ListView.separated(
            padding: const EdgeInsets.all(16),
            itemCount: controller.dailyNotesList.length,
            separatorBuilder: (context, index) => const SizedBox(height: 12),
            itemBuilder: (context, index) {
              final note = controller.dailyNotesList[index];
              final String name = note['editor'] ?? note['name'] ?? 'Unknown';
              final String phone = note['phone'] ?? note['phone'] ?? 'Unknown';
              final String content = note['note'] ?? note['content'] ?? '';
              final String time = note['createdAt'] ?? '';

              return Container(
                padding: const EdgeInsets.all(16),
                decoration: BoxDecoration(
                    color: Colors.white,
                    borderRadius: BorderRadius.circular(16),
                    boxShadow: [
                      BoxShadow(
                        color: Colors.black.withOpacity(0.03),
                        blurRadius: 10,
                        offset: const Offset(0, 4),
                      )
                    ],
                    border: Border(
                        right: BorderSide(
                            color: _getEmployeeColor(name), width: 4))),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Row(
                          children: [
                            CircleAvatar(
                              radius: 14,
                              backgroundColor:
                                  _getEmployeeColor(name).withOpacity(0.1),
                              child: Icon(Icons.person,
                                  size: 16, color: _getEmployeeColor(name)),
                            ),
                            const SizedBox(width: 8),
                            Text(
                              name.toUpperCase(),
                              style: TextStyle(
                                  fontWeight: FontWeight.bold,
                                  color: Colors.grey.shade800,
                                  fontSize: 14),
                            ),
                            const SizedBox(width: 100),
                            InkWell(
                              onTap: () {
                                makePhoneCall('+$phone');
                              },
                              child: Row(
                                children: [
                                  Text(
                                    phone,
                                    style: TextStyle(
                                        fontWeight: FontWeight.bold,
                                        color: Colors.grey.shade800,
                                        fontSize: 14),
                                  ),
                                  Icon(Icons.phone)
                                ],
                              ),
                            ),
                            const SizedBox(width: 22),
                            Text(
                              time.split(' ').last, // عرض الوقت فقط
                              style: TextStyle(
                                  color: Colors.grey.shade400, fontSize: 12),
                              textDirection: TextDirection.ltr,
                            ),
                          ],
                        ),
                      ],
                    ),
                    const Divider(height: 20),
                    Text(
                      content,
                      style: TextStyle(
                          fontSize: 14,
                          color: Colors.grey.shade700,
                          height: 1.5),
                    ),
                  ],
                ),
              );
            },
          );
        },
      ),
    );
  }

  Color _getEmployeeColor(String name) {
    String n = name.toLowerCase().trim();
    if (n.contains('shahd')) return Colors.redAccent;
    if (n.contains('mayar')) return Colors.amber.shade700;
    if (n.contains('rama2')) return Colors.green;
    if (n.contains('rama1')) return Colors.blue;
    return Colors.blueGrey;
  }
}
