import 'package:flutter/material.dart';
import 'package:get/get.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 MainMedicationStatusFrom extends StatefulWidget {
  const MainMedicationStatusFrom({
    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<MainMedicationStatusFrom> {
  MainMedication mainMedication = MainMedication();

  @override
  void initState() {
    if (widget.admissionJson != null) {
      mainMedication = MainMedication.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: mainMedication.medicineName,
              onTap: () async {
                final result = await _showInputDialog(
                  title: '药物名称',
                  initialValue: mainMedication.medicineName ?? '',
                );

                mainMedication.medicineName = result;
                setState(() {});
              },
            ),
            _buildFormCell(
              label: '用法',
              content: mainMedication.medicineUsage,
              onTap: () async {
                final result = await _showInputDialog(
                  title: '用法',
                  initialValue: mainMedication.medicineUsage ?? '',
                );

                mainMedication.medicineUsage = result;
                setState(() {});
              },
            ),
            _buildFormCell(
              label: '用量',
              content: mainMedication.medicineDosage,
              onTap: () async {
                final result = await _showInputDialog(
                  title: '用量',
                  initialValue: mainMedication.medicineDosage ?? '',
                );

                mainMedication.medicineDosage = result;
                setState(() {});
              },
            ),
            _buildFormCell(
              label: '用药时间',
              content: mainMedication.medicineTime,
              onTap: () async {
                final result = await _showInputDialog(
                  title: '用药时间',
                  initialValue: mainMedication.medicineTime ?? '',
                );

                mainMedication.medicineTime = result;
                setState(() {});
              },
            ),
            _buildFormCell(
              label: '服药依从性',
              content: mainMedication.medicineCompliance,
              onTap: () async {
                final result = await _showInputDialog(
                  title: '服药依从性',
                  initialValue: mainMedication.medicineCompliance ?? '',
                );

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