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/all_requests_and_count_model.dart';
import 'package:test_sa/new_views/common_widgets/default_app_bar.dart';
import 'package:test_sa/views/pages/user/ppm/update_ppm/edit_ppm.dart';
import 'package:test_sa/views/widgets/loaders/app_loading.dart';
import 'package:test_sa/views/widgets/loaders/no_item_found.dart';

import '../../../../controllers/providers/api/user_provider.dart';
import '../../../../models/enums/user_types.dart';
import '../../../../models/ppm/ppm.dart';
import '../../../../new_views/app_style/app_color.dart';
import '../../../../new_views/common_widgets/app_filled_button.dart';
import '../../../widgets/requests/request_status.dart';

class PpmDetailsPage extends StatelessWidget {
  static const String id = "/visit-details";
  final RequestsDetails request;

  const PpmDetailsPage({Key key, this.request}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    UserProvider userProvider = Provider.of<UserProvider>(context);
    PpmProvider ppmProvider = Provider.of<PpmProvider>(context);

    return Scaffold(
      appBar: DefaultAppBar(title: context.translation.preventiveMaintenance),
      body: SafeArea(
        child: FutureBuilder(
            future: ppmProvider.getPpmById(request.id),
            builder: (context, snap) {
              if (snap.connectionState == ConnectionState.waiting) {
                return const ALoading();
              } else if (snap.hasData) {
                Ppm ppm = snap.data;

                return Column(children: [
                  SingleChildScrollView(
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Row(
                          children: [
                            if (request.priority != null)
                              StatusLabel(
                                label: request.priority,
                                textColor: AppColor.getRequestStatusTextColorByName(context, request.priority),
                                backgroundColor: AppColor.getRequestStatusColorByName(context, request.priority),
                              ),
                            if (request.priority != null) 8.width,
                            StatusLabel(
                              label: ppm.visitStatusName,
                              id: ppm.deviceStatusId,
                              textColor: AppColor.getRequestStatusTextColorByName(context, ppm.visitStatusName),
                              backgroundColor: AppColor.getRequestStatusColorByName(context, ppm.visitStatusName),
                            ),
                            1.width.expanded,
                          ],
                        ),
                        8.height,
                        ppm.assetName.heading5(context),
                        8.height,
                        '${context.translation.assetNumber}:  ${ppm.assetNumber}'.bodyText(context),
                        '${context.translation.assetSN}: ${ppm.assetSerialNo}'.bodyText(context),
                        '${context.translation.code}: ${ppm.visitCode}'.bodyText(context),
                        const Divider().defaultStyle(context),
                        '${context.translation.expectDate}:  ${ppm.expectedDate?.toServiceRequestDetailsFormat}'.bodyText(context),
                        '${context.translation.actualDate}: ${ppm.actualDate?.toServiceRequestDetailsFormat}'.bodyText(context),
                        const Divider().defaultStyle(context),
                        '${context.translation.assignedTo}:  ${ppm.assignedToName}'.bodyText(context),
                        '${context.translation.engineerName}:  ${ppm.assignedEmployeeName}'.bodyText(context),
                        '${context.translation.site}:  ${ppm.siteName}'.bodyText(context),
                      ],
                    ).toShadowContainer(context).paddingAll(16),
                  ).expanded,
                  if (userProvider.user.type == UsersTypes.engineer && (ppm.visitStatusId != 270 || ppm.visitStatusId != 269))
                    AppFilledButton(
                      onPressed: () {
                        Navigator.of(context).push(MaterialPageRoute(builder: (_) => EditPpm(ppm: ppm, details: request)));
                      },
                      label: context.translation.updateRequest,
                    ).paddingAll(16)
                ]);
              }
              return NoItemFound(message: context.translation.noDataFound);
            }),
      ),
    );
  }
}
