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/data_model/reserve/table_model.dart';

typedef GetAreaTableListFunc = List<TableModel?> Function(String areaId);
typedef TableClickFunc = void Function(TableModel table);

class ReserveRightTableList extends StatelessWidget {
  final List<TableAreaModel?> areas;
  final GetAreaTableListFunc getAreaTableListFunc;
  final TableClickFunc? tableClickFunc;

  const ReserveRightTableList({
    super.key,
    required this.areas,
    required this.getAreaTableListFunc,
    this.tableClickFunc,
  });

  @override
  Widget build(BuildContext context) {
    return ListView.builder(
        itemBuilder: _buildTableAreaItem, itemCount: areas.length);
  }

  Widget _buildTableAreaItem(BuildContext context, int index) {
    TableAreaModel? area = areas[index];
    List<TableModel?> tables = getAreaTableListFunc(area?.id.toString() ?? "");
    return Column(
      mainAxisAlignment: MainAxisAlignment.start,
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        _buildTableAreaTitle(context, area, tables),
        const SizedBox(height: 5),
        _buildTableList(context, tables),
        const SizedBox(height: 10),
      ],
    );
  }

  Widget _buildTableAreaTitle(
      BuildContext context, TableAreaModel? area, List<TableModel?> tables) {
    return Row(
      crossAxisAlignment: CrossAxisAlignment.center,
      children: [
        Container(
          width: 2,
          height: 15,
          color: Colors.blue,
        ),
        const SizedBox(width: 10),
        Text(
          "${area?.name ?? ""}（${tables.length}）",
          style: const TextStyle(
            fontSize: 15,
            fontWeight: FontWeight.bold,
          ),
        ),
      ],
    );
  }

  Widget _buildTableList(BuildContext context, List<TableModel?> tables) {
    return Wrap(
      children: tables.map((e) => _buildTableItem(context, e)).toList(),
    );
  }

  Widget _buildTableItem(BuildContext context, TableModel? table) {
    bool isBooking = table?.bookingInfo != null;

    const itemNormalTextStyle = TextStyle(
      color: Color(0xff333333),
      fontSize: 12,
    );
    return GestureDetector(
      onTap: () {
        if (tableClickFunc != null) {
          tableClickFunc!(table!);
        }
      },
      child: Container(
        width: 103,
        height: 129,
        margin: const EdgeInsets.fromLTRB(0, 0, 10, 0),
        decoration: BoxDecoration(
          color: isBooking ? const Color(0xffFFF4DF) : Colors.white,
          borderRadius: BorderRadius.circular(5),
        ),
        child: Column(
          children: [
            Container(
              padding: const EdgeInsets.all(10),
              child: Column(
                children: [
                  Text(
                    table?.name ?? "",
                    style: const TextStyle(
                      color: Color(0xff333333),
                      fontSize: 16,
                    ),
                  ),
                  const SizedBox(height: 3),
                  if (!isBooking)
                    Text(
                      "${table?.maxCapacity ?? ""} 人",
                      style: itemNormalTextStyle,
                    ),
                  if (isBooking)
                    Text(
                      "${(table?.bookingInfo?.createUserName?.length ?? 0) > 3 ? '${table?.bookingInfo?.createUserName?.substring(0, 3)}...' : table?.bookingInfo?.createUserName} 订",
                      style: itemNormalTextStyle,
                      maxLines: 1,
                      overflow: TextOverflow.ellipsis,
                    ),
                  if (isBooking) const SizedBox(height: 3),
                  if (isBooking)
                    Text(
                      "${table?.bookingInfo?.bookingPerson ?? ""}(${table?.bookingInfo?.gender == 1 ? '先生' : '女士'})",
                      style: itemNormalTextStyle,
                    ),
                  if (isBooking) const SizedBox(height: 3),
                  if (isBooking)
                    Text(
                      "${table?.bookingInfo?.dinerNum ?? ""}人/(${table?.bookingInfo?.phoneNumber?.substring(7) ?? ""})",
                      style: itemNormalTextStyle,
                    ),
                ],
              ),
            ),
            Expanded(child: Container()),
            if (isBooking)
              Container(
                width: double.infinity,
                alignment: Alignment.center,
                decoration: const BoxDecoration(
                  color: Color(0xffF8AD13),
                  borderRadius: BorderRadius.only(
                    bottomLeft: Radius.circular(5),
                    bottomRight: Radius.circular(5),
                  ),
                ),
                child: const Row(
                  children: [
                    SizedBox(width: 5),
                    Text(
                      "新",
                      style: TextStyle(
                        color: Colors.white,
                        fontSize: 11,
                      ),
                    ),
                  ],
                ),
              ),
          ],
        ),
      ),
    );
  }
}
