/*
 * @Author: XianKaiQun
 * @LastEditors  : WuWei
 * @Date: 2020-10-26 11:07:26
 * @LastEditTime : 2023-05-25 15:50:45
 */
import 'package:flutter/material.dart';
import 'package:wisdom_cli/wisdom_cli.dart';

///
/* 日期选择组件
 * * @ [title] 左边标题名称
 * * @ [isRequired] 是否为必填项
 * * @ [hintText] 提示语
 * * @ [onChanged] 选择函数 void Function(DateTime datetime)
 * * * //
 * * @ [initialDateTime] 左边标题名称 DateTime
 */
class WFDatePicker extends StatelessWidget {
  const WFDatePicker({
    Key? key,
    this.title,
    this.isRequired = false,
    this.hintText,
    this.onChanged,
    this.initialDateTime,
    this.firstDate,
    this.lastDate,
  }) : super(key: key);
  final Widget? title;
  final firstDate;
  final lastDate;

  ///是否必须
  final bool isRequired;
  final String? hintText;
  final dynamic initialDateTime;
  final void Function(dynamic datetime)? onChanged;

  @override
  Widget build(BuildContext context) {
    final style = WTheme.of(context);
    final colorScheme = style.colorScheme;
    String? _initialDate = initialDateTime;
    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 {
                  final timeArr =
                      _initialDate != null ? _initialDate!.split('-') : [];
                  final res = await WPickerUtil.date(
                    context,
                    title: title,
                    firstDate:
                        firstDate != null ? DateTime.parse(firstDate) : null,
                    lastDate:
                        lastDate != null ? DateTime.parse(lastDate) : null,
                    initialDate: _initialDate != null
                        ? DateTime(int.parse(timeArr[0]), int.parse(timeArr[1]),
                            int.parse(timeArr[2]))
                        : null,
                  );

                  if (res != null) {
                    _initialDate = res.toFormat(WisDateTimeFormat.date);
                    setState(() {});
                    if (onChanged != null) onChanged!(_initialDate);
                  }
                },
                style: TextStyle(
                  fontSize: 15.pt,
                  fontWeight: FontWeight.w600,
                  color: colorScheme.mainTitle,
                ),
                decoration: InputDecoration(
                  fillColor: Colors.transparent,
                  filled: true,
                  hintText: hintText,
                  labelStyle: TextStyle(
                    color: colorScheme.placeholder2,
                    fontSize: 15.pt,
                  ),
                ),
              ).asExpanded(),
              _initialDate != null
                  ? WFClose(
                      onTap: () {
                        _initialDate = null;
                        setState(() {});
                        if (onChanged != null) onChanged!(null);
                      },
                    )
                  : Icon(
                      FaIcons.angle_right,
                      size: 18.pt,
                      color: colorScheme.placeholder,
                    )
            ],
          );
        },
      ),
    );
  }
}
