import 'package:cashier_reserve/common/base/ui.dart';
import 'package:cashier_reserve/common/print/print.dart';
import 'package:cashier_reserve/data_model/reserve/table_area_model.dart';
import 'package:cashier_reserve/home/reserve_view_model.dart';

import 'reserve_right_table_list.dart';

class ReserveRightContentView extends StatelessWidget {
  final ReserveViewModel provider;
  final TabController? tabController;

  const ReserveRightContentView(
      {super.key, required this.provider, this.tabController});

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      width: double.infinity,
      child: Column(
        mainAxisAlignment: MainAxisAlignment.start,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          _buildTabBar(context),
          _buildTableListWidget(context),
        ],
      ),
    );
  }

  Widget _buildTabBar(BuildContext context) {
    if ((provider.tableAreaList?.length ?? 0) == 0) {
      return Container();
    }

    return TabBar(
      tabs:
          provider.tableAreaList!.map((e) => Tab(text: e?.name ?? '')).toList(),
      controller: tabController,
      isScrollable: true,
      labelColor: Colors.blue,
      unselectedLabelColor: Colors.grey,
      indicatorColor: Colors.blue,
      indicatorSize: TabBarIndicatorSize.label,
      indicatorWeight: 2,
      labelPadding: const EdgeInsets.only(left: 10, right: 10),
      onTap: (index) {
        provider.pageController.jumpToPage(index);
      },
    );
  }

  Widget _buildTableListWidget(BuildContext context) {
    if ((provider.tableAreaList?.length ?? 0) == 0) {
      return Container();
    }

    return Expanded(
      child: Container(
        padding: const EdgeInsets.only(left: 15, right: 15),
        color: const Color(0xFFF5F5F5),
        child: PageView(
          controller: provider.pageController,
          physics: const NeverScrollableScrollPhysics(),
          children: _buildTableList(context),
        ),
      ),
    );
  }

  List<Widget> _buildTableList(BuildContext context) {
    List<Widget> list = [];

    for (int i = 0; i < provider.tableAreaList!.length; i++) {
      List<TableAreaModel?> areas = [];
      if (i == 0) {
        /// 排除第一个全部
        areas.addAll(provider.tableAreaList!.sublist(1));
      } else {
        areas.add(provider.tableAreaList![i]);
      }

      list.add(
        ReserveRightTableList(
          areas: areas,
          getAreaTableListFunc: (areaId) {
            return provider.tableMap[areaId] ?? [];
          },
          tableClickFunc: (table) {
            yjPrint("table: ${table.name}");
            provider.clickTable(table);
          },
        ),
      );
    }

    return list;
  }
}
