import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutterappfuyou/code/base/YSNetWorking.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'YSDoctorDetail.dart';
import 'base/YSBase.dart';

class YSAskQuestion extends StatefulWidget {
  final Map info;
  final bool isPolicy;
  const YSAskQuestion({Key key, this.info,this.isPolicy}) : super(key: key);
  @override
  _YSAskQuestionState createState() => _YSAskQuestionState();
}

class _YSAskQuestionState extends State<YSAskQuestion> {
  int selected = 0;
  TextEditingController _title = TextEditingController();
  TextEditingController _content = TextEditingController();
  @override
  Widget build(BuildContext context) {
    return YSBase(
      ystitle: '提问',
      yschild: SingleChildScrollView(
        physics: NeverScrollableScrollPhysics(),
        child: Column(
          children: [
            Container(
              height: MediaQuery.of(context).size.height-155,
              child: SingleChildScrollView(
                child: Column(
                  children: [
                    GestureDetector(
                      onTap: (){
                        Navigator.of(context).push(
                          CupertinoPageRoute(
                            builder: (context){
                              return YSDoctorDetail(doctorId: widget.info['id'],isPolicy: widget.isPolicy,);
                            }
                          )
                        );
                      },
                      child: Container(
                        height: 150,
                        decoration: BoxDecoration(borderRadius: BorderRadius.only(topLeft: Radius.circular(20),topRight: Radius.circular(20)),color: Colors.white,),
                        padding: EdgeInsets.only(left: 15,right: 15),
                        child: Row(
                          children: [
                            Container(
                              height: 120,
                              width: 100,
                              decoration: BoxDecoration(
                                color: Color(0xFF41898C),
                                borderRadius: BorderRadius.all(Radius.circular(5)),
                                image: DecorationImage(image: NetworkImage('${widget.info['avatar']}'),fit: BoxFit.fill)
                              ),
                            ),
                            Container(
                              height: 120,
                              margin: EdgeInsets.only(left: 10),
                              child: Column(
                                children: [
                                  Container(
                                    child: Text('${widget.info['name']}',style: TextStyle(fontSize: 16,color: Color(0xFF292929),fontWeight: FontWeight.w600),overflow: TextOverflow.ellipsis,),
                                    width: MediaQuery.of(context).size.width-140,
                                    margin: EdgeInsets.only(bottom: 5,top: 5),
                                  ),
                                  Container(
                                    child: Text('${widget.info['introduction']}',
                                      style: TextStyle(fontSize: 12,color: Color(0xFF808080),fontWeight: FontWeight.normal),overflow: TextOverflow.ellipsis,maxLines: 5,),
                                    width: MediaQuery.of(context).size.width-140,
                                  )
                                ],
                              ),
                            )
                          ],
                        ),
                      ),
                    ),
                    Container(
                      width: MediaQuery.of(context).size.width,
                      color: Colors.white,
                      margin: EdgeInsets.all(5),
                      padding: EdgeInsets.all(15),
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Container(
                            child: Row(
                              children: [
                                GestureDetector(
                                  onTap: (){
                                    setState(() {
                                      selected = 0;
                                    });
                                  },
                                  child: Container(
                                    margin: EdgeInsets.only(right: 20, bottom: 5),
                                    child: Text('公开',style: TextStyle(color: selected==0?Color(0xFFDB5279):Color(0xFF292929),fontSize: 14,decoration: TextDecoration.none,fontWeight: FontWeight.w600),),
                                  ),
                                ),
                                GestureDetector(
                                  onTap: (){
                                    setState(() {
                                      selected = 1;
                                    });
                                  },
                                  child: Container(
                                    margin: EdgeInsets.only(bottom: 5),
                                    child: Text('私密',style: TextStyle(color: selected==1?Color(0xFFDB5279):Color(0xFF292929),fontSize: 14,decoration: TextDecoration.none,fontWeight: FontWeight.w600),),
                                  ),
                                )
                              ],
                            ),
                          ),
                          Container(height: 2,width: 15,color: Color(0xFFDB5279),margin: EdgeInsets.only(left: selected==0?7:55),),
                          Container(
                            margin: EdgeInsets.only(top: 10),
                            width: MediaQuery.of(context).size.width,
                            height: 1.5,
                            child: Image.asset('lib/images/line.png'),
                          ),
                          Container(
                            height: 50,
                            child: Row(
                              children: [
                                Text('标题',style: TextStyle(fontSize: 15,color: Color(0xFF292929),fontWeight: FontWeight.w600),),
                                Container(
                                  height: 30,
                                  margin: EdgeInsets.only(left: 10),
                                  width: MediaQuery.of(context).size.width-90,
                                  child: CupertinoTextField(
                                    placeholder: '请输入问题梗概',
                                    style: TextStyle(fontSize: 14,color: Color(0xFF292929)),
                                    placeholderStyle: TextStyle(fontSize: 14,color: Color(0xFF808080)),
                                    decoration: BoxDecoration(),
                                    controller: _title,
                                  ),
                                )
                              ],
                            ),
                          ),
                          Container(
                            margin: EdgeInsets.only(bottom: 10),
                            width: MediaQuery.of(context).size.width,
                            height: 1.5,
                            child: Image.asset('lib/images/line.png'),
                          ),
                          Text('详情',style: TextStyle(fontSize: 15,color: Color(0xFF292929),fontWeight: FontWeight.w600),),
                          Container(
                            height: 180,
                            child: CupertinoTextField(
                              placeholder: '请输入问题内容（10-500个字内）',
                              style: TextStyle(fontSize: 14,color: Color(0xFF292929)),
                              placeholderStyle: TextStyle(fontSize: 14,color: Color(0xFF808080)),
                              decoration: BoxDecoration(),
                              maxLines: 1000,
                              maxLength: 500,
                              controller: _content,
                            ),
                          )
                        ],
                      ),
                    ),
                  ],
                ),
              ),
            ),
            Container(
              height: 70,
              width: MediaQuery.of(context).size.width,
              color: Colors.white,
              child: CupertinoButton(
                padding: EdgeInsets.only(top: 5),
                child: Container(
                  width: MediaQuery.of(context).size.width-150,
                  height: 40,
                  decoration: BoxDecoration(
                      color: Color(0xFFEA6C8F),
                      borderRadius: BorderRadius.all(Radius.circular(20))
                  ),
                  alignment: Alignment.center,
                  child: Text('提交',style: TextStyle(fontSize: 16,color: Colors.white,decoration: TextDecoration.none),),
                ),
                onPressed: (){
                  _postAnswerData();
                },
              )
            )
          ],
        ),
      ),
    );
  }

  _postAnswerData() async{
    if(_title.text.isEmpty){
      ysFlutterToast(context, '请输入标题');
      return;
    }
    if(_content.text.length<10||_content.text.length>500){
      ysFlutterToast(context, '内容应该为10-500个字内');
      return;
    }
    Map request = Map();
    request['question_title'] = _title.text;
    request['question_body'] = _content.text;
    request['is_show'] = selected==0?1:0;
    if(widget.isPolicy==false){
      SharedPreferences prefer = await SharedPreferences.getInstance();
      request['category_id'] = prefer.getInt('chapters');
      request['hospital_doctor_id'] = widget.info['id'];
    }else{
      request['question_doctor_id'] = widget.info['id'];
      request['question_area_id'] = widget.info['area_id'];
    }
    Map dict = await ysRequestHttp(context, requestType.post, widget.isPolicy==false?'question/ask':'policy/ask', request);
    if(dict!=null){
      ysFlutterToast(context, '问题提交成功，等待医师回复');
      _title.text = '';
      _content.text = '';
    }
  }
}
