import 'package:flutter/material.dart';
import 'package:get/get.dart';
import '../controllers/tenants_management_controller.dart';
import 'add_tenant_view.dart';

class TenantsManagementView extends StatelessWidget {
  const TenantsManagementView({super.key});

  @override
  Widget build(BuildContext context) {
    final controller = Get.put(TenantsManagementController());
    final isDark = Theme.of(context).brightness == Brightness.dark;

    return Scaffold(
      appBar: AppBar(
        title: const Text('إدارة المكاتب المحاسبية', style: TextStyle(fontFamily: 'El Messiri')),
        centerTitle: true,
        backgroundColor: const Color(0xFF0F4C81),
        foregroundColor: Colors.white,
        elevation: 0,
        actions: [
          IconButton(
            icon: const Icon(Icons.add),
            onPressed: () {
              Get.to(() => const AddTenantView());
            },
          ),
        ],
      ),
      body: Obx(() {
        if (controller.isLoading.value) {
          return const Center(child: CircularProgressIndicator());
        }

        if (controller.tenants.isEmpty) {
          return Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Icon(Icons.account_balance_outlined, size: 80, color: Colors.grey.shade400),
                const SizedBox(height: 16),
                const Text('لا يوجد مكاتب محاسبية مسجلة', style: TextStyle(fontSize: 18, color: Colors.grey)),
              ],
            ),
          );
        }

        return RefreshIndicator(
          onRefresh: controller.fetchTenants,
          child: ListView.builder(
            padding: const EdgeInsets.all(16),
            itemCount: controller.tenants.length,
            itemBuilder: (context, index) {
              final tenant = controller.tenants[index];
              return Card(
                elevation: 2,
                margin: const EdgeInsets.only(bottom: 12),
                shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
                child: ListTile(
                  leading: CircleAvatar(
                    backgroundColor: const Color(0xFF0F4C81).withValues(alpha: 0.1),
                    child: const Icon(Icons.account_balance, color: Color(0xFF0F4C81)),
                  ),
                  title: Text(
                    tenant['name'] ?? 'مكتب محاسبي',
                    style: const TextStyle(fontWeight: FontWeight.bold),
                  ),
                  subtitle: Text(tenant['email'] ?? ''),
                  trailing: Container(
                    padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
                    decoration: BoxDecoration(
                      color: const Color(0xFF0F4C81).withValues(alpha: 0.1),
                      borderRadius: BorderRadius.circular(6),
                    ),
                    child: Text(
                      tenant['status'] ?? 'active',
                      style: const TextStyle(color: Color(0xFF0F4C81), fontSize: 12),
                    ),
                  ),
                ),
              );
            },
          ),
        );
      }),
    );
  }
}
