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

class YSPeriod extends StatefulWidget {
  @override
  _YSPeriodState createState() => _YSPeriodState();
}

class _YSPeriodState extends State<YSPeriod> {
  TextEditingController _time = TextEditingController();
  TextEditingController _period = TextEditingController();
  _getChapterData() async{
    Map dict = await ysRequestHttpNoLoading(context, requestType.get, 'chapter/info', {'category_id':1});
    if(dict!=null){
      Map data = dict['data']??{};
      _time.text = '${data['duration']??''}';
      _period.text = '${data['cycle']??''}';
      setState(() {});
    }
  }
  @override
  void initState() {
    Future.delayed(Duration(seconds: 0)).then((value) {
      _getChapterData();
    });
    super.initState();
  }
  @override
  Widget build(BuildContext context) {
    return YSBase(
      ystitle: '经期设置',
      yschild: SingleChildScrollView(
        child: Column(
          children: [
            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),),
            ),
            Container(
                width: MediaQuery.of(context).size.width-50,
                margin: EdgeInsets.only(bottom: 10),
                padding: EdgeInsets.only(left: 10,right: 10),
                height: 40,
                decoration: BoxDecoration(
                    border: Border.all(color: Color(0xFFD7D7D7),width: 0.5),
                    borderRadius: BorderRadius.all(Radius.circular(20))
                ),
                child: CupertinoTextField(
                  style: TextStyle(fontSize: 12,color: Color(0xFF808080),decoration: TextDecoration.none,fontWeight: FontWeight.normal),
                  placeholderStyle: TextStyle(fontSize: 12,color: Color(0xFF808080),decoration: TextDecoration.none,fontWeight: FontWeight.normal),
                  placeholder: '请输入周期天数',
                  decoration: BoxDecoration(),
                  prefix: Image(height: 15,width: 15,image: AssetImage('lib/images/time.png'),),
                  inputFormatters: [FilteringTextInputFormatter.digitsOnly],
                  controller: _period,
                  keyboardType: TextInputType.number,
                )
            ),
            Container(
              width: MediaQuery.of(context).size.width,
              margin: EdgeInsets.only(left: 40,top: 10,bottom: 10),
              child: Text('月经持续天数',style: TextStyle(fontSize: 12,color: Color(0xFF808080),decoration: TextDecoration.none,fontWeight: FontWeight.w600),),
            ),
            Container(
                width: MediaQuery.of(context).size.width-50,
                margin: EdgeInsets.only(bottom: 80),
                padding: EdgeInsets.only(left: 10,right: 10),
                height: 40,
                decoration: BoxDecoration(
                    border: Border.all(color: Color(0xFFD7D7D7),width: 0.5),
                    borderRadius: BorderRadius.all(Radius.circular(20))
                ),
                child: CupertinoTextField(
                  style: TextStyle(fontSize: 12,color: Color(0xFF808080),decoration: TextDecoration.none,fontWeight: FontWeight.normal),
                  placeholderStyle: TextStyle(fontSize: 12,color: Color(0xFF808080),decoration: TextDecoration.none,fontWeight: FontWeight.normal),
                  placeholder: '请输入持续天数',
                  inputFormatters: [FilteringTextInputFormatter.digitsOnly],
                  decoration: BoxDecoration(),
                  prefix: Image(height: 15,width: 15,image: AssetImage('lib/images/water.png'),),
                  controller: _time,
                  keyboardType: TextInputType.number,
                )
            ),
            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: (){
                FocusScope.of(context).unfocus();
                _postPeriodData();
              },
            )
          ],
        ),
      ),
    );
  }
  _postPeriodData() async{
    if(_period.text.isEmpty){
      ysFlutterToast(context, '请输入周期时间');
      return;
    }
    if(int.parse(_period.text)>51){
      ysFlutterToast(context, '周期时间过长');
      return;
    }
    if(_time.text.isEmpty){
      ysFlutterToast(context, '请输入持续时间');
      return;
    }
    if(int.parse(_time.text)>51){
      ysFlutterToast(context, '持续时间过长');
      return;
    }
    Map dict = await ysRequestHttp(context,requestType.put, 'motherhood/period', {'cycle':_period.text,'duration':_time.text});
    if(dict!=null){
      Navigator.of(context).pop('完成');
    }
  }
}
