/*
 * @Author: XianKaiQun
 * @LastEditors: ChenYaJin
 * @Date: 2020-10-26 11:07:31
 * @LastEditTime: 2023-04-06 09:32:36
 */
import 'package:flutter/material.dart';
import 'package:wisdom_cli/wisdom_cli.dart';

class WFTimePicker extends StatelessWidget {
  const WFTimePicker({
    Key? key,
    this.title,
    this.isRequired = false,
    this.hintText,
    required this.onChanged,
    this.firstDate,
    this.lastDate,
    this.initialDate,
    this.type,
    this.minuteInterval,
  }) : super(key: key);
  final Widget? title;

  ///是否必须
  final bool isRequired;
  final String? hintText;
  final dynamic initialDate;

  final firstDate;
  final lastDate;
  final void Function(dynamic datetime) onChanged;
  final String? type;
  final int? minuteInterval;

  @override
  Widget build(BuildContext context) {
    final style = WTheme.of(context);
    final colorScheme = style.colorScheme;
    String? _initialDate = initialDate;
    String _type = type ?? 'time';
    return WisFormLayoutComponent(
      title: title,
      isRequired: isRequired,
      axis: Axis.horizontal,
      child: StatefulBuilder(
        builder: (BuildContext context, setState) {
          return Row(
            children: [
              TextField(
                controller: TextEditingController(
                  text: _initialDate ?? '',
                ),
                readOnly: true,
                onTap: () async {
                  if (_type == 'time') {
                    final res = await WPickerUtil.time(
                      context,
                      title: title,
                      minuteInterval: minuteInterval ?? 5,
                    );

                    if (res != null) {
                      _initialDate = res.toFormat('{hh}:{mm}');
                      setState(() {});
                      onChanged(_initialDate);
                    }
                  } else {
                    final res = await WPickerUtil.dateAndTime(
                      context,
                      title: title,
                      firstDate: firstDate,
                      lastDate: lastDate,
                      minuteInterval: minuteInterval ?? 5,
                    );

                    if (res != null) {
                      _initialDate = res.toFormat('{YYYY}-{MM}-{DD} {hh}:{mm}');
                      setState(() {});
                      onChanged(_initialDate);
                    }
                  }
                },
                style: TextStyle(
                  fontSize: 14.pt,
                  color: colorScheme.mainTitle,
                ),
                decoration: InputDecoration(
                  fillColor: Colors.transparent,
                  filled: true,
                  hintText: hintText,
                  hintStyle: TextStyle(
                    fontSize: 15.pt,
                    // color: colorScheme.placeholder2,
                    fontWeight: FontWeight.w400,
                  ),
                ),
              ).asExpanded(),
              _initialDate != null
                  ? WFClose(
                      onTap: () {
                        _initialDate = null;
                        setState(() {});
                        onChanged(null);
                      },
                    )
                  : Icon(
                      FaIcons.angle_right,
                      size: 18.pt,
                      color: colorScheme.placeholder,
                    )
            ],
          );
        },
      ),
    );
  }
}
