import 'package:data_table_2/data_table_2.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:marking_app/common/model/job/job_data_report.dart';
import 'package:marking_app/routes/RouterManager.dart';
import 'package:marking_app/utils/index.dart';

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

  const QuickStudentDataTable({
    Key? key,
    required this.headList,
    required this.bodyList,
    required this.jobId,
    required this.hasUnrated,
    required this.kgCount,
    required this.zgCount,
    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;
  bool _sortAscending = true;

  DataRow _getRow(int index, [Color? color]) {
    assert(index >= 0);
   StudentDetails item = widget.bodyList[index];
    /* int num = 0;
    int total = 0;
    item.kgDetails.forEach((element) {
      if (element.state == 0) {
        num = num + 1;
      }
      total = total + 1;
    });
    item.zgDetails.forEach((element) {
      if (element.state == 0) {
        num = num + 1;
      }
      total = total + 1;
    });*/
    return DataRow2.byIndex(
      index: index,
      color: color != null
          ? item.allNotDone
              ? MaterialStateProperty.all(Color(0xFFFFD79C))
              : MaterialStateProperty.all(color)
          : null,
      cells: [
        DataCell(InkWell(
          onTap: () {
            RouterManager.router.navigateTo(
              context,
              RouterManager.quickCheckPersonalPath +
                  '?jobId=${widget.jobId}&studentId=${item.studentId}',
              transition: getTransition(),
            );
          },
          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: 12.sp, color: Color(0xFF6888FD))),
                  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: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Text(
                    '${item.kgCorrect}',
                    style: TextStyle(fontSize: 12.sp, color: Color(0xFF4CC793))),
                Text(
                    '/',
                    style: TextStyle(fontSize: 12.sp, color: Color(0xFF525252))),
                Text(
                    '${item.kgError}',
                    style: TextStyle(fontSize: 12.sp, color: Color(0xFFFF7474))),
              ],
            ),
          ),
        )),
        DataCell(Center(
          child: Padding(
            padding: EdgeInsets.symmetric(horizontal: 5.r),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Text(
                    '${item.zgCorrect}',
                    style: TextStyle(fontSize: 12.sp, color: Color(0xFF4CC793))),
                Text(
                    '/',
                    style: TextStyle(fontSize: 12.sp, color: Color(0xFF525252))),
                Text(
                    '${item.zgError}',
                    style: TextStyle(fontSize: 12.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: 2,
                  children: List.generate(item.kgDetails.length, (index) {
                    KgDetails kgInfo = item.kgDetails[index];
                    return Container(
                      width: 14.r,
                      height: 14.r,
                      decoration: BoxDecoration(
                          color: kgInfo.state == 0
                              ? Color(0xFFD3D3D3)
                              : kgInfo.state == 3?Colors.white:kgInfo.state == 1
                                  ? Color(0xFFFF7474)
                                  : Color(0xFF4CC793),
                          borderRadius: BorderRadius.all(Radius.circular(7.r))),
                      child: Center(
                          child: Text(
                        kgInfo.questionNo,
                        style: TextStyle(
                            fontSize: 10.sp,
                            color: kgInfo.state == 3
                                ? 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: 2,
                  children: List.generate(item.zgDetails.length, (index) {
                    KgDetails kgInfo = item.zgDetails[index];
                    return Container(
                      width: 14.r,
                      height: 14.r,
                      decoration: BoxDecoration(
                          color: kgInfo.state == 0
                              ? Color(0xFFD3D3D3)
                              : kgInfo.state == 3?Colors.white:kgInfo.state == 1
                              ? Color(0xFFFF7474)
                              : Color(0xFF4CC793),
                          borderRadius: BorderRadius.all(Radius.circular(7.r))),
                      child: Center(
                          child: Text(
                        kgInfo.questionNo,
                        style: TextStyle(
                            fontSize: 10.sp,
                            color: kgInfo.state == 3
                                ? Color(0xFF525252)
                                : Colors.white),
                      )),
                    );
                  })),
            ),
          ),
        ),
        if(widget.hasUnrated)
        DataCell(Center(
          child: Padding(
            padding: EdgeInsets.symmetric(horizontal: 5.r),
            child: Text('${item.unrated}',
                style:
                TextStyle(fontSize: 12.sp, color: Color(0xFF6888FD))),
          ),
        )),
      ],
    );
  }

  @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: 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 == 1?DataColumn2(
            label: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  Text(item,
                      style: TextStyle(fontSize: 12.sp, color: Color(0xFF505767))),
                  Text('(${widget.kgCount})',
                      style: TextStyle(fontSize: 10.sp, color: Color(0xFF505767))),
                ]
            ),
            // size: ColumnSize.S,
            fixedWidth: (MediaQuery.of(context).size.width - 20.r - 28.r) / widget.headList.length,
          ):index == 2?DataColumn2(
            label: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  Text(item,
                      style: TextStyle(fontSize: 12.sp, color: Color(0xFF505767))),
                  Text('(${widget.zgCount})',
                      style: TextStyle(fontSize: 10.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: 12.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))));
  }
}
