//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/modules/cm_module/cm_detail_page.dart';
import 'package:test_sa/new_views/app_style/app_color.dart';
import 'package:test_sa/views/widgets/item_views/info_date_widget.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/requests/request_status.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: [
              if (Provider.of<SettingProvider>(context, listen: false).showPriority && requestData?.priorityName != null && requestData!.priorityName!.isNotEmpty) ...[
                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(context, requestData!.statusName!),
                backgroundColor: AppColor.getHistoryLogStatusColorByName(context, requestData!.statusName!),
              ),
              1.width.expanded,
              InfoDateWidget(requestData!.transactionDate!),
            ],
          ),
          InfoHeaderWidget(requestData?.typeTransaction ?? context.translation.correctiveMaintenance),
          InfoTextWidget(label: "CM Number", value: requestData!.requestNo),
          InfoTextWidget(label: context.translation.assetNumber, value: requestData!.assetNumber),
          InfoTextWidget(label: context.translation.assetName, value: requestData?.assetName?.cleanupWhitespace.capitalizeFirstOfEach, showArrow: requestData?.statusName == 'Canceled' ? false : true),
          if (requestData?.statusName == 'Canceled') ...[
            InfoTextWidget(label: context.translation.rejectionReason, value: requestData?.rejectReason, showArrow: true),
          ],
        ],
      ).toShadowContainer(context, withShadow: showShadow).onPress(() async {
        await Navigator.of(context).push(MaterialPageRoute(builder: (_) => CMDetailPage(requestId: requestData!.id!, moduleId: requestData!.transactionNo!)));
        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,
        spacing: 1,
        children: [
          Row(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              if (Provider.of<SettingProvider>(context, listen: false).showPriority && requestDetails!.priority != null && requestDetails!.priority!.isNotEmpty) ...[
                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,
              InfoDateWidget(requestDetails!.date!),
            ],
          ),
          InfoHeaderWidget(requestDetails?.nameOfType ?? context.translation.correctiveMaintenance),
          InfoTextWidget(label: "CM Number", value: requestDetails!.requestNo),
          InfoTextWidget(label: context.translation.assetNumber, value: requestDetails!.assetNo),
          InfoTextWidget(label: context.translation.serialNo, value: requestDetails!.assetSN, showEmptyValue: true),
          InfoTextWidget(label: context.translation.assetName, value: requestDetails!.assetName?.cleanupWhitespace.capitalizeFirstOfEach, showArrow: true),
        ],
      ).toShadowContainer(context, withShadow: showShadow).onPress(() {
        Navigator.of(context).push(MaterialPageRoute(builder: (_) => CMDetailPage(requestId: requestDetails!.id!, moduleId: requestDetails!.transactionType!)));
      });
    }

    return SizedBox();
  }
}
