import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:vitalapp/components/alert_dialog.dart';
import 'package:vitalapp/components/button.dart';
import 'package:vitalapp/pages/check/examination/controller.dart';
import 'package:vitalapp/pages/check/widgets/check_category_widget.dart';
import 'package:vitalapp/pages/check/widgets/configurable_card.dart';

class ExaminationPage extends GetView<ExaminationController> {
  const ExaminationPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GetBuilder<ExaminationController>(
      init: ExaminationController(),
      id: "examination",
      builder: (_) {
        return Scaffold(
          backgroundColor: const Color.fromRGBO(238, 238, 238, 1),
          // appBar: VAppBar(
          //   titleWidget: const Text('健康体检'),
          // ),
          body: Stack(
            children: [
              Scrollbar(
                thumbVisibility: true,
                child: SingleChildScrollView(
                  child: Center(
                    child: Container(
                      padding: const EdgeInsets.symmetric(vertical: 20),
                      child: buildExaminationList(),
                    ),
                  ),
                ),
              ),
              Positioned(
                right: 20,
                bottom: 50,
                child: SizedBox(
                  width: 200,
                  height: 70,
                  child: VButton(
                    label: '结束本轮体检',
                    onTap: () {
                      Get.dialog(VAlertDialog(
                        title: '提示',
                        content: Container(
                          margin: const EdgeInsets.only(bottom: 20),
                          child: const Text(
                            '当前体检是否已完成，请确定是否结束本次体检',
                            style: TextStyle(fontSize: 20),
                            textAlign: TextAlign.center,
                          ),
                        ),
                        showCancel: true,
                        onConfirm: () {
                          controller.deleteCached();
                        },
                      ));
                    },
                  ),
                ),
              ),
            ],
          ),
        );
      },
    );
  }

  Widget buildExaminationList() {
    return Wrap(
      spacing: 26,
      runSpacing: 26,
      children: controller.menuList
          .map((e) => CheckCategoryWidget(
                label: e.label,
                assetName: "${e.label}.png",
                onTap: () {
                  changePage(e.value);
                },
              ))
          .toList(),
    );
  }

  void changePage(String key) {
    Get.to(
      ConfigurableCard(
        cardKey: key,
        callBack: (key, templateCode, data) async {
          await controller.createExam(key, templateCode, data);
          Get.back();
        },
        patientCode: controller.patientCode,
      ),
      transition: Transition.rightToLeft,
    );
  }
}
