import 'package:fis_i18n/i18n.dart';
import 'package:fis_ui/base_define/page.dart';
import 'package:fis_ui/index.dart';
import 'package:fis_ui/values/font_sizes.dart';
import 'package:flutter/material.dart';
import 'package:flyinsono/views/desktop/real_time_consultation/controller/real_time_consultation_controller.dart';
import 'package:get/get.dart';

class ConsultationVisitView extends FStatefulWidget implements FPage {
  const ConsultationVisitView({super.key});

  @override
  String get pageName => "ConsultationVisitView";

  @override
  FState<ConsultationVisitView> createState() => _ConsultationVisitViewState();
}

///会诊随访详情页
class _ConsultationVisitViewState extends FState<ConsultationVisitView> {
  final controller = Get.find<RealTimeConsultationController>();
  final scrollController = ScrollController();
  int currentPageIndex = 1;
  int totalPageCount = 1;

  bool get _isFirstPage => currentPageIndex == 1;

  bool get _isLastPage => currentPageIndex == totalPageCount;

  @override
  FWidget build(BuildContext context) {
    if (controller.state.followUpVisistInfoList.length > 0) {
      var occurredTime = controller.state
              .followUpVisistInfoList[currentPageIndex - 1].occurredTime ??
          DateTime.now();
      final window = WidgetsBinding.instance.window;
      final devicePixelRatio = window.devicePixelRatio;

      return FContainer(
        margin: EdgeInsets.only(
          left: 20,
          right: 20,
          top: 15,
        ),
        child: FFlex(
          direction: Axis.vertical,
          children: [
            FExpanded(
              child: FColumn(
                children: [
                  _buildCommonWidget(
                      i18nBook.common.fullName.t,
                      controller
                              .state
                              .followUpVisistInfoList[currentPageIndex - 1]
                              .patientName ??
                          ""),
                  _buildSpaceWidget(),
                  _buildCommonWidget(
                      i18nBook.common.mobileNumber.t,
                      controller
                              .state
                              .followUpVisistInfoList[currentPageIndex - 1]
                              .patientPhone ??
                          ""),
                  _buildSpaceWidget(),
                  _buildCommonWidget(
                      i18nBook.realTimeConsultation.followUpPerson.t,
                      controller
                              .state
                              .followUpVisistInfoList[currentPageIndex - 1]
                              .doctorName ??
                          ""),
                  _buildSpaceWidget(),
                  _buildCommonWidget(
                      i18nBook.realTimeConsultation.followUpTime.t,
                      occurredTime.toLocal().toString().substring(0, 16)),
                  _buildSpaceWidget(),
                  FFlex(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      FExpanded(
                        child: FText(
                          i18nBook.realTimeConsultation.followUpRecord.t,
                          style: const TextStyle(
                            fontWeight: FontWeight.bold,
                            fontSize: FFontSizes.H6,
                          ),
                          overflow: TextOverflow.ellipsis,
                        ),
                        flex: 1,
                      ),
                      FExpanded(
                        child: FTextField(
                          maxLines: devicePixelRatio > 1
                              ? devicePixelRatio > 1.25
                                  ? 6
                                  : 10
                              : 15,
                          scrollController: scrollController,
                          controller: TextEditingController(
                              text: controller
                                      .state
                                      .followUpVisistInfoList[
                                          currentPageIndex - 1]
                                      .generalCase ??
                                  ""),
                          style: const TextStyle(
                            fontSize: FFontSizes.H6,
                            color: Colors.black87,
                          ),
                          readOnly: true,
                          //maxLines: null,
                          decoration: InputDecoration(
                            border: InputBorder.none,
                          ),
                          //   overflow: TextOverflow.ellipsis,
                        ),
                        flex: 3,
                      ),
                    ],
                  ),
                  // _buildSpaceWidget(),
                ],
              ),
              flex: 11,
            ),
            _buildLine(),
            _buildBottomWidget(),
          ],
        ),
      );
    } else {
      return _mockEmptyView();
    }
  }

  @override
  void initState() {
    super.initState();
    totalPageCount = controller.state.followUpVisistInfoList.length;
  }

  ///创建底部widget
  FWidget _buildBottomWidget() {
    return FExpanded(
      child: FStack(
        children: [
          FText("$currentPageIndex / $totalPageCount"),
          FRow(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              _buildButtons(i18nBook.realTimeConsultation.previous.t,
                  this.widget, _isFirstPage ? null : _previous),
              _buildButtons(i18nBook.realTimeConsultation.next.t, this.widget,
                  _isLastPage ? null : _next),
            ],
          ),
        ],
      ),
    );
  }

  ///创建底部按钮
  FWidget _buildButtons(String buttonName, var widget, void Function()? func) {
    bool _disable = func == null;
    return FContainer(
      width: 100,
      height: 40,
      margin: const EdgeInsets.only(right: 15),
      child: FOutlinedButton(
        onPressed: func,
        style: ButtonStyle(
          padding: MaterialStateProperty.all(
            const EdgeInsets.symmetric(
              vertical: 8,
            ),
          ),
        ),
        businessParent: widget,
        name: buttonName,
        child: FCenter(
          child: FText(
            buttonName,
            style: TextStyle(color: _disable ? Colors.grey : Colors.black),
          ),
        ),
      ),
    );
  }

  FWidget _buildCommonWidget(String title, String contentText) {
    return FFlex(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        FExpanded(
          child: FText(
            title,
            style: const TextStyle(
              fontWeight: FontWeight.bold,
              fontSize: FFontSizes.H6,
            ),
            overflow: TextOverflow.ellipsis,
          ),
          flex: 1,
        ),
        FExpanded(
          child: FText(
            maxLines: 15,
            contentText,
            style: const TextStyle(
              fontSize: FFontSizes.H6,
              color: Colors.black87,
            ),
            overflow: TextOverflow.ellipsis,
          ),
          flex: 3,
        ),
      ],
    );
  }

  ///分隔线
  FWidget _buildLine() {
    return FContainer(
      margin: EdgeInsets.only(bottom: 10),
      color: Color.fromRGBO(216, 216, 216, 1),
      height: 0.5,
    );
  }

  ///行与行之间的间隔
  FWidget _buildSpaceWidget() {
    return FContainer(
      height: 20,
    );
  }

  //点击下一页
  void _next() {
    if (currentPageIndex < totalPageCount) {
      currentPageIndex++;
      setState(() {});
    }
  }

  ///点击上一页
  void _previous() {
    if (currentPageIndex > 1) {
      currentPageIndex--;
      setState(() {});
    }
  }

  ///缺省显示
  FWidget _mockEmptyView() {
    return FContainer(
      margin: EdgeInsets.only(top: 80),
      child: FColumn(
        children: [
          FCenter(
            child: FImage.asset(
              "assets/images/no_data.png",
              width: 300,
              height: 300,
              fit: BoxFit.cover,
            ),
          ),
          FText(i18nBook.common.noneDataTip.t),
        ],
      ),
    );
  }
}
