import 'package:deus_app/common/event/RefreshPatrolEdit.dart';
import 'package:deus_app/common/event/multiple_choice.dart';
import 'package:deus_app/common/style/TitleBar.dart';
import 'package:deus_app/common/style/gsy_style.dart';
import 'package:deus_app/common/utils/CommonUtils.dart';
import 'package:deus_app/common/utils/ConstantString.dart';
import 'package:deus_app/common/utils/DioUtil.dart';
import 'package:deus_app/common/utils/ToastUtils.dart';
import 'package:deus_app/main.dart';
import 'package:deus_app/model/patrol_job_select_entity_entity.dart';
import 'package:flutter/material.dart';

class PatrolJobAdd extends StatefulWidget {

  var id;

  final PatrolJobSelectEntityDataPatrolItemModelVOS responseData;

  PatrolJobAdd({super.key,required this.responseData,required this.id});

  @override
  State createState() {
    return new _PatrolJobAdd(responseData,id);
  }
}

class _PatrolJobAdd extends State<PatrolJobAdd> {
  PatrolJobSelectEntityDataPatrolItemModelVOS responseData;

  var id;

  _PatrolJobAdd(this.responseData,this.id);

  String groupValue = '';

  List<MultipleChoice>mList=[];

  @override
  void initState() {
    super.initState();
    if(responseData.type==2){
      groupValue=responseData.param[0];
    }else if(responseData.type==3){
      for (var element in responseData.param) {
        mList.add(MultipleChoice(element, false));
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: TitleBar().backAppbar("添加巡检项目"),
      // backgroundColor: const Color(0xfff2f2f2),
      body: Stack(
        children: [
          Column(
            children: [
              Container(
                padding: const EdgeInsets.fromLTRB(15.0, 15.0, 15.0, 15.0),
                child: Column(
                  children: [
                    Container(
                      alignment: Alignment.centerLeft,
                      child: Text(
                        responseData.name,
                        style: const TextStyle(
                          fontSize: GSYConstant.TextSize15,
                          color: Colors.black,
                        ),
                      ),
                    ),
                    Container(
                      margin: const EdgeInsets.only(top: 10),
                      // padding: const EdgeInsets.fromLTRB(30.0, 0.0, 0.0, 10.0),
                      // alignment: Alignment.centerLeft,
                      alignment: Alignment.centerLeft,
                      child: Text(
                        responseData.params,
                        style: GSYConstant.textLight,
                        textAlign: TextAlign.left,
                      ),
                    ),
                    SizedBox(
                      height: 10,
                    ),
                    Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [
                      Container(
                        child: Text(
                          '类型:',
                          style: TextStyle(
                            color: GSYColors.primaryLightValue,
                            fontSize: GSYConstant.TextSize15,
                          ),
                        ),
                      ),
                      Container(
                        child: Text(
                          responseData.type == 0
                              ? '数值'
                              : responseData.type == 1
                              ? '长文本'
                              : responseData.type == 2
                              ? '单选'
                              : '多选',
                          textAlign: TextAlign.right,
                          style: TextStyle(
                            color: GSYColors.primaryLightValue,
                            fontSize: GSYConstant.TextSize15,
                          ),
                        ),
                      ),
                    ]),
                  ],
                ),
              ),
              Divider(
                height: 0.8,
                color: Colors.grey,
              ),
              SizedBox(
                height: 10,
              ),
              Expanded(
                child:responseData.type==2||responseData.type==3? Container(
                  child: ListView.builder(
                    itemCount: responseData.param.length,
                    itemBuilder: (context, index) {
                      return Container(
                          child: ListTile(
                              title:responseData.type==2?Row(
                                children: [
                                  Radio(
                                    value: responseData.param[index],
                                    groupValue: groupValue,
                                    onChanged: (value) {
                                      // groupValue = value;
                                      setState(() {
                                        groupValue = value!;
                                      });
                                    },
                                    // selected: groupValue == index,
                                  ),
                                  Text(responseData.param[index],style: TextStyle(
                                    color: GSYColors.primaryLightValue,
                                    fontSize: GSYConstant.TextSize15,
                                  ),)
                                ],
                              ):Row(
                                children: [
                                  Checkbox(
                                    value: mList[index].isCheck,
                                    activeColor: Colors.blue,
                                    onChanged: (bool? value) {
                                      setState(() {
                                        mList[index].isCheck=value!;
                                      });
                                    },
                                  ),
                                  Text(responseData.param[index],style: TextStyle(
                                    color: GSYColors.primaryLightValue,
                                    fontSize: GSYConstant.TextSize15,
                                  )),
                                ],
                              )
                          )
                      );
                    },
                  ),
                ):Container(
                  padding: EdgeInsets.only(left: 12,right: 12,bottom: 12),
                  decoration: BoxDecoration(color: Colors.white),
                  child: Column(
                    children: [
                      const SizedBox(
                        height: 10,
                      ),
                      TextField(
                        maxLines: 5,
                        decoration: InputDecoration(border: OutlineInputBorder()),
                        style: TextStyle(fontSize: 14),
                        onChanged: (value) {
                          groupValue = value;
                        },
                      )
                    ],
                  ),
                )
              ),
            ],
          ),
          Positioned(
            left: 0,
            right: 0,
            bottom:0,
            // flex: 7,
            child:SizedBox(
              height: 50,
              width: double.infinity,
              child: TextButton(
                onPressed: () {
                  appAddItem();
                },
                style: ButtonStyle(
                  backgroundColor: MaterialStateProperty.all<Color>(
                      Color(0xFF4875EC)),
                  shape: MaterialStateProperty.all(
                      BeveledRectangleBorder(
                          borderRadius: BorderRadius.circular(0))),
                  foregroundColor:
                  MaterialStateProperty.all<Color>(Colors.white),
                  // padding: MaterialStateProperty.all(EdgeInsets.zero)
                ),
                child: const Text(ConstantString.complete),
              ),
            ),
          )
        ],
      ),
    );
  }

  appAddItem() async {
    // Navigator.pushAndRemoveUntil(context, MaterialPageRoute(
    //     builder: (context) =>
    //         PatrolJobEdit(id: responseData.id)), (route) => false);
    if(responseData.type==3){
      for (var element in mList) {
        if(element.isCheck){
          if(!CommonUtils.validationInput(groupValue)){
            groupValue=element.name;
          }else{
            groupValue='$groupValue,${element.name}';
          }
        }
      }
    }
    if(!CommonUtils.validationInput(groupValue)){
      showToast(ConstantString.patrolAddNull);
      return;
    }
      var result = await DioUtil().request("patrolItem/appAddItem",
          method: DioMethod.post,
          data: {'id': responseData.id,'patrolJobId':id,'result':groupValue});
      if (result['code'] == 0) {
        setState(() {
          eventBus.fire(RefreshPatrolEdit());
          Navigator.of(context)..pop()..pop();
        });
      } else {
        showToast(result['msg']);
      }
  }

}
