import 'package:fis_ui/base_define/page.dart';
import 'package:fis_ui/index.dart';
import 'package:flutter/material.dart';

/// 实时诊断筛选
class FRealTimeConsulationFilterTime extends Dialog implements FPage {
  final ValueCallback timeCallback;
  final List? position;
  final String? globalKey;
  final DateTime? initialDate;
  @override
  final String pageName;

  FRealTimeConsulationFilterTime({
    Key? key,

    /// 数据回调
    required this.timeCallback,

    /// 弹框出现位置
    this.position,

    /// 初始日期
    this.initialDate,

    /// 当前key
    this.globalKey,
    this.pageName = 'ProductListDialog',
  }) : super(key: key);
  @override
  FWidget build(BuildContext context) {
    return FMaterial(
      type: MaterialType.transparency,
      child: _buildProductListView(context, globalKey),
    );
  }

  FWidget _buildProductListView(context, globalKey) => FSafeArea(
        child: FGestureDetector(
          name: "back",
          businessParent: this,
          child: FStack(
            children: [
              FContainer(
                width: MediaQuery.of(context).size.width,
                height: MediaQuery.of(context).size.height,
                color: Colors.transparent, // 它的颜色为透明色
              ),
              FPositioned(
                top: position![1],
                left: position![0],
                child: FContainer(
                  constraints: BoxConstraints(
                    maxWidth: 310,
                    maxHeight: 400,
                  ),
                  decoration: BoxDecoration(
                    color: Colors.white,
                    boxShadow: [
                      BoxShadow(
                        blurRadius: 5,
                        color: Color(0xffeeeeee),
                      )
                    ],
                    borderRadius: BorderRadius.circular(5),
                  ),
                  child: _calendarDatePicker(
                    DatePickerMode.day,
                    context,
                    globalKey,
                    initialDate,
                  ),
                ),
              ),
            ],
          ),
          onTap: () {
            Navigator.of(context).pop();
          },
        ),
      );
  FCalendarDatePicker _calendarDatePicker(
    DatePickerMode mode,
    BuildContext context,
    String buildKey,
    DateTime? initialDate,
  ) {
    return FCalendarDatePicker(
      initialDate: initialDate ?? DateTime.now(),
      firstDate: DateTime(1996, 10, 18), // 开始日期
      lastDate: DateTime(3020, 10, 18), // 结束日期
      initialCalendarMode: mode, // 日期选择样式
      // 选中日期改变回调函数
      onDateChanged: (dateTime) {
        initialDate = dateTime;
        Navigator.of(context).pop();
        timeCallback.call(dateTime);
      },
      // 月份改变回调函数
      onDisplayedMonthChanged: (dateTime) {},
      // 筛选日期可不可点回调函数
      selectableDayPredicate: (dayTime) {
        if (dayTime == DateTime(2020, 10, 15) ||
            dayTime == DateTime(2020, 10, 16) ||
            dayTime == DateTime(2020, 10, 17)) {
          return false;
        }
        return true;
      },
    );
  }
}
