import 'dart:async';
import 'dart:developer';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:mmmarvellous/generated/l10n.dart';
import 'package:mmmarvellous/src/core/constans/enum_job_type.dart';
import 'package:mmmarvellous/src/core/utils/date_format.dart';
import 'package:mmmarvellous/src/feature/domain/models/regulars/assign_regular_entity.dart';
import 'package:mmmarvellous/src/core/constans/widgets.dart';
import 'package:mmmarvellous/src/feature/presentation/bloc/counter/counter_bloc.dart';
import 'package:mmmarvellous/src/feature/presentation/bloc/counter/counter_event.dart';
import 'package:mmmarvellous/src/feature/presentation/bloc/regular/regular_bloc.dart';
import 'package:mmmarvellous/src/feature/presentation/bloc/regular_request/regular_request_bloc.dart';
import 'package:mmmarvellous/src/feature/presentation/widgets/info_container.dart';
import 'package:mmmarvellous/src/feature/presentation/widgets/my_job_bottom_bar.dart';
import 'package:mmmarvellous/src/feature/presentation/widgets/new_job_bottom_bar.dart';
import 'package:mmmarvellous/src/feature/presentation/widgets/request_job_bottom_bar.dart';
class RegularDetailsPage extends StatefulWidget {
final AssignRegularEntity loaded;
final Enum regularType;
const RegularDetailsPage(
{super.key, required this.loaded, required this.regularType});
@override
State<RegularDetailsPage> createState() => _RegularDetailsPageState();
}
class _RegularDetailsPageState extends State<RegularDetailsPage> {
bool isActive = true;
// Functions
Future<void> _apply(BuildContext context, SnackBar snackBar) async {
ScaffoldMessenger.of(context).clearSnackBars();
setState(() {
isActive = false;
});
try {
final completer = Completer<dynamic>();
ScaffoldMessenger.of(context).showSnackBar(snackBar);
context.read<AssignRegularBloc>().add(SendApplyAssignRegularJobEvent(
id: widget.loaded.id, completer: completer));
await completer.future;
if (context.mounted) {
context.read<AssignRegularBloc>().add(FetchAssignRegularDataEvent());
context.read<CounterBloc>().add(const FetchCounterJobEvent());
}
} catch (e) {
log(e.toString());
}
}
Future<void> _requestDelete(BuildContext context, SnackBar snackBar) async {
ScaffoldMessenger.of(context).clearSnackBars();
setState(() {
isActive = false;
});
try {
final completer = Completer<dynamic>();
ScaffoldMessenger.of(context).showSnackBar(snackBar);
context.read<RegularRequestBloc>().add(SendRegularRequestDeleteEvent(
id: widget.loaded.id, completer: completer));
await completer.future;
if (context.mounted) {
context.read<RegularRequestBloc>().add(FetchRegularRequestEvent());
context.read<CounterBloc>().add(const FetchCounterJobEvent());
}
} catch (e) {
log(e.toString());
}
}
// Widgets
Widget dateAndLocationBox({required double width, required double height}) {
return InfoContainer(
width: width / 2.2,
height: height / 5,
child: ListView(
scrollDirection: Axis.vertical,
children: [
Text(S.of(context).need_from),
Text(
widget.loaded.from!.toMMMMdEEE,
style: const TextStyle(fontWeight: FontWeight.w600),
),
const SizedBox(height: 4),
Text(S.of(context).post_code),
Text(
'${widget.loaded.job!.client!.city}',
style: const TextStyle(fontWeight: FontWeight.w600),
),
Text(
'${widget.loaded.job!.client!.post}',
style: const TextStyle(fontWeight: FontWeight.w600),
),
],
),
);
}
Widget tasksBox({required double width, required double height}) {
return InfoContainer(
width: width / 2.2,
height: height / 5,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(S.of(context).tasks),
const SizedBox(height: 2),
Expanded(child: TasksWidget(loaded: widget.loaded)),
],
),
);
}
Widget detailsBox({required double width, required double height}) {
return InfoContainer(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
widget.regularType == RegularJobType.my
? Wrap(
direction: Axis.vertical,
children: [
Text(S.of(context).client),
Text(
widget.loaded.job!.client!.name!,
style: const TextStyle(fontWeight: FontWeight.w600),
),
const SizedBox(height: 12.0),
Text(S.of(context).address),
Text(
widget.loaded.job!.client!.address!,
style: const TextStyle(
fontWeight: FontWeight.w600,
),
),
const SizedBox(height: 12.0),
],
)
: const SizedBox(),
Row(
mainAxisAlignment: MainAxisAlignment.start,
children: [
SizedBox(
width: width / 4,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(S.of(context).hours),
JobDetailsHoursWidget(
loaded: widget.loaded.job!.jobDetails),
],
),
),
SizedBox(
width: width / 4,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(S.of(context).freq),
JobDetailsFreqWidget(loaded: widget.loaded.job!.jobDetails),
],
),
),
],
),
const SizedBox(height: 12.0),
Text(S.of(context).details),
Text(
'${widget.loaded.hkDetails}' != ''
? '${widget.loaded.hkDetails}'
: S.of(context).empty,
style: const TextStyle(fontWeight: FontWeight.w600),
),
],
),
);
}
Widget descriptionBox() {
return InfoContainer(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(S.of(context).job_description),
Text(
'${widget.loaded.job!.description}' != ''
? '${widget.loaded.job!.description}'
: S.of(context).empty,
style: const TextStyle(fontWeight: FontWeight.w600),
),
],
),
);
}
@override
Widget build(BuildContext context) {
double width = MediaQuery.of(context).size.width;
double height = MediaQuery.of(context).size.height;
final snackBar = SnackBar(
duration: const Duration(seconds: 4),
content: Text(
widget.regularType == RegularJobType.regular
? S.of(context).request_sent
: S.of(context).request_deleted,
style: const TextStyle(fontWeight: FontWeight.w600),
),
action: SnackBarAction(
label: S.of(context).snack_bar_action,
onPressed: () {},
),
);
return Scaffold(
appBar: AppBar(
title: Text(
S.of(context).regular_details,
style: const TextStyle(fontWeight: FontWeight.w600),
),
centerTitle: true,
),
body: Padding(
padding: const EdgeInsets.fromLTRB(12, 0, 12, 0),
child: ListView(
children: [
const SizedBox(height: 20.0),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
dateAndLocationBox(height: height, width: width),
tasksBox(height: height, width: width),
],
),
const SizedBox(height: 12.0),
detailsBox(width: width, height: height),
const SizedBox(height: 12.0),
widget.regularType == RegularJobType.my
? descriptionBox()
: const SizedBox(),
widget.regularType == RegularJobType.my
? const SizedBox(height: 12.0)
: const SizedBox(),
IssuesListFormatWidget(loaded: widget.loaded.job!.client!.issues),
const SizedBox(height: 12.0),
],
),
),
bottomNavigationBar: widget.regularType == RegularJobType.regular
? NewJobBottomBar(
height: height,
width: width,
onPressed: isActive ? () => _apply(context, snackBar) : null)
: widget.regularType == RegularJobType.request
? RequestJobBottomBar(
height: height,
width: width,
onPressed:
isActive ? () => _requestDelete(context, snackBar) : null)
: widget.regularType == RegularJobType.my
? MyJobBottomBar(
height: height,
width: width,
number: widget.loaded.job!.client!.mobile ?? '',
)
: null,
);
}
}