283 lines
12 KiB
Dart
283 lines
12 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:get/get.dart';
|
|
import 'package:marco/helpers/services/tenant_service.dart';
|
|
import 'package:marco/helpers/services/app_logger.dart';
|
|
|
|
class TenantCreateScreen extends StatefulWidget {
|
|
const TenantCreateScreen({super.key});
|
|
|
|
@override
|
|
State<TenantCreateScreen> createState() => _TenantCreateScreenState();
|
|
}
|
|
|
|
class _TenantCreateScreenState extends State<TenantCreateScreen> {
|
|
final _formKey = GlobalKey<FormState>();
|
|
final _firstNameCtrl = TextEditingController();
|
|
final _lastNameCtrl = TextEditingController();
|
|
final _orgNameCtrl = TextEditingController();
|
|
final _emailCtrl = TextEditingController();
|
|
final _contactCtrl = TextEditingController();
|
|
final _billingCtrl = TextEditingController();
|
|
final _orgSizeCtrl = TextEditingController();
|
|
final _referenceCtrl = TextEditingController();
|
|
|
|
final TenantService _tenantService = TenantService();
|
|
bool _loading = false;
|
|
|
|
List<Map<String, dynamic>> _industries = [];
|
|
String? _selectedIndustryId;
|
|
bool _loadingIndustries = true;
|
|
|
|
late final String planId;
|
|
late final double amount;
|
|
late final String planName;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
final args = (Get.arguments ?? {}) as Map<String, dynamic>;
|
|
planId = args['planId'] ?? '';
|
|
amount = (args['amount'] ?? 0).toDouble();
|
|
planName = args['planName'] ?? 'Subscription';
|
|
_fetchIndustries();
|
|
}
|
|
|
|
Future<void> _fetchIndustries() async {
|
|
try {
|
|
setState(() => _loadingIndustries = true);
|
|
final list = await _tenantService.getIndustries();
|
|
setState(() {
|
|
_industries = (list ?? []).whereType<Map<String, dynamic>>().toList();
|
|
_loadingIndustries = false;
|
|
});
|
|
} catch (e, s) {
|
|
logSafe("❌ Failed to fetch industries: $e\n$s", level: LogLevel.error);
|
|
setState(() => _loadingIndustries = false);
|
|
}
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_firstNameCtrl.dispose();
|
|
_lastNameCtrl.dispose();
|
|
_orgNameCtrl.dispose();
|
|
_emailCtrl.dispose();
|
|
_contactCtrl.dispose();
|
|
_billingCtrl.dispose();
|
|
_orgSizeCtrl.dispose();
|
|
_referenceCtrl.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
Future<void> _submit() async {
|
|
if (!_formKey.currentState!.validate()) return;
|
|
if (_selectedIndustryId == null || _selectedIndustryId!.isEmpty) {
|
|
Get.snackbar("Error", "Please select industry",
|
|
backgroundColor: Colors.red, colorText: Colors.white);
|
|
return;
|
|
}
|
|
|
|
final payload = {
|
|
"firstName": _firstNameCtrl.text.trim(),
|
|
"lastName": _lastNameCtrl.text.trim(),
|
|
"organizationName": _orgNameCtrl.text.trim(),
|
|
"email": _emailCtrl.text.trim(),
|
|
"contactNumber": _contactCtrl.text.trim(),
|
|
"billingAddress": _billingCtrl.text.trim(),
|
|
"organizationSize": _orgSizeCtrl.text.trim(),
|
|
"industryId": _selectedIndustryId,
|
|
"reference": _referenceCtrl.text.trim(),
|
|
};
|
|
|
|
setState(() => _loading = true);
|
|
final resp = await _tenantService.createTenant(payload);
|
|
if (!mounted) return;
|
|
setState(() => _loading = false);
|
|
|
|
if (resp == null || resp['success'] == false) {
|
|
Get.snackbar("Error",
|
|
resp?['message'] ?? "Failed to create tenant. Try again later.",
|
|
backgroundColor: Colors.red, colorText: Colors.white);
|
|
return;
|
|
}
|
|
|
|
final data = Map<String, dynamic>.from(resp['data'] ?? resp);
|
|
final tenantEnquireId =
|
|
data['tenantEnquireId'] ?? data['id'] ?? data['tenantId'];
|
|
|
|
if (tenantEnquireId == null) {
|
|
logSafe("❌ Missing tenant ID in response: $resp");
|
|
return;
|
|
}
|
|
|
|
Get.toNamed('/payment', arguments: {
|
|
'amount': amount,
|
|
'description': 'Subscription for ${_orgNameCtrl.text}',
|
|
'tenantEnquireId': tenantEnquireId,
|
|
'planId': planId,
|
|
});
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
resizeToAvoidBottomInset: true, // ensures scroll works with keyboard
|
|
appBar: AppBar(
|
|
title: const Text(
|
|
"Create Tenant",
|
|
style: TextStyle(fontWeight: FontWeight.bold, color: Colors.black),
|
|
),
|
|
centerTitle: true,
|
|
elevation: 0,
|
|
),
|
|
body: SafeArea(
|
|
child: LayoutBuilder(
|
|
builder: (context, constraints) {
|
|
return SingleChildScrollView(
|
|
physics: const BouncingScrollPhysics(),
|
|
padding: const EdgeInsets.all(20),
|
|
child: ConstrainedBox(
|
|
constraints: BoxConstraints(minHeight: constraints.maxHeight),
|
|
child: IntrinsicHeight(
|
|
child: Form(
|
|
key: _formKey,
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
const Text("Personal Info",
|
|
style: TextStyle(
|
|
fontSize: 18, fontWeight: FontWeight.w600)),
|
|
const Divider(height: 20),
|
|
_buildTextField(
|
|
_firstNameCtrl, "First Name *", Icons.person,
|
|
validator: (v) => v!.isEmpty ? "Required" : null),
|
|
_buildTextField(_lastNameCtrl, "Last Name *",
|
|
Icons.person_outline,
|
|
validator: (v) => v!.isEmpty ? "Required" : null),
|
|
const SizedBox(height: 12),
|
|
const Text("Organization",
|
|
style: TextStyle(
|
|
fontSize: 18, fontWeight: FontWeight.w600)),
|
|
const Divider(height: 20),
|
|
_buildTextField(_orgNameCtrl, "Organization Name *",
|
|
Icons.business,
|
|
validator: (v) => v!.isEmpty ? "Required" : null),
|
|
const SizedBox(height: 8),
|
|
_loadingIndustries
|
|
? const Center(
|
|
child: Padding(
|
|
padding: EdgeInsets.all(16.0),
|
|
child: CircularProgressIndicator(),
|
|
))
|
|
: Container(
|
|
decoration: BoxDecoration(
|
|
borderRadius: BorderRadius.circular(10),
|
|
border:
|
|
Border.all(color: Colors.grey.shade400),
|
|
),
|
|
padding: const EdgeInsets.symmetric(
|
|
horizontal: 12, vertical: 2),
|
|
child: DropdownButtonFormField<String>(
|
|
isExpanded: true,
|
|
borderRadius: BorderRadius.circular(10),
|
|
dropdownColor: Colors.white,
|
|
icon: const Icon(
|
|
Icons.keyboard_arrow_down_rounded),
|
|
value: _selectedIndustryId,
|
|
decoration: const InputDecoration(
|
|
prefixIcon:
|
|
Icon(Icons.apartment_outlined),
|
|
labelText: "Industry *",
|
|
border: InputBorder.none,
|
|
),
|
|
items: _industries.map((itm) {
|
|
final id = itm['id']?.toString() ?? '';
|
|
final name = itm['name'] ??
|
|
itm['displayName'] ??
|
|
'Unknown';
|
|
return DropdownMenuItem(
|
|
value: id, child: Text(name));
|
|
}).toList(),
|
|
onChanged: (v) =>
|
|
setState(() => _selectedIndustryId = v),
|
|
validator: (v) => v == null || v.isEmpty
|
|
? "Select industry"
|
|
: null,
|
|
),
|
|
),
|
|
const SizedBox(height: 16),
|
|
const Text("Contact Details",
|
|
style: TextStyle(
|
|
fontSize: 18, fontWeight: FontWeight.w600)),
|
|
const Divider(height: 20),
|
|
_buildTextField(
|
|
_emailCtrl, "Email *", Icons.email_outlined,
|
|
validator: (v) => v == null || !v.contains('@')
|
|
? "Invalid email"
|
|
: null),
|
|
_buildTextField(
|
|
_contactCtrl, "Contact Number", Icons.phone),
|
|
const SizedBox(height: 16),
|
|
const Text("Additional Info",
|
|
style: TextStyle(
|
|
fontSize: 18, fontWeight: FontWeight.w600)),
|
|
const Divider(height: 20),
|
|
_buildTextField(_billingCtrl, "Billing Address",
|
|
Icons.home_outlined),
|
|
_buildTextField(_orgSizeCtrl, "Organization Size",
|
|
Icons.people_alt_outlined),
|
|
_buildTextField(_referenceCtrl, "Reference",
|
|
Icons.note_alt_outlined),
|
|
const Spacer(),
|
|
const SizedBox(height: 24),
|
|
Center(
|
|
child: SizedBox(
|
|
width: double.infinity,
|
|
child: ElevatedButton(
|
|
onPressed: _loading ? null : _submit,
|
|
style: ElevatedButton.styleFrom(
|
|
padding:
|
|
const EdgeInsets.symmetric(vertical: 14),
|
|
shape: RoundedRectangleBorder(
|
|
borderRadius: BorderRadius.circular(10)),
|
|
backgroundColor: Colors.deepPurple,
|
|
),
|
|
child: _loading
|
|
? const CircularProgressIndicator(
|
|
color: Colors.white)
|
|
: const Text("Create Tenant & Continue",
|
|
style: TextStyle(fontSize: 16)),
|
|
),
|
|
),
|
|
),
|
|
]),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
},
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget _buildTextField(
|
|
TextEditingController controller, String label, IconData icon,
|
|
{String? Function(String?)? validator}) {
|
|
return Padding(
|
|
padding: const EdgeInsets.only(bottom: 12),
|
|
child: TextFormField(
|
|
controller: controller,
|
|
validator: validator,
|
|
decoration: InputDecoration(
|
|
prefixIcon: Icon(icon),
|
|
labelText: label,
|
|
border: OutlineInputBorder(
|
|
borderRadius: BorderRadius.circular(10),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|