marco.pms.mobileapp/lib/model/dailyTaskPlaning/create_task_botom_sheet.dart

292 lines
12 KiB
Dart

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:marco/controller/task_planing/add_task_controller.dart';
import 'package:marco/helpers/widgets/my_text.dart';
import 'package:logger/logger.dart';
import 'package:marco/helpers/widgets/my_snackbar.dart';
final Logger log = Logger();
void showManageTaskBottomSheet({
required String workArea,
required String activity,
required String completedWork,
required String unit,
required Function(String) onCategoryChanged,
required String parentTaskId,
required int plannedTask,
required String activityId,
required String workAreaId,
required VoidCallback onSubmit,
}) {
final controller = Get.put(AddTaskController());
final TextEditingController plannedTaskController =
TextEditingController(text: plannedTask.toString());
final TextEditingController descriptionController = TextEditingController();
Get.bottomSheet(
StatefulBuilder(
builder: (context, setState) {
return LayoutBuilder(
builder: (context, constraints) {
final isLarge = constraints.maxWidth > 600;
final horizontalPadding =
isLarge ? constraints.maxWidth * 0.2 : 16.0;
return SafeArea(
child: Container(
constraints: const BoxConstraints(maxHeight: 760),
padding: EdgeInsets.fromLTRB(
horizontalPadding, 12, horizontalPadding, 24),
decoration: const BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.vertical(top: Radius.circular(20)),
),
child: SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Center(
child: Container(
width: 40,
height: 4,
margin: const EdgeInsets.only(bottom: 16),
decoration: BoxDecoration(
color: Colors.grey.shade400,
borderRadius: BorderRadius.circular(2),
),
),
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const SizedBox(width: 8),
MyText.titleMedium(
"Create Task",
fontWeight: 700,
),
],
),
const SizedBox(height: 16),
_infoRowWithIcon(
Icons.workspaces, "Selected Work Area", workArea),
_infoRowWithIcon(
Icons.list_alt, "Selected Activity", activity),
const SizedBox(height: 12),
_infoRowWithIcon(Icons.check_circle_outline,
"Completed Work", completedWork),
const SizedBox(height: 12),
Row(
children: [
Icon(Icons.edit_calendar,
color: Colors.grey[700], size: 18),
const SizedBox(width: 8),
MyText.bodyMedium("Planned Work", fontWeight: 600),
],
),
const SizedBox(height: 6),
TextField(
controller: plannedTaskController,
keyboardType: TextInputType.number,
decoration: InputDecoration(
hintText: "Enter planned work",
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(8)),
contentPadding: const EdgeInsets.symmetric(
horizontal: 12, vertical: 10),
),
),
const SizedBox(height: 16),
Row(
children: [
Icon(Icons.description_outlined,
color: Colors.grey[700], size: 18),
const SizedBox(width: 8),
MyText.bodyMedium("Comment", fontWeight: 600),
],
),
const SizedBox(height: 6),
TextField(
controller: descriptionController,
maxLines: 3,
decoration: InputDecoration(
hintText: "Enter task description",
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(8)),
contentPadding: const EdgeInsets.symmetric(
horizontal: 12, vertical: 10),
),
),
const SizedBox(height: 24),
Row(
children: [
Icon(Icons.category_outlined,
color: Colors.grey[700], size: 18),
const SizedBox(width: 8),
MyText.bodyMedium("Selected Work Category",
fontWeight: 600),
],
),
const SizedBox(height: 6),
Obx(() {
final categoryMap = controller.categoryIdNameMap;
final String selectedName =
controller.selectedCategoryId.value != null
? (categoryMap[
controller.selectedCategoryId.value!] ??
'Select Category')
: 'Select Category';
return Container(
width: double.infinity,
padding: const EdgeInsets.symmetric(
horizontal: 12, vertical: 14),
decoration: BoxDecoration(
border: Border.all(color: Colors.grey.shade300),
borderRadius: BorderRadius.circular(8),
),
child: PopupMenuButton<String>(
padding: EdgeInsets.zero,
onSelected: (val) {
controller.selectCategory(val);
onCategoryChanged(val);
},
itemBuilder: (context) => categoryMap.entries
.map(
(entry) => PopupMenuItem<String>(
value: entry.key,
child: Text(entry.value),
),
)
.toList(),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
selectedName,
style: const TextStyle(
fontSize: 14, color: Colors.black87),
),
const Icon(Icons.arrow_drop_down),
],
),
),
);
}),
const SizedBox(height: 24),
Row(
children: [
Expanded(
child: OutlinedButton(
onPressed: () {
Get.back();
},
style: OutlinedButton.styleFrom(
side: const BorderSide(color: Colors.grey),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10)),
),
child: MyText.bodyMedium(
"Cancel",
fontWeight: 600,
color: Colors.black,
),
),
),
const SizedBox(width: 12),
Expanded(
child: ElevatedButton(
onPressed: () async {
final plannedValue = int.tryParse(
plannedTaskController.text.trim()) ??
0;
final comment =
descriptionController.text.trim();
final assignmentDate = DateTime.now();
// 🪵 Log the task creation input values
log.i({
"message": "Creating task with data",
"parentTaskId": parentTaskId,
"plannedTask": plannedValue,
"comment": comment,
"workAreaId": workAreaId,
"activityId": activityId,
});
final selectedCategoryId =
controller.selectedCategoryId.value;
if (selectedCategoryId == null) {
showAppSnackbar(
title: "error",
message: "Please select a work category!",
type: SnackbarType.error,
);
return;
}
final success = await controller.createTask(
parentTaskId: parentTaskId,
plannedTask: plannedValue,
comment: comment,
workAreaId: workAreaId,
activityId: activityId,
categoryId:
selectedCategoryId,
);
if (success) {
Get.back();
onSubmit();
}
},
style: ElevatedButton.styleFrom(
backgroundColor: Colors.blueAccent,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10)),
),
child: MyText.bodyMedium(
"Submit",
fontWeight: 600,
color: Colors.white,
),
),
),
],
),
],
),
),
),
);
},
);
},
),
isScrollControlled: true,
);
}
Widget _infoRowWithIcon(IconData icon, String title, String value) {
return Padding(
padding: const EdgeInsets.symmetric(vertical: 6),
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Icon(icon, color: Colors.grey[700], size: 18),
const SizedBox(width: 8),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
MyText.bodyMedium(title, fontWeight: 600),
const SizedBox(height: 2),
MyText.bodySmall(value, color: Colors.grey[800]),
],
),
),
],
),
);
}