import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:special_equipment_flutter/common/color_const.dart';
import 'package:special_equipment_flutter/common/route_string.dart';
import 'package:special_equipment_flutter/dio/http_utils.dart';
import 'package:special_equipment_flutter/model/exam/eq_question_bo.dart';
import 'package:special_equipment_flutter/model/exam/equipment_list_bo.dart';
import 'package:special_equipment_flutter/model/exam/test_setting_bo.dart';
import 'package:special_equipment_flutter/utils/storage_util.dart';
import 'package:special_equipment_flutter/utils/toast_utils.dart';
import 'package:special_equipment_flutter/widgets/custom_button.dart';
import 'package:special_equipment_flutter/widgets/custom_textfield.dart';

///测试练习设置
class TestSettingPage extends StatefulWidget {
  TestSettingPage({super.key});

  @override
  State<TestSettingPage> createState() => _TestSettingPageState();
}

class _TestSettingPageState extends State<TestSettingPage> {
  TextEditingController? equipmentText = TextEditingController();
  FocusNode? equipmentFocusNode = FocusNode();
  TextEditingController? singleText = TextEditingController();
  FocusNode? singleZJFocusNode = FocusNode();
  TextEditingController? judgmentText = TextEditingController();
  FocusNode? judgmentZJFocusNode = FocusNode();
  int allQuestions = 0;
  EquiList? mEquiList;
  DataEq? mDataEq;

  @override
  void initState() {
    super.initState();
    // getFindQuestionNum();
    // getFindEqQuestionNum(true);
    getEquipmentList();
  }

  ///获取设备类型
  void getEquipmentList() {
    HttpUtils.getEquipmentList(context, StorageUtil.getInstance().getOfficeId())
        .then((value) {
      EquipmentListBo listBo = value;
      if (mounted) {
        setState(() {
          try {
            mEquiList = listBo.list![0];
            equipmentText!.text = mEquiList!.name!;
          } catch (e) {}
        });
      }
    });
  }

  ///获取题数
  // void getFindQuestionNum() {
  //   HttpUtils.getFindQuestionNum(context).then((value) {
  //     FindQuestionNum listBo = value;
  //     if (mounted) {
  //       setState(() {
  //         DataNum dataNum = listBo.data!;
  //         singleText!.text = dataNum.aqyDx!.toString();
  //         judgmentText!.text = dataNum.aqyPd!.toString();
  //         allQuestions = dataNum.aqyDx! + dataNum.aqyPd!;
  //       });
  //     }
  //   });
  // }

  ///根据设备类型获取总题数
  // void getFindEqQuestionNum(isFirst) {
  //   HttpUtils.getFindEqQuestionNum(context, mEquiList!.id!).then((value) {
  //     EqQuestionBo listBo = value;
  //     if (mounted) {
  //       setState(() {
  //         mDataEq = listBo.data!;
  //         if (isFirst) {
  //           singleText!.text = mDataEq!.dxNum!.toString();
  //           judgmentText!.text = mDataEq!.pdNum!.toString();
  //           allQuestions = mDataEq!.dxNum! + mDataEq!.pdNum!;
  //         } else {
  //           if (int.parse(singleText!.text) > mDataEq!.dxNum!) {
  //             singleText!.text = mDataEq!.dxNum!.toString();
  //             allQuestions = (singleText!.text.isNotEmpty
  //                     ? int.parse(singleText!.text)
  //                     : 0) +
  //                 (judgmentText!.text.isNotEmpty
  //                     ? int.parse(judgmentText!.text)
  //                     : 0);
  //           }
  //           if (int.parse(judgmentText!.text) > mDataEq!.pdNum!) {
  //             judgmentText!.text = mDataEq!.pdNum!.toString();
  //             allQuestions = (singleText!.text.isNotEmpty
  //                     ? int.parse(singleText!.text)
  //                     : 0) +
  //                 (judgmentText!.text.isNotEmpty
  //                     ? int.parse(judgmentText!.text)
  //                     : 0);
  //           }
  //         }
  //       });
  //     }
  //   });
  // }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.grey[100],
      body: Stack(
        children: [
          Image.asset('assets/home/rigister_bg.png'),
          Container(
            height: 230,
            margin:
                const EdgeInsets.only(left: 15, top: 130, right: 15, bottom: 0),
            decoration: const BoxDecoration(
              color: ColorConst.whiteColor,
              borderRadius: BorderRadius.all(Radius.circular(10)),
            ),
            child: CustomScrollView(
              primary: false,
              shrinkWrap: true,
              slivers: <Widget>[
                SliverToBoxAdapter(
                  child: Column(
                    children: [
                      buildUnitInformation(),
                      buildSubmit()
                      // const SizedBox(height: 30)
                    ],
                  ),
                ),
              ],
            ),
          ),
          buildTopTitle(context)
        ],
      ),
    );
  }

  ///单位信息
  Container buildUnitInformation() {
    return Container(
      margin: const EdgeInsets.only(left: 10, top: 10, right: 10, bottom: 10),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          const SizedBox(height: 10),
          const Row(children: [
            Icon(Icons.touch_app, color: ColorConst.orangeColor, size: 15),
            Text('设备类型', style: TextStyle(fontSize: 14))
          ]),
          const SizedBox(height: 5),
          CustomTextField(
              enabled: true,
              readOnly: true,
              hintText: '请选择设备类型',
              controller: equipmentText,
              focusNode: equipmentFocusNode,
              onTop: () {
                Get.toNamed(RouteString.EQUIPMENT_LIST_PAGE, arguments: {
                  'officeId': StorageUtil.getInstance().getOfficeId()
                })?.then((map) {
                  if (map != null) {
                    mEquiList = map;
                    setState(() {
                      equipmentText!.text = mEquiList!.name!;
                      // getFindEqQuestionNum(false);
                    });
                  }
                });
              }),
          const SizedBox(height: 20),
          // const Row(children: [
          //   Icon(Icons.drive_file_rename_outline_rounded,
          //       color: ColorConst.orangeColor, size: 15),
          //   Text('设置题数', style: TextStyle(fontSize: 14))
          // ]),
          // const SizedBox(height: 10),
          // Container(
          //   padding:
          //       const EdgeInsets.only(left: 5, top: 0, right: 5, bottom: 0),
          //   child: Row(
          //     mainAxisAlignment: MainAxisAlignment.spaceBetween,
          //     children: [
          //       Row(children: [
          //         const Text('单选题数', style: TextStyle(fontSize: 14)),
          //         SizedBox(
          //           width: 40,
          //           height: 30,
          //           child: TextFormField(
          //             textAlign: TextAlign.center,
          //             inputFormatters: [
          //               FilteringTextInputFormatter.digitsOnly,
          //               LengthLimitingTextInputFormatter(3),
          //               // 设置输入长度限制
          //             ],
          //             focusNode: singleZJFocusNode,
          //             controller: singleText,
          //             onChanged: (num) {
          //               setState(() {
          //                 if (mDataEq != null &&
          //                     int.parse(num) > mDataEq!.dxNum!) {
          //                   ToastUtils.showCenter('题库只有${mDataEq!.dxNum}道单选题');
          //                   setState(() {
          //                     singleText!.text = mDataEq!.dxNum.toString();
          //                   });
          //                 }
          //                 allQuestions = (singleText!.text.isNotEmpty
          //                         ? int.parse(singleText!.text)
          //                         : 0) +
          //                     (judgmentText!.text.isNotEmpty
          //                         ? int.parse(judgmentText!.text)
          //                         : 0);
          //               });
          //             },
          //           ),
          //         )
          //       ]),
          //       Row(children: [
          //         const Text('判断题数', style: TextStyle(fontSize: 14)),
          //         SizedBox(
          //           width: 40,
          //           height: 30,
          //           child: TextFormField(
          //             textAlign: TextAlign.center,
          //             inputFormatters: [
          //               FilteringTextInputFormatter.digitsOnly,
          //               LengthLimitingTextInputFormatter(3),
          //             ],
          //             focusNode: judgmentZJFocusNode,
          //             controller: judgmentText,
          //             onChanged: (num) {
          //               setState(() {
          //                 if (mDataEq != null &&
          //                     int.parse(num) > mDataEq!.pdNum!) {
          //                   ToastUtils.showCenter('题库只有${mDataEq!.pdNum}道判断题');
          //                   setState(() {
          //                     judgmentText!.text = mDataEq!.pdNum.toString();
          //                   });
          //                 }
          //                 allQuestions = (judgmentText!.text.isNotEmpty
          //                         ? int.parse(judgmentText!.text)
          //                         : 0) +
          //                     (singleText!.text.isNotEmpty
          //                         ? int.parse(singleText!.text)
          //                         : 0);
          //               });
          //             },
          //           ),
          //         )
          //       ]),
          //       Text('总题数: ${allQuestions.toString()}',
          //           style: const TextStyle(color: Colors.grey, fontSize: 14)),
          //     ],
          //   ),
          // ),
        ],
      ),
    );
  }

  Container buildTopTitle(BuildContext context) {
    return Container(
      margin: const EdgeInsets.only(top: 50),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        mainAxisAlignment: MainAxisAlignment.start,
        children: [
          IconButton(
            onPressed: () {
              Navigator.of(context).pop();
            },
            icon: const Icon(Icons.arrow_back, color: Colors.white),
            iconSize: 30,
          ),
          Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            mainAxisAlignment: MainAxisAlignment.start,
            children: [
              Container(
                  margin: const EdgeInsets.only(top: 5),
                  child: const Text(
                    '测试练习设置',
                    style: TextStyle(
                        fontSize: 23,
                        color: Colors.white,
                        fontWeight: FontWeight.bold),
                  )),
              Container(
                  height: 3,
                  width: 50,
                  alignment: Alignment.topLeft,
                  margin: const EdgeInsets.only(top: 3, bottom: 3),
                  decoration: const BoxDecoration(
                    color: Colors.white54,
                    borderRadius: BorderRadius.all(Radius.circular(5)),
                  )),
              const Text(
                '',
                style: TextStyle(fontSize: 12, color: Colors.white),
              ),
            ],
          ),
        ],
      ),
    );
  }

  ///交卷
  Container buildSubmit() {
    return Container(
        margin: const EdgeInsets.only(top: 40, bottom: 0),
        padding: const EdgeInsets.only(left: 20, right: 20, top: 15, bottom: 0),
        width: double.infinity,
        decoration: const BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.all(Radius.circular(5)),
        ),
        child: GradientButton(
            tapCallback: () {
              if (equipmentText!.text.isEmpty) {
                ToastUtils.showCenter('请选择设备类型');
                return;
              }
              // if (allQuestions == 0) {
              //   ToastUtils.showCenter('总题数不能为 0');
              //   return;
              // }
              TestSubmitBo mTestSubmitBo = TestSubmitBo();
              mTestSubmitBo.mEquiList = mEquiList;
              // mTestSubmitBo.dxQuestionsNum =
              //     singleText!.text.isNotEmpty ? int.parse(singleText!.text) : 0;
              // mTestSubmitBo.pdQuestionsNum = judgmentText!.text.isNotEmpty
              //     ? int.parse(judgmentText!.text)
              //     : 0;
              Get.offNamed(RouteString.TEST_PAGE,
                  arguments: {'mTestSubmitBo': mTestSubmitBo})?.then((value) {
                if (value != null && value) {}
              });
            },
            width: 300,
            height: 40,
            borderRadius: const BorderRadius.only(
                topLeft: Radius.circular(10),
                topRight: Radius.circular(20),
                bottomLeft: Radius.circular(20),
                bottomRight: Radius.circular(10)),
            disable: false,
            colors: const [ColorConst.blueColor, ColorConst.blue1Color],
            child: const Text(
              "去练习",
              style: TextStyle(fontSize: 14, color: ColorConst.whiteColor),
            )));
  }
}
