import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

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

class YSChapterInputView extends StatefulWidget {
  final Map item;
  const YSChapterInputView({Key key, this.item}) : super(key: key);

  @override
  _YSChapterInputViewState createState() => _YSChapterInputViewState();
}

class _YSChapterInputViewState extends State<YSChapterInputView> {
  TextEditingController _editingController = TextEditingController();
  @override
  void dispose() {
    _editingController.dispose();
    super.dispose();
  }
  @override
  void initState() {
    _editingController.text = widget.item['value']??'';
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    bool isNumber = '${widget.item['title']}'.contains('数');
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Container(
          margin: EdgeInsets.only(bottom: 10),
          child: Text('${widget.item['title']}',style: TextStyle(fontSize: 14,color: Color(0xFF606266),decoration: TextDecoration.none,fontWeight: FontWeight.w600),),
        ),
        Container(
          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))
          ),
          child: CupertinoTextField(
            style: TextStyle(fontSize: 14,color: Color(0xFF606266),decoration: TextDecoration.none,fontWeight: FontWeight.normal),
            placeholderStyle: TextStyle(fontSize: 14,color: Color(0xFF8A8A8A),decoration: TextDecoration.none,fontWeight: FontWeight.normal),
            placeholder: '请输入${widget.item['title']}',
            decoration: BoxDecoration(),
            prefix: Image(height: 15,width: 15,image: AssetImage('lib/images/${widget.item['image']}.png'),),
            inputFormatters: [if(isNumber)FilteringTextInputFormatter.digitsOnly],
            controller: _editingController,
            keyboardType: isNumber?TextInputType.number:TextInputType.text,
            onChanged: (value){
              if(value.isNotEmpty){
                widget.item['value'] = value;
              }else{
                widget.item.remove('value');
              }
            },
          )
        ),
      ],
    );
  }
}

class YSChapterChooseView extends StatefulWidget {
  final Map item;
  const YSChapterChooseView({Key key, this.item}) : super(key: key);

  @override
  _YSChapterChooseViewState createState() => _YSChapterChooseViewState();
}

class _YSChapterChooseViewState extends State<YSChapterChooseView> {

  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Container(
          margin: EdgeInsets.only(bottom: 10),
          child: Text('${widget.item['title']}',style: TextStyle(fontSize: 12,color: Color(0xFF808080),decoration: TextDecoration.none,fontWeight: FontWeight.w600),),
        ),
        Container(
          height: 40,
          decoration: BoxDecoration(
            border: Border.all(color: Color(0xFFD7D7D7),width: 0.5),
            borderRadius: BorderRadius.all(Radius.circular(20))
          ),
          child: CupertinoButton(
            padding: EdgeInsets.only(left: 15,right: 15),
            child: Row(
              children: [
                Padding(
                  padding: EdgeInsets.only(right: 5),
                  child: Image(height: 15,width: 15,image: AssetImage('lib/images/${widget.item['image']}.png'),)
                ),
                Container(
                  height: 40,
                  alignment: Alignment.centerLeft,
                  child: Text(widget.item['value']==null?'选择${widget.item['title']}':widget.item['value'],style: TextStyle(fontSize: 12,color: Color(0xFF808080),decoration: TextDecoration.none,fontWeight: FontWeight.normal),)
                )
              ],
            ),
            onPressed: (){
              FocusScope.of(context).unfocus();
              if('${widget.item['title']}'.contains('生日')||'${widget.item['title']}'.contains('预产期')){
                ysDatePicker(context, (value){
                  setState(() {
                    widget.item['value'] = value;
                  });
                });
              }else if('${widget.item['title']}'.contains('性别')){
                ysShowCenterAlertView(context, YSChooseBabySexView(postSex: (value){
                  widget.item['value'] = value;
                  setState(() {});
                },));
              }
            },
          ),
        ),
      ],
    );
  }
}

class YSChooseBabySexView extends StatefulWidget {
  final ValueSetter postSex;
  const YSChooseBabySexView({Key key, this.postSex}) : super(key: key);

  @override
  _YSChooseBabySexViewState createState() => _YSChooseBabySexViewState();
}

class _YSChooseBabySexViewState extends State<YSChooseBabySexView> {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: Container(
        width: ysWidth(context)-60,
        height: 150,
        decoration: BoxDecoration(
          borderRadius: BorderRadius.all(Radius.circular(10)),
          color: Colors.white,
        ),
        child: Column(
          children: [
            Container(
              height: 50,
              alignment: Alignment.centerLeft,
              padding: EdgeInsets.only(left: 20,right: 20),
              child: Text('选择性别',style: TextStyle(fontSize: 14,color: Color(0xFF292929),decoration: TextDecoration.none,fontWeight: FontWeight.w600)),
            ),
            CupertinoButton(
              padding: EdgeInsets.all(0),
              child: Container(
                height: 50,
                alignment: Alignment.centerLeft,
                padding: EdgeInsets.only(left: 20,right: 20),
                child: Text('男',style: TextStyle(fontSize: 14,color: Color(0xFF292929),decoration: TextDecoration.none,fontWeight: FontWeight.normal),),
              ),
              onPressed: (){
                Navigator.pop(context);
                widget.postSex('男');
              },
            ),
            CupertinoButton(
              padding: EdgeInsets.all(0),
              child: Container(
                height: 50,
                alignment: Alignment.centerLeft,
                padding: EdgeInsets.only(left: 20,right: 20),
                child: Text('女',style: TextStyle(fontSize: 14,color: Color(0xFF292929),decoration: TextDecoration.none,fontWeight: FontWeight.normal),),
              ),
              onPressed: (){
                Navigator.pop(context);
                widget.postSex('女');
              },
            )
          ],
        ),
      ),
    );
  }
}


class YSSubmitView extends StatelessWidget {
  final VoidCallback callback;
  final String title;
  const YSSubmitView({Key key, this.callback, this.title = '确定'}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      child: Container(
        height: 40,
        decoration: BoxDecoration(
            color: Color(0xFFEA6C8F),
            borderRadius: BorderRadius.all(Radius.circular(20))
        ),
        alignment: Alignment.center,
        child: Text(title,style: TextStyle(fontSize: 16,color: Colors.white,decoration: TextDecoration.none),),
      ),
      onTap: (){
        FocusScope.of(context).unfocus();
        callback();
      },
    );
  }
}
