import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flyinsono/lab/components/lab_dialog.dart';
import 'package:get/get.dart';

import 'alert_dialog.dart';
import 'custom_date_picker.dart';

class _VDialogDateValueCache {
  DateTime value;
  _VDialogDateValueCache(this.value);
}

/// 弹窗日期选择器
class VDialogDate extends StatelessWidget {
  /// 标题
  final String? title;

  /// 初始值
  final DateTime? initialValue;

  /// 最小时间
  final DateTime? minimumDate;

  /// 最大值
  final DateTime? maxValue;

  const VDialogDate({
    super.key,
    this.title,
    this.initialValue,
    this.minimumDate,
    this.maxValue,
  });

  Future<DateTime?> show<DateTime>() => LabDialog.showDialog<DateTime>(this);

  @override
  Widget build(BuildContext context) {
    DateTime initialDateTime = initialValue ?? DateTime.now();
    final valueCache = _VDialogDateValueCache(
      DateTime(
        initialDateTime.year,
        initialDateTime.month,
        initialDateTime.day,
      ),
    );

    return LabAlertDialog(
      title: title ?? "",
      content: _buildContent(context, valueCache),
      // showCancel: true,
      onConfirm: () {
        Get.back(result: valueCache.value);
      },
    );
  }

  Widget _buildContent(BuildContext context, _VDialogDateValueCache cache) {
    // 不保留时间部分
    return Container(
      margin: const EdgeInsets.all(10),
      height: 216,
      // width: 460,
      child: Stack(
        children: [
          Positioned.fill(
            child: VCustomCupertinoDatePicker(
              initialDateTime: cache.value,
              minimumYear: 1900,
              maximumYear: 2100,
              minimumDate: minimumDate,
              maximumDate: maxValue,
              backgroundColor: CupertinoColors.white,
              onDateTimeChanged: (date) {
                cache.value = date;
              },
            ),
          ),
          Positioned(
            top: 72,
            left: 0,
            right: 0,
            child: Divider(
              thickness: 1,
              color: Colors.black.withOpacity(0.2),
              indent: 12,
              endIndent: 12,
            ),
          ),
          Positioned(
            top: 72 + 58,
            left: 0,
            right: 0,
            child: Divider(
              thickness: 1,
              color: Colors.black.withOpacity(0.2),
              indent: 12,
              endIndent: 12,
            ),
          ),
        ],
      ),
    );
  }
}
