import 'package:diplomaticquarterapp/core/viewModels/ancillary_orders_view_model.dart';
import 'package:diplomaticquarterapp/pages/base/base_view.dart';
import 'package:diplomaticquarterapp/uitl/translations_delegate_base.dart';
import 'package:diplomaticquarterapp/widgets/buttons/button.dart';
import 'package:diplomaticquarterapp/widgets/data_display/text.dart';
import 'package:flutter/material.dart';
import 'package:diplomaticquarterapp/widgets/others/app_scaffold_widget.dart';
import 'package:diplomaticquarterapp/uitl/date_uitl.dart';
import "package:collection/collection.dart";
import 'package:diplomaticquarterapp/widgets/text/app_texts_widget.dart';
import 'package:diplomaticquarterapp/widgets/transitions/fade_page.dart';
import 'package:diplomaticquarterapp/pages/AlHabibMedicalService/ancillary-orders/ordersPayment.dart';

class AnicllaryOrdersDetails extends StatefulWidget {
  final dynamic appoNo;
  final dynamic orderNo;
  final dynamic projectID;

  AnicllaryOrdersDetails(this.appoNo, this.orderNo, this.projectID);
  @override
  _AnicllaryOrdersState createState() => _AnicllaryOrdersState();
}

class _AnicllaryOrdersState extends State<AnicllaryOrdersDetails>
    with SingleTickerProviderStateMixin {
  void initState() {
    super.initState();
  }

  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return BaseView<AnciallryOrdersViewModel>(
        onModelReady: (model) => model.getOrdersDetails(
            widget.appoNo, widget.orderNo, widget.projectID),
        builder: (_, model, widget) => AppScaffold(
            isShowAppBar: true,
            baseViewModel: model,
            appBarTitle: TranslationBase.of(context).anicllaryOrders,
            body: SingleChildScrollView(
                padding: EdgeInsets.all(12),
                child: model.ancillaryListsDetails.length > 0
                    ? Column(children: [
                        getPatientInfo(model),
                        getInvoiceDetails(model),
                        getInsuranceDetails(model),
                        getAncillaryDetails(model),
                        Row(
                          mainAxisAlignment: MainAxisAlignment.spaceAround,
                          children: [
                            Texts(
                              TranslationBase.of(context).total,
                              fontSize: 20,
                              fontWeight: FontWeight.bold,
                            ),
                            Texts(
                              getTotalValue(model),
                              fontSize: 20,
                              fontWeight: FontWeight.bold,
                            )
                          ],
                        ),
                        Row(
                          mainAxisAlignment: MainAxisAlignment.end,
                          children: [
                            Button(
                              label: TranslationBase.of(context).payNow,
                              backgroundColor: Colors.red[800],
                              onTap: () {
                                Navigator.push(
                                  context,
                                  FadePage(
                                    page: OrdersPayment(),
                                  ),
                                );
                              },
                            )
                          ],
                        )
                      ])
                    : SizedBox())));
  }

  Widget getPatientInfo(AnciallryOrdersViewModel model) {
    print(model.ancillaryListsDetails);
    return Padding(
      child: Column(
        children: [
          Row(
            children: [
              Texts(
                TranslationBase.of(context).mrn,
                fontWeight: FontWeight.bold,
                fontSize: 22,
              ),
              Texts(
                " : ",
                fontSize: 20,
              ),
              Texts(
                model.ancillaryLists[0].patientID.toString(),
              )
            ],
          ),
          Row(
            children: [
              Texts(
                TranslationBase.of(context).patientName,
                fontWeight: FontWeight.bold,
                fontSize: 20,
              ),
              Texts(
                " : ",
                fontSize: 20,
              ),
              Texts(
                model.ancillaryLists[0].patientName,
              )
            ],
          ),
          Divider(
            color: Colors.black26,
          )
        ],
      ),
      padding: EdgeInsets.only(top: 5.0, bottom: 5.0),
    );
  }

  Widget getInvoiceDetails(model) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Row(
          children: [
            Texts(
              TranslationBase.of(context).invoiceNo,
//              fontWeight: FontWeight.bold,
              color: Colors.red[500],
            ),
            Texts(" : "),
            Texts(
              model.ancillaryListsDetails[0].appointmentNo.toString(),
            )
          ],
        ),
        Row(
          children: [
//             Texts(
//               TranslationBase.of(context).invoiceDate,
// //              fontWeight: FontWeight.bold,
//               color: Colors.red[500],
//             ),
            Texts(" : "),
            Texts(
              DateUtil.getDayMonthYearDateFormatted(
                DateUtil.convertStringToDate(
                    model.ancillaryListsDetails[0].appointmentDate),
              ),
            )
          ],
        ),
        Row(
          children: [
            Texts(
              TranslationBase.of(context).doctorName,
//              fontWeight: FontWeight.bold,
              color: Colors.red[500],
            ),
            Texts(" : "),
            Texts(
              model.ancillaryListsDetails[0].doctorName,
            ),
          ],
        ),
        SizedBox(
          height: 10,
        ),
        Divider(
          color: Colors.black26,
        )
      ],
    );
  }

  Widget getInsuranceDetails(model) {
    return Padding(
        padding: EdgeInsets.only(top: 10, bottom: 10),
        child: Column(
          children: [
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              children: [
                Texts(
                  TranslationBase.of(context).insurance,
                  fontWeight: FontWeight.bold,
                ),
                Texts(
                  TranslationBase.of(context).insuranceID,
                  fontWeight: FontWeight.bold,
                )
              ],
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              children: [
                Texts(
                  model.ancillaryListsDetails[0].policyName,
                ),
                Texts(
                  model.ancillaryListsDetails[0].insurancePolicyNo,
                )
              ],
            ),
            SizedBox(
              height: 15,
            ),
            Divider(
              color: Colors.red[800],
              thickness: 3,
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              children: [
                Row(
                  children: [],
                ),
                Row(
                    mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                    children: [
                      Texts(
                        TranslationBase.of(context).price,
                        color: Colors.grey[500],
                        fontSize: 14,
                      ),
                      SizedBox(width: 15),
                      Texts(
                        TranslationBase.of(context).vat,
                        color: Colors.grey[500],
                        fontSize: 14,
                      ),
                      SizedBox(width: 15),
                      Texts(
                        TranslationBase.of(context).total,
                        color: Colors.grey[500],
                        fontSize: 14,
                      ),
                    ]),
              ],
            ),
            Divider(
              color: Colors.black26,
            ),
          ],
        ));
  }

  Widget getAncillaryDetails(model) {
    Map newMap = groupBy(
        model.ancillaryListsDetails[0].ancillaryOrderProcDetailsList,
        (obj) => obj.procedureCategoryName);
    print(newMap);

    return Padding(
        padding: EdgeInsets.only(top: 10, bottom: 10),
        child: getHeaderDetails(newMap));
  }

  Widget getHeaderDetails(newMap) {
    List<Widget> list = new List<Widget>();

    newMap.forEach((key, value) {
      list.add(
        Texts(
          key,
          fontWeight: FontWeight.bold,
        ),
      );
      list.add(Row(
        mainAxisAlignment: MainAxisAlignment.start,
        children: [
          getLabDetails(value),
        ],
      ));
    });

    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: list,
    );
  }

  String getTotalValue(value) {
    double total = 0.0;
    value.ancillaryListsDetails[0].ancillaryOrderProcDetailsList
        .forEach((result) => {total += result.companyShareWithTax});
    return total.toStringAsFixed(2);
  }

  getLabDetails(value) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: value.map<Widget>((result) {
        return Container(
            width: MediaQuery.of(context).size.width * .9,
            padding: EdgeInsets.all(10),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              children: [
                Expanded(
                    flex: 3,
                    child: Text(result.procedureName.toString(),
                        overflow: TextOverflow.ellipsis)),
                Expanded(child: AppText(result.companyShare.toString())),
                Expanded(child: AppText(result.companyTaxAmount.toString())),
                Expanded(
                    child: AppText(
                  result.companyShareWithTax.toString(),
                ))
              ],
            ));
      }).toList(),
    );
  }
}
