import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:intl/intl.dart';
import 'package:vitalapp/components/alert_dialog.dart';
import 'package:vitalapp/components/cell.dart';
import 'package:vitalapp/components/dialog_date.dart';
import 'package:vitalapp/components/dialog_input.dart';

class VProfileTimeInputModel {
  VProfileTimeInputModel({this.name, this.time});
  late String? name;
  late DateTime? time;

  factory VProfileTimeInputModel.fromJson(Map<String, dynamic> jsonMap) {
    return VProfileTimeInputModel(
      name: jsonMap["name"],
      time: DateTime.parse(jsonMap["time"]),
    );
  }

  Map<String, dynamic> toJson() {
    Map<String, dynamic> jsonMap = {
      "name": name,
      "time": time != null ? DateFormat('yyyy-MM-dd').format(time!) : "",
    };
    return jsonMap;
  }
}

class VDialogProfileTimeInput extends StatefulWidget {
  const VDialogProfileTimeInput({
    Key? key,
    this.title,
    this.initialValue,
  }) : super(key: key);

  final String? title;
  final VProfileTimeInputModel? initialValue;

  @override
  // ignore: library_private_types_in_public_api
  _VDialogProfileTimeInputState createState() =>
      _VDialogProfileTimeInputState();

  Future<VProfileTimeInputModel?> show<VProfileTimeInputModel>() =>
      VAlertDialog.showDialog<VProfileTimeInputModel>(this);
}

class _VDialogProfileTimeInputState extends State<VDialogProfileTimeInput> {
  late VProfileTimeInputModel initialValue;

  @override
  void initState() {
    super.initState();
    initialValue = widget.initialValue ?? VProfileTimeInputModel();
  }

  @override
  Widget build(BuildContext context) {
    return VAlertDialog(
      title: widget.title,
      width: 440,
      contentPadding: const EdgeInsets.symmetric(vertical: 12, horizontal: 24),
      content: _buildContent(context),
      showCancel: true,
      onConfirm: () {
        Get.back(result: initialValue);
      },
    );
  }

  Widget _buildContent(BuildContext context) {
    final children = <Widget>[];
    children.add(_buildTimeWidget());
    return SingleChildScrollView(
      child: Column(
        mainAxisSize: MainAxisSize.min,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: children,
      ),
    );
  }

  Widget _buildTimeWidget() {
    return VListFormCellGroup(
      children: [
        VListFormCell(
          label: '名称',
          content: initialValue.name,
          onTap: () async {
            var result = await const VDialogInput().show();
            if (result != null) {
              setState(() {
                initialValue.name = result;
              });
            }
          },
        ),
        VListFormCell(
          label: '时间',
          content: initialValue.time != null
              ? DateFormat('yyyy-MM-dd').format(initialValue.time!)
              : '',
          onTap: () async {
            var result = await const VDialogDate().show();
            if (result != null) {
              setState(() {
                initialValue.time = result;
              });
            }
          },
        )
      ],
    );
  }
}
