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/utils/common_utils.dart';
import 'package:marking_app/utils/easy_refresh/MyEmptyWidget.dart';
import 'package:photo_view/photo_view.dart';

class StudentZgTable extends StatefulWidget {
  final List headList;
  final List bodyList;
  final int? fixedRows;
  final int? fixedCols;

  const StudentZgTable({
    Key? key,
    required this.headList,
    required this.bodyList,
    this.fixedCols = 0,
    this.fixedRows = 0,
  }) : super(key: key);

  @override
  State<StudentZgTable> createState() => _StudentZgTableState();
}

class _StudentZgTableState extends State<StudentZgTable> {
  final ScrollController _controller = ScrollController();
  int? _sortColumnIndex;
  bool _sortAscending = true;
  
  void showImgDialog(BuildContext context,String imgUrl){
    Navigator.push(
      context,
      MaterialPageRoute(builder: (_) {
        return Scaffold(
          appBar: AppBar(),
          body: SizedBox(
           /* width: MediaQuery.of(context).size.width * 0.6,
            height: MediaQuery.of(context).size.height * 0.6,*/
            child: PhotoView(
                imageProvider:
                NetworkImage(imgUrl)),
          ),
        );
      }),
    );
  /*  showDialog(context: context, builder: (BuildContext context){
      return AlertDialog(
        // insetPadding: EdgeInsets.symmetric(vertical: 20.r,horizontal: 20.r),
         backgroundColor:Colors.transparent,
          contentPadding: EdgeInsets.all(20.r),
          shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.all(Radius.circular(15.r))),
        content: Container(
          color: Colors.transparent,
          width: MediaQuery.of(context).size.width * 0.6,
          height: MediaQuery.of(context).size.height * 0.6,
          child: PhotoView(
              imageProvider:
              NetworkImage(imgUrl)),
        ),
      );
    });*/
  }

  DataRow _getRow(int index, [Color? color]) {
    assert(index >= 0);
    KgDetails item = widget.bodyList[index];
    return DataRow2.byIndex(
      index: index,
      color: color != null ? MaterialStateProperty.all(color): null,
      cells: [
        DataCell(Center(
          child: Padding(
            padding: EdgeInsets.symmetric(horizontal: 5.r),
            child: Text(item.questionNo,
                style: TextStyle(fontSize: 12.sp, color: Color(0xFF6888FD))),
          ),
        )),
        DataCell(Center(
          child: Padding(
            padding: EdgeInsets.symmetric(horizontal: 5.r),
            child: Text(CommonUtils.second2HMS(item.useTime!),
                style: TextStyle(fontSize: 12.sp, color: Color(0xFF525252))),
          ),
        )),
        DataCell(InkWell(
          onTap: (){
           if(item.state != 0){
             showImgDialog(context,item.studentAnswer!);
           }

          },
          child: Center(
            child: Padding(
              padding: EdgeInsets.symmetric(horizontal: 5.r),
              child: Text(item.state!=0 ?'查看':'--',
                  style: TextStyle(fontSize: 12.sp, color: Color(0xFF3661FE))),
            ),
          ),
        )),
        DataCell(Center(
          child: Padding(
            padding: EdgeInsets.symmetric(horizontal: 5.r),
            child: item.state == 2?
            Image.asset('assets/images/job_personal_correct_icon.png',width: 18.r,height: 18.r,):item.state == 1?
            Image.asset('assets/images/job_personal_error_icon.png',width: 10.r,height: 10.r,):Text('', style: TextStyle(fontSize: 12.sp, color: Color(0xFF525252))),
          ),
        )),
        DataCell(InkWell(
          onTap: (){
            if(item.state==1 || item.state==2){
              showImgDialog(context,item.annotateAnswers!);
            }

          },
          child: Center(
            child: Padding(
              padding: EdgeInsets.symmetric(horizontal: 5.r),
              child: Text(item.state==1 || item.state==2?'查看':'--',
                  style: TextStyle(fontSize: 12.sp, color: Color(0xFF3661FE))),
            ),
          ),
        )),
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    return DataTable2(
        dividerThickness: 0,
        scrollController: _controller,
        columnSpacing: 0,
        horizontalMargin: 0,
        bottomMargin: 0,
        headingRowHeight: 40.r,
        dataRowHeight: 40.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 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)/5,
          );
        }),
        rows: List<DataRow>.generate(widget.bodyList.length,
            (index) => _getRow(index, Color(0xFFF5F5F5))));
  }
}
