import 'package:diplomaticquarterapp/core/enum/viewstate.dart';
import 'package:diplomaticquarterapp/core/model/AlHabibMedicalService/HomeHealthCare/get_hhc_all_pres_orders_response_model.dart';
import 'package:diplomaticquarterapp/core/model/AlHabibMedicalService/HomeHealthCare/update_pres_oreder_request_model.dart';
import 'package:diplomaticquarterapp/core/viewModels/AlHabibMedicalService/cmc_view_model.dart';
import 'package:diplomaticquarterapp/core/viewModels/project_view_model.dart';
import 'package:diplomaticquarterapp/uitl/app_toast.dart';
import 'package:diplomaticquarterapp/uitl/date_uitl.dart';
import 'package:diplomaticquarterapp/uitl/translations_delegate_base.dart';
import 'package:diplomaticquarterapp/uitl/utils.dart';
import 'package:diplomaticquarterapp/widgets/buttons/secondary_button.dart';
import 'package:diplomaticquarterapp/widgets/data_display/text.dart';
import 'package:diplomaticquarterapp/widgets/others/app_scaffold_widget.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import 'Dialog/confirm_cancel_order_dialog.dart';

class OrdersLogDetailsPage extends StatelessWidget {
  final CMCViewModel model;

  const OrdersLogDetailsPage({Key key, this.model}) : super(key: key);

  @override
  Widget build(BuildContext context) {

    ProjectViewModel projectViewModel = Provider.of(context);

    void showConfirmMessage(
        CMCViewModel model, GetHHCAllPresOrdersResponseModel order) {
      showDialog(
          context: context,
          child: ConfirmCancelOrderDialog(
            model: model,
            onTap: () async {
              UpdatePresOrderRequestModel updatePresOrderRequestModel =
                  UpdatePresOrderRequestModel(
                      presOrderID: order.iD,
                      rejectionReason: "",
                      presOrderStatus: 4, editedBy: 3);
              await model.updateCmcPresOrder(updatePresOrderRequestModel);
              if(model.state == ViewState.ErrorLocal) {
                Utils.showErrorToast(model.error); 
              } else {
                AppToast.showSuccessToast(message:TranslationBase.of(context).processDoneSuccessfully );
                await model.getCmcAllPresOrders();
              }
            },
          ));
    }

    return AppScaffold(
        isShowAppBar: false,
        baseViewModel: model,
        body: SingleChildScrollView(
          physics: ScrollPhysics(),
          child: Container(
            margin: EdgeInsets.all(12),
            child: Center(
              child: FractionallySizedBox(
                widthFactor: 0.94,
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    SizedBox(
                      height: 50,
                    ),
                    Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: model.cmcAllPresOrders.map((order) {
                          return Container(
                            width: double.infinity,
                            margin: EdgeInsets.only(top: 15),
                            decoration: BoxDecoration(
                                border:
                                Border.all(color: Colors.grey, width: 1),
                                borderRadius: BorderRadius.circular(12),
                                color: Colors.white),
                            child: Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                SizedBox(
                                  height: 12,
                                ),
                                Container(
                                  width: double.infinity,
                                  padding: EdgeInsets.only(
                                      left: 15, bottom: 15, top: 15,right: 15),
                                  decoration: BoxDecoration(
                                      border: Border(
                                        bottom: BorderSide(
                                          color: Colors.grey,
                                          width: 1.0,
                                        ),
                                      ),
                                      // borderRadius: BorderRadius.circular(12),
                                      color: Colors.white),
                                  child: Column(
                                    crossAxisAlignment: CrossAxisAlignment.start,
                                    children: [
                                      Texts(
                                        TranslationBase
                                            .of(context)
                                            .requestID,
                                        bold: false,
                                        fontSize: 13,
                                      ),
                                      SizedBox(
                                        height: 4,
                                      ),
                                      Texts(
                                        order.iD.toString(),
                                        fontSize: 22,
                                      ),
                                    ],
                                  ),
                                ),
                                Container(
                                  width: double.infinity,
                                  padding: EdgeInsets.only(
                                      left: 15, bottom: 15, top: 15,right: 15),
                                  decoration: BoxDecoration(
                                      border: Border(
                                        bottom: BorderSide(
                                          color: Colors.grey,
                                          width: 1.0,
                                        ),
                                      ),
                                      // borderRadius: BorderRadius.circular(12),
                                      color: Colors.white),
                                  child: Column(
                                    crossAxisAlignment: CrossAxisAlignment.start,
                                    children: [
                                      Texts(
                                        TranslationBase
                                            .of(context)
                                            .OrderStatus,
                                        bold: false,
                                        fontSize: 13,
                                      ),
                                      SizedBox(
                                        height: 4,
                                      ),
                                      Texts(

                                        projectViewModel.isArabic ? order
                                            .descriptionN : order.description,
                                        fontSize: 22,
                                      ),
                                    ],
                                  ),
                                ),
                                Container(
                                  width: double.infinity,
                                  padding: EdgeInsets.only(
                                      left: 15, bottom: 15, top: 15,right: 15),
                                  decoration: BoxDecoration(
                                      border: Border(
                                        bottom: BorderSide(
                                          color: Colors.grey,
                                          width: 1.0,
                                        ),
                                      ),
                                      // borderRadius: BorderRadius.circular(12),
                                      color: Colors.white),
                                  child: Column(
                                    crossAxisAlignment: CrossAxisAlignment.start,
                                    children: [
                                      Texts(
                                        TranslationBase.of(context).pickupDate,
                                        bold: false,
                                        fontSize: 13,
                                      ),
                                      SizedBox(
                                        height: 4,
                                      ),
                                      Texts(
                                        DateUtil.getDayMonthYearDateFormatted(
                                            DateUtil.convertStringToDate(order.createdOn)),
                                        fontSize: 22,
                                      ),
                                    ],
                                  ),
                                ),
                                Container(
                                  width: double.infinity,
                                  padding: EdgeInsets.only(
                                      left: 15, bottom: 15, top: 15,right: 15),
                                  decoration: BoxDecoration(
                                      border: Border(
                                        bottom: BorderSide(
                                          color: Colors.grey,
                                          width: 1.0,
                                        ),
                                      ),
                                      // borderRadius: BorderRadius.circular(12),
                                      color: Colors.white),
                                  child: Column(
                                    crossAxisAlignment:
                                    CrossAxisAlignment.start,
                                    children: [
                                      Texts(
                                        TranslationBase.of(context).orderLocation,
                                        bold: false,
                                        fontSize: 13,
                                      ),
                                      SizedBox(
                                        height: 4,
                                      ),
                                      Texts(
                                        !projectViewModel.isArabic?order.
                                        projectDescription.toString() :
                                        order
                                            .projectDescriptionN
                                            .toString(),
                                        fontSize: 22,
                                      ),
                                    ],
                                  ),
                                ),
                                SizedBox(
                                  height: 12,
                                ),
                                if (order.status == 1 ||order.status == 2 )
                                Center(
                                  child: Container(
                                    width: MediaQuery
                                        .of(context)
                                        .size
                                        .width *
                                        0.85,
                                    child: SecondaryButton(
                                        label: TranslationBase.of(context).cancel.toUpperCase(),
                                        onTap: () {
                                          showConfirmMessage(model,
                                              order);
                                        }
                                        ,
                                        color: Colors.red[800],
                                        disabled: false,
                                        textColor: Theme
                                            .of(context)
                                            .backgroundColor),
                                  ),
                                ),
                                SizedBox(
                                  height: 22,
                                ),
                              ],
                            ),
                          );
                      }).toList())
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }
}
