import 'package:hmg_patient_app/core/viewModels/project_view_model.dart';
import 'package:hmg_patient_app/models/Appointments/DoctorListResponse.dart';
import 'package:hmg_patient_app/pages/BookAppointment/widgets/DoctorView.dart';
import 'package:hmg_patient_app/services/appointment_services/GetDoctorsList.dart';
import 'package:hmg_patient_app/uitl/app_toast.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/widgets/others/app_scaffold_widget.dart';
import 'package:hmg_patient_app/widgets/progress_indicator/app_circular_progress_Indeicator.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class VisitTicket extends StatefulWidget {
  List<DoctorList> appoList = [];

  @override
  _VisitTicketState createState() => _VisitTicketState();
}

class _VisitTicketState extends State<VisitTicket> {
  bool isLoading = false;
  ProjectViewModel? projectViewModel;

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

  @override
  Widget build(BuildContext context) {
    projectViewModel = Provider.of<ProjectViewModel>(context);
    return AppScaffold(
      appBarTitle: TranslationBase.of(context).todoList,
      body: isLoading == false
          ? SingleChildScrollView(
              child: ListView.builder(
                scrollDirection: Axis.vertical,
                shrinkWrap: true,
                physics: ScrollPhysics(),
                padding: EdgeInsets.all(0.0),
                itemCount: widget.appoList.length,
                itemBuilder: (context, index) {
                  return DoctorView(
                    doctor: widget.appoList[index],
                  );
                },
              ),
            )
          : AppCircularProgressIndicator(),
    );
  }

  getPatientAppointmentCurfewHistory(BuildContext context) {
    int languageID = projectViewModel!.isArabic ? 1 : 2;
    DoctorsListService service = new DoctorsListService();
    GifLoaderDialogUtils.showMyDialog(context);
    service.getPatientAppointmentHistory(true, languageID, context).then((res) {
      GifLoaderDialogUtils.hideDialog(context);
      if (res['MessageStatus'] == 1) {
        setState(() {
          if (res['AppoimentAllHistoryResultList'].length != 0) {
            widget.appoList.clear();
            res['AppoimentAllHistoryResultList'].forEach((v) {
              widget.appoList.add(new DoctorList.fromJson(v));
            });
          } else {}
        });
      } else {
        AppToast.showErrorToast(message: res['ErrorEndUserMessage']);
      }
    }).catchError((err) {
      GifLoaderDialogUtils.hideDialog(context);
      AppToast.showErrorToast(message: err);
      print(err);
    });
  }
}
