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 HospitalizationHistoryForm extends StatefulWidget {
  const HospitalizationHistoryForm({
    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<HospitalizationHistoryForm> {
  Admission admission = Admission();

  @override
  void initState() {
    if (widget.admissionJson != null) {
      admission = Admission.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: admission.admissionDate,
              onTap: () async {
                final result = await _showDateDialog(
                  title: '入院日期',
                  initialValue: admission.admissionDate,
                );
                if (result != null) {
                  admission.admissionDate =
                      DateFormat("yyyy-MM-dd").format(result);
                }

                setState(() {});
              },
            ),
            _buildFormCell(
              label: '出院日期',
              content: admission.dischargeDate,
              onTap: () async {
                final result = await _showDateDialog(
                  title: '出院日期',
                  initialValue: admission.dischargeDate,
                );
                if (result != null) {
                  admission.dischargeDate =
                      DateFormat("yyyy-MM-dd").format(result);
                }
                setState(() {});
              },
            ),
            _buildFormCell(
              label: '原因',
              content: admission.reason,
              onTap: () async {
                final result = await _showInputDialog(
                  title: '原因',
                  initialValue: admission.reason ?? '',
                );

                admission.reason = result;
                setState(() {});
              },
            ),
            _buildFormCell(
              label: '医疗机构名称',
              content: admission.nameOfMedicalInstitution,
              onTap: () async {
                final result = await _showInputDialog(
                  title: '医疗机构名称',
                  initialValue: admission.nameOfMedicalInstitution ?? '',
                );

                admission.nameOfMedicalInstitution = result;
                setState(() {});
              },
            ),
            _buildFormCell(
              label: '病案号',
              content: admission.patientNumber,
              onTap: () async {
                final result = await _showInputDialog(
                  title: '病案号',
                  initialValue: admission.patientNumber ?? '',
                );

                admission.patientNumber = result;
                setState(() {});
              },
            ),
          ],
        ),
      ),
      onConfirm: () {
        if (widget.admissionJson != null) {
          widget.fromResult.call(
            EditTableValue(
              id: widget.currentId,
              value: admission.toJson(),
            ),
          );
        } else {
          admission.id = widget.currentId.toString();
          widget.fromResult.call(admission.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,
    );
  }
}
