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 createState() => _TenantCreateScreenState(); } class _TenantCreateScreenState extends State { final _formKey = GlobalKey(); final _firstNameCtrl = TextEditingController(); final _lastNameCtrl = TextEditingController(); final _orgNameCtrl = TextEditingController(); final _emailCtrl = TextEditingController(); final _contactCtrl = TextEditingController(); final _billingCtrl = TextEditingController(); final _orgSizeCtrl = TextEditingController(); final _industryIdCtrl = TextEditingController(); final _referenceCtrl = TextEditingController(); final TenantService _tenantService = TenantService(); bool _loading = false; // Values from subscription screen late final String planId; late final double amount; late final String planName; @override void initState() { super.initState(); final args = (Get.arguments ?? {}) as Map; planId = args['planId'] ?? ''; amount = (args['amount'] ?? 0).toDouble(); planName = args['planName'] ?? 'Subscription'; } @override void dispose() { _firstNameCtrl.dispose(); _lastNameCtrl.dispose(); _orgNameCtrl.dispose(); _emailCtrl.dispose(); _contactCtrl.dispose(); _billingCtrl.dispose(); _orgSizeCtrl.dispose(); _industryIdCtrl.dispose(); _referenceCtrl.dispose(); super.dispose(); } Future _submit() async { if (!_formKey.currentState!.validate()) 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": _industryIdCtrl.text.trim(), "reference": _referenceCtrl.text.trim(), }; setState(() => _loading = true); final resp = await _tenantService.createTenant(payload); setState(() => _loading = false); if (resp == null) { Get.snackbar("Error", "Failed to create tenant. Try again later.", backgroundColor: Colors.red, colorText: Colors.white); return; } final data = resp['data'] ?? resp; final tenantEnquireId = data['tenantEnquireId'] ?? data['id'] ?? data['tenantId']; if (tenantEnquireId == null) { logSafe("❌ Create tenant response missing id: $resp"); Get.snackbar("Error", "Tenant created but server didn't return id.", backgroundColor: Colors.red, colorText: Colors.white); return; } // ✅ Go to payment screen with all details Get.toNamed('/payment', arguments: { 'amount': amount, 'description': 'Subscription for ${_orgNameCtrl.text}', 'tenantEnquireId': tenantEnquireId, 'planId': planId, }); } @override Widget build(BuildContext context) { final divider = Divider(color: Colors.grey.shade300, height: 32); return Scaffold( appBar: AppBar( title: const Text("Create Tenant"), centerTitle: true, elevation: 0, backgroundColor: Colors.white, foregroundColor: Colors.black, ), body: SingleChildScrollView( padding: const EdgeInsets.all(16), child: Form( key: _formKey, child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ const Text( "Personal Info", style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold), ), const SizedBox(height: 12), TextFormField( controller: _firstNameCtrl, decoration: const InputDecoration( labelText: "First Name *", hintText: "e.g., John", prefixIcon: Icon(Icons.person_outline), border: OutlineInputBorder(), ), validator: (v) => v == null || v.isEmpty ? "Required" : null, ), const SizedBox(height: 12), TextFormField( controller: _lastNameCtrl, decoration: const InputDecoration( labelText: "Last Name *", hintText: "e.g., Doe", prefixIcon: Icon(Icons.person_outline), border: OutlineInputBorder(), ), validator: (v) => v == null || v.isEmpty ? "Required" : null, ), divider, const Text( "Organization Details", style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold), ), const SizedBox(height: 12), TextFormField( controller: _orgNameCtrl, decoration: const InputDecoration( labelText: "Organization Name *", hintText: "e.g., MarcoBMS", border: OutlineInputBorder(), ), validator: (v) => v == null || v.isEmpty ? "Required" : null, ), const SizedBox(height: 12), TextFormField( controller: _billingCtrl, decoration: const InputDecoration( labelText: "Billing Address", hintText: "e.g., 123 Main Street, Mumbai", border: OutlineInputBorder(), ), ), const SizedBox(height: 12), TextFormField( controller: _orgSizeCtrl, decoration: const InputDecoration( labelText: "Organization Size", hintText: "e.g., 1-10, 11-50", border: OutlineInputBorder(), ), ), const SizedBox(height: 12), TextFormField( controller: _industryIdCtrl, decoration: const InputDecoration( labelText: "Industry ID (UUID)", hintText: "e.g., 3fa85f64-5717-4562-b3fc-2c963f66afa6", border: OutlineInputBorder(), ), ), divider, const Text( "Contact Info", style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold), ), const SizedBox(height: 12), TextFormField( controller: _emailCtrl, decoration: const InputDecoration( labelText: "Email", hintText: "e.g., john.doe@example.com", prefixIcon: Icon(Icons.email_outlined), border: OutlineInputBorder(), ), validator: (v) => v == null || !v.contains('@') ? "Invalid email" : null, ), const SizedBox(height: 12), TextFormField( controller: _contactCtrl, decoration: const InputDecoration( labelText: "Contact Number", hintText: "e.g., +91 9876543210", prefixIcon: Icon(Icons.phone_outlined), border: OutlineInputBorder(), ), ), divider, const Text( "Other Details", style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold), ), const SizedBox(height: 12), TextFormField( controller: _referenceCtrl, decoration: const InputDecoration( labelText: "Reference", hintText: "e.g., Referral Name or Code", border: OutlineInputBorder(), ), ), const SizedBox(height: 24), Center( child: SizedBox( width: double.infinity, child: ElevatedButton( style: ElevatedButton.styleFrom( padding: const EdgeInsets.symmetric(vertical: 14), shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(10), ), ), onPressed: _loading ? null : _submit, child: _loading ? const SizedBox( height: 22, width: 22, child: CircularProgressIndicator( strokeWidth: 2, color: Colors.white, ), ) : const Text( "Create Tenant & Continue", style: TextStyle(fontSize: 16), ), ), ), ), ], ), ), ), ); } }