import 'package:hmg_patient_app/core/model/reports/admission_for_medical_report.dart';
import 'package:hmg_patient_app/core/viewModels/medical/reports_view_model.dart';
import 'package:hmg_patient_app/extensions/string_extensions.dart';
import 'package:hmg_patient_app/pages/base/base_view.dart';
import 'package:hmg_patient_app/pages/medical/reports/report_list_widget.dart';
import 'package:hmg_patient_app/uitl/translations_delegate_base.dart';
import 'package:hmg_patient_app/widgets/buttons/defaultButton.dart';
import 'package:hmg_patient_app/widgets/dialogs/ConfirmWithMessageDialog.dart';
import 'package:hmg_patient_app/widgets/others/app_scaffold_widget.dart';
import 'package:flutter/material.dart';

class InPatientMedicalReports extends StatefulWidget {
  final AdmissionMedicalReport admissionMedicalReport;

  InPatientMedicalReports({required this.admissionMedicalReport});

  @override
  State<InPatientMedicalReports> createState() => _InPatientMedicalReportsState();
}

class _InPatientMedicalReportsState extends State<InPatientMedicalReports> {
  int _currentPage = 0;

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return BaseView<ReportsViewModel>(
      onModelReady: (model) {
        model.getInPatientReports();
      },
      builder: (_, model, widget) => AppScaffold(
        isShowAppBar: true,
        appBarTitle: TranslationBase.of(context).newMedReport,
        title: TranslationBase.of(context).medReport,
        showNewAppBar: true,
        showNewAppBarTitle: true,
        backgroundColor: Color(0xffF7F7F7),
        body: Container(
          child: Column(
            children: [
              Padding(
                padding: EdgeInsets.all(21),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    myRadioButton(TranslationBase.of(context).requested, 0),
                    myRadioButton(TranslationBase.of(context).ready, 1),
                    myRadioButton(TranslationBase.of(context).cancelled, 2),
                  ],
                ),
              ),
              Expanded(
                child: IndexedStack(
                  index: _currentPage,
                  children: [
                    ReportListWidget(reportList: model.reportsInPatientOrderRequestList, emailAddress: model.user!.emailAddress!),
                    ReportListWidget(reportList: model.reportsInPatientOrderReadyList, emailAddress: model.user!.emailAddress!),
                    ReportListWidget(reportList: model.reportsInPatientOrderCanceledList, emailAddress: model.user!.emailAddress!),
                  ],
                ),
              ),
              Padding(
                padding: EdgeInsets.only(top: 16, bottom: 16, right: 21, left: 21),
                child: DefaultButton(
                  TranslationBase.of(context).requestMedicalReport.toLowerCase().capitalizeFirstofEach,
                  () {
                    confirmBox(model);
                  },
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }

  void confirmBox(ReportsViewModel reportsViewModel) {
    showDialog(
      context: context,
      builder: (cxt) => ConfirmWithMessageDialog(
        message: TranslationBase.of(context).confirmMsgReport,
        onTap: () => reportsViewModel.insertRequestForInPatientMedicalReport(widget.admissionMedicalReport.clinicID!, widget.admissionMedicalReport.doctorID!, widget.admissionMedicalReport.setupID!,
            widget.admissionMedicalReport.admissionNo!, widget.admissionMedicalReport.projectID!, TranslationBase.of(context).successSendReport),
      ),
    );
    return;
  }

  Widget myRadioButton(String _label, int _value) {
    return InkWell(
      onTap: () {
        setState(() {
          _currentPage = _value;
        });
      },
      child: Row(
        mainAxisSize: MainAxisSize.min,
        children: [
          SizedBox(
            width: 22,
            height: 22,
            child: Radio(
              value: _value,
              activeColor: _value == _currentPage ? Color(0xffD02127) : Color(0xffE8E8E8),
              groupValue: _currentPage,
              onChanged: (index) {
                setState(() {
                  _currentPage = index!;
                });
              },
            ),
          ),
          SizedBox(width: 10),
          Text(
            _label,
            style: TextStyle(
              fontSize: 14,
              fontWeight: FontWeight.w600,
              color: Color(0xff575757),
              letterSpacing: -0.56,
            ),
          ),
        ],
      ),
    );
  }
}
