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

import 'YSTools.dart';

class YSDatePicker extends StatefulWidget {
  final ValueSetter<String> choose;
  final int type;
  const YSDatePicker({Key? key, required this.choose, this.type = 1}) : super(key: key);
  @override
  YSDatePickerState createState() => YSDatePickerState();
}

class YSDatePickerState extends State<YSDatePicker> {
  String birthday = '';
  @override
  void initState() {
    DateTime now = DateTime.now();
    if(widget.type==1){
      birthday = '${now.year}-${now.month.toString().padLeft(2,'0')}';
    }else if(widget.type==2){
      birthday = '${now.year}-${now.month.toString().padLeft(2,'0')}-${now.day.toString().padLeft(2,'0')}';
    }else if(widget.type==3){
      birthday = '${now.year}-${now.month.toString().padLeft(2,'0')}-${now.day.toString().padLeft(2,'0')} ${now.hour.toString().padLeft(2,'0')}:${now.minute.toString().padLeft(2,'0')}';
    }
    super.initState();
  }
  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.transparent,
      height: 280,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.end,
        children: [
          Container(
            height: 40,
            padding: const EdgeInsets.only(left: 20,right: 20),
            decoration: const BoxDecoration(
                color: Colors.white,
                border: Border(bottom: BorderSide(width: 0.5,color: Color(0xFFEEEEEE)))
            ),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                GestureDetector(
                  child: const Text('取消',style: TextStyle(fontSize: 15,color: Color(0xFF999999)),),
                  onTap: (){
                    Navigator.pop(context);
                  },
                ),
                GestureDetector(
                  child: const Text('确定',style: TextStyle(fontSize: 15,color: Color(0xFF108DE9)),),
                  onTap: (){
                    widget.choose(birthday);
                    Navigator.pop(context);
                  },
                )
              ],
            ),
          ),
          Container(
            height: 240,
            padding: const EdgeInsets.all(10),
            decoration: const BoxDecoration(
              color: Colors.white,
            ),
            child: CupertinoDatePicker(
              initialDateTime: DateTime.now(),
              onDateTimeChanged: (date) {//+' '+date.hour.toString().padLeft(2,'0')+':'+date.minute.toString().padLeft(2,'0')
                if(widget.type==1){
                  birthday = '${date.year}-${date.month.toString().padLeft(2,'0')}';
                }else if(widget.type==2){
                  birthday = '${date.year}-${date.month.toString().padLeft(2,'0')}-${date.day.toString().padLeft(2,'0')}';
                }else if(widget.type==3){
                  birthday = '${date.year}-${date.month.toString().padLeft(2,'0')}-${date.day.toString().padLeft(2,'0')} ${date.hour.toString().padLeft(2,'0')}:${date.minute.toString().padLeft(2,'0')}';
                }
              },
              mode: widget.type==3?CupertinoDatePickerMode.dateAndTime:CupertinoDatePickerMode.date,
            ),
          ),
        ],
      ),
    );
  }
}

class YSMonthPicker extends StatefulWidget {
  final ValueSetter valueSetter;
  const YSMonthPicker({Key? key, required this.valueSetter}) : super(key: key);

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

class YSMonthPickerState extends State<YSMonthPicker> {
  late FixedExtentScrollController _yearController;
  late FixedExtentScrollController _monthController;
  final List _yearArray = [];
  String _yearStr = '';
  final List _monthArray = [];
  String _monthStr = '';
  @override
  void initState() {
    int yNumber= 2000;
    for(int i=0;i<40;i++){
      yNumber++;
      _yearArray.add({'title':'$yNumber年','value':'$yNumber'});
    }
    int mNumber= 0;
    for(int i=0;i<12;i++){
      mNumber++;
      _monthArray.add({'title':'$mNumber月','value':'$mNumber'});
    }
    DateTime now = DateTime.now();
    _yearStr = '${now.year}';
    _monthStr = '${now.month}'.padLeft(2,'0');
    int yearIndex = _yearArray.indexWhere((element) => element['value']=='${now.year}');
    int monthIndex = _monthArray.indexWhere((element) => element['value']=='${now.month}');
    _yearController = FixedExtentScrollController(initialItem: yearIndex);
    _monthController = FixedExtentScrollController(initialItem: monthIndex);
    super.initState();
  }
  @override
  void dispose() {
    _yearController.dispose();
    _monthController.dispose();
    super.dispose();
  }
  @override
  Widget build(BuildContext context) {
    return ClipRRect(
      borderRadius: const BorderRadius.only(topLeft: Radius.circular(5),topRight: Radius.circular(5)),
      child: Container(
        color: Colors.transparent,
        height: hsp(300),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.end,
          children: [
            Container(
              height: hsp(50),
              padding: const EdgeInsets.only(left: 20,right: 20),
              decoration: const BoxDecoration(
                color: Colors.white,
                border: Border(bottom: BorderSide(width: 0.5,color: Color(0xFFEEEEEE))),
              ),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  GestureDetector(
                    child: const Text('取消',style: TextStyle(fontSize: 15,color: Color(0xFF999999)),),
                    onTap: (){
                      Navigator.pop(context);
                    },
                  ),
                  Text('选择年月',style: TextStyle(fontSize: zsp(16),color: const Color(0xFF323233)),),
                  GestureDetector(
                    child: const Text('确定',style: TextStyle(fontSize: 15,color: Color(0xFF108DE9)),),
                    onTap: (){
                      Map value = {'title':'$_yearStr年$_monthStr月','value':'$_yearStr年$_monthStr月'};
                      widget.valueSetter(value);
                      Navigator.pop(context);
                    },
                  )
                ],
              ),
            ),
            Container(
              height: hsp(250),
              padding: const EdgeInsets.all(10),
              decoration: const BoxDecoration(
                color: Colors.white,
              ),
              child: LayoutBuilder(
                builder: (context,conSize){
                  return Row(
                    children: [
                      SizedBox(
                        width: conSize.maxWidth/2,
                        child: CupertinoPicker(
                          onSelectedItemChanged: (int value) {
                            _yearStr = _yearArray[value]['value'];
                          },
                          itemExtent: hsp(50),
                          scrollController: _yearController,
                          children: _yearArray.map((e) => Container(
                            alignment: Alignment.center,
                            child: Text(e['title']),
                          )).toList(),
                        ),
                      ),
                      SizedBox(
                        width: conSize.maxWidth/2,
                        child: CupertinoPicker(
                          onSelectedItemChanged: (int value) {
                            _monthStr = '${_monthArray[value]['value']}'.padLeft(2,'0');
                          },
                          itemExtent: hsp(50),
                          scrollController: _monthController,
                          children: _monthArray.map((e) => Container(
                            alignment: Alignment.center,
                            child: Text(e['title']),
                          )).toList(),
                        ),
                      ),
                    ],
                  );
                },
              ),
            ),
          ],
        ),
      ),
    );
  }
}
