import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:get/get_core/src/get_main.dart';
import 'package:sk_base_mobile/util/date.util.dart';
import 'package:sk_base_mobile/util/screen_adaper_util.dart';

class ZtDatePicker extends StatelessWidget {
  final TextEditingController textController;
  final VoidCallback? onClear;
  final Function? onDateSelected;
  final bool isRequired;
  final String labelText;

  const ZtDatePicker({
    super.key,
    this.onClear,
    this.onDateSelected,
    this.labelText = '日期',
    this.isRequired = false,
    required this.textController,
  });

  @override
  Widget build(BuildContext context) {
    return TextFormField(
      controller: textController,
      onTapOutside: (event) {
        FocusScope.of(context).unfocus();
      },
      decoration: InputDecoration(
          floatingLabelBehavior: FloatingLabelBehavior.always,
          prefixIcon: Icon(
            Icons.date_range_outlined,
            size: ScreenAdaper.sp(40),
          ),
          suffixIcon: textController.text.isNotEmpty
              ? IconButton(
                  icon: const Icon(Icons.clear),
                  // 当点击这个按钮时，清除TextFormField的值
                  onPressed: () {
                    textController.clear();
                    if (onClear != null) {
                      onClear!();
                    }
                  },
                )
              : const SizedBox(),
          hintText: '请选择',
          label: Row(
              crossAxisAlignment: CrossAxisAlignment.center,
              mainAxisSize: MainAxisSize.min,
              children: [
                if (isRequired)
                  Text(
                    "*",
                    style: TextStyle(
                        color: Colors.red, fontSize: ScreenAdaper.sp(30)),
                  ),
                Text(
                  labelText,
                  style: TextStyle(fontSize: ScreenAdaper.sp(30)),
                ),
              ])),
      keyboardType: TextInputType.none,
      onTap: () async {
        final picker = await showDatePicker(
            context: Get.overlayContext!,
            initialDate: DateTime.now(),
            helpText: '选择的日期',
            confirmText: '确定',
            cancelText: '取消',
            firstDate: DateTime(2000, 1, 1),
            lastDate: DateTime.now().add(const Duration(days: 7)));

        if (onDateSelected != null) {
          onDateSelected!(picker);
        }
      },
    );
  }
}
