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/modules/cm_module/views/components/action_button/footer_action_button.dart';
import 'package:test_sa/new_views/common_widgets/default_app_bar.dart';
import 'package:test_sa/views/widgets/item_views/info_header_widget.dart';
import 'package:test_sa/views/widgets/item_views/info_text_widget.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: Consumer<PpmProvider>(builder: (context, ppmProvider, child) {
        PlanPreventiveVisit? planPreventiveVisit = ppmProvider.planPreventiveVisit;
        return ppmProvider.isLoading
            ? const ALoading()
            : planPreventiveVisit != null
                ? Column(children: [
                    SingleChildScrollView(
                      padding: const EdgeInsets.all(16),
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Row(
                            children: [
                              StatusLabel(
                                label: planPreventiveVisit.visitStatus!.name!,
                                id: planPreventiveVisit.visitStatus?.id ?? 0,
                                textColor: AppColor.getRequestStatusTextColorByName(context, planPreventiveVisit.visitStatus!.name!),
                                backgroundColor: AppColor.getRequestStatusColorByName(context, planPreventiveVisit.visitStatus!.name!),
                              ),
                            ],
                          ),
                          InfoHeader16Widget(planPreventiveVisit.assetName?.cleanupWhitespace.capitalizeFirstOfEach ?? ''),
                          InfoTextWidget(label: context.translation.assetNumber, value: planPreventiveVisit.asset?.assetNumber ?? '-'),
                          InfoTextWidget(label: context.translation.assetSN, value: planPreventiveVisit.asset?.assetSerialNo ?? '-'),
                          InfoTextWidget(label: context.translation.requestNo, value: planPreventiveVisit.visitNo ?? '-', copyValue: true),
                          if (planPreventiveVisit.acutalDateOfVisit != null) ...[
                            const Divider().defaultStyle(context),
                            InfoTextWidget(label: context.translation.actualDate, value: planPreventiveVisit.acutalDateOfVisit?.toIso8601String().toAssetDetailsFormat ?? ""),
                          ],
                          const Divider().defaultStyle(context),
                          InfoTextWidget(label: context.translation.assignedEmployee, value: planPreventiveVisit.assignedEmployee?.userName ?? ""),
                          InfoTextWidget(label: context.translation.site, value: planPreventiveVisit.siteName?.cleanupWhitespace.capitalizeFirstOfEach ?? '-'),
                          InfoTextWidget(label: context.translation.building, value: planPreventiveVisit.buildingName?.cleanupWhitespace.capitalizeFirstOfEach ?? '-'),
                          InfoTextWidget(label: context.translation.floor, value: planPreventiveVisit.floorName?.cleanupWhitespace.capitalizeFirstOfEach ?? '-'),
                          InfoTextWidget(label: context.translation.department, value: planPreventiveVisit.departmentName?.cleanupWhitespace.capitalizeFirstOfEach ?? '-'),
                          InfoTextWidget(label: context.translation.room, value: (planPreventiveVisit.roomName ?? "").cleanupWhitespace.capitalizeFirstOfEach),
                        ],
                      ).toShadowContainer(context),
                    ).expanded,
                    if (userProvider!.user!.type == UsersTypes.engineer && (!ppmProvider.isReadOnly)) ...[
                      FooterActionButton.footerContainer(
                        child: AppFilledButton(
                          onPressed: () async {
                            await Navigator.of(context).push(MaterialPageRoute(builder: (_) => UpdatePpm(ppm: null, planPreventiveVisit: planPreventiveVisit)));
                            getVisitData();
                          },
                          label: context.translation.updateWorkOrder,
                        ),
                        context: context,
                      )
                    ] else ...[
                      // FooterActionButton.footerContainer(
                      //   context: context,
                      //   child: AppFilledButton(
                      //     onPressed: () async {
                      //       await Navigator.of(context).push(MaterialPageRoute(builder: (_) => UpdatePpm(ppm: null, planPreventiveVisit: planPreventiveVisit)));
                      //       getVisitData();
                      //     },
                      //     label: context.translation.viewDetails,
                      //   ),
                      // )
                    ]
                  ])
                : const Center(child: NoDataFound());
      }),
    );
  }
}
