import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:test_sa/controllers/providers/api/ppm_provider.dart';
import 'package:test_sa/extensions/context_extension.dart';
import 'package:test_sa/extensions/int_extensions.dart';
import 'package:test_sa/extensions/string_extensions.dart';
import 'package:test_sa/extensions/text_extensions.dart';
import 'package:test_sa/extensions/widget_extensions.dart';
import 'package:test_sa/models/plan_preventive_visit/plan_preventive_visit_model.dart';
import 'package:test_sa/new_views/common_widgets/default_app_bar.dart';
import 'package:test_sa/views/widgets/loaders/app_loading.dart';
import 'package:test_sa/views/widgets/loaders/no_data_found.dart';
import 'package:test_sa/views/widgets/requests/request_status.dart';

import '../../../../../controllers/providers/api/user_provider.dart';
import '../../../../../models/enums/user_types.dart';
import '../../../../../new_views/app_style/app_color.dart';
import '../../../../../new_views/common_widgets/app_filled_button.dart';
import 'update_ppm/update_ppm.dart';

class PpmDetailsPage extends StatefulWidget {
  static const String id = "/visit-details";

  // final RequestsDetails request;
  final int requestId;

  const PpmDetailsPage({Key? key, required this.requestId}) : super(key: key);

  @override
  _PpmDetailsPageState createState() {
    return _PpmDetailsPageState();
  }
}

class _PpmDetailsPageState extends State<PpmDetailsPage> {
  UserProvider? userProvider;

  PpmProvider? ppmProvider;

  @override
  void initState() {
    getVisitData();
    super.initState();
  }

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

  Future<void> getVisitData() async {
    ppmProvider = Provider.of<PpmProvider>(context, listen: false);
    await ppmProvider?.getPlanPreventiveVisitById(widget.requestId);
  }

  @override
  Widget build(BuildContext context) {
    userProvider ??= Provider.of<UserProvider>(context, listen: false);
    // ppmProvider ??= Provider.of<PpmProvider>(context, listen: false);
    // ppmProvider!.getPlanPreventiveVisitById(widget.request.id!);
    return Scaffold(
      appBar: DefaultAppBar(title: context.translation.preventiveMaintenance),
      body: SafeArea(
        child: Consumer<PpmProvider>(builder: (context, ppmProvider, child) {
          PlanPreventiveVisit? planPreventiveVisit = ppmProvider.planPreventiveVisit;
          return ppmProvider.isLoading
              ? const ALoading()
              :planPreventiveVisit!=null? Column(children: [
                  SingleChildScrollView(
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Row(
                          children: [
                            // if (widget.request.priority != null)
                            //   StatusLabel(
                            //     label: widget.request.priority,
                            //     textColor: AppColor.getRequestStatusTextColorByName(context, widget.request.priority!),
                            //     backgroundColor: AppColor.getRequestStatusColorByName(context, widget.request.priority!),
                            //   ),
                           // if (planPreventiveVisit?.visitStatus != null) 8.width,
                            StatusLabel(
                              label: planPreventiveVisit?.visitStatus!.name!,
                              id: planPreventiveVisit?.visitStatus?.id??0,
                              textColor: AppColor.getRequestStatusTextColorByName(context, planPreventiveVisit.visitStatus!.name!),
                              backgroundColor: AppColor.getRequestStatusColorByName(context, planPreventiveVisit.visitStatus!.name!),
                            ),
                           // 1.width.expanded,
                          ],
                        ),
                        8.height,
                        planPreventiveVisit.assetName!.cleanupWhitespace.capitalizeFirstOfEach.heading5(context),
                        8.height,
                        '${context.translation.assetNumber}:  ${planPreventiveVisit.asset!.assetNumber}'.bodyText(context),
                        '${context.translation.assetSN}: ${planPreventiveVisit.asset!.assetSerialNo}'.bodyText(context),
                        '${context.translation.requestNo}: ${planPreventiveVisit.visitNo}'.bodyText(context),
                        const Divider().defaultStyle(context),
                        //     '${context.translation.expectDate}:  ${planPreventiveVisit.expectedDate?.toAssetDetailsFormat ?? ""}'.bodyText(context), //todo @baha,
                        '${context.translation.actualDate}: ${planPreventiveVisit.acutalDateOfVisit?.toIso8601String().toAssetDetailsFormat ?? ""}'.bodyText(context), //todo @baha,
                        const Divider().defaultStyle(context),
                        '${context.translation.assignedEmployee}:  ${planPreventiveVisit.assignedEmployee?.userName ?? ""}'.bodyText(context), //todo @baha,
                        '${context.translation.site}:  ${planPreventiveVisit.siteName?.cleanupWhitespace.capitalizeFirstOfEach}'.bodyText(context), //todo @baha,
                        '${context.translation.building}:  ${planPreventiveVisit.buildingName?.cleanupWhitespace.capitalizeFirstOfEach}'.bodyText(context), //todo @baha,
                        '${context.translation.floor}:  ${planPreventiveVisit.floorName?.cleanupWhitespace.capitalizeFirstOfEach}'.bodyText(context), //todo @baha,
                        '${context.translation.department}:  ${planPreventiveVisit.departmentName?.cleanupWhitespace.capitalizeFirstOfEach}'.bodyText(context), //todo @baha,
                        '${context.translation.room}:  ${(planPreventiveVisit.roomName ?? "").cleanupWhitespace.capitalizeFirstOfEach}'.bodyText(context), //todo @baha,
                      ],
                    ).toShadowContainer(context).paddingAll(16),
                  ).expanded,
                  if (userProvider!.user!.type == UsersTypes.engineer && (!ppmProvider.isReadOnly))...[
                    AppFilledButton(
                      onPressed: () async {
                        await Navigator.of(context).push(MaterialPageRoute(builder: (_) => UpdatePpm(ppm: null, planPreventiveVisit: planPreventiveVisit)));
                        getVisitData();
                      },
                      label: context.translation.updateWorkOrder,
                    ).paddingAll(16)
                   ]
                  else ...[
                    AppFilledButton(
                      onPressed: () async {
                        await Navigator.of(context).push(MaterialPageRoute(builder: (_) => UpdatePpm(ppm: null, planPreventiveVisit: planPreventiveVisit)));
                        getVisitData();
                      },
                      label: context.translation.viewDetails,
                    ).paddingAll(16)
                  ]
                ]):const Center(child:  NoDataFound());
        }),
      ),
    );
  }
}
