395 lines
12 KiB
Dart
395 lines
12 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:flutter/services.dart';
|
|
import 'package:get/get.dart';
|
|
import 'package:marco/controller/employee/add_employee_controller.dart';
|
|
import 'package:marco/controller/employee/employees_screen_controller.dart';
|
|
import 'package:marco/helpers/utils/mixins/ui_mixin.dart';
|
|
import 'package:marco/helpers/widgets/my_spacing.dart';
|
|
import 'package:marco/helpers/widgets/my_text.dart';
|
|
import 'package:marco/helpers/widgets/my_text_style.dart';
|
|
import 'package:marco/helpers/utils/base_bottom_sheet.dart';
|
|
import 'package:intl/intl.dart';
|
|
import 'package:marco/helpers/widgets/my_snackbar.dart';
|
|
|
|
class AddEmployeeBottomSheet extends StatefulWidget {
|
|
final Map<String, dynamic>? employeeData;
|
|
AddEmployeeBottomSheet({this.employeeData});
|
|
|
|
@override
|
|
State<AddEmployeeBottomSheet> createState() => _AddEmployeeBottomSheetState();
|
|
}
|
|
|
|
class _AddEmployeeBottomSheetState extends State<AddEmployeeBottomSheet>
|
|
with UIMixin {
|
|
late final AddEmployeeController _controller;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_controller = Get.put(
|
|
AddEmployeeController(),
|
|
tag: UniqueKey().toString(),
|
|
);
|
|
|
|
if (widget.employeeData != null) {
|
|
_controller.editingEmployeeData = widget.employeeData;
|
|
_controller.prefillFields();
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return GetBuilder<AddEmployeeController>(
|
|
init: _controller,
|
|
builder: (_) {
|
|
return BaseBottomSheet(
|
|
title: widget.employeeData != null ? "Edit Employee" : "Add Employee",
|
|
onCancel: () => Navigator.pop(context),
|
|
onSubmit: _handleSubmit,
|
|
child: Form(
|
|
key: _controller.basicValidator.formKey,
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
_sectionLabel("Personal Info"),
|
|
MySpacing.height(16),
|
|
_inputWithIcon(
|
|
label: "First Name",
|
|
hint: "e.g., John",
|
|
icon: Icons.person,
|
|
controller:
|
|
_controller.basicValidator.getController('first_name')!,
|
|
validator:
|
|
_controller.basicValidator.getValidation('first_name'),
|
|
),
|
|
MySpacing.height(16),
|
|
_inputWithIcon(
|
|
label: "Last Name",
|
|
hint: "e.g., Doe",
|
|
icon: Icons.person_outline,
|
|
controller:
|
|
_controller.basicValidator.getController('last_name')!,
|
|
validator:
|
|
_controller.basicValidator.getValidation('last_name'),
|
|
),
|
|
MySpacing.height(16),
|
|
_sectionLabel("Joining Details"),
|
|
MySpacing.height(16),
|
|
_buildDatePickerField(
|
|
label: "Joining Date",
|
|
value: _controller.joiningDate != null
|
|
? DateFormat("dd MMM yyyy")
|
|
.format(_controller.joiningDate!)
|
|
: "",
|
|
hint: "Select Joining Date",
|
|
onTap: () => _pickJoiningDate(context),
|
|
),
|
|
MySpacing.height(16),
|
|
_sectionLabel("Contact Details"),
|
|
MySpacing.height(16),
|
|
_buildPhoneInput(context),
|
|
MySpacing.height(24),
|
|
_sectionLabel("Other Details"),
|
|
MySpacing.height(16),
|
|
_buildDropdownField(
|
|
label: "Gender",
|
|
value: _controller.selectedGender?.name.capitalizeFirst ?? '',
|
|
hint: "Select Gender",
|
|
onTap: () => _showGenderPopup(context),
|
|
),
|
|
MySpacing.height(16),
|
|
_buildDropdownField(
|
|
label: "Role",
|
|
value: _controller.roles.firstWhereOrNull((role) =>
|
|
role['id'] == _controller.selectedRoleId)?['name'] ??
|
|
"",
|
|
hint: "Select Role",
|
|
onTap: () => _showRolePopup(context),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
},
|
|
);
|
|
}
|
|
|
|
Widget _requiredLabel(String text) {
|
|
return Row(
|
|
children: [
|
|
MyText.labelMedium(text),
|
|
const SizedBox(width: 4),
|
|
const Text("*", style: TextStyle(color: Colors.red)),
|
|
],
|
|
);
|
|
}
|
|
|
|
Widget _buildDatePickerField({
|
|
required String label,
|
|
required String value,
|
|
required String hint,
|
|
required VoidCallback onTap,
|
|
}) {
|
|
return Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
_requiredLabel(label),
|
|
MySpacing.height(8),
|
|
GestureDetector(
|
|
onTap: onTap,
|
|
child: AbsorbPointer(
|
|
child: TextFormField(
|
|
readOnly: true,
|
|
controller: TextEditingController(text: value),
|
|
validator: (val) {
|
|
if (val == null || val.trim().isEmpty) {
|
|
return "$label is required";
|
|
}
|
|
return null;
|
|
},
|
|
decoration: _inputDecoration(hint).copyWith(
|
|
suffixIcon: const Icon(Icons.calendar_today),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
],
|
|
);
|
|
}
|
|
|
|
Future<void> _pickJoiningDate(BuildContext context) async {
|
|
final picked = await showDatePicker(
|
|
context: context,
|
|
initialDate: _controller.joiningDate ?? DateTime.now(),
|
|
firstDate: DateTime(2000),
|
|
lastDate: DateTime(2100),
|
|
);
|
|
|
|
if (picked != null) {
|
|
_controller.setJoiningDate(picked);
|
|
_controller.update();
|
|
}
|
|
}
|
|
|
|
Future<void> _handleSubmit() async {
|
|
final isValid =
|
|
_controller.basicValidator.formKey.currentState?.validate() ?? false;
|
|
|
|
if (!isValid ||
|
|
_controller.joiningDate == null ||
|
|
_controller.selectedGender == null ||
|
|
_controller.selectedRoleId == null) {
|
|
showAppSnackbar(
|
|
title: "Missing Fields",
|
|
message: "Please complete all required fields.",
|
|
type: SnackbarType.warning,
|
|
);
|
|
return;
|
|
}
|
|
|
|
final result = await _controller.createOrUpdateEmployee();
|
|
|
|
if (result != null && result['success'] == true) {
|
|
final employeeController = Get.find<EmployeesScreenController>();
|
|
final projectId = employeeController.selectedProjectId;
|
|
|
|
if (projectId == null) {
|
|
await employeeController.fetchAllEmployees();
|
|
} else {
|
|
await employeeController.fetchEmployeesByProject(projectId);
|
|
}
|
|
|
|
employeeController.update(['employee_screen_controller']);
|
|
|
|
Navigator.pop(context, result['data']);
|
|
}
|
|
}
|
|
|
|
Widget _sectionLabel(String title) => Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
MyText.labelLarge(title, fontWeight: 600),
|
|
MySpacing.height(4),
|
|
Divider(thickness: 1, color: Colors.grey.shade200),
|
|
],
|
|
);
|
|
|
|
Widget _inputWithIcon({
|
|
required String label,
|
|
required String hint,
|
|
required IconData icon,
|
|
required TextEditingController controller,
|
|
required String? Function(String?)? validator,
|
|
}) {
|
|
return Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
_requiredLabel(label),
|
|
MySpacing.height(8),
|
|
TextFormField(
|
|
controller: controller,
|
|
validator: (val) {
|
|
if (val == null || val.trim().isEmpty) {
|
|
return "$label is required";
|
|
}
|
|
return validator?.call(val);
|
|
},
|
|
decoration: _inputDecoration(hint).copyWith(
|
|
prefixIcon: Icon(icon, size: 20),
|
|
),
|
|
),
|
|
],
|
|
);
|
|
}
|
|
|
|
Widget _buildPhoneInput(BuildContext context) {
|
|
return Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
_requiredLabel("Phone Number"),
|
|
MySpacing.height(8),
|
|
Row(
|
|
children: [
|
|
Container(
|
|
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 14),
|
|
decoration: BoxDecoration(
|
|
border: Border.all(color: Colors.grey.shade300),
|
|
borderRadius: BorderRadius.circular(12),
|
|
color: Colors.grey.shade100,
|
|
),
|
|
child: const Text("+91"),
|
|
),
|
|
MySpacing.width(12),
|
|
Expanded(
|
|
child: TextFormField(
|
|
controller:
|
|
_controller.basicValidator.getController('phone_number'),
|
|
validator: (value) {
|
|
if (value == null || value.trim().isEmpty) {
|
|
return "Phone Number is required";
|
|
}
|
|
if (value.trim().length != 10) {
|
|
return "Phone Number must be exactly 10 digits";
|
|
}
|
|
if (!RegExp(r'^\d{10}$').hasMatch(value.trim())) {
|
|
return "Enter a valid 10-digit number";
|
|
}
|
|
return null;
|
|
},
|
|
keyboardType: TextInputType.number,
|
|
inputFormatters: [
|
|
FilteringTextInputFormatter.digitsOnly,
|
|
LengthLimitingTextInputFormatter(10),
|
|
],
|
|
decoration: _inputDecoration("e.g., 9876543210").copyWith(
|
|
suffixIcon: IconButton(
|
|
icon: const Icon(Icons.contacts),
|
|
onPressed: () => _controller.pickContact(context),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
],
|
|
);
|
|
}
|
|
|
|
Widget _buildDropdownField({
|
|
required String label,
|
|
required String value,
|
|
required String hint,
|
|
required VoidCallback onTap,
|
|
}) {
|
|
return Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
_requiredLabel(label),
|
|
MySpacing.height(8),
|
|
GestureDetector(
|
|
onTap: onTap,
|
|
child: AbsorbPointer(
|
|
child: TextFormField(
|
|
readOnly: true,
|
|
controller: TextEditingController(text: value),
|
|
validator: (val) {
|
|
if (val == null || val.trim().isEmpty) {
|
|
return "$label is required";
|
|
}
|
|
return null;
|
|
},
|
|
decoration: _inputDecoration(hint).copyWith(
|
|
suffixIcon: const Icon(Icons.expand_more),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
],
|
|
);
|
|
}
|
|
|
|
InputDecoration _inputDecoration(String hint) {
|
|
return InputDecoration(
|
|
hintText: hint,
|
|
hintStyle: MyTextStyle.bodySmall(xMuted: true),
|
|
filled: true,
|
|
fillColor: Colors.grey.shade100,
|
|
border: OutlineInputBorder(
|
|
borderRadius: BorderRadius.circular(12),
|
|
borderSide: BorderSide(color: Colors.grey.shade300),
|
|
),
|
|
enabledBorder: OutlineInputBorder(
|
|
borderRadius: BorderRadius.circular(12),
|
|
borderSide: BorderSide(color: Colors.grey.shade300),
|
|
),
|
|
focusedBorder: OutlineInputBorder(
|
|
borderRadius: BorderRadius.circular(12),
|
|
borderSide: const BorderSide(color: Colors.blueAccent, width: 1.5),
|
|
),
|
|
contentPadding: MySpacing.all(16),
|
|
);
|
|
}
|
|
|
|
void _showGenderPopup(BuildContext context) async {
|
|
final selected = await showMenu<Gender>(
|
|
context: context,
|
|
position: _popupMenuPosition(context),
|
|
items: Gender.values.map((gender) {
|
|
return PopupMenuItem<Gender>(
|
|
value: gender,
|
|
child: Text(gender.name.capitalizeFirst!),
|
|
);
|
|
}).toList(),
|
|
);
|
|
|
|
if (selected != null) {
|
|
_controller.onGenderSelected(selected);
|
|
_controller.update();
|
|
}
|
|
}
|
|
|
|
void _showRolePopup(BuildContext context) async {
|
|
final selected = await showMenu<String>(
|
|
context: context,
|
|
position: _popupMenuPosition(context),
|
|
items: _controller.roles.map((role) {
|
|
return PopupMenuItem<String>(
|
|
value: role['id'],
|
|
child: Text(role['name']),
|
|
);
|
|
}).toList(),
|
|
);
|
|
|
|
if (selected != null) {
|
|
_controller.onRoleSelected(selected);
|
|
_controller.update();
|
|
}
|
|
}
|
|
|
|
RelativeRect _popupMenuPosition(BuildContext context) {
|
|
final RenderBox overlay =
|
|
Overlay.of(context).context.findRenderObject() as RenderBox;
|
|
return RelativeRect.fromLTRB(100, 300, overlay.size.width - 100, 0);
|
|
}
|
|
}
|