import 'package:fis_ui/index.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flyinsono/architecture/define.dart';
import 'package:flyinsono/architecture/router/router.dart';
import 'package:flyinsono/architecture/states/states.dart';
import 'package:flyinsono/managers/interfaces/user_info.dart';
import 'package:flyinsono/managers/server_config_manager.dart';
import 'package:flyinsono/pages/remedical/consultation/widgets/muti_select.dart';
import 'package:flyinsono/pages/remedical/consultation_record_view/controller.dart';
import 'package:flyinsono/pages/remedical/consultation_record_view/widgets/time_filter.dart';
import 'package:flyinsono/pages/user/widgets/search_input.dart';
import 'package:fis_i18n/i18n.dart';
import 'package:fis_jsonrpc/rpc.dart';
import 'package:fis_ui/widgets/layout/button_group.dart';
import 'package:fis_ui/values/paddings.dart';
import 'package:fis_ui/values/spacings.dart';
import 'package:fis_ui/base_define/page.dart';
import 'package:fis_theme/theme.dart';
import 'package:flyinsono/views/desktop/check_list_page_creation/bindings.dart';
import 'package:flyinsono/views/desktop/check_list_page_creation/view.dart';
import 'package:get/get.dart';
import 'package:intl/intl.dart';
import 'capture_live_view.dart';
import 'package:fis_common/index.dart';

import 'create_record.dart';

class ConsultationRecordFilter extends FisView<ConsultationRecordViewController>
    implements FPage {
  ConsultationRecordFilter({Key? key}) : super(key: key);

  final userInfoManager = Get.find<IUserInfoManager>();

  @override
  String get pageName => 'ConsultationRecordFilter';

  @override
  FWidget build(BuildContext context) {
    return FContainer(
      margin: EdgeInsets.symmetric(vertical: 10, horizontal: 15),
      child: FColumn(
        children: [
          FRow(
            children: [
              FContainer(
                width: 400,
                child: SearchInput(
                  businessParent: this,
                  onChanged: (value) {
                    controller.keyWord = value;
                  },
                  hintText: i18nBook.remedical.searchKeyword.t,
                  focusNode: controller.searchInputFocusNode,
                  serchOnPressed: () {
                    controller.searchFindRecordPages();
                  },
                ),
              ),
              const FSizedBox(width: 10),
              FInkWell(
                onTap: () {
                  controller.refreshData();
                },
                child: FIcon(
                  Icons.refresh,
                  color: FTheme.ins.colorScheme.primary,
                ),
              ),
              const FSizedBox(width: 10),
              FisBuilder<ConsultationRecordViewController>(
                  id: "record_more_filter_icon",
                  builder: (_) {
                    return FIconButton(
                      businessParent: this,
                      name: i18nBook.common.filter.t,
                      onPressed: () {
                        controller.openOrCloseMoreFilter();
                        controller.scaffoldKey.currentState?.openEndDrawer();
                      },
                      icon: FIcon(
                        Icons.filter_alt_rounded,
                        color: FTheme.ins.colorScheme.primary,
                      ),
                    );
                  }),
              FExpanded(child: FSizedBox()),
              if (Store.user.rootOrganizationCode?.isNotEmpty ?? false)
                _buildTextButton(
                  i18nBook.remedical.createCheck.t,
                  () async {
                    if (ServerConfigManager.isScreenshot &&
                        (CartSystemPlatform.isCartSystemWithOldPlatform)) {
                      await router.dialog(
                        () => CreateRecord(),
                      );
                      controller.refreshData();
                    } else {
                      router.dialog(
                        () => CheckListPageCreation(),
                        binding: CheckListPageCreationBinding(),
                      );
                    }
                  },
                ),
            ],
          ),
          FisBuilder<ConsultationRecordViewController>(
            id: "record_filter_tab",
            builder: (_) {
              return FContainer(
                margin: EdgeInsets.only(left: 15),
                child: FRow(
                  children: [
                    _tabRadio(
                      title: i18nBook.common.all.t,
                      value: RecordProcessStateEnum.All,
                    ),
                    _tabRadio(
                      title: i18nBook.remedical.pending.t,
                      value: RecordProcessStateEnum.Wait,
                    ),
                    _tabRadio(
                      title: i18nBook.remedical.completed.t,
                      value: RecordProcessStateEnum.Done,
                    ),
                    _tabRadio(
                      title: i18nBook.remedical.transferredOut.t,
                      value: RecordProcessStateEnum.ReferralOut,
                    ),
                  ],
                ),
              );
            },
          ),
        ],
      ),
    );
  }

  FWidget _tabRadio({
    required String title,
    required RecordProcessStateEnum value,
  }) {
    return FInkWell(
      onTap: () {
        controller.changeTypeFilter(value);
      },
      child: FContainer(
        margin: EdgeInsets.only(right: 15),
        child: FRow(
          children: [
            FRadio(
              value: value,
              groupValue: controller.typeFilter,
              onChanged: (v) {
                controller.changeTypeFilter(value);
              },
            ),
            FText(
              title,
              style: TextStyle(
                color: controller.typeFilter == value
                    ? FTheme.ins.colorScheme.primary
                    : FTheme.ins.colorScheme.text,
              ),
            ),
          ],
        ),
      ),
    );
  }

  /// 普通按钮
  FWidget _buildTextButton(
    String textString,
    Function onPressed, {
    MaterialStateProperty<Size?>? fixedSize,
    MaterialStateProperty<EdgeInsetsGeometry?>? textButtonPadding,
  }) {
    return FElevatedButton(
      name: textString,
      businessParent: this,
      onPressed: () => onPressed.call(),
      child: FText(
        textString,
        style: TextStyle(color: Colors.white),
      ),
      style: ButtonStyle(
        padding: textButtonPadding ??
            MaterialStateProperty.all(
              EdgeInsets.symmetric(
                vertical: 15,
                horizontal: 15,
              ),
            ),
        fixedSize: fixedSize,
      ),
    );
  }

  /// 边框按钮
  FWidget _buildTextOutlinedButton(String textString, Function onPressed) {
    return FOutlinedButton(
      name: textString,
      businessParent: this,
      onPressed: () => onPressed.call(),
      child: FText(
        textString,
        style: TextStyle(color: Colors.black),
      ),
      style: ButtonStyle(
        padding: MaterialStateProperty.all(
          EdgeInsets.symmetric(
            vertical: 15,
          ),
        ),
      ),
    );
  }
}
