import 'package:diplomaticquarterapp/core/enum/viewstate.dart';
import 'package:diplomaticquarterapp/core/model/AlHabibMedicalService/ComprehensiveMedicalCheckup/GetCMCAllOrdersResponseModel.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/gif_loader_dialog_utils.dart';
import 'package:diplomaticquarterapp/uitl/translations_delegate_base.dart';
import 'package:diplomaticquarterapp/uitl/utils.dart';
import 'package:diplomaticquarterapp/uitl/utils_new.dart';
import 'package:diplomaticquarterapp/widgets/buttons/defaultButton.dart';
import 'package:diplomaticquarterapp/widgets/buttons/secondary_button.dart';
import 'package:diplomaticquarterapp/widgets/data_display/text.dart';
import 'package:diplomaticquarterapp/widgets/dialogs/ConfirmWithMessageDialog.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, GetCMCAllOrdersResponseModel order) {
      showDialog(
          context: context,
          builder: (cxt) => ConfirmWithMessageDialog(
            message: TranslationBase.of(context).cancelOrderMsg,
            onTap: () {
              UpdatePresOrderRequestModel updatePresOrderRequestModel = UpdatePresOrderRequestModel(presOrderID: order.iD, rejectionReason: "", presOrderStatus: 4, editedBy: 3);
              Future.delayed(new Duration(milliseconds: 300)).then((value) async {
                GifLoaderDialogUtils.showMyDialog(context);
                await model.updateCmcPresOrder(updatePresOrderRequestModel);
                if (model.state == ViewState.ErrorLocal) {
                  Utils.showErrorToast(model.error);
                  GifLoaderDialogUtils.hideDialog(context);
                } else {
                  AppToast.showSuccessToast(message: TranslationBase.of(context).processDoneSuccessfully);
                  await model.getCmcAllPresOrders();
                  GifLoaderDialogUtils.hideDialog(context);
                }
              });
            },
          ));
      return;
    }

    return AppScaffold(
      isShowAppBar: false,
      baseViewModel: model,
      body: model.cmcAllPresOrders.length > 0 ? ListView.separated(
          padding: EdgeInsets.all(21),
          physics: BouncingScrollPhysics(),
          itemBuilder: (context, index) {
            GetCMCAllOrdersResponseModel order = model.cmcAllPresOrders.reversed.toList()[index];

            int status = order.statusId;
            String _statusDisp = order.statusText;
            Color _color;
            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 Container(
              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),
                  ),
                ),
                // clipBehavior: Clip.antiAlias,
                child: 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).hospital + ": ",
                                style: TextStyle(fontSize: 12, fontWeight: FontWeight.w600, color: Color(0xff575757), letterSpacing: -0.4, height: 16 / 10),
                              ),
                              Expanded(
                                child: Text(
                                  // !projectViewModel.isArabic ? order.nearestProjectDescription.trim().toString() : order.nearestProjectDescriptionN.toString(),
                                  order.projectName != null ? order.projectName.trim().toString() : "",
                                  style: TextStyle(fontSize: 14, fontWeight: FontWeight.w600, color: Color(0xff2B353E), letterSpacing: -0.56),
                                ),
                              ),
                            ],
                          )
                        ],
                      ),
                    ),
                    Column(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      crossAxisAlignment: CrossAxisAlignment.end,
                      children: [
                        Text(
                          DateUtil.getDayMonthYearDateFormatted(DateTime.tryParse(order.created)),
                          style: TextStyle(fontSize: 12, fontWeight: FontWeight.w600, color: Color(0xff2B353E), letterSpacing: -0.4, height: 16 / 10),
                        ),
                        SizedBox(height: 12),
                        if (order.statusId == 1 || order.statusId == 2)
                          InkWell(
                            onTap: () {
                              showConfirmMessage(model, order);
                            },
                            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),
                              ),
                            ),
                          ),
                      ],
                    ),
                  ],
                ),
              ),
            );
          },
          separatorBuilder: (context, index) => SizedBox(height: 12),
          itemCount: model.cmcAllPresOrders.length) : getNoDataWidget(context),
    );
  }
}
