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

class DropdownSearchView extends StatefulWidget {
  const DropdownSearchView({super.key});

  @override
  State<DropdownSearchView> createState() => _DropdownSearchViewState();
}

class _DropdownSearchViewState extends State<DropdownSearchView> {
  DropdownSearchController controller = Get.put(DropdownSearchController());

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Search Menu')),
      body: Column(
        children: [
          // TabBar untuk memilih metode pencarian
          TabBar(
            controller: controller.tabController,
            tabs: [
              Tab(text: "BM25F"),
              Tab(text: "VSM"),
            ],
          ),
          Expanded(
            child: Padding(
              padding: const EdgeInsets.all(16.0),
              child: Column(
                children: [
                  Obx(() => Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          TextField(
                            controller: controller.searchController,
                            onChanged: controller.searchDropdown,
                            onEditingComplete: controller.searchMenu,
                            decoration: InputDecoration(
                              hintText: "Cari menu...",
                              border: OutlineInputBorder(),
                              suffixIcon: controller
                                      .searchController.text.isNotEmpty
                                  ? IconButton(
                                      icon: Icon(Icons.clear),
                                      onPressed: () {
                                        controller.searchController.clear();
                                        controller.searchResultsBM25F.clear();
                                        controller.searchResultsVSM.clear();
                                        controller.isDropdownVisible(false);
                                      },
                                    )
                                  : Icon(Icons.search),
                            ),
                          ),
                          if (controller.isDropdownVisible.value)
                            Container(
                              height: MediaQuery.of(context).size.height * 0.25,
                              margin: EdgeInsets.only(top: 4),
                              decoration: BoxDecoration(
                                color: Colors.white,
                                borderRadius: BorderRadius.circular(8),
                                border: Border.all(color: Colors.grey),
                              ),
                              child: ListView.builder(
                                itemCount:
                                    controller.selectedTabIndex.value == 0
                                        ? controller.searchResultsBM25F.length
                                        : controller.searchResultsVSM.length,
                                itemBuilder: (context, index) {
                                  final result =
                                      controller.selectedTabIndex.value == 0
                                          ? controller.searchResultsBM25F[index]
                                          : controller.searchResultsVSM[index];

                                  return ListTile(
                                    title: Text(result),
                                    onTap: () =>
                                        controller.onSelectMenu(result),
                                  );
                                },
                              ),
                            ),
                        ],
                      )),
                  SizedBox(height: 16),
                  Expanded(
                    child: Obx(() {
                      if (controller.isLoading.value) {
                        return Center(child: CircularProgressIndicator());
                      }
                      if (controller.menuResults.isEmpty) {
                        return Center(child: Text("Tidak ada hasil ditemukan"));
                      }
                      return ListView.builder(
                        itemCount: controller.menuResults.length,
                        itemBuilder: (context, index) {
                          final item = controller.menuResults[index];
                          return ListTile(
                            title: Text(item['nama']),
                            subtitle:
                                Text("Harga: ${item['harga'].toString()}"),
                          );
                        },
                      );
                    }),
                  ),
                ],
              ),
            ),
          ),
        ],
      ),
    );
  }
}
