//service request item page
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:test_sa/controllers/providers/settings/setting_provider.dart';
import 'package:test_sa/dashboard_latest/dashboard_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/models/new_models/dashboard_detail.dart';
import 'package:test_sa/service_request_latest/views/service_request_detail_main_view.dart';

import '../../../../views/widgets/requests/request_status.dart';
import '../../../app_style/app_color.dart';

class ServiceRequestItemView extends StatelessWidget {
  final Data? requestData;
  final RequestsDetails? requestDetails;
  final bool showShadow;
  final bool refreshData;

  const ServiceRequestItemView({Key? key, this.requestData, this.requestDetails, this.showShadow = true, this.refreshData = true}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    if (requestData != null) {
      return Column(
        mainAxisSize: MainAxisSize.min,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              StatusLabel(
                label: requestData!.priorityName!,
                textColor: AppColor.getRequestStatusTextColorByName(context, requestData!.priorityName!),
                backgroundColor: AppColor.getRequestStatusColorByName(context, requestData!.priorityName!),
              ),
              8.width,
              StatusLabel(
                label: requestData!.statusName!,
                textColor: AppColor.getHistoryLogStatusTextColorByName(requestData!.statusName!),
                backgroundColor: AppColor.getHistoryLogStatusColorByName(requestData!.statusName!),
              ),
              1.width.expanded,
              Text(requestData!.transactionDate!.toServiceRequestCardFormat,
                  textAlign: TextAlign.end, style: AppTextStyles.tinyFont.copyWith(color: context.isDark ? AppColor.neutral10 : AppColor.neutral50)),
            ],
          ),
          8.height,
          (requestData?.typeTransaction ?? context.translation.correctiveMaintenance).heading5(context),
          8.height,
          'CM Number: ${requestData!.requestNo}'.bodyText(context),
          '${context.translation.assetNumber}: ${requestData?.assetNumber}'.bodyText(context),
          '${context.translation.assetName}: ${requestData?.assetName?.cleanupWhitespace.capitalizeFirstOfEach}'.bodyText(context),
          //  '${context.translation.assetNumber}:  ${request.assetNo}'.bodyText(context),
          // '${context.translation.requestType}: ${requestData!.requestTypeName}'.bodyText(context),

          if (requestData?.statusName == 'Canceled') ...[
            '${context.translation.rejectionReason}: ${requestData?.rejectReason}'.bodyText(context),
          ],
          8.height,
          Row(
            // mainAxisSize: MainAxisSize.min,
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Text(
                context.translation.viewDetails,
                style: AppTextStyles.bodyText.copyWith(color: AppColor.blueStatus(context)),
              ),
              4.width,
              Icon(
                Icons.arrow_forward,
                color: AppColor.blueStatus(context),
                size: 14,
              )
            ],
          ),
        ],
      ).toShadowContainer(context, withShadow: showShadow).onPress(() async {
        await Navigator.of(context).push(MaterialPageRoute(builder: (_) => ServiceRequestDetailMain(requestId: requestData!.id!)));
        if (refreshData) {
          Provider.of<DashBoardProvider>(context, listen: false).refreshDashboard(userType: Provider.of<SettingProvider>(context, listen: false).user!.type!, context: context);
        }
      });
    }
    if (requestDetails != null) {
      return Column(
        mainAxisSize: MainAxisSize.min,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              StatusLabel(
                label: requestDetails!.priority,
                textColor: AppColor.getRequestStatusTextColorByName(context, requestDetails!.priority),
                backgroundColor: AppColor.getRequestStatusColorByName(context, requestDetails!.priority),
              ),
              8.width,
              StatusLabel(
                label: requestDetails!.status,
                textColor: AppColor.getRequestStatusTextColorByName(context, requestDetails!.status),
                backgroundColor: AppColor.getRequestStatusColorByName(context, requestDetails!.status),
              ),
              1.width.expanded,
              Text(requestDetails!.date!.toServiceRequestCardFormat, textAlign: TextAlign.end, style: AppTextStyles.tinyFont.copyWith(color: context.isDark ? AppColor.neutral10 : AppColor.neutral50)),
            ],
          ),
          8.height,
          (requestDetails!.nameOfType ?? context.translation.correctiveMaintenance).heading5(context),
          8.height,

          // '${context.translation.requestType}: ${requestDetails!.requestType}'.bodyText(context),
          'CM Number: ${requestDetails!.requestNo}'.bodyText(context),
          '${context.translation.assetNumber}:  ${requestDetails!.assetNo}'.bodyText(context),
          '${context.translation.assetName}: ${requestDetails!.assetName?.cleanupWhitespace.capitalizeFirstOfEach}'.bodyText(context),

          8.height,
          Row(
            // mainAxisSize: MainAxisSize.min,
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Text(
                context.translation.viewDetails,
                style: AppTextStyles.bodyText.copyWith(color: AppColor.blueStatus(context)),
              ),
              4.width,
              Icon(
                Icons.arrow_forward,
                color: AppColor.blueStatus(context),
                size: 14,
              )
            ],
          ),
        ],
      ).toShadowContainer(context, withShadow: showShadow).onPress(() {
        Navigator.of(context).push(MaterialPageRoute(builder: (_) => ServiceRequestDetailMain(requestId: requestDetails!.id!)));
      });
    }

    return SizedBox();
  }
}
