import 'package:data_table_2/data_table_2.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_widget_from_html_core/flutter_widget_from_html_core.dart';
import 'package:marking_app/common/model/report/report_marking_detail.dart';
import 'package:photo_view/photo_view.dart';

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

  const CompleteTable({
    Key? key,
    required this.bodyList,
     this.fixedCols,
     this.fixedRows,
  }) : super(key: key);

  @override
  State<CompleteTable> createState() => _CompleteTableState();
}

class _CompleteTableState extends State<CompleteTable> {
  final ScrollController _controller = ScrollController();
  int? _sortColumnIndex;
  bool _sortAscending = true;
  final List<String> headList = ['题号','小题分','得分','用户答案','正确答案'];

  showImg(String imgUrl) {
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          // insetPadding: EdgeInsets.symmetric(vertical: 10.r,horizontal: 45.r),
          backgroundColor: Colors.transparent,
          contentPadding: EdgeInsets.all(0),
          shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.all(Radius.circular(15.r))),
          content: Container(
              width: MediaQuery.of(context).size.width - 48.r,
              height: MediaQuery.of(context).size.height * 0.6,
              color: Colors.white,
              child: PhotoView(imageProvider: NetworkImage(imgUrl),backgroundDecoration: BoxDecoration(color: Colors.transparent),)),
        );
      },
    );
  }

  DataRow _getRow(int index, [Color? color]) {
    assert(index >= 0);
    QuestionAnswers body = 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(body.questionNum,
                    style:
                    TextStyle(fontSize: 12.sp, color: Color(0xFF262626))),
              ),
            )),
        DataCell(
            Center(
              child: Padding(
                padding: EdgeInsets.symmetric(horizontal: 5.r),
                child: Text(body.totalScore.toString(),
                    style:
                    TextStyle(fontSize: 12.sp, color: Color(0xFF262626))),
              ),
            )),
        DataCell(
            Center(
              child: Padding(
                padding: EdgeInsets.symmetric(horizontal: 5.r),
                child: Text(body.score.toString(),
                    style:
                    TextStyle(fontSize: 12.sp, color: Color(0xFF262626))),
              ),
            )),
        DataCell(
            Center(
              child: Padding(
                padding: EdgeInsets.symmetric(horizontal: 5.r),
                child:
                body.yourAnswer.contains('http')?
                InkWell(
                  onTap: (){
                    showImg(body.yourAnswer);
                  },
                  child: Text('查看原题',style: TextStyle(fontSize: 12.sp,color: Color(0xFF6988FD)),),
                ):
                Text(body.yourAnswer,
                    style:
                    TextStyle(fontSize: 12.sp, color: Color(0xFF262626)))
               ,
              ),
            )),
        DataCell(
            Center(
              child: Padding(
                padding: EdgeInsets.symmetric(horizontal: 5.r),
                child: Text(body.answer,
                    style:
                    TextStyle(fontSize: 12.sp, color: Color(0xFF262626))),
              ),
            )),
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    return DataTable2(
        dividerThickness: 0,
        scrollController: _controller,
        columnSpacing: 0,
        horizontalMargin: 0,
        dataRowHeight:40.r,
        headingRowHeight: 30.r,
        bottomMargin: 0,
        border: TableBorder(
            left: BorderSide(
                width: 1, color: Color(0xFFEAEAEA), style: BorderStyle.solid),
            right: BorderSide(
                width: 1, color: Color(0xFFEAEAEA), style: BorderStyle.solid),
            horizontalInside: BorderSide(
                width: 1, color: Color(0xFFEAEAEA), style: BorderStyle.solid),
            bottom: BorderSide(
                width: 1, color: Color(0xFFEAEAEA), style: BorderStyle.solid),
            verticalInside: BorderSide(
                width: 1, color: Color(0xFFEAEAEA), style: BorderStyle.solid)),
        headingRowColor: MaterialStateProperty.resolveWith((states) =>
            widget.fixedCols! > 0 ? Color(0xFFF0F3FF) : Colors.transparent),
        headingRowDecoration: BoxDecoration(color: Color(0xFFF0F3FF)),
        fixedColumnsColor: Color(0xFFF0F3FF),
        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(headList.length, (index) {
          var item = 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 - 48.r)/headList.length,
          );
        }),
        rows: List<DataRow>.generate(widget.bodyList.length,
            (index) => _getRow(index, Colors.transparent)));
  }
}
