import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:hello_doctor_admin_dashboard/app/models/help_model.dart';
import 'package:hello_doctor_admin_dashboard/app/modules/help_center/controllers/help_center_controller.dart';
import 'package:hello_doctor_admin_dashboard/app/utils/timeformat.dart';

class HelpDataSource extends DataTableSource {
  late final List<HelpModel> listHelp;
  HelpDataSource(this.listHelp);

  @override
  DataRow? getRow(int index) {
    return DataRow(cells: [
      DataCell(Text(listHelp[index].name)),
      DataCell(Text(listHelp[index].email)),
      DataCell(Text(listHelp[index].problem)),
      DataCell(
          Text(TimeFormat().formatDate(listHelp[index].createdAt).toString())),
      DataCell(
        Container(
          padding: EdgeInsets.symmetric(horizontal: 8, vertical: 4),
          decoration: BoxDecoration(
            color: _getStatusColor(listHelp[index].status),
            borderRadius: BorderRadius.circular(12),
          ),
          child: Text(
            listHelp[index].status,
            style: TextStyle(color: Colors.white),
          ),
        ),
      ),
      DataCell(Row(
        mainAxisSize: MainAxisSize.min,
        children: [
          IconButton(
            icon: Icon(Icons.remove_red_eye),
            onPressed: () {
              final controller = Get.find<HelpCenterController>();
              controller.showDetailsDialog(listHelp[index]);
            },
          ),
          IconButton(
            icon: Icon(Icons.edit),
            onPressed: () {
              final controller = Get.find<HelpCenterController>();
              controller.showEditStatusDialog(listHelp[index]);
            },
          ),
        ],
      )),
    ]);
  }

  Color _getStatusColor(String status) {
    switch (status) {
      case 'pending':
        return Colors.orange;
      case 'in_progress':
        return Colors.blue;
      case 'resolved':
        return Colors.green;
      default:
        return Colors.grey;
    }
  }

  @override
  bool get isRowCountApproximate => false;

  @override
  int get rowCount => listHelp.length;

  @override
  int get selectedRowCount => 0;
}
