import 'package:flutter/material.dart';
import '../base/YSBase.dart';
import '../base/YSNetWorking.dart';

class YSMineQuestionnaireDetail extends StatefulWidget {
  final questionId;
  const YSMineQuestionnaireDetail({Key key, this.questionId}) : super(key: key);

  @override
  _YSMineQuestionnaireDetailState createState() => _YSMineQuestionnaireDetailState();
}

class _YSMineQuestionnaireDetailState extends State<YSMineQuestionnaireDetail> {
  bool _isEnable = false;
  List _dataArray = [];
  MaterialColor _selectedColor = Colors.blue;
  @override
  void initState() {
    Future.delayed(Duration(seconds: 0)).then((value) {
      _getQuestionData();
    });
    super.initState();
  }
  @override
  Widget build(BuildContext context) {
    return YSBase(
      ystitle: '调查问卷详情',
      yschild: Stack(
        children: [
          Container(
            height: MediaQuery.of(context).size.height,
            width: MediaQuery.of(context).size.width,
            child: ListView.separated(
              itemBuilder: (context,index){
                Map item = _dataArray[index];
                List array = item['options'];
                return Container(
                  padding: EdgeInsets.all(15),
                  child: LayoutBuilder(
                    builder: (context,conSize){
                      return Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Text('$index.${item['title']}',style: TextStyle(fontSize: 16,color: Color(0xFF606266)),),
                          Container(
                            padding: EdgeInsets.only(top: 10,bottom: 10),
                            child: Text(item['description'],style: TextStyle(fontSize: 12,color: Color(0xFF989898)),),
                          ),
                          ListView.separated(
                            itemBuilder: (context,index){
                              Map itemSub = array[index];
                              bool isChoose = itemSub['select']??false;
                              return GestureDetector(
                                onTap: (){
                                  if(_isEnable==false)return;
                                  if(item['type']=='choice_single'){
                                    array.forEach((element) {
                                      element['select'] = false;
                                    });
                                    itemSub['select'] = true;
                                  }else{
                                    if(itemSub['select']==true){
                                      itemSub['select'] = false;
                                    }else{
                                      itemSub['select'] = true;
                                    }
                                  }
                                  setState(() {});
                                },
                                child: Container(
                                  child: Row(
                                    children: [
                                      Icon(
                                        isChoose?Icons.check_circle:Icons.radio_button_unchecked,
                                        size: 20,
                                        color: isChoose?_selectedColor:Color(0xFFD5D8DE),
                                      ),
                                      Container(
                                        width: conSize.maxWidth-20,
                                        padding: EdgeInsets.only(left: 5),
                                        child: Text(itemSub['name'],style: TextStyle(fontSize: 14,color: Color(0xFF989898)),),
                                      )
                                    ],
                                  ),
                                ),
                              );
                            },
                            separatorBuilder: (context,index){
                              return Container(height: 10,);
                            },
                            itemCount: array.length,
                            shrinkWrap: true,
                            physics: NeverScrollableScrollPhysics(),
                            padding: EdgeInsets.all(0),
                          )
                        ],
                      );
                    },
                  ),
                );
              },
              separatorBuilder: (context,index){
                return Container(height: 5,color: Color(0xFFF5F3F0),);
              },
              itemCount: _dataArray.length,
              padding: EdgeInsets.only(top: 20),
            ),
          ),
          if(_isEnable)Positioned(child: GestureDetector(
            onTap: () async{
              Map request = {'id':widget.questionId};
              List list = [];
              bool isSubmit = true;
              _dataArray.forEach((element) {
                if(isSubmit==false)return;
                Map item = {};
                item['random'] = element['random'];
                item['type'] = element['type'];
                List options = element['options'];
                List listSub = [];
                options.forEach((elementSub) {
                  if(elementSub['select']==true){
                    listSub.add(elementSub['name']);
                  }
                });
                if(listSub.isEmpty){
                  ysFlutterToast(context, '${element['title']}的选项不能为空');
                  isSubmit = false;
                  return;
                }
                item['answers'] = listSub;
                list.add(item);
              });
              if(isSubmit==false)return;
              request['list'] = list;
              // LogUtil.d(request);
              // return;
              Map dict = await ysRequestHttp(context, requestType.post, 'naire/submit', request);
              if(dict!=null){
                showGeneralDialog(context: context, pageBuilder: (_,__,___){
                  return Center(
                    child: Container(
                      width: MediaQuery.of(context).size.width-100,
                      height: 150,
                      decoration: BoxDecoration(
                          color: Colors.white,
                          borderRadius: BorderRadius.all(Radius.circular(10))
                      ),
                      child: Column(
                        children: [
                          Container(
                            height: 100,
                            decoration: BoxDecoration(
                                border: Border(bottom: BorderSide(color: Colors.grey,width: 0.1))
                            ),
                            alignment: Alignment.center,
                            child: Text('感谢您的答卷',style: TextStyle(fontSize: 14,color: Color(0xFF4A4A4A),decoration: TextDecoration.none,fontWeight: FontWeight.normal),),
                          ),
                          GestureDetector(
                            onTap: (){
                              Navigator.pop(context);
                              Navigator.pop(context);
                            },
                            behavior: HitTestBehavior.opaque,
                            child: Container(
                              height: 50,
                              alignment: Alignment.center,
                              child: Text('确认',style: TextStyle(fontSize: 16,color: Color(0xFFEE0A24),decoration: TextDecoration.none,fontWeight: FontWeight.normal),),
                            ),
                          )
                        ],
                      ),
                    ),
                  );
                });
              }
            },
            child: Container(
              decoration: BoxDecoration(
                color: Colors.pinkAccent,
                borderRadius: BorderRadius.all(Radius.circular(50))
              ),
              alignment: Alignment.center,
              child: Text('提交',style: TextStyle(fontSize: 14,color: Colors.white),),
            ),
          ),bottom: 30,left: MediaQuery.of(context).size.width/2-50,width: 100,height: 40,)
        ],
      ),
    );
  }

  _getQuestionData() async{
    Map dict = await ysRequestHttp(context, requestType.get, 'naire/info',{'naire_id':widget.questionId});
    if(dict!=null){
      _dataArray = dict['data']['questions']??'';
      _isEnable = dict['data']['is_can_submit']??false;
      if(_isEnable==false){
        _selectedColor = Colors.grey;
      }
      setState(() {});
    }
  }

}
