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 'base/YSBase.dart';

class YSAddChildBody extends StatefulWidget {
  final Map infoDict;
  const YSAddChildBody({Key key, this.infoDict}) : super(key: key);
  @override
  _YSAddChildBodyState createState() => _YSAddChildBodyState();
}

class _YSAddChildBodyState extends State<YSAddChildBody> {
  String monthStr;
  String height;
  String weight;
  @override
  void initState() {
    if(widget.infoDict!=null){
      monthStr = '${widget.infoDict['month_age']}';
    }
    super.initState();
  }
  @override
  Widget build(BuildContext context) {
    return YSBase(
      ystitle: '添加身高体重',
      ysright: CupertinoButton(
        padding: EdgeInsets.all(0),
        child: Container(
          height: 30,
          width: 55,
          decoration: BoxDecoration(
              color: Color(0xFFFFEB3B),
              border: Border.all(color: Color(0xFF292929),width: 1),
              borderRadius: BorderRadius.all(Radius.circular(20))
          ),
          alignment: Alignment.center,
          child: Text('保存',style: TextStyle(fontSize: 14,color: Color(0xFF292929),decoration: TextDecoration.none),),
        ),
        onPressed: (){
          _postChildBodyData();
        },
      ),
      yschild: SingleChildScrollView(
        padding: EdgeInsets.only(left: 15,right: 15,top: 5),
        child: Column(
          children: [
            Container(
              height: 50,
              child: Row(
                children: [
                  Text('身高',style: TextStyle(fontSize: 14,color: Color(0xFF292929)),),
                  CupertinoButton(
                    padding: EdgeInsets.all(0),
                    child: Container(
                      width: MediaQuery.of(context).size.width-200,
                      margin: EdgeInsets.only(left: 35,right: 10),
                      child: Text(height!=null?height:widget.infoDict!=null?'${widget.infoDict['height']}':'请选择身高',style: TextStyle(fontSize: 14,color: Color(0xFF808080)),),
                    ),
                    onPressed: (){
                      _chooseNumber(1);
                    },
                  ),
                  Text('CM',style: TextStyle(fontSize: 14,color: Color(0xFF292929)),),
                ],
              ),
            ),
            Container(
              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: 14,color: Color(0xFF292929)),),
                  CupertinoButton(
                    padding: EdgeInsets.all(0),
                    child: Container(
                      width: MediaQuery.of(context).size.width-200,
                      margin: EdgeInsets.only(left: 35,right: 10),
                      child: Text(weight!=null?weight:widget.infoDict!=null?'${widget.infoDict['weight']}':'请选择体重',style: TextStyle(fontSize: 14,color: Color(0xFF808080)),),
                    ),
                    onPressed: (){
                      _chooseNumber(2);
                    },
                  ),
                  Text('KG',style: TextStyle(fontSize: 14,color: Color(0xFF292929)),),
                ],
              ),
            ),
            Container(
              width: MediaQuery.of(context).size.width,
              height: 1.5,
              child: Image.asset('lib/images/line.png'),
            ),
            Container(
              height: 50,
              child: monthStr=='0'?Row(
                children: [
                  Text('月龄',style: TextStyle(fontSize: 14,color: Color(0xFF292929)),),
                  CupertinoButton(
                    padding: EdgeInsets.all(0),
                    child: Container(
                      width: MediaQuery.of(context).size.width-200,
                      margin: EdgeInsets.only(left: 35,right: 10),
                      child: Text('新生儿',style: TextStyle(fontSize: 14,color: Color(0xFF808080)),),
                    ),
                    onPressed: widget.infoDict!=null?null:(){
                      _chooseNumber(3);
                    },
                  ),
                  Text('',style: TextStyle(fontSize: 14,color: Color(0xFF292929)),),
                ],
              ):Row(
                children: [
                  Text('月龄',style: TextStyle(fontSize: 14,color: Color(0xFF292929)),),
                  CupertinoButton(
                    padding: EdgeInsets.all(0),
                    child: Container(
                      width: MediaQuery.of(context).size.width-200,
                      margin: EdgeInsets.only(left: 35,right: 10),
                      child: Text(monthStr!=null?monthStr:widget.infoDict!=null?'${widget.infoDict['month_age']}':'请选择月龄',style: TextStyle(fontSize: 14,color: Color(0xFF808080)),),
                    ),
                    onPressed: widget.infoDict!=null?null:(){
                      _chooseNumber(3);
                    },
                  ),
                  Text('月',style: TextStyle(fontSize: 14,color: Color(0xFF292929)),),
                ],
              ),
            ),
            Container(
              width: MediaQuery.of(context).size.width,
              height: 1.5,
              child: Image.asset('lib/images/line.png'),
            ),
          ],
        ),
      ),
    );
  }

  _chooseNumber(int type){
    int min,max;
    if(type==1){
      widget.infoDict!=null?height = '${widget.infoDict['height']}':height = '30';
      min = 30;
      max = 125;
    }else if(type==2){
      widget.infoDict!=null?weight = '${widget.infoDict['weight']}':weight = '1';
      min = 1;
      max = 30;
    }else{
      widget.infoDict!=null?monthStr = '${widget.infoDict['month_age']}':monthStr = '0';
      min = 0;
      max = 72;
    }
    List array = [];
    for(int i = min;i<=max;i++){
      if(type==3){
        if(i==0){
          array.add({'label':'新生儿','value':i});
        }else{
          String age = '新生儿';
          if(i<=12){
            age = '$i个月';
          }else{
            int a = i%12;
            if(a==0){
              age = '${i~/12}岁';
            }else{
              age = '${i~/12}岁零$a个月';
            }
          }
          array.add({'label':age,'value':i});
        }
      }else{
        array.add({'label':i,'value':i});
      }
    }
    showModalBottomSheet(
        backgroundColor: Colors.transparent,
        context: context,
        builder: (context){
          return Container(
            height: 300,
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.end,
              children: [
                CupertinoButton(
                  padding: EdgeInsets.all(0),
                  child: Container(
                    height: 20,
                    child: Image.asset('lib/images/off.png'),
                  ),
                  onPressed: (){
                    Navigator.pop(context);
                  },
                ),
                Container(
                  height: 200,
                  padding: EdgeInsets.all(10),
                  decoration: BoxDecoration(
                      color: Colors.white,
                      borderRadius: BorderRadius.only(topLeft: Radius.circular(20),topRight: Radius.circular(20))
                  ),
                  child: CupertinoPicker(
                    itemExtent: 45,
                    onSelectedItemChanged: (index){
                      if(type==1){
                        height = (array[index]['value']).toString();
                      }else if(type==2){
                        weight = (array[index]['value']).toString();
                      }else{
                        monthStr = (array[index]['value']).toString();
                      }
                    },
                    children: array.map((e) {
                      return Container(child: Text('${e['label']}'),alignment: Alignment.center,);
                    }).toList(),
                  ),
                ),
                Container(
                  height: 56,
                  width: MediaQuery.of(context).size.width,
                  color: Colors.white,
                  child: CupertinoButton(
                    padding: EdgeInsets.all(0),
                    child: Container(
                      height: 30,
                      width: 100,
                      decoration: BoxDecoration(
                          color: Colors.white,
                          borderRadius: BorderRadius.all(Radius.circular(15)),
                          boxShadow: [
                            BoxShadow(color: Colors.grey,blurRadius: 5,offset: Offset(1, 1))
                          ]
                      ),
                      child: Text('确定',style: TextStyle(fontSize: 14,color: Color(0xFF4CC17C),decoration: TextDecoration.none,fontWeight: FontWeight.normal)),
                      alignment: Alignment.center,
                    ),
                    onPressed: (){
                      Navigator.pop(context);
                      setState(() {});
                    },
                  ),
                  alignment: Alignment.center,
                )
              ],
            ),
          );
        }
    );
  }

  _postChildBodyData() async{
    if(height==null&&widget.infoDict!=null)height = '${widget.infoDict['height']}';
    if(weight==null&&widget.infoDict!=null)weight = '${widget.infoDict['weight']}';
    if(monthStr==null&&widget.infoDict!=null)monthStr = '${widget.infoDict['month_age']}';
    SharedPreferences prefer = await SharedPreferences.getInstance();
    if(height==null){
      ysFlutterToast(context, '请选择身高');
      return;
    }
    if(weight==null){
      ysFlutterToast(context, '请选择体重');
      return;
    }
    if(monthStr==null){
      ysFlutterToast(context, '请选择月龄');
      return;
    }
    Map request = Map();
    request['child_id'] = prefer.getInt('childId');
    request['height'] = height;
    request['weight'] = weight;
    request['month_age'] = monthStr;
    Map dict = await ysRequestHttp(context, requestType.post, 'child/body/addOrUp', request);
    if(dict!=null){
      Navigator.of(context).pop('确定');
    }
  }

}
