import 'package:doctor_app_flutter/config/config.dart';
import 'package:doctor_app_flutter/core/service/AnalyticsService.dart';
import 'package:doctor_app_flutter/core/viewModel/leave_rechdule_response.dart';
import 'package:doctor_app_flutter/core/viewModel/project_view_model.dart';
import 'package:doctor_app_flutter/core/viewModel/sick_leave_view_model.dart';
import 'package:doctor_app_flutter/locator.dart';
import 'package:doctor_app_flutter/screens/base/base_view.dart';
import 'package:doctor_app_flutter/screens/reschedule_leaves/reschedule_leave.dart';
import 'package:doctor_app_flutter/utils/date-utils.dart';
import 'package:doctor_app_flutter/utils/translations_delegate_base_utils.dart';
import 'package:doctor_app_flutter/widgets/patients/profile/add-order/addNewOrder.dart';
import 'package:doctor_app_flutter/widgets/shared/app_scaffold_widget.dart';
import 'package:doctor_app_flutter/widgets/shared/app_texts_widget.dart';
import 'package:doctor_app_flutter/widgets/shared/rounded_container_widget.dart';
import 'package:doctor_app_flutter/widgets/transitions/fade_page.dart';
import 'package:flutter/material.dart';
import 'package:hexcolor/hexcolor.dart';
import 'package:provider/provider.dart';

class AddRescheduleLeaveScreen extends StatelessWidget {
  ProjectViewModel? projectsProvider;

  @override
  Widget build(BuildContext context) {
    projectsProvider = Provider.of(context);
    return BaseView<SickLeaveViewModel>(
      onModelReady: (model) => {model.getRescheduleLeave(), model.getCoveringDoctors()},
      builder: (_, model, w) => AppScaffold(
          baseViewModel: model,
          isShowAppBar: true,
          appBarTitle: TranslationBase.of(context).rescheduleLeaves,
          body: SingleChildScrollView(
              child: Column(children: [
            Padding(
              padding: const EdgeInsets.all(8.0),
              child: AddNewOrder(
                onTap: () async {
                  await locator<AnalyticsService>().logEvent(
                    eventCategory: "Add Reschedule"
                        "Leave Screen",
                    eventAction: "apply For Reschedule",
                  );
                  openLeave(
                    context,
                    false,
                  );
                },
                label: TranslationBase.of(context).applyForReschedule,
              ),
            ),
            Column(
              children: model.getReschduleLeave.map<Widget>((GetRescheduleLeavesResponse item) {
                return Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: RoundedContainer(
                    child: Column(
                      children: [
                        Container(
                            decoration: BoxDecoration(
                                border: Border(
                                    left: BorderSide(
                              color: item.status == 10
                                  ? Colors.red[800]!
                                  : item.status == 2
                                      ? HexColor('#CC9B14')
                                      : item.status == 9
                                          ? AppGlobal.appGreenColor
                                          : Colors.red,
                              width: 5.0,
                            ))),
                            padding: EdgeInsets.only(left: 10, right: 10),
                            child: Row(
                              mainAxisAlignment: MainAxisAlignment.start,
                              children: <Widget>[
                                Expanded(
                                  flex: 4,
                                  child: Wrap(
                                    children: <Widget>[
                                      Column(
                                        crossAxisAlignment: CrossAxisAlignment.start,
                                        children: <Widget>[
                                          Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [
                                            Container(
                                              padding: EdgeInsets.all(3),
                                              margin: EdgeInsets.only(top: 10),
                                              child: AppText(
                                                item.statusDescription!,
                                                fontWeight: FontWeight.bold,
                                                color: item.status == 10
                                                    ? Colors.red[800]!
                                                    : item.status == 2
                                                        ? HexColor('#CC9B14')
                                                        : item.status == 9
                                                            ? AppGlobal.appGreenColor
                                                            : Colors.red,
                                                fontSize: 14,
                                              ),
                                            ),
                                            Padding(
                                                padding: EdgeInsets.only(top: 10),
                                                child: AppText(
                                                  AppDateUtils.convertStringToDateFormat(item.createdOn!, 'yyyy-MM-dd HH:mm'),
                                                  fontWeight: FontWeight.bold,
                                                ))
                                          ]),
                                          SizedBox(
                                            height: 5,
                                          ),
                                          Container(
                                              child: AppText(
                                            item.requisitionType == 1
                                                ? TranslationBase.of(context).offTime
                                                : item.requisitionType == 2
                                                    ? TranslationBase.of(context).holiday
                                                    : item.requisitionType == 3
                                                        ? TranslationBase.of(context).changeOfSchedule
                                                        : TranslationBase.of(context).newSchedule,
                                            fontWeight: FontWeight.bold,
                                          )),
                                          SizedBox(
                                            height: 5,
                                          ),
                                          Row(children: [
                                            AppText(TranslationBase.of(context).startDate),
                                            AppText(
                                              AppDateUtils.convertStringToDateFormat(item.dateTimeFrom!, 'yyyy-MM-dd HH:mm'),
                                              fontWeight: FontWeight.bold,
                                            )

                                            // overflow:
                                            //     TextOverflow.ellipsis,
                                          ]),

                                          // overflow:
                                          //     TextOverflow.ellipsis,

                                          SizedBox(
                                            height: 5,
                                          ),
                                          Row(
                                            children: [
                                              AppText(TranslationBase.of(context).endDate),
                                              AppText(
                                                AppDateUtils.convertStringToDateFormat(item.dateTimeTo!, 'yyyy-MM-dd HH:mm'),
                                                fontWeight: FontWeight.bold,
                                              )
                                            ],
                                          ),

                                          SizedBox(
                                            height: 5,
                                          ),
                                          model.coveringDoctors.length > 0
                                              ? Row(children: [
                                                  AppText(
                                                    TranslationBase.of(context).coveringDoctor,
                                                  ),
                                                  AppText(
                                                    getDoctor(model.coveringDoctors, item.coveringDoctorId),
                                                    fontWeight: FontWeight.bold,
                                                  )
                                                ])
                                              : SizedBox(),
                                          Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [
                                            Padding(padding: EdgeInsets.only(bottom: 5), child: AppText(getReasons(model.allReasons, item.reasonId))),
                                            (item.status == 2)
                                                ? IconButton(
                                                    icon: Image.asset('assets/images/edit.png'),
                                                    // color: AppGlobal.appGreenColor, //Colors.black,
                                                    onPressed: () => {openLeave(context, true, extendedData: item)},
                                                  )
                                                : SizedBox(),
                                          ]),
                                        ],
                                      ),
                                      SizedBox(
                                        width: 10,
                                      ),
                                    ],
                                  ),
                                ),
                              ],
                            )),
                      ],
                    ),
                  ),
                );
              }).toList(),
            )
          ]))),
    );
  }

  openLeave(BuildContext context, isExtend, {extendedData}) {
    Navigator.push(
      context,
      FadePage(
        page: RescheduleLeaveScreen(
          isExtend,
          extendedData,
        ),
      ),
    );
  }

  getDoctor(model, doctorId) {
    var obj;
    obj = model.where((i) => i['doctorID'].toString() == doctorId).toList();
    //print(obj);

    return obj.length > 0 ? obj[0]['doctorName'] : "";
  }

  getReasons(model, reasonID) {
    var obj;
    obj = model.where((i) => i['id'] == reasonID).toList();
    //print(obj);

    return obj.length > 0
        ? projectsProvider!.isArabic == true
            ? obj[0]['nameAr']
            : obj[0]['nameEn']
        : "";
  }
}
