import 'package:hmg_patient_app/core/viewModels/project_view_model.dart';
import 'package:hmg_patient_app/models/InPatientServices/get_inpatient_advance_history_response_model.dart';
import 'package:hmg_patient_app/services/clinic_services/get_clinic_service.dart';
import 'package:hmg_patient_app/theme/colors.dart';
import 'package:hmg_patient_app/uitl/date_uitl.dart';
import 'package:hmg_patient_app/uitl/gif_loader_dialog_utils.dart';
import 'package:hmg_patient_app/uitl/translations_delegate_base.dart';
import 'package:hmg_patient_app/uitl/utils_new.dart';
import 'package:hmg_patient_app/widgets/my_rich_text.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class InPatientPaidAdvancePayment extends StatefulWidget {
  const InPatientPaidAdvancePayment({Key? key}) : super(key: key);

  @override
  State<InPatientPaidAdvancePayment> createState() => _InPatientPaidAdvancePaymentState();
}

class _InPatientPaidAdvancePaymentState extends State<InPatientPaidAdvancePayment> {
  late ProjectViewModel projectViewModel;
  InPatientAdvanceHistoryResponseModel inPatientAdvanceHistoryResponseModel = InPatientAdvanceHistoryResponseModel();

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((_) {
      getInPatientAdvancePaymentHistory();
    });
  }

  @override
  Widget build(BuildContext context) {
    projectViewModel = Provider.of(context);
    return Padding(
      padding: const EdgeInsets.all(16.0),
      child: inPatientAdvanceHistoryResponseModel != null && inPatientAdvanceHistoryResponseModel.responseInpatientAdvanceInfo != null
          ? ListView.separated(
              itemCount: inPatientAdvanceHistoryResponseModel.responseInpatientAdvanceInfo!.length,
              itemBuilder: (BuildContext context, int index) {
                return Container(
                  decoration: cardRadius(12),
                  padding: EdgeInsets.all(12),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      MyRichText(
                          TranslationBase.of(context).admissionNo + ": ", inPatientAdvanceHistoryResponseModel.responseInpatientAdvanceInfo![index].admissionNo.toString(), projectViewModel.isArabic),
                      MyRichText(TranslationBase.of(context).admissionReqNo + ": ", inPatientAdvanceHistoryResponseModel.responseInpatientAdvanceInfo![index].admissionReqNo.toString(),
                          projectViewModel.isArabic),
                      MyRichText(
                          TranslationBase.of(context).date + ": ",
                          DateUtil.formatDateToDate(DateUtil.convertStringToDate(inPatientAdvanceHistoryResponseModel.responseInpatientAdvanceInfo![index].createdOn), false),
                          projectViewModel.isArabic),
                      MyRichText(
                          TranslationBase.of(context).amount_ + ": ", inPatientAdvanceHistoryResponseModel.responseInpatientAdvanceInfo![index].requestedAmount.toString(), projectViewModel.isArabic),
                      MyRichText(TranslationBase.of(context).status + ": ", getStatus(inPatientAdvanceHistoryResponseModel.responseInpatientAdvanceInfo![index]), projectViewModel.isArabic),
                    ],
                  ),
                );
              },
              separatorBuilder: (BuildContext context, int index) {
                return SizedBox(
                  height: 12.0,
                );
              },
            )
          : getNoDataWidget(context),
    );
  }

  String getStatus(ResponseInpatientAdvanceInfo responseInpatientAdvanceInfo) {
    switch (responseInpatientAdvanceInfo.status) {
      case 1:
        return "Hold";
        break;
      case 2:
        return "Active";
        break;
      case 4:
        return "Cancelled";
        break;
      default:
        return "";
    }
  }

  void getInPatientAdvancePaymentHistory() {
    ClinicListService service = new ClinicListService();
    GifLoaderDialogUtils.showMyDialog(context);
    service
        .getInPatientAdvancePaymentHistory(
            projectViewModel.inPatientProjectID,
            projectViewModel.isPatientAdmitted ? projectViewModel.getAdmissionInfoResponseModel.admissionNo! : 0,
            projectViewModel.isPatientAdmitted ? projectViewModel.getAdmissionInfoResponseModel.admissionRequestNo! : projectViewModel.getAdmissionRequestInfoResponseModel.admissionRequestNo!,
            context)
        .then((res) {
      GifLoaderDialogUtils.hideDialog(context);
      inPatientAdvanceHistoryResponseModel = InPatientAdvanceHistoryResponseModel.fromJson(res["responseInpatient"]);
      setState(() {});
    }).catchError((err) {
      GifLoaderDialogUtils.hideDialog(context);
      print(err);
    });
  }

}
