import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:intl/intl.dart';
import 'package:vitalapp/architecture/types/index.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';
import 'package:vitalapp/pages/check/models/form.dart';

/// 住院史
class InoculateHistoryFrom extends StatefulWidget {
  const InoculateHistoryFrom({
    Key? key,
    required this.fromResult,
    required this.currentId,
    this.admissionJson,
  }) : super(key: key);

  final ValueCallback fromResult;
  final int currentId;
  final Map<String, dynamic>? admissionJson;

  @override
  _HospitalizationHistoryFormState createState() =>
      _HospitalizationHistoryFormState();
}

class _HospitalizationHistoryFormState extends State<InoculateHistoryFrom> {
  VaccinationHistory vaccinationHistory = VaccinationHistory();

  @override
  void initState() {
    if (widget.admissionJson != null) {
      vaccinationHistory = VaccinationHistory.fromJson(widget.admissionJson!);
    }
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return VAlertDialog(
      title: "预防接种史填写",
      width: 600,
      content: Container(
        height: 350,
        padding: const EdgeInsets.symmetric(horizontal: 24),
        alignment: Alignment.center,
        child: VListFormCellGroup(
          children: [
            _buildFormCell(
              label: '疫苗名称',
              content: vaccinationHistory.vaccineName,
              onTap: () async {
                final result = await _showInputDialog(
                  title: '疫苗名称',
                  initialValue: vaccinationHistory.vaccineName ?? '',
                );

                vaccinationHistory.vaccineName = result;

                setState(() {});
              },
            ),
            _buildFormCell(
              label: '接种日期',
              content: vaccinationHistory.vaccinationDate,
              onTap: () async {
                final result = await _showDateDialog(
                  title: '接种日期',
                  initialValue: vaccinationHistory.vaccinationDate,
                );
                if (result != null) {
                  vaccinationHistory.vaccinationDate =
                      DateFormat("yyyy-MM-dd").format(result);
                }
                setState(() {});
              },
            ),
            _buildFormCell(
              label: '接种机构',
              content: vaccinationHistory.vaccinationFacility,
              onTap: () async {
                final result = await _showInputDialog(
                  title: '接种机构',
                  initialValue: vaccinationHistory.vaccinationFacility ?? '',
                );

                vaccinationHistory.vaccinationFacility = result;
                setState(() {});
              },
            ),
          ],
        ),
      ),
      onConfirm: () {
        if (widget.admissionJson != null) {
          widget.fromResult.call(
            EditTableValue(
              id: widget.currentId,
              value: vaccinationHistory.toJson(),
            ),
          );
        } else {
          vaccinationHistory.id = widget.currentId.toString();
          widget.fromResult.call(vaccinationHistory.toJson());
        }

        Get.back();
      },
    );
  }

  Future<DateTime?> _showDateDialog({
    required String title,
    String? initialValue,
  }) async {
    final result = await VDialogDate(
      title: title,
      initialValue:
          initialValue == null ? DateTime.now() : DateTime.parse(initialValue),
    ).show();
    return result;
  }

  Future<String?> _showInputDialog({
    required String title,
    required String initialValue,
  }) async {
    final result = await VDialogInput(
      title: title,
      initialValue: initialValue,
    ).show();
    return result;
  }

  Widget _buildFormCell({
    required String label,
    String? content,
    required VoidCallback onTap,
  }) {
    return VListFormCell(
      label: label,
      content: content ?? '',
      onTap: onTap,
    );
  }
}
