import 'package:fis_jsonrpc/rpc.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:vitalapp/components/cell.dart';
import 'package:vitalapp/pages/controllers/crowd_labels.dart';

class CrowdLabelView extends GetView<CrowdLabelsController> {
  const CrowdLabelView({super.key});
  @override
  Widget build(BuildContext context) {
    return Container(
        padding: const EdgeInsets.only(bottom: 80),
        child: buildCrowdLables(context));
  }

  Widget buildCrowdLables(BuildContext context) {
    return Obx(() {
      final state = controller.state;
      return SingleChildScrollView(
        child: Column(
          children: [
            VListFormCellGroup(
              children: [
                ...state.normalOptions
                    .map((LabelDTO e) => buildItem(e, context))
                    .toList(),
                ...state.diseaseOptions
                    .map((LabelDTO e) => buildItem(e, context))
                    .toList(),
                ...state.specialCareOptions
                    .map((LabelDTO e) => buildItem(e, context))
                    .toList(),
              ],
            ),
          ],
        ),
      );
    });
  }

  Widget buildItem(LabelDTO dto, BuildContext context) {
    return InkWell(
      onTap: () {
        controller.onItemCheckChanged(dto.code!);
      },
      child: Row(
        children: [
          Expanded(
            child: VListFormCell(
              label: dto.labelName ?? '',
              contentWidget: Obx(
                () => Switch(
                  value: controller.state.selectedCodes.contains(dto.code),
                  onChanged: (value) {
                    controller.onItemCheckChanged(dto.code!);
                  },
                  activeColor: Theme.of(context).primaryColor,
                  inactiveThumbColor: Colors.grey,
                  // inactiveTrackColor:Colors.grey,
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }
}
