import 'package:data_table_2/data_table_2.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:making_school_asignment_app/common/job/homework_details.dart';
import 'package:making_school_asignment_app/routes/app_pages.dart';

class QuickStudentDataTable extends StatefulWidget {
  final List headList;
  final List bodyList;
  final int? fixedRows;
  final int? fixedCols;
  final String jobId;
  final bool hasUnrated;
  final int kgCount;
  final int zgCount;
  final int subject;

  const QuickStudentDataTable({
    Key? key,
    required this.headList,
    required this.bodyList,
    required this.jobId,
    required this.hasUnrated,
    required this.kgCount,
    required this.zgCount,
    required this.subject,
    this.fixedCols = 0,
    this.fixedRows = 0,
  }) : super(key: key);

  @override
  State<QuickStudentDataTable> createState() => _QuickStudentDataTableState();
}

class _QuickStudentDataTableState extends State<QuickStudentDataTable> {
  final ScrollController _controller = ScrollController();
  int? _sortColumnIndex;
  final bool _sortAscending = true;

  DataRow _getRow(int index, [Color? color]) {
    assert(index >= 0);
    Students item = widget.bodyList[index];
    return DataRow2.byIndex(
      index: index,
      color: color != null
          ? item.allNotDone!
              ? WidgetStateProperty.all(const Color(0xFFFFD79C))
              : WidgetStateProperty.all(color)
          : null,
      cells: [
        DataCell(InkWell(
          onTap: () {
            Get.toNamed(Routes.studentPersonalPage, arguments: {'studentId': item.studentId, 'homeworkId': widget.jobId,'subject':widget.subject});
          },
          child: Center(
            child: Padding(
              padding: EdgeInsets.symmetric(horizontal: 5.r),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  Text(item.studentName!, style: TextStyle(fontSize: 10.sp, color: const Color(0xFF4CC793), overflow: TextOverflow.ellipsis)),
                  SizedBox(
                    width: 5.r,
                  ),
                  Image.asset(
                    'assets/images/job_data_right_icon.png',
                    width: 10.r,
                    height: 10.r,
                  )
                ],
              ),
            ),
          ),
        )),
        DataCell(Center(
          child: Padding(
            padding: EdgeInsets.symmetric(horizontal: 5.r),
            child:  Text(item.ttlSec.toString(), style: TextStyle(fontSize: 10.sp, color: const Color(0xFF525252), overflow: TextOverflow.ellipsis)),
          ),
        )),
        DataCell(Center(
          child: Padding(
            padding: EdgeInsets.symmetric(horizontal: 5.r),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Text('${item.kgtOkCount}', style: TextStyle(fontSize: 10.sp, color: Color(0xFF4CC793))),
                Text('/', style: TextStyle(fontSize: 10.sp, color: Color(0xFF525252))),
                Text('${item.kgtErrorCount}', style: TextStyle(fontSize: 10.sp, color: Color(0xFFFF7474))),
              ],
            ),
          ),
        )),
        DataCell(Center(
          child: Padding(
            padding: EdgeInsets.symmetric(horizontal: 5.r),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Text('${item.zgtOkCount}', style: TextStyle(fontSize: 10.sp, color: Color(0xFF4CC793))),
                Text('/', style: TextStyle(fontSize: 10.sp, color: Color(0xFF525252))),
                Text('${item.zgtErrorCount}', style: TextStyle(fontSize: 10.sp, color: Color(0xFFFF7474))),
              ],
            ),
          ),
        )),
        DataCell(
          Padding(
            padding: EdgeInsets.symmetric(vertical: 2.r, horizontal: 5.r),
            child: SingleChildScrollView(
              child: Wrap(
                  direction: Axis.horizontal,
                  alignment: WrapAlignment.start,
                  spacing: 2,
                  runSpacing: 3,
                  children: List.generate(item.kgtStu!.length, (index) {
                    Dtls kgInfo = item.kgtStu![index];
                    return Container(
                      width: 20.r,
                      height: 20.r,
                      decoration: BoxDecoration(
                          color: kgInfo.state == 0
                              ? const Color(0xFFD3D3D3)
                              : kgInfo.state == 1
                                  ? Colors.white
                                  : kgInfo.state == 2
                                      ? const Color(0xFFFF7474)
                                      : const Color(0xFF4CC793),
                          borderRadius: BorderRadius.all(Radius.circular(10.r))),
                      child: Center(
                          child: Text(
                        kgInfo.questionNo.toString(),
                        style: TextStyle(fontSize: 8.sp, color: kgInfo.state == 1 ? Color(0xFF525252) : Colors.white),
                      )),
                    );
                  })),
            ),
          ),
        ),
        DataCell(
          Padding(
            padding: EdgeInsets.symmetric(vertical: 2.r, horizontal: 5.r),
            child: SingleChildScrollView(
              child: Wrap(
                  direction: Axis.horizontal,
                  alignment: WrapAlignment.start,
                  spacing: 2,
                  runSpacing: 3,
                  children: List.generate(item.zgtStu!.length, (index) {
                    Dtls kgInfo = item.zgtStu![index];
                    return Container(
                      width: 20.r,
                      height: 20.r,
                      decoration: BoxDecoration(
                          color: kgInfo.state == 0
                              ? const Color(0xFFD3D3D3)
                              : kgInfo.state == 1
                                  ? Colors.white
                                  : kgInfo.state == 2
                                      ? const Color(0xFFFF7474)
                                      : const Color(0xFF4CC793),
                          borderRadius: BorderRadius.all(Radius.circular(10.r))),
                      child: Center(
                          child: Text(
                        kgInfo.questionNo.toString(),
                        style: TextStyle(fontSize: 8.sp, color: kgInfo.state == 1 ? Color(0xFF525252) : Colors.white),
                      )),
                    );
                  })),
            ),
          ),
        ),
        if (widget.hasUnrated)
          DataCell(Center(
            child: Padding(
              padding: EdgeInsets.symmetric(horizontal: 5.r),
              child: Text('${item.zgtUnrated}', style: TextStyle(fontSize: 10.sp, color: const Color(0xFFE8E8E8))),
            ),
          )),
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    if (!widget.hasUnrated) {
      widget.headList.removeLast();
    }
    return DataTable2(
        dividerThickness: 0,
        scrollController: _controller,
        columnSpacing: 0,
        horizontalMargin: 0,
        bottomMargin: 0,
        dataRowHeight: 50.r,
        border: const TableBorder(
            horizontalInside: BorderSide(width: 1, color: Colors.white, style: BorderStyle.solid),
            bottom: BorderSide(width: 1, color: Colors.white, style: BorderStyle.solid),
            verticalInside: BorderSide(width: 1, color: Colors.white, style: BorderStyle.solid)),
        headingRowColor: MaterialStateProperty.resolveWith((states) => widget.fixedCols! > 0 ? Colors.white : Colors.transparent),
        headingRowDecoration: BoxDecoration(color: Color(0xFFE6E6E6)),
        fixedColumnsColor: Color(0xFFE6E6E6),
        fixedCornerColor: Colors.grey[400],
        minWidth: MediaQuery.of(context).size.width,
        fixedTopRows: widget.fixedRows!,
        fixedLeftColumns: widget.fixedCols!,
        sortColumnIndex: _sortColumnIndex,
        sortAscending: _sortAscending,
        // onSelectAll: (val) => setState(() => selectAll(val)),
        columns: List.generate(widget.headList.length, (index) {
          var item = widget.headList[index];
          return index == 2
              ? DataColumn2(
                  label: Row(mainAxisAlignment: MainAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.center, children: [
                    Text(item, style: TextStyle(fontSize: 10.sp, color: Color(0xFF505767))),
                    Text('(${widget.kgCount})', style: TextStyle(fontSize: 8.sp, color: Color(0xFF505767))),
                  ]),
                  // size: ColumnSize.S,
                  fixedWidth: (MediaQuery.of(context).size.width - 20.r - 28.r) / widget.headList.length,
                )
              : index == 3
                  ? DataColumn2(
                      label: Row(mainAxisAlignment: MainAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.center, children: [
                        Text(item, style: TextStyle(fontSize: 10.sp, color: Color(0xFF505767))),
                        Text('(${widget.zgCount})', style: TextStyle(fontSize: 8.sp, color: Color(0xFF505767))),
                      ]),
                      // size: ColumnSize.S,
                      fixedWidth: (MediaQuery.of(context).size.width - 20.r - 28.r) / widget.headList.length,
                    )
                  : DataColumn2(
                      label: Center(
                        child: Text(item, style: TextStyle(fontSize: 10.sp, color: Color(0xFF505767))),
                      ),
                      // size: ColumnSize.S,
                      fixedWidth: (MediaQuery.of(context).size.width - 20.r - 28.r) / widget.headList.length,
                    );
        }),
        rows: List<DataRow>.generate(widget.bodyList.length, (index) => _getRow(index, Color(0xFFF5F5F5))));
  }
}
