import 'package:fis_ui/base_define/page.dart';
import 'package:flutter/material.dart';
import 'package:flyinsono/architecture/define.dart';
import 'package:fis_i18n/i18n.dart';
import 'package:fis_ui/index.dart';
import 'package:flyinsono/common_business.dart';
import 'package:flyinsono/views/desktop/appointment_consultation/view.dart';
import 'package:flyinsono/views/desktop/real_time_consultation/widgets/consultation_card.dart';
import 'package:flyinsono/views/desktop/real_time_consultation/widgets/main_content.dart';
import 'package:get/get.dart';

import 'index.dart';

/// 老的实时诊断的页面 后期应用到手机端
class RealTimeConsultationPage extends FisView<RealTimeConsultationController>
    implements FPage {
  RealTimeConsultationPage({Key? key}) : super(key: key);

  @override
  String get pageName => 'RealTimeConsultationPage';
  @override
  FWidget build(BuildContext context) {
    return _DesktopLayout();
  }
}

class _DesktopLayout extends FStatefulWidget implements FPage {
  @override
  FState<FStatefulWidget> createState() {
    return _DesktopLayoutState();
  }

  @override
  String get pageName => 'DesktopRealTimeConsultationPage';
}

class _DesktopLayoutState extends FState<_DesktopLayout> {
  final controller = Get.find<RealTimeConsultationController>();

  String roleType = '';
  bool isFirstGetRoleType = true; //用于判断是否第一次获取用户角色
  @override
  FWidget build(BuildContext context) {
    return _buildRealTimeConsultationMain(context);
  }

  ///获取用户角色类型
  Future<void> _getRoleType() async {
    isFirstGetRoleType = false;
    var result = await controller.getRoleType();
    roleType = result;
    setState(() {});
  }

  ///是否创建会诊申请按钮
  FWidget _buildAddButtonOrNot(BuildContext context) {
    if (isFirstGetRoleType) {
      _getRoleType();
    }
    if (roleType != RoleType.internshipDoctor) {
      //如果不是实习医生
      return _buildAddButton(context);
    } else {
      return FSizedBox();
    }
  }

  FWidget _buildRealTimeConsultationMain(BuildContext context) {
    return FContainer(
      color: Colors.grey.shade200,
      child: FRow(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          FContainer(
            decoration: const BoxDecoration(
              color: Colors.white,
            ),
            width: 400,
            child: FColumn(
              children: [
                const SearchInput(),
                FContainer(
                  margin: const EdgeInsets.only(
                    left: 20,
                    right: 20,
                  ),
                  child: FRow(
                    mainAxisAlignment: MainAxisAlignment.spaceAround,
                    children: [
                      _buildFilterButton(context),
                      //实习医生无法创建申请
                      _buildAddButtonOrNot(context),
                      _buildRefershButton(context),
                    ],
                  ),
                ),
                const FSizedBox(height: 7),
                const FPadding(
                  padding: EdgeInsets.symmetric(
                    horizontal: 20,
                  ),
                  child: FDivider(height: 1, color: Colors.grey),
                ),
                const FSizedBox(height: 7),
                FObx(
                  () => controller.state.consultationsRecordCardList.isEmpty
                      ? FContainer(
                          margin: const EdgeInsets.only(top: 80),
                          child: FColumn(
                            children: [
                              FCenter(
                                child: FImage.asset(
                                  "assets/images/no_data.png",
                                  width: 300,
                                  height: 300,
                                  fit: BoxFit.cover,
                                ),
                              ),
                              // const FText('暂无'),
                            ],
                          ),
                        )
                      : FExpanded(
                          child: _BuildCard(),
                        ),
                )
              ],
            ),
          ),
          FExpanded(
            child: FContainer(
              child: const FRealTimeConsultationMainContentView(),
            ),
          ),
        ],
      ),
    );
  }

  /// 筛选按钮
  FWidget _buildFilterButton(BuildContext context) {
    return FMouseRegion(
      cursor: SystemMouseCursors.click,
      child: FGestureDetector(
        onTap: () async {},
        businessParent: widget,
        name: '',
        child: FContainer(
          width: 90,
          child: FRow(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              FIcon(
                Icons.filter_alt_outlined,
                size: 24,
                color: Theme.of(context).primaryColor,
              ),
              FText(
                i18nBook.common.filter.t,
                style: TextStyle(
                  height: 1,
                  fontSize: 16,
                  color: Colors.black,
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }

  ///添加
  FWidget _buildAddButton(BuildContext context) {
    return FGestureDetector(
      onTap: () async {
        await Get.dialog(
          FAppointmentConsultationPage(
            realTimeConsultationController: controller,
          ),
        );
      },
      businessParent: widget,
      name: '',
      child: FContainer(
        decoration: const BoxDecoration(
          borderRadius: BorderRadius.all(
            Radius.circular(8.0),
          ), //圆角弧度
        ),
        width: 90,
        height: 34,
        child: FMouseRegion(
          cursor: SystemMouseCursors.click,
          child: FCenter(
            child: FRow(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                FIcon(
                  Icons.add_box_outlined,
                  size: 24,
                  color: Theme.of(context).primaryColor,
                ),
                FText(
                  i18nBook.realTimeConsultation.applyForAppointment.t,
                  style: TextStyle(
                    height: 1,
                    fontSize: 16,
                    color: Colors.black,
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }

  ///刷新按钮
  FWidget _buildRefershButton(BuildContext context) {
    return FGestureDetector(
      onTap: () {
        controller.refreshRealTimeConsultation();
      },
      businessParent: widget,
      name: '',
      child: FContainer(
        decoration: const BoxDecoration(
          borderRadius: BorderRadius.all(Radius.circular(8.0)), //圆角弧度
        ),
        width: 90,
        height: 34,
        child: FMouseRegion(
          cursor: SystemMouseCursors.click,
          child: FCenter(
            child: FRow(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                FIcon(
                  Icons.refresh,
                  size: 24,
                  color: Theme.of(context).primaryColor,
                ),
                FText(
                  i18nBook.common.refresh.t,
                  style: TextStyle(
                    height: 1,
                    fontSize: 16,
                    color: Colors.black,
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}

class SearchInput extends FisView<RealTimeConsultationController>
    implements FPage {
  const SearchInput({Key? key}) : super(key: key);

  @override
  String get pageName => 'SearchInput';
  @override
  FWidget build(BuildContext context) {
    return FContainer(
      margin: const EdgeInsets.symmetric(vertical: 10, horizontal: 20),
      color: Colors.white,
      child: FRow(
        children: [
          FExpanded(
            child: FContainer(
              height: 45,
              child: FRow(
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  FBorderInput(
                    hintSize: 16,
                    contentSize: 16,
                    maxLength: 20,
                    borderColor: const Color.fromARGB(255, 187, 180, 180),
                    suffixIcon: FMaterial(
                      color: Colors.transparent,
                      child: FIconButton(
                        onPressed: () {
                          // controller.patientInfoController.getPatients(true);
                        },
                        icon: const FIcon(
                          Icons.search,
                        ),
                        businessParent: this,
                        name: i18nBook.realTimeConsultation.record.t,
                      ),
                    ),
                    height: 55,
                    hintText: i18nBook.realTimeConsultation.searchRecords.t,
                    onChanged: (value) {
                      // controller.state.keyWord = value;
                    },
                  ),
                ],
              ),
            ),
          ),
        ],
      ),
    );
  }
}

class _BuildCard extends FisView<RealTimeConsultationController> {
  final ScrollController _scrollController = ScrollController();
  _BuildCard({Key? key}) : super(key: key);
  final fnIsCardActive = (Set<MaterialState> states) {
    return states.contains(MaterialState.hovered) ||
        states.contains(MaterialState.pressed);
  };

  @override
  FWidget build(BuildContext context) {
    return FContainer(
      child: FAlwaysScrollListView(
        scrollController: _scrollController,
        child: QuickFWidget(ScrollConfiguration(
          behavior: ScrollConfiguration.of(context).copyWith(scrollbars: false),
          child: FObx(() {
            return FListView.builder(
              controller: _scrollController,
              padding: EdgeInsets.only(right: 10),
              shrinkWrap: true,
              itemCount: controller.state.consultationsRecordCardList.length,
              itemBuilder: (context, index) {
                return FContainer(
                  child: FConsultationCard(
                    controller.state.consultationsRecordCardList[index],
                  ),
                );
              },
            );
          }),
        )),
      ),
    );
  }
}
