import 'package:flutter/material.dart';
import 'package:provider/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/widget_extensions.dart';
import 'package:test_sa/models/all_requests_and_count_model.dart';
import 'package:test_sa/models/enums/user_types.dart';
import 'package:test_sa/models/ppm/ppm.dart';
import 'package:test_sa/new_views/app_style/app_color.dart';
import 'package:test_sa/views/pages/user/ppm/ppm_details_page.dart';
import 'package:test_sa/views/pages/user/ppm/update_ppm/update_ppm.dart';

import '../../../controllers/providers/api/user_provider.dart';
import '../../../extensions/text_extensions.dart';
import '../requests/request_status.dart';

class PpmItem extends StatelessWidget {
  final Ppm ppm;
  final RequestsDetails? request;

  const PpmItem({Key? key, required this.ppm, this.request}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    UserProvider userProvider = Provider.of<UserProvider>(context);
    return Row(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        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!.toInt(),
                  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),
            16.height,
            Row(
              mainAxisSize: MainAxisSize.min,
              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)
              ],
            ).onPress(() {
              Navigator.of(context).push(MaterialPageRoute(builder: (_) => PpmDetailsPage(requestId: ppm.id!.toInt())));
            }),
          ],
        ).expanded,
        8.width,
        Column(
          crossAxisAlignment: CrossAxisAlignment.end,
          children: [
            if (userProvider.user!.type == UsersTypes.engineer && (ppm.deviceStatusId != 270 || ppm.deviceStatusId != 269))
              "edit".toSvgAsset(height: 48, width: 48).onPress(() {
              //  Navigator.of(context).push(MaterialPageRoute(builder: (_) => UpdatePpm(ppm: ppm, details: request, planPreventiveVisit: null,)));
              }),
            if (userProvider.user!.type == UsersTypes.engineer && (ppm.deviceStatusId != 270 || ppm.deviceStatusId != 269)) 8.height,
            Text(
              ppm.createdOn?.toServiceRequestCardFormat ?? "",
              textAlign: TextAlign.end,
              style: AppTextStyles.tinyFont.copyWith(color: context.isDark ? AppColor.neutral30 : AppColor.neutral50),
            ),
          ],
        )
      ],
    ).toShadowContainer(context).paddingOnly(bottom: 8);
  }
}
