import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:intl/intl.dart';
import 'package:praresi/presentation/controllers/riwayat_bulanan_controller.dart';
import 'package:praresi/presentation/views/menu_akun/detail_riwayat_pengiriman.dart';

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

  @override
  Widget build(BuildContext context) {
    final riwayatBulananC = Get.put(RiwayatBulananController(), permanent: true);

    return Scaffold(
      backgroundColor: Colors.grey[50],
      appBar: AppBar(
        backgroundColor: const Color(0xFF1976D2),
        elevation: 0,
        iconTheme: const IconThemeData(color: Colors.white),
        centerTitle: true,
        title: const Text(
          'Riwayat Pengiriman',
          style: TextStyle(color: Colors.white),
        ),
      ),
      body: Obx(() {
        if (riwayatBulananC.isLoading.value) {
          return const Center(
            child: CircularProgressIndicator(color: Color(0xFF1976D2)),
          );
        }

        if (riwayatBulananC.monthlySummary.isEmpty) {
          return const Center(
            child: Text(
              "Belum ada data bulanan",
              style: TextStyle(color: Colors.black54, fontSize: 16),
            ),
          );
        }

        return SingleChildScrollView(
          padding: const EdgeInsets.all(16),
          child: Column(
            children: riwayatBulananC.monthlySummary.map((data) {
              return Padding(
                padding: const EdgeInsets.only(bottom: 15),
                child: _monthlyCard(riwayatBulananC, data),
              );
            }).toList(),
          ),
        );
      }),
    );
  }

  Widget _monthlyCard(
      RiwayatBulananController controller, Map<String, dynamic> data) {
    final month = data['month'];
    final total = data['total'];
    final cod = data['cod'];
    final nonCod = data['nonCod'];
    final incomeCod = data['incomeCod'];
    final incomeNonCod = data['incomeNonCod'];
    final totalIncome = incomeCod + incomeNonCod;

    return InkWell(
    onTap: () {
      // Navigasi ke halaman detail riwayat bulanan
      Get.to(() => RiwayatBulananDetailView(month: month));
    },
    borderRadius: BorderRadius.circular(20),
    child: Container(
      width: double.infinity,
      padding: const EdgeInsets.all(18),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(20),
        boxShadow: [
          BoxShadow(
            color: Colors.black.withOpacity(0.08),
            blurRadius: 8,
            offset: const Offset(0, 5),
          ),
        ],
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          // 🔹 Nama Bulan
          Text(
            month,
            style: const TextStyle(
              fontSize: 15,
              fontWeight: FontWeight.bold,
              color: Colors.black87,
            ),
          ),
          const SizedBox(height: 15),

          // 🔹 Total dan COD/NON-COD
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Expanded(
                flex: 2,
                child: Container(
                  height: 90,
                  margin: const EdgeInsets.only(right: 10),
                  decoration: BoxDecoration(
                    border: Border.all(color: const Color(0xFF1976D2), width: 2),
                    borderRadius: BorderRadius.circular(14),
                  ),
                  child: Center(
                    child: Text(
                      "Total \n$total",
                      textAlign: TextAlign.center,
                      style: const TextStyle(
                        fontSize: 16,
                        fontWeight: FontWeight.bold,
                        color: Color(0xFF1976D2),
                      ),
                    ),
                  ),
                ),
              ),
              Expanded(
                flex: 2,
                child: Column(
                  children: [
                    // 🔸 COD
                    InkWell(
                      onTap: () async {
                        final income = await controller.getPendapatanBulanan(month);
                        final codPendapatan = income['cod'] ?? 0.0;

                        Get.defaultDialog(
                          title: "Pendapatan COD",
                          titleStyle: const TextStyle(
                            color: Color(0xFFFF9800),
                            fontWeight: FontWeight.bold,
                            fontSize: 18,
                          ),
                          radius: 14,
                          content: Column(
                            children: [
                              const Icon(Icons.local_shipping,
                                  color: Color(0xFFFF9800), size: 48),
                              const SizedBox(height: 12),
                              Text(
                                month,
                                style: const TextStyle(fontSize: 14),
                              ),
                              const SizedBox(height: 8),
                              Text(
                                "Rp ${NumberFormat('#,###', 'id_ID').format(codPendapatan)}",
                                style: const TextStyle(
                                  fontSize: 22,
                                  fontWeight: FontWeight.bold,
                                  color: Color(0xFFFF9800),
                                ),
                              ),
                            ],
                          ),
                          confirm: ElevatedButton(
                            style: ElevatedButton.styleFrom(
                              backgroundColor: const Color(0xFFFF9800),
                              shape: RoundedRectangleBorder(
                                borderRadius: BorderRadius.circular(12),
                              ),
                            ),
                            onPressed: () => Get.back(),
                            child: const Text(
                              "Tutup",
                              style: TextStyle(color: Colors.white),
                            ),
                          ),
                        );
                      },
                      child: Container(
                        height: 40,
                        margin: const EdgeInsets.only(bottom: 10),
                        decoration: BoxDecoration(
                          border: Border.all(color: const Color(0xFFFF9800), width: 2),
                          borderRadius: BorderRadius.circular(12),
                        ),
                        child: Center(
                          child: Text(
                            "COD: $cod",
                            style: const TextStyle(
                              color: Color(0xFFFF9800),
                              fontWeight: FontWeight.bold,
                              fontSize: 14,
                            ),
                          ),
                        ),
                      ),
                    ),

                    // 🔸 Non COD
                    InkWell(
                      onTap: () async {
                        final income = await controller.getPendapatanBulanan(month);
                        final nonCodPendapatan = income['nonCod'] ?? 0.0;

                        Get.defaultDialog(
                          title: "Pendapatan Non COD",
                          titleStyle: const TextStyle(
                            color: Color(0xFF4CAF50),
                            fontWeight: FontWeight.bold,
                            fontSize: 18,
                          ),
                          radius: 14,
                          content: Column(
                            children: [
                              const Icon(Icons.account_balance_wallet,
                                  color: Color(0xFF4CAF50), size: 48),
                              const SizedBox(height: 12),
                              Text(
                                month,
                                style: const TextStyle(fontSize: 14),
                              ),
                              const SizedBox(height: 8),
                              Text(
                                "Rp ${NumberFormat('#,###', 'id_ID').format(nonCodPendapatan)}",
                                style: const TextStyle(
                                  fontSize: 22,
                                  fontWeight: FontWeight.bold,
                                  color: Color(0xFF4CAF50),
                                ),
                              ),
                            ],
                          ),
                          confirm: ElevatedButton(
                            style: ElevatedButton.styleFrom(
                              backgroundColor: const Color(0xFF4CAF50),
                              shape: RoundedRectangleBorder(
                                borderRadius: BorderRadius.circular(12),
                              ),
                            ),
                            onPressed: () => Get.back(),
                            child: const Text(
                              "Tutup",
                              style: TextStyle(color: Colors.white),
                            ),
                          ),
                        );
                      },
                      child: Container(
                        height: 40,
                        decoration: BoxDecoration(
                          border: Border.all(color: const Color(0xFF4CAF50), width: 2),
                          borderRadius: BorderRadius.circular(12),
                        ),
                        child: Center(
                          child: Text(
                            "Non COD: $nonCod",
                            style: const TextStyle(
                              color: Color(0xFF4CAF50),
                              fontWeight: FontWeight.bold,
                              fontSize: 14,
                            ),
                          ),
                        ),
                      ),
                    ),
                  ],
                ),
              ),
            ],
          ),

          const SizedBox(height: 10),

          // 🔸 Pendapatan Total
          Container(
            width: double.infinity,
            height: 40,
            decoration: BoxDecoration(
              border: Border.all(color: Colors.grey.shade400, width: 1.2),
              borderRadius: BorderRadius.circular(12),
            ),
            child: Center(
              child: Text(
                "Rp ${NumberFormat('#,###', 'id_ID').format(totalIncome)}",
                style: const TextStyle(
                  fontSize: 18,
                  fontWeight: FontWeight.bold,
                  color: Colors.black87,
                ),
              ),
            ),
          ),
        ],
      ),
    ),
    );
  }
}
