import 'package:diplomaticquarterapp/core/model/ImagesInfo.dart';
import 'package:diplomaticquarterapp/core/viewModels/project_view_model.dart';
import 'package:diplomaticquarterapp/models/Appointments/DoctorListResponse.dart' as DoctorListResponse;
import 'package:diplomaticquarterapp/models/MyInvoices/DentalInvoiceDetailResponse.dart';
import 'package:diplomaticquarterapp/models/MyInvoices/GetDentalAppointmentsResponse.dart';
import 'package:diplomaticquarterapp/pages/medical/my_invoices/invoice_detail_page.dart';
import 'package:diplomaticquarterapp/services/my_invoice_service/my_invoice_services.dart';
import 'package:diplomaticquarterapp/uitl/app_toast.dart';
import 'package:diplomaticquarterapp/uitl/date_uitl.dart';
import 'package:diplomaticquarterapp/uitl/gif_loader_dialog_utils.dart';
import 'package:diplomaticquarterapp/uitl/translations_delegate_base.dart';
import 'package:diplomaticquarterapp/widgets/data_display/medical/doctor_card.dart';
import 'package:diplomaticquarterapp/widgets/others/app_scaffold_widget.dart';
import 'package:diplomaticquarterapp/widgets/transitions/fade_page.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class MyInvoices extends StatefulWidget {
  @override
  _MyInvoicesState createState() => _MyInvoicesState();
}

class _MyInvoicesState extends State<MyInvoices> {
  bool isDataLoaded = false;
  GetDentalAppointmentsResponse getDentalAppointmentsResponse;
  DentalInvoiceDetailResponse dentalInvoiceDetailResponse;

  ProjectViewModel projectViewModel;
  List<ImagesInfo> imagesInfo = List();

  @override
  void initState() {
    imagesInfo
        .add(ImagesInfo(imageEn: 'https://hmgwebservices.com/Images/MobileApp/imges-info/my-invoice/en/0.png', imageAr: 'https://hmgwebservices.com/Images/MobileApp/imges-info/my-invoice/ar/0.png'));

    WidgetsBinding.instance.addPostFrameCallback((_) {
      if (projectViewModel.isLogin) getDentalAppointments();
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    projectViewModel = Provider.of(context);
    return AppScaffold(
        appBarTitle: TranslationBase.of(context).myInvoice,
        isShowAppBar: true,
        isShowDecPage: true,
        showNewAppBar: true,
        showNewAppBarTitle: true,
        imagesInfo: imagesInfo,
        body: Container(
          child: SingleChildScrollView(
            physics: BouncingScrollPhysics(),
            child: isDataLoaded
                ? Column(
                    children: [
                      ListView.separated(
                        shrinkWrap: true,
                        physics: NeverScrollableScrollPhysics(),
                        padding: EdgeInsets.only(bottom: 14, top: 14, left: 21, right: 21),
                        itemBuilder: (context, index) {
                          return DoctorCard(
                            onTap: () => openInvoiceDetailsPage(getDentalAppointmentsResponse.listDentalAppointments[index]),
                            isInOutPatient: true,
                            name: TranslationBase.of(context).dr.toString() + " " + (projectViewModel.isArabic ? getDentalAppointmentsResponse.listDentalAppointments[index].doctorNameN.toString() : getDentalAppointmentsResponse.listDentalAppointments[index].doctorName.toString()),
                            billNo: getDentalAppointmentsResponse.listDentalAppointments[index].invoiceNo.toString(),
                            profileUrl: getDentalAppointmentsResponse.listDentalAppointments[index].doctorImageURL,
                            subName: getDentalAppointmentsResponse.listDentalAppointments[index].projectName,
                            isLiveCareAppointment: false,
                            date: DateUtil.convertStringToDate(getDentalAppointmentsResponse.listDentalAppointments[index].appointmentDate),
                            isSortByClinic: true,
                          );
                        },
                        itemCount: getDentalAppointmentsResponse.listDentalAppointments.length,
                        separatorBuilder: (context, index) => SizedBox(height: 14),
                      ),
                    ],
                  )
                : Container(),
          ),
        ));
  }

  openInvoiceDetailsPage(ListDentalAppointments listDentalAppointments) {
    GifLoaderDialogUtils.showMyDialog(context);
    MyInvoicesService myInvoicesService = new MyInvoicesService();

    DoctorListResponse.DoctorList doctor = new DoctorListResponse.DoctorList();

    doctor.name = projectViewModel.isArabic ? listDentalAppointments.doctorNameN : listDentalAppointments.doctorName;
    doctor.projectName = listDentalAppointments.projectName;
    doctor.date = listDentalAppointments.appointmentDate;
    doctor.actualDoctorRate = 0;
    doctor.doctorImageURL = listDentalAppointments.doctorImageURL;
    doctor.projectID = listDentalAppointments.projectID;
    doctor.dayName = listDentalAppointments.invoiceNo;
    doctor.clinicName = listDentalAppointments.invoiceNo.toString();
    doctor.date = listDentalAppointments.appointmentDate;
    doctor.noOfPatientsRate = listDentalAppointments.patientNumber;
    doctor.actualDoctorRate = listDentalAppointments.doctorRate;
    doctor.decimalDoctorRate = listDentalAppointments.decimalDoctorRate;
    doctor.doctorID = listDentalAppointments.doctorID;

    myInvoicesService.getDentalAppointmentInvoice(listDentalAppointments.projectID, listDentalAppointments.appointmentNo, listDentalAppointments.invoiceNo, context).then((res) {
      GifLoaderDialogUtils.hideDialog(context);
      setState(() {
        if (res['MessageStatus'] == 1) {
          dentalInvoiceDetailResponse = DentalInvoiceDetailResponse.fromJson(res);
          Navigator.push(context, FadePage(page: InvoiceDetail(doctor, listDentalAppointments, dentalInvoiceDetailResponse, context)));
        } else {
          AppToast.showErrorToast(message: res['ErrorEndUserMessage']);
        }
        isDataLoaded = true;
      });
    }).catchError((err) {
      GifLoaderDialogUtils.hideDialog(context);
      print(err);
      AppToast.showErrorToast(message: err.toString());
    });
  }

  getDentalAppointments() {
    GifLoaderDialogUtils.showMyDialog(context);
    MyInvoicesService myInvoicesService = new MyInvoicesService();

    myInvoicesService.getAllDentalAppointments(12, context).then((res) {
      GifLoaderDialogUtils.hideDialog(context);
      setState(() {
        if (res['MessageStatus'] == 1) {
          getDentalAppointmentsResponse = GetDentalAppointmentsResponse.fromJson(res);
        } else {
          AppToast.showErrorToast(message: res['ErrorEndUserMessage']);
        }
        isDataLoaded = true;
      });
    }).catchError((err) {
      GifLoaderDialogUtils.hideDialog(context);
      print(err);
      AppToast.showErrorToast(message: err);
      Navigator.of(context).pop();
    });
  }
}
