import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:on_field_work/controller/tenant/all_organization_controller.dart';
import 'package:on_field_work/helpers/widgets/my_text.dart';
import 'package:on_field_work/model/all_organization_model.dart';

class AllOrganizationListView extends StatelessWidget {
  final AllOrganizationController controller;

  /// Optional callback when an organization is tapped
  final void Function(AllOrganization)? onTapOrganization;

  const AllOrganizationListView({
    super.key,
    required this.controller,
    this.onTapOrganization,
  });

  Widget _loadingPlaceholder() {
    return ListView.separated(
      itemCount: 5, 
      separatorBuilder: (_, __) => const Divider(height: 1),
      itemBuilder: (context, index) {
        return Padding(
          padding: const EdgeInsets.symmetric(vertical: 8.0, horizontal: 16.0),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Container(
                width: 150,
                height: 14,
                color: Colors.grey.shade400,
              ),
              Container(
                width: 18,
                height: 18,
                decoration: BoxDecoration(
                  color: Colors.grey.shade400,
                  shape: BoxShape.circle,
                ),
              ),
            ],
          ),
        );
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return Obx(() {
      if (controller.isLoadingOrganizations.value) {
        return _loadingPlaceholder();
      }

      if (controller.organizations.isEmpty) {
        return Center(
          child: MyText.bodyMedium(
            "No organizations found",
            color: Colors.grey,
          ),
        );
      }

      return ListView.separated(
        itemCount: controller.organizations.length,
        separatorBuilder: (_, __) => const Divider(height: 1),
        itemBuilder: (context, index) {
          final org = controller.organizations[index];
          return ListTile(
            title: Text(org.name),
            onTap: () {
              if (onTapOrganization != null) {
                onTapOrganization!(org);
              }
            },
          );
        },
      );
    });
  }
}
