import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:sk_base_mobile/util/logger_util.dart';
import 'package:sk_base_mobile/util/screen_adaper_util.dart';

class ZtBaseDatePicker extends StatelessWidget {
  final Function? onDateTimeChanged;
  final yearController =
      FixedExtentScrollController(initialItem: DateTime.now().year - 2000);
  final monthController =
      FixedExtentScrollController(initialItem: DateTime.now().month - 1);
  final dayController =
      FixedExtentScrollController(initialItem: DateTime.now().day - 1);
  ZtBaseDatePicker({super.key, this.onDateTimeChanged});

  @override
  Widget build(BuildContext context) {
    return Container(
      height: ScreenAdaper.height(250),
      color: Colors.white,
      child: Column(
        children: [
          Row(
            children: [
              TextButton(
                  onPressed: () {
                    Get.back();
                  },
                  child: Text(
                    '取消',
                    style: TextStyle(fontSize: ScreenAdaper.sp(25)),
                  )),
              const Spacer(),
              TextButton(
                  onPressed: () {
                    Get.back();
                    final year = 2000 + yearController.selectedItem;
                    String month = '${monthController.selectedItem + 1}';
                    if (int.parse(month) < 10) {
                      month = '0$month';
                    }
                    final day = dayController.selectedItem + 1;
                    if (onDateTimeChanged != null) {
                      onDateTimeChanged!('$year-$month-$day');
                    }
                    LoggerUtil().info('$year-$month-$day');
                  },
                  child: Text(
                    '确定',
                    style: TextStyle(fontSize: ScreenAdaper.sp(25)),
                  ))
            ],
          ),
          Expanded(
              child: Row(
            children: <Widget>[
              Expanded(
                child: CupertinoPicker(
                  scrollController: yearController,
                  itemExtent: ScreenAdaper.height(60),
                  onSelectedItemChanged: (int index) {
                    // 处理用户选择的年份
                  },
                  children: List<Widget>.generate(DateTime.now().year - 1999,
                      (int index) {
                    return Center(child: Text('${2000 + index}年'));
                  }),
                ),
              ),
              Expanded(
                child: CupertinoPicker(
                  scrollController: monthController,
                  itemExtent: ScreenAdaper.height(60),
                  onSelectedItemChanged: (int index) {
                    // 处理用户选择的月份
                  },
                  children: List<Widget>.generate(12, (int index) {
                    return Center(child: Text('${index + 1}月'));
                  }),
                ),
              ),
              Expanded(
                child: CupertinoPicker(
                  scrollController: dayController,
                  itemExtent: ScreenAdaper.height(60),
                  onSelectedItemChanged: (int index) {
                    // 处理用户选择的日期
                  },
                  children: List<Widget>.generate(31, (int index) {
                    return Center(child: Text('${index + 1}日'));
                  }),
                ),
              ),
            ],
          ))
        ],
      ),
    );
  }
}
