/*
 * @Author: XianKaiQun
 * @Date: 2020-04-23 09:40:46
 * @LastEditors  : WuWei
 * @LastEditTime : 2021-04-08 10:37:14
 * @Descripttion: 
 */

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

///配合[showModalBottomSheet]使用的容器组件
///[title]  标题，一般传入Text()
///[body]   主体内容
///
///注意：[showModalBottomSheet]中的[backgroundColor]
///请给[Colors.transparent],这样才能达到圆角效果
class WisBottomModal extends StatelessWidget {
  ///配合[showModalBottomSheet]使用的容器组件
  ///[title]  标题，一般传入Text()
  ///[body]   主体内容
  ///
  ///注意：[showModalBottomSheet]中的[backgroundColor]
  ///请给[Colors.transparent],这样才能达到圆角效果
  ///```dart
  ///showModalBottomSheet(
  ///   context: context,
  ///   backgroundColor: Colors.transparent,
  ///   builder: (_) {
  ///     return WisBottomModal(
  ///       title: Text('收费二维码'),
  ///       body: body: WisView.column(
  ///         padding: EdgeInsets.all(15.pt),
  ///          children: <Widget>[
  ///             WisText('一层一栋一单元', 15.pt),
  ///             WisText('欠费总额', 15.pt, height: 2),
  ///             Expanded(
  ///             child: Padding(
  ///             padding: EdgeInsets.all(15.pt),
  ///             child: WImage.network(
  ///              'https://bkimg.cdn.bcebos.com/pic/2934349b033b5bb571dc8c5133d3d539b600bc12?x-bce-process=image/watermark,g_7,image_d2F0ZXIvYmFpa2U4MA==,xp_5,yp_5',
  ///              ),
  ///             ),
  ///            ),
  ///           WisText('扫描二维码进行缴费', 15.pt, height: 2.5),
  ///         ],
  ///       ),
  ///     );
  ///   },
  /// );
  /// ```
  const WisBottomModal({Key? key, this.title, this.body}) : super(key: key);

  final Widget? title;
  final Widget? body;
  static Future<T?> show<T>({
    required BuildContext context,
    Widget? title,
    Widget? body,
  }) async {
    return await showModalBottomSheet<T>(
      context: context,
      backgroundColor: Colors.transparent,
      isScrollControlled: true,
      builder: (_) => WisBottomModal(
        title: title,
        body: body,
      ),
    );
  }

  static Future<DateTime> showDatePicker({
    required BuildContext context,
    Widget? title,
    DateTime? initialDate,
    DateTime? firstDate,
    DateTime? lastDate,
    DatePickerMode initialCalendarMode = DatePickerMode.day,
    ValueChanged<DateTime>? onDisplayedMonthChanged,
    SelectableDayPredicate? selectableDayPredicate,
    bool hasBottom = true,
  }) async {
    Completer completer = Completer<DateTime>();
    DateTime? current = initialDate;
    _complete(DateTime? e) {
      completer.complete(e);
      Navigator.pop(context);
    }

    show(
      context: context,
      title: title,
      body: Column(
        mainAxisSize: MainAxisSize.min,
        children: <Widget>[
          CalendarDatePicker(
            initialCalendarMode: initialCalendarMode,
            initialDate: initialDate ?? DateTime.now(),
            firstDate: firstDate ?? DateTime(2015),
            lastDate: lastDate ?? DateTime(2099),
            onDateChanged: hasBottom
                ? (e) => current = e
                : (e) {
                    _complete(e);
                  },
            selectableDayPredicate: selectableDayPredicate,
            onDisplayedMonthChanged: onDisplayedMonthChanged,
          ),
          if (hasBottom)
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              children: <Widget>[
                WisButton(
                  onPressed: () => _complete(DateTime(0)),
                  color: Colors.black54,
                  constraints: BoxConstraints(minWidth: 120, minHeight: 40),
                  textColor: Colors.black54,
                  plain: true,
                  child: Text('不限时间'),
                ),
                WisButton(
                  onPressed: () => _complete(current),
                  constraints: BoxConstraints(minWidth: 120, minHeight: 40),
                  color: Theme.of(context).primaryColor,
                  child: Text('确定'),
                ),
              ],
            ),
        ],
      ),
    );
    return completer.future as FutureOr<DateTime>;
  }

  @override
  Widget build(BuildContext context) {
    return Wisdom(
      borderRadius: BorderRadius.only(
        topLeft: Radius.circular(10),
        topRight: Radius.circular(10),
      ),
      color: Colors.white,
      minHeight: 350,
      child: SafeArea(
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: <Widget>[
            Wisdom.row(
              height: 53.pt,
              padding: EdgeInsets.fromLTRB(25.pt, 10.pt, 10.pt, 0),
              crossAxisAlignment: CrossAxisAlignment.center,
              children: <Widget>[
                Expanded(
                  child: DefaultTextStyle(
                    style: TextStyle(
                        fontSize: 19.pt,
                        fontWeight: FontWeight.w600,
                        color: Colors.black87),
                    child: title ?? Text(''),
                  ),
                ),
                CloseButton(),
              ],
            ),
            if (body != null) body!.asFlexible(),
          ],
        ),
      ),
    );
  }
}
