/*
 * @Author       : WuWei
 * @LastEditors  : WuWei
 * @Date         : 2021-03-31 20:25:14
 * @LastEditTime : 2023-05-25 15:50:29
 * @FilePath     : /wisdom-huiguanjia-app/wisdom_cli/lib/src/form_widget/components/date_range_picker.dart
 * @Description  : Do not edit
 */

import 'package:flutter/material.dart';
import 'package:wisdom_cli/wisdom_cli.dart';
import 'package:intl/intl.dart';

///
/* 日期范围选择组件
 * 
 *  * 函数1 [createNoteBook]在数据库中创建笔记本
 *   * 参数1 [noteBookName]用户提供的笔记本名
 * 
 * [示例代码]
 * ```dart
 *  //调用createNoteBook方法以创建新的笔记本
 *    await NotesTool.createNoteBook(noteBookName);
 *  
 ```
 *  * 参数2 
 *  */
class WFDateRangePicker extends StatelessWidget {
  const WFDateRangePicker({
    Key? key,
    this.title,
    this.isRequired = false,
    this.hintText,
    this.onChanged,
    this.initialDateRange,
    this.firstDate,
    this.lastDate,
  }) : super(key: key);
  final Widget? title;

  ///是否必须
  final bool isRequired;

  /// 提示语
  final String? hintText;

  /// 初始值
  final List? initialDateRange;
  final void Function(dynamic value)? onChanged;
  final firstDate;
  final lastDate;
  @override
  Widget build(BuildContext context) {
    final style = WTheme.of(context);
    final colorScheme = style.colorScheme;
    List? _initialDateRange = initialDateRange ?? [];
    return WisFormLayoutComponent(
      title: title,
      isRequired: isRequired,
      axis: Axis.horizontal,
      child: StatefulBuilder(
        builder: (BuildContext context, setState) {
          return Row(
            children: [
              Wisdom.row(
                onTap: () async {
                  var res = await showDateRangePicker(
                    context: context,
                    // initialDateRange: _initialDateRange,
                    firstDate: DateTime(firstDate ?? 2010), //DateTime(2010)
                    lastDate: DateTime(lastDate ?? 2025),
                  );
                  if (res != null) {
                    final DateFormat outputFormat = DateFormat("yyyy-MM-dd");
                    List arr = [];
                    arr.add(
                      outputFormat.format(
                        res.start,
                      ),
                    );
                    arr.add(
                      outputFormat.format(
                        res.end,
                      ),
                    );
                    if (onChanged != null) onChanged!(arr);
                  }
                },
                children: [
                  _initialDateRange != null && _initialDateRange!.length > 0
                      ? Text(
                          _initialDateRange!.join('至'),
                          style: TextStyle(
                            color: colorScheme.mainTitle,
                            fontWeight: FontWeight.w600,
                          ),
                        )
                      : Text(
                          hintText ?? '',
                          style: TextStyle(
                            color: colorScheme.placeholder2,
                            fontSize: 15.pt,
                          ),
                        )
                ],
              ).asExpanded(),
              _initialDateRange != null && _initialDateRange!.length != 0
                  ? WFClose(
                      onTap: () {
                        _initialDateRange = null;
                        setState(() {});
                        if (onChanged != null) onChanged!(null);
                      },
                    )
                  : Icon(
                      FaIcons.angle_right,
                      size: 18.pt,
                      color: colorScheme.placeholder,
                    )
            ],
          );
        },
      ),
    );
  }
}
