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

import 'base/YSBase.dart';
import 'base/YSTools.dart';

class YSDueDate extends StatefulWidget {
  final String dueDate;
  const YSDueDate({Key key, this.dueDate}) : super(key: key);
  @override
  _YSDueDateState createState() => _YSDueDateState();
}

class _YSDueDateState extends State<YSDueDate> {
  String dueDate;
  @override
  void initState() {
    // dueDate = widget.dueDate;
    Future.delayed(Duration(seconds: 0)).then((value) {
      _getChapterData();
    });
    super.initState();
  }
  _getChapterData() async{
    Map param = Map();
    param['category_id'] = 2;
    Map dict = await ysRequestHttpNoLoading(context, requestType.get, 'chapter/info', param);
    if(dict!=null){
      setState(() {
        dueDate =  dict['data']['expected_time'];
      });
    }
  }
  @override
  Widget build(BuildContext context) {
    return YSBase(
      ystitle: '设置预产期',
      yschild: SingleChildScrollView(
        child: Column(
          children: [
            Container(
              height: 45,
              width: MediaQuery.of(context).size.width,
              padding: EdgeInsets.all(15),
              child: Text('请输入产检时医生告知的预产期,也可以使用“计算预产期”',style: TextStyle(fontSize: 12,color: Color(0xFF8E1B3C),decoration: TextDecoration.none,fontWeight: FontWeight.w600),),
            ),
            Container(
              width: MediaQuery.of(context).size.width,
              height: 1.5,
              child: Image.asset('lib/images/line.png'),
            ),
            Container(
              width: MediaQuery.of(context).size.width,
              margin: EdgeInsets.only(left: 40,top: 25,bottom: 10),
              child: Text('预产期',style: TextStyle(fontSize: 12,color: Color(0xFF808080),decoration: TextDecoration.none,fontWeight: FontWeight.w600),),
            ),
            CupertinoButton(
              padding: EdgeInsets.all(0),
              child: Container(
                  width: MediaQuery.of(context).size.width-50,
                  margin: EdgeInsets.only(bottom: 20),
                  padding: EdgeInsets.only(left: 15,right: 15),
                  height: 40,
                  decoration: BoxDecoration(
                      border: Border.all(color: Color(0xFFD7D7D7),width: 0.5),
                      borderRadius: BorderRadius.all(Radius.circular(20))
                  ),
                  alignment: Alignment.centerLeft,
                  child: Text(dueDate==null?'选择预产期':dueDate,style: TextStyle(fontSize: 12,color: Color(0xFF808080),decoration: TextDecoration.none,fontWeight: FontWeight.normal),)
              ),
              onPressed: (){
                ysDatePicker(context, (value){
                  setState(() {
                    dueDate = value;
                  });
                });
              },
            ),
            CupertinoButton(
              padding: EdgeInsets.all(0),
              child: Container(
                  width: MediaQuery.of(context).size.width-50,
                  height: 20,
                  margin: EdgeInsets.only(bottom: 100),
                  alignment: Alignment.centerRight,
                  child: Container(
                    height: 20,
                    width: 100,
                    child: Row(
                      children: [
                        Image(height: 15,width: 15,image: AssetImage('lib/images/calculte.png'),),
                        Text('  计算预产期',style: TextStyle(fontSize: 12,color: Color(0xFF808080),decoration: TextDecoration.none,fontWeight: FontWeight.normal),)
                      ],
                    ),
                  )
              ),
              onPressed: (){
                String dateStr = '';
                // if(dueDate!=null){
                //   dateStr = dueDate;
                // }
                showDialog(context: context,builder: (context){
                  return Dialog(
                    backgroundColor: Colors.transparent,
                    child: StatefulBuilder(
                      builder: (context,setYSState){
                        return Container(
                          width: MediaQuery.of(context).size.width,
                          padding: EdgeInsets.all(15),
                          height: 150,
                          decoration: BoxDecoration(
                              color: Colors.white,
                              borderRadius: BorderRadius.all(Radius.circular(10))
                          ),
                          child: Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              Text('计算预产期',style: TextStyle(fontSize: 14,color: Color(0xFF292929),decoration: TextDecoration.none,fontWeight: FontWeight.w600),),
                              CupertinoButton(
                                padding: EdgeInsets.all(0),
                                child: Container(
                                  height: 30,
                                  width: MediaQuery.of(context).size.width-30,
                                  child: Text(dateStr.isNotEmpty?dateStr:'请选择末次月经时间进行计算',style: TextStyle(fontSize: 13,color: Color(0xFF808080),decoration: TextDecoration.none,fontWeight: FontWeight.normal),),
                                  decoration: BoxDecoration(
                                    border: Border(bottom: BorderSide(width: 0.5,color: Color(0xFFD7D7D7)))
                                  ),
                                ),
                                onPressed: (){
                                  ysDatePicker(context, (value){
                                    setYSState(() {
                                      dateStr = value;
                                    });
                                  },lastDate: DateTime.now().subtract(Duration(days: 1)),initDate: DateTime.now().subtract(Duration(days: 1)));
                                },
                              ),
                              Row(
                                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                children: [
                                  Container(),
                                  Row(
                                    children: [
                                      CupertinoButton(
                                        padding: EdgeInsets.all(0),
                                        child: Text('取消',style: TextStyle(fontSize: 13,color: Color(0xFF8E1B3C),decoration: TextDecoration.none,fontWeight: FontWeight.w600),),
                                        onPressed: (){
                                          Navigator.pop(context);
                                        },
                                      ),
                                      CupertinoButton(
                                        padding: EdgeInsets.all(0),
                                        child: Text('计算',style: TextStyle(fontSize: 13,color: Color(0xFF8E1B3C),decoration: TextDecoration.none,fontWeight: FontWeight.w600),),
                                        onPressed: () async{
                                          if(dateStr.isEmpty){
                                            ysFlutterToast(context, '请选择时间后进行计算');
                                            return;
                                          }
                                          Map dict = await ysRequestHttpNoLoading(context, requestType.post, 'phase/dueDateQuery', {'date':dateStr});
                                          if(dict!=null){
                                            Navigator.pop(context);
                                            dueDate = dict['data']['data'];
                                            setState(() {});
                                          }
                                        },
                                      )
                                    ],
                                  )
                                ],
                              )
                            ],
                          ),
                        );
                      },
                    ),
                  );
                });
              },
            ),
            CupertinoButton(
              padding: EdgeInsets.all(0),
              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: () async{
                if(dueDate==null){
                  ysFlutterToast(context, '请选择预产期');
                  return;
                }
                Map dict = await ysRequestHttpNoLoading(context, requestType.post, 'phase/dueDate', {'expected_time':dueDate});
                if(dict!=null){
                  Navigator.pop(context,'完成');
                }

              },
            )
          ],
        ),
      ),
    );
  }
}
