import 'package:hmg_patient_app/core/enum/viewstate.dart';
import 'package:hmg_patient_app/core/model/er/AmbulanceRequestOrdersModel.dart';
import 'package:hmg_patient_app/core/model/er/PatientER.dart';
import 'package:hmg_patient_app/core/model/er/PatientER_RC.dart';
import 'package:hmg_patient_app/core/viewModels/er/am_request_view_model.dart';
import 'package:hmg_patient_app/core/viewModels/project_view_model.dart';
import 'package:hmg_patient_app/pages/ErService/widgets/StepsWidget.dart';
import 'package:hmg_patient_app/theme/colors.dart';
import 'package:hmg_patient_app/uitl/app_toast.dart';
import 'package:hmg_patient_app/uitl/date_uitl.dart';
import 'package:hmg_patient_app/uitl/translations_delegate_base.dart';
import 'package:hmg_patient_app/uitl/utils.dart';
import 'package:hmg_patient_app/uitl/utils_new.dart';
import 'package:hmg_patient_app/widgets/dialogs/ConfirmWithMessageDialog.dart';
import 'package:hmg_patient_app/widgets/others/app_scaffold_widget.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import 'BillAmount.dart';
import 'PickupLocation.dart';
import 'SelectTransportationMethod.dart';
import 'Summary.dart';

class AmbulanceRequestIndexPage extends StatefulWidget {
  final AmRequestViewModel amRequestViewModel;
  final TabController tabController;
  AmbulanceRequestIndexPage({Key? key, required this.amRequestViewModel, required this.tabController});

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

class _AmbulanceRequestIndexPageState extends State<AmbulanceRequestIndexPage> {
  int currentIndex = 0;
  late PageController pageController;
  PatientER _patientER = PatientER();

  PatientER_RC _patientER_RC = PatientER_RC();

  TransportationDetails transportationDetails = new TransportationDetails();

  late int status;
  late String _statusDisp;
  late Color _color;

  _changeCurrentTab(int tab) {
    setState(() {
      currentIndex = tab;
    });
    pageController.animateToPage(tab, duration: Duration(milliseconds: 800), curve: Curves.easeOutQuart);
  }

  @override
  void initState() {
    super.initState();
    pageController = new PageController();
    _patientER_RC.transportationDetails = transportationDetails;
  }

  @override
  Widget build(BuildContext context) {
    ProjectViewModel projectViewModel = Provider.of(context);

    AmbulanceRequestOrdersModel order = widget.amRequestViewModel.pendingAmbulanceRequestOrder ?? AmbulanceRequestOrdersModel();

    if(widget.amRequestViewModel.pendingAmbulanceRequestOrder != null) {
      int status = order.statusId!;
      _statusDisp = order.statusText!;
      if (status == 1) {
        //pending
        _color = Color(0xffCC9B14);
      } else if (status == 2) {
        //processing
        _color = Color(0xff2E303A);
      } else if (status == 3) {
        //completed
        _color = Color(0xff359846);
      } else if (status == 4 || status == 6 || status == 7) {
        //cancel // Rejected
        _color = Color(0xffD02127);
      }
    }

    return AppScaffold(
      body: widget.amRequestViewModel.pendingAmbulanceRequestOrder != null && order != null
          ? SingleChildScrollView(
              child: Column(
                children: [
                  SizedBox(
                    height: 10,
                  ),
                  Container(
                    margin: EdgeInsets.all(21),
                    decoration: BoxDecoration(
                      color: _color,
                      borderRadius: BorderRadius.all(
                        Radius.circular(10.0),
                      ),
                      boxShadow: [
                        BoxShadow(
                          color: Color(0xff000000).withOpacity(.05),
                          blurRadius: 27,
                          offset: Offset(0, -3),
                        ),
                      ],
                    ),
                    child: Container(
                      margin: EdgeInsets.only(left: projectViewModel.isArabic ? 0 : 6, right: projectViewModel.isArabic ? 6 : 0),
                      padding: EdgeInsets.symmetric(vertical: 14, horizontal: 12),
                      decoration: BoxDecoration(
                        color: Colors.white,
                        border: Border.all(color: Colors.white, width: 1),
                        borderRadius: BorderRadius.only(
                          bottomRight: projectViewModel.isArabic ? Radius.circular(0) : Radius.circular(10.0),
                          topRight: projectViewModel.isArabic ? Radius.circular(0) : Radius.circular(10.0),
                          bottomLeft: projectViewModel.isArabic ? Radius.circular(10.0) : Radius.circular(0),
                          topLeft: projectViewModel.isArabic ? Radius.circular(10.0) : Radius.circular(0),
                        ),
                      ),
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Row(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              Expanded(
                                child: Column(
                                  mainAxisAlignment: MainAxisAlignment.start,
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  children: [
                                    Text(
                                      _statusDisp,
                                      style: TextStyle(fontSize: 12, fontWeight: FontWeight.w600, color: _color, letterSpacing: -0.4, height: 16 / 10),
                                    ),
                                    SizedBox(height: 6),
                                    Text(
                                      '${TranslationBase.of(context).requestID}: ${order.iD}',
                                      style: TextStyle(fontSize: 16, fontWeight: FontWeight.w600, color: Color(0xff2E303A), letterSpacing: -0.64, height: 25 / 16),
                                    ),
                                    Row(
                                      crossAxisAlignment: CrossAxisAlignment.start,
                                      children: [
                                        Text(
                                          TranslationBase.of(context).pickupDate + ": ",
                                          style: TextStyle(fontSize: 10, fontWeight: FontWeight.w600, color: Color(0xff575757), letterSpacing: -0.4, height: 16 / 10),
                                        ),
                                        Expanded(
                                          child: Text(
                                            DateUtil.getDayMonthYearDateFormatted(DateTime.tryParse(order.created!)!),
                                            style: TextStyle(fontSize: 12, fontWeight: FontWeight.w600, color: Color(0xff2B353E), letterSpacing: -0.48, height: 16 / 10),
                                          ),
                                        ),
                                      ],
                                    ),
                                    Row(
                                      crossAxisAlignment: CrossAxisAlignment.start,
                                      children: [
                                        Text(
                                          TranslationBase.of(context).transportMethod + ": ",
                                          style: TextStyle(fontSize: 10, fontWeight: FontWeight.w600, color: Color(0xff575757), letterSpacing: -0.4, height: 16 / 10),
                                        ),
                                        Expanded(
                                          child: Text(
                                            order.serviceText!,
                                            style: TextStyle(fontSize: 12, fontWeight: FontWeight.w600, color: Color(0xff2B353E), letterSpacing: -0.48, height: 16 / 10),
                                          ),
                                        ),
                                      ],
                                    ),
                                    SizedBox(
                                      height: 20.0,
                                    ),
                                  ],
                                ),
                              ),
                              Column(
                                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                crossAxisAlignment: CrossAxisAlignment.end,
                                children: [
                                  SizedBox(height: 12),
                                  if (order.statusId == 1 || order.statusId == 2)
                                    InkWell(
                                      onTap: () {
                                        showConfirmMessage(widget.amRequestViewModel, order.iD!, context);
                                      },
                                      child: Container(
                                        padding: EdgeInsets.symmetric(vertical: 8, horizontal: 14),
                                        decoration: BoxDecoration(
                                          color: Color(0xffD02127),
                                          border: Border.all(color: Colors.white, width: 1),
                                          borderRadius: BorderRadius.circular(10),
                                        ),
                                        child: Text(
                                          TranslationBase.of(context).cancel_nocaps,
                                          style: TextStyle(fontSize: 12, fontWeight: FontWeight.w600, color: Colors.white, letterSpacing: -0.4),
                                        ),
                                      ),
                                    ),
                                ],
                              ),
                            ],
                          ),
                          Row(
                            children: [
                              Container(
                                margin: projectViewModel.isArabic ? EdgeInsets.only(left: 10.0) : EdgeInsets.only(right: 10.0),
                                width: 8,
                                height: 8,
                                decoration: containerRadius(
                                  CustomColors.textColor,
                                  100,
                                ),
                              ),
                              Column(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: [
                                  Text(
                                    TranslationBase.of(context).pickupLocation + ": ",
                                    style: TextStyle(fontSize: 10, fontWeight: FontWeight.w600, color: Color(0xff575757), letterSpacing: -0.4, height: 16 / 10),
                                  ),
                                  Container(
                                    width: MediaQuery.of(context).size.width * 0.7,
                                    child: Text(
                                      order.pickupLocation!.trim().toString(),
                                      style: TextStyle(fontSize: 12, fontWeight: FontWeight.w600, color: Color(0xff2B353E), letterSpacing: -0.48),
                                    ),
                                  ),
                                ],
                              ),
                            ],
                          ),
                          Container(
                            height: 20.0,
                            width: 1.0,
                            color: CustomColors.grey2,
                            margin: const EdgeInsets.only(left: 3.0, right: 3.0),
                          ),
                          Row(
                            children: [
                              Container(
                                margin: projectViewModel.isArabic ? EdgeInsets.only(left: 10.0) : EdgeInsets.only(right: 10.0),
                                width: 8,
                                height: 8,
                                decoration: containerRadius(
                                  CustomColors.accentColor,
                                  100,
                                ),
                              ),
                              Column(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: [
                                  Text(
                                    TranslationBase.of(context).dropoffLocation + ": ",
                                    style: TextStyle(fontSize: 10, fontWeight: FontWeight.w600, color: Color(0xff575757), letterSpacing: -0.4, height: 16 / 10),
                                  ),
                                  Text(
                                    order.dropOffLocation!.trim().toString(),
                                    style: TextStyle(fontSize: 12, fontWeight: FontWeight.w600, color: Color(0xff2B353E), letterSpacing: -0.48),
                                  ),
                                ],
                              ),
                            ],
                          ),
                        ],
                      ),
                    ),
                  ),

                  // Container(
                  //   margin: EdgeInsets.only(left: 18, right: 18),
                  //   decoration: BoxDecoration(
                  //     color: Colors.white,
                  //     borderRadius: BorderRadius.circular(2),
                  //   ),
                  //   child: Column(
                  //     crossAxisAlignment: CrossAxisAlignment.start,
                  //     children: [
                  //       OrderLogItem(
                  //         title: TranslationBase.of(context).reqId,
                  //         value: widget.amRequestViewModel.pendingAmbulanceRequestOrder.iD.toString(),
                  //       ),
                  //       OrderLogItem(
                  //         title: TranslationBase.of(context).status,
                  //         value: widget.amRequestViewModel.pendingAmbulanceRequestOrder.statusText,
                  //       ),
                  //       OrderLogItem(
                  //         title: TranslationBase.of(context).pickupDate,
                  //         value: DateUtil.getDayMonthYearDateFormatted(DateTime.tryParse(widget.amRequestViewModel.pendingAmbulanceRequestOrder.created)),
                  //       ),
                  //       OrderLogItem(
                  //         title: TranslationBase.of(context).pickupLocation,
                  //         value: widget.amRequestViewModel.pendingAmbulanceRequestOrder.pickupLocation,
                  //       ),
                  //       OrderLogItem(
                  //         title: TranslationBase.of(context).dropoffLocation,
                  //         value: widget.amRequestViewModel.pendingAmbulanceRequestOrder.dropOffLocation,
                  //       ),
                  //       OrderLogItem(
                  //         title: TranslationBase.of(context).transportMethod,
                  //         value: widget.amRequestViewModel.pendingAmbulanceRequestOrder.serviceText,
                  //       ),
                  //       Container(
                  //         padding: EdgeInsets.all(10),
                  //         width: double.maxFinite,
                  //         margin: EdgeInsets.only(bottom: 4, left: 4, right: 4),
                  //         decoration: BoxDecoration(
                  //             borderRadius: BorderRadius.only(
                  //               bottomRight: Radius.circular(12),
                  //               bottomLeft: Radius.circular(12),
                  //             ),
                  //             color: Colors.white),
                  //         child: SecondaryButton(
                  //           color: Colors.red[900],
                  //           textColor: Colors.white,
                  //           label: TranslationBase.of(context).cancel,
                  //           onTap: () {
                  //             widget.amRequestViewModel.updatePressOrder(presOrderID: widget.amRequestViewModel.pendingAmbulanceRequestOrder.iD);
                  //           },
                  //         ),
                  //       )
                  //     ],
                  //   ),
                  // ),
                ],
              ),
            )
          : Column(
              children: [
                SizedBox(
                  height: 20,
                ),
                Container(
                  margin: EdgeInsets.only(left: 12, right: 12),
                  child: StepsWidget(
                    index: currentIndex,
                    changeCurrentTab: _changeCurrentTab,
                  ),
                ),
                Expanded(
                  child: PageView(
                    physics: NeverScrollableScrollPhysics(),
                    controller: pageController,
                    children: <Widget>[
                      //Container(),
                      SelectTransportationMethod(
                        changeCurrentTab: _changeCurrentTab,
                        patientER: _patientER,
                        patientER_RC: _patientER_RC,
                        amRequestViewModel: widget.amRequestViewModel,
                      ),
                      PickupLocation(
                        changeCurrentTab: _changeCurrentTab,
                        patientER: _patientER,
                        patientER_RC: _patientER_RC,
                        amRequestViewModel: widget.amRequestViewModel,
                      ),
                      Container(),
                      // BillAmount(
                      //   changeCurrentTab: _changeCurrentTab,
                      //   patientER: _patientER,
                      //   patientER_RC: _patientER_RC,
                      //   amRequestViewModel: widget.amRequestViewModel,
                      // ),
                      Summary(
                        changeCurrentTab: _changeCurrentTab,
                        patientER: _patientER,
                        patientER_RC: _patientER_RC,
                        amRequestViewModel: widget.amRequestViewModel,
                        tabController: widget.tabController,
                      ),
                    ],
                  ),
                ),
              ],
            ),
    );
  }

  void showConfirmMessage(AmRequestViewModel model, int presOrderID, BuildContext context) {
    showDialog(
        context: context,
        builder: (cxt) => ConfirmWithMessageDialog(
          message: TranslationBase.of(context).cancelOrderMsg,
          onTap: () {
            Future.delayed(new Duration(milliseconds: 300)).then((value) async {
              // GifLoaderDialogUtils.showMyDialog(context);
              await model.updatePressOrder(presOrderID: presOrderID);
              if (model.state == ViewState.ErrorLocal) {
                Utils.showErrorToast(model.error);
                // GifLoaderDialogUtils.hideDialog(context);
              } else {
                AppToast.showSuccessToast(message: TranslationBase.of(context).processDoneSuccessfully);
                // GifLoaderDialogUtils.hideDialog(context);
              }
            });
          },
        ));
    return;
  }
}
