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 HomecareBedHistoryFrom extends StatefulWidget {
  const HomecareBedHistoryFrom({
    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
  _HomecareBedHistoryFromState createState() => _HomecareBedHistoryFromState();
}

class _HomecareBedHistoryFromState extends State<HomecareBedHistoryFrom> {
  BedConstruction bedConstruction = BedConstruction();

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

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

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

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

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