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/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,
        imagesInfo: imagesInfo,
        body: Container(
          child: SingleChildScrollView(
            physics: BouncingScrollPhysics(),
            child: isDataLoaded
                ? Column(
                    children: [
                      ...List.generate(
                        getDentalAppointmentsResponse
                            .listDentalAppointments.length,
                        (index) => InkWell(
                          onTap: () {
                            openInvoiceDetailsPage(getDentalAppointmentsResponse
                                .listDentalAppointments[index]);
                          },
                          child: Container(
                            decoration: BoxDecoration(
                              color: Colors.white,
                              borderRadius:
                                  BorderRadius.all(Radius.circular(8.0)),
                            ),
                            margin: EdgeInsets.all(10.0),
                            width: MediaQuery.of(context).size.width,
                            child: Row(
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              crossAxisAlignment: CrossAxisAlignment.center,
                              children: [
                                Container(
                                  child: Container(
                                    margin: EdgeInsets.fromLTRB(
                                        20.0, 10.0, 20.0, 10.0),
                                    child: ClipRRect(
                                      borderRadius:
                                          BorderRadius.circular(100.0),
                                      child: Image.asset(
                                          "assets/images/new-design/ViewDetailsIco.png",
                                          fit: BoxFit.fill,
                                          height: 60.0,
                                          width: 60.0),
                                    ),
                                  ),
                                ),
                                Column(
                                  mainAxisSize: MainAxisSize.min,
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  children: [
                                    Container(
                                      margin: EdgeInsets.only(top: 10.0),
                                      child: Text("Appointment No: ",
                                          style: TextStyle(
                                              fontSize: 14.0,
                                              color: Colors.black,
                                              letterSpacing: 0.5)),
                                    ),
                                    Container(
                                      margin: EdgeInsets.only(top: 10.0),
                                      child: Text("Appointment Date: ",
                                          style: TextStyle(
                                              fontSize: 14.0,
                                              color: Colors.black,
                                              letterSpacing: 0.5)),
                                    ),
                                    Container(
                                      margin: EdgeInsets.only(
                                          top: 10.0, bottom: 10.0),
                                      child: Text("Clinic: ",
                                          style: TextStyle(
                                              fontSize: 14.0,
                                              color: Colors.black,
                                              letterSpacing: 0.5)),
                                    ),
                                  ],
                                ),
                                Column(
                                  mainAxisSize: MainAxisSize.min,
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  children: [
                                    Container(
                                      margin: EdgeInsets.only(top: 10.0),
                                      child: Text(
                                          getDentalAppointmentsResponse
                                              .listDentalAppointments[index]
                                              .appointmentNo
                                              .toString(),
                                          style: TextStyle(
                                              fontSize: 14.0,
                                              color: Colors.black,
                                              fontWeight: FontWeight.bold,
                                              letterSpacing: 0.5)),
                                    ),
                                    Container(
                                      margin: EdgeInsets.only(top: 10.0),
                                      child: Text(
                                          DateUtil.getMonthDayYearDateFormatted(
                                              DateUtil.convertStringToDate(
                                                  getDentalAppointmentsResponse
                                                      .listDentalAppointments[
                                                          index]
                                                      .appointmentDate)),
                                          style: TextStyle(
                                              fontSize: 14.0,
                                              color: Colors.black,
                                              fontWeight: FontWeight.bold,
                                              letterSpacing: 0.5)),
                                    ),
                                    Container(
                                      margin: EdgeInsets.only(
                                          top: 10.0, bottom: 10.0),
                                      child: Text(
                                          getDentalAppointmentsResponse
                                              .listDentalAppointments[index]
                                              .clinicName,
                                          style: TextStyle(
                                              fontSize: 14.0,
                                              color: Colors.black,
                                              fontWeight: FontWeight.bold,
                                              letterSpacing: 0.5)),
                                    ),
                                  ],
                                ),
                                projectViewModel.isArabic
                                    ? Container(
                                        margin: EdgeInsets.only(left: 15.0),
                                        child: Image.asset(
                                            "assets/images/new-design/arrow_menu_black-ar.png",
                                            fit: BoxFit.fill,
                                            height: 20.0,
                                            width: 12.0),
                                      )
                                    : Container(
                                        margin: EdgeInsets.only(right: 15.0),
                                        child: Image.asset(
                                            "assets/images/new-design/arrow_menu_black-en.png",
                                            fit: BoxFit.fill,
                                            height: 20.0,
                                            width: 12.0),
                                      ),
                              ],
                            ),
                          ),
                        ),
                      ),
                    ],
                  )
                : Container(),
          ),
        ));
  }

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

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

    doctor.name = listDentalAppointments.doctorName;
    doctor.projectName = listDentalAppointments.projectName;
    doctor.date = listDentalAppointments.appointmentDate;
    doctor.actualDoctorRate = 0;
    doctor.doctorImageURL = listDentalAppointments.doctorImageURL;
    doctor.dayName = listDentalAppointments.invoiceNo;
    doctor.doctorTitle =  "Dr.";
    doctor.clinicName = "InvoiceNo: " + listDentalAppointments.invoiceNo.toString();

    myInvoicesService.getDentalAppointmentInvoice(12, listDentalAppointments.appointmentNo, context).then((res) {
      GifLoaderDialogUtils.hideDialog(context);
      setState(() {
        if (res['MessageStatus'] == 1) {
          dentalInvoiceDetailResponse =
              DentalInvoiceDetailResponse.fromJson(res);
          print(dentalInvoiceDetailResponse.listEInvoiceForDental[0].listConsultation.length);
          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());
      Navigator.of(context).pop();
    });
  }

  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();
    });
  }
}
