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

class YSRecord extends StatefulWidget {
  @override
  _YSRecordState createState() => _YSRecordState();
}

class _YSRecordState extends State<YSRecord> {
  List _periodList = [];
  List _roomList = [];
  @override
  void initState() {
    Future.delayed(Duration(seconds: 0)).then((value){
      _getRecordListData();
    });
    super.initState();
  }
  _deletePeriodData(int type,String date) async{
    Map dict = await ysRequestHttpNoLoading(context, requestType.delete, 'motherhood/periodDate/delete', {'type':type.toString(),'date':date});
    if(dict!=null){
      _getRecordListData();
    }
  }
  @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: (){
          _getRecordListData();
        },
      ),
      yschild: SingleChildScrollView(
        child: Column(
          children: [
            Container(
              height: MediaQuery.of(context).size.height-ysTOP(context)-120,
              color: Colors.white,
              child: Column(
                children: [
                  Row(
                    children: [
                      Container(
                        width: MediaQuery.of(context).size.width/2,
                        height: 80,
                        padding: EdgeInsets.only(top: 15,bottom: 15,left: 25),
                        child: Row(
                          children: [
                            Container(
                              child: Image.asset('lib/images/yuejing.png'),
                              height: 45,
                              width: 45,
                            ),
                            Text('  月经记录',style: TextStyle(fontSize: 14,color: Color(0xFF292929)),)
                          ],
                        ),
                      ),
                      Container(
                        width: MediaQuery.of(context).size.width/2,
                        height: 80,
                        padding: EdgeInsets.only(top: 15,bottom: 15,left: 25),
                        child: Row(
                          children: [
                            Container(
                              child: Image.asset('lib/images/tongfang.png'),
                              height: 45,
                              width: 45,
                            ),
                            Text('  同房记录',style: TextStyle(fontSize: 14,color: Color(0xFF292929)),)
                          ],
                        ),
                      ),
                    ],
                  ),
                  Container(
                    height:  MediaQuery.of(context).size.height-ysTOP(context)-200,
                    width: MediaQuery.of(context).size.width,
                    child: ListView.separated(
                      itemBuilder: (context,index){
                        return Row(
                          children: [
                          _periodList.length>index?GestureDetector(
                            child: Container(
                                margin: EdgeInsets.only(left: 20,right: 20,bottom: 5),
                                height: 40,
                                decoration: BoxDecoration(
                                    color: Colors.white,
                                    boxShadow: [
                                      BoxShadow(color: Color(0x1F1D2129),blurRadius: 3,offset: Offset(0.5, 0.5))
                                    ]
                                ),
                                child: Row(
                                  children: [
                                    Container(
                                      height: 40,
                                      width: 5,
                                      decoration: BoxDecoration(
                                          color: Color(0xFFDB5278),
                                          borderRadius: BorderRadius.only(topLeft: Radius.circular(3),bottomLeft: Radius.circular(3))
                                      ),
                                    ),
                                    Container(
                                      alignment: Alignment.center,
                                      width: MediaQuery.of(context).size.width/2-45,
                                      child: Text('${_periodList[index]['date']}',style: TextStyle(fontSize: 14,color: Color(0xFF292929)),),
                                    )
                                  ],
                                ),
                              ),
                            onLongPress: () {
                              ysShowCenterAlertView(context, YSTipsAlertView(
                                tipsStr: '是否删除此记录?',
                                valueSetter: (value){
                                  if(value){
                                    _deletePeriodData(1, _periodList[index]['date']);
                                  }
                                },
                              ));
                            },
                          ):Container(width: MediaQuery.of(context).size.width/2,),
                        _roomList.length>index?GestureDetector(
                          child: Container(
                                margin: EdgeInsets.only(left: 20,right: 20,bottom: 5),
                                height: 40,
                                decoration: BoxDecoration(
                                    color: Colors.white,
                                    boxShadow: [
                                      BoxShadow(color: Color(0x1F1D2129),blurRadius: 3,offset: Offset(0.5, 0.5))
                                    ]
                                ),
                                child: Row(
                                  children: [
                                    Container(
                                      height: 40,
                                      width: 5,
                                      decoration: BoxDecoration(
                                          color: Color(0xFF7292DB),
                                          borderRadius: BorderRadius.only(topLeft: Radius.circular(3),bottomLeft: Radius.circular(3))
                                      ),
                                    ),
                                    Container(
                                      alignment: Alignment.center,
                                      width: MediaQuery.of(context).size.width/2-45,
                                      child: Text('${_roomList[index]['date']}',style: TextStyle(fontSize: 14,color: Color(0xFF292929)),),
                                    )
                                  ],
                                )
                              ),
                          onLongPress: (){
                            ysShowCenterAlertView(context, YSTipsAlertView(
                              tipsStr: '是否删除此记录?',
                              valueSetter: (value){
                                if(value){
                                  _deletePeriodData(2, _roomList[index]['date']);
                                }
                              },
                            ));
                          },
                        ):Container(width: MediaQuery.of(context).size.width/2,),
                          ],
                        );
                      },
                      separatorBuilder: (context,index){
                        return Divider(color: Colors.white,height: 10,thickness: 10,);
                      },
                      itemCount: _roomList.length>_periodList.length?_roomList.length:_periodList.length,
                      padding: EdgeInsets.only(top: 5,bottom: 5),
                    ),
                  )
                ],
              ),
            ),
            CupertinoButton(
              color: Colors.white,
              padding: EdgeInsets.all(0),
              child: Container(
                height: 40,
                width: MediaQuery.of(context).size.width-150,
                decoration: BoxDecoration(
                    color: Color(0xFFDB5278),
                    borderRadius: BorderRadius.all(Radius.circular(20))
                ),
                alignment: Alignment.center,
                child: Text('添加',style: TextStyle(fontSize: 16,color: Colors.white),),
                margin: EdgeInsets.only(top: 15,bottom: 15,left: 75,right: 75),
              ),
              onPressed: (){
                showModalBottomSheet(
                  backgroundColor: Colors.transparent,
                  context: context,
                  builder: (context){
                    return Container(
                      height: 200,
                      decoration: BoxDecoration(
                        borderRadius: BorderRadius.only(topLeft: Radius.circular(20),topRight: Radius.circular(20)),
                        color: Colors.white
                      ),
                      child: Column(
                        children: [
                          Container(
                            width: MediaQuery.of(context).size.width,
                            height: 49.5,
                            alignment: Alignment.center,
                            child: Text('添加',style: TextStyle(fontSize: 16,color: Color(0xFF292929)),),
                          ),
                          Divider(color: Color(0xFFE6E6E6),height: 0.5,thickness: 0.5,),
                          Row(
                            children: [
                              CupertinoButton(
                                child: Container(
                                  height: 100,
                                  width: MediaQuery.of(context).size.width/2,
                                  child: Column(
                                    children: [
                                      Container(
                                        child: Image.asset('lib/images/yuejing.png'),
                                        height: 45,
                                        width: 45,
                                      ),
                                      Text('月经记录',style: TextStyle(fontSize: 14,color: Color(0xFF292929)),)
                                    ],
                                    mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                                  ),
                                ),
                                padding: EdgeInsets.all(0),
                                onPressed: (){
                                  _postPeriodData(1);
                                },
                              ),
                              CupertinoButton(
                                child: Container(
                                  height: 100,
                                  width: MediaQuery.of(context).size.width/2,
                                  child: Column(
                                    children: [
                                      Container(
                                        child: Image.asset('lib/images/tongfang.png'),
                                        height: 45,
                                        width: 45,
                                      ),
                                      Text('同房记录',style: TextStyle(fontSize: 14,color: Color(0xFF292929)),)
                                    ],
                                    mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                                  ),
                                ),
                                padding: EdgeInsets.all(0),
                                onPressed: (){
                                  _postPeriodData(2);
                                },
                              )
                            ],
                          ),
                          Divider(color: Color(0xFFE6E6E6),height: 0.5,thickness: 0.5,),
                          CupertinoButton(
                            child: Container(
                              width: MediaQuery.of(context).size.width,
                              height: 49.5,
                              alignment: Alignment.center,
                              child: Text('取消',style: TextStyle(fontSize: 16,color: Color(0xFF292929)),),
                            ),
                            padding: EdgeInsets.all(0),
                            onPressed: (){
                              Navigator.pop(context);
                            },
                          ),
                        ],
                      ),
                    );
                  }
                );
              },
            )
          ],
        ),
      ),
    );
  }
  
  _getRecordListData() async{
    _periodList.clear();
    _roomList.clear();
    Map dict = await ysRequestHttp(context, requestType.get, 'motherhood/periodDate/list', {});
    if(dict!=null){
      List array = dict['data'];
      array.forEach((element) {
        if(element['type']==1){
          _periodList.add(element);
        }else{
          _roomList.add(element);
        }
      });
      setState(() {});
    }
  }

  _postPeriodData(int type) {
    Navigator.pop(context);
    ysDatePicker(context, (value) async{
      Map dict = await ysRequestHttpNoLoading(context, requestType.post, 'motherhood/periodDate/add', {'type':type.toString(),'date':value});
      if(dict!=null){
        _getRecordListData();
      }
    });
  }

}
