import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_svg/svg.dart';
import 'package:get/get.dart';
import 'package:intl/intl.dart';
import 'package:web_panel/utils/App_color.dart';

import '../../../constants.dart';
import '../../../controllers/dashboard_controller.dart';
import '../../../models/RecentFile.dart';

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

  @override
  Widget build(BuildContext context) {
    return GetX<DashBoardController>(
      init: DashBoardController(),
      builder: (controller) {
        List emiList = controller.userDetails['emi']??[];
        debugPrint("emiList::${emiList}");
        return Container(
          padding: EdgeInsets.all(defaultPadding),
          decoration: BoxDecoration(
            color: AppColor.secondaryColor,
            borderRadius: BorderRadius.all(Radius.circular(10)),
          ),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Row(
                spacing: 2.sp,
                children: [
                  Image.asset('assets/icons/ic_emi.png', height: 32, width: 32),
                  Text(
                    'EMI Schedule',
                    style: Theme.of(context).textTheme.titleLarge,
                  ),
                ],
              ),
              if(emiList.isNotEmpty)
              SizedBox(
                width: double.infinity,
                child: DataTable(
                  horizontalMargin: 0,
                  columnSpacing: 5.w,
                  columns: [
                    DataColumn(label: Text('#')),
                    DataColumn(label: Text('Due Date')),
                    DataColumn(label: Text('Amount')),
                    DataColumn(label: Text('Status')),
                    DataColumn(label: Text('Late fee')),
                  ],
                  rows: List.generate(
                    emiList.length,
                    (index) => recentFileDataRow(context, emiList[index]),
                  ),
                ),
              ),
            ],
          ),
        );
      },
    );
  }

  DataRow recentFileDataRow(BuildContext context,  Map<String, dynamic> emiList) {
    return DataRow(
      cells: [
        DataCell(
          Text(
            emiList['id'].toString(),
            style: Theme.of(context).textTheme.titleSmall,
          ),
        ),
        DataCell(
          Text(
            emiList['due_date'] != null
                ? DateFormat(
                    'dd MMM yyyy',
                  ).format(DateTime.parse(emiList['due_date']))
                : 'N/A',
            style: Theme.of(context).textTheme.titleSmall,
          ),
        ),
        DataCell(
          Text(
            emiList['amount'].toString(),
            style: Theme.of(
              context,
            ).textTheme.titleSmall?.copyWith(color: AppColor.green),
          ),
        ),
        DataCell(
          Text(
            emiList['status'] ?? "N/A",
            style: Theme.of(
              context,
            ).textTheme.titleSmall?.copyWith(color: AppColor.green),
          ),
        ),
        DataCell(
          Text(
            emiList['late_fee'].toString(),
            style: Theme.of(context).textTheme.titleSmall,
          ),
        ),
      ],
    );
  }
}
