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/spacings.dart';
import 'package:flutter/material.dart';
import 'package:flyinsono/architecture/define.dart';
import 'package:flyinsono/architecture/router/router.dart';
import 'package:flyinsono/pages/consultation/controller.dart';
import 'package:flyinsono/pages/consultation/widgets/mobile_add_follow_up_page.dart';
import 'package:get/get.dart';

import 'light_title.dart';
import 'line_patient_data.dart';

class MobileFollowUpPage extends FisView<MobileConsultationController>
    implements FPage {
  @override
  String get pageName => 'FollowUpPage';

  @override
  FWidget build(BuildContext context) {
    var patientName =
        controller.state.currentConsultationDetail?.patientName ?? '';
    var patientDatas =
        controller.state.currentConsultationDetail?.patientDatas ?? [];
    var phone = patientDatas
        .firstWhereOrNull((element) => element.key == 'Phone')
        ?.value;
    return FScaffold(
      backgroundColor: Colors.grey[100],
      appBar: FAppBar.simple(
        title: i18nBook.realTimeConsultation.followUp.t,
        onBackClicked: () {
          router.back();
        },
        actions: [
          _buildTitleBar(phone, patientName),
        ],
      ),
      body: _buildMainView(patientName, phone),
    );
  }

  FObx _buildMainView(String patientName, String? phone) {
    return FObx(() {
      if (controller.state.isFollowUpInfoListEmpty) {
        // 没有随访记录
        return FContainer(
          child: FCenter(
            child: FText(
              i18nBook.common.noData.t,
              style: TextStyle(fontSize: 16, color: Colors.grey),
            ),
          ),
        );
      }
      var doctorName = controller
              .state
              .followUpVisistInfoList[controller.state.currentFollowUpIndex]
              .doctorName ??
          "";
      var occurredTime = controller
              .state
              .followUpVisistInfoList[controller.state.currentFollowUpIndex]
              .occurredTime ??
          DateTime.now();
      return FContainer(
        child: FColumn(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            LightTitle(i18nBook.realTimeConsultation.patientInformation.t),
            //姓名
            LinePatientData(i18nBook.common.fullName.t, patientName),
            //手机号
            LinePatientData(i18nBook.common.mobileNumber.t, phone),
            // 随访人
            LinePatientData(
                i18nBook.realTimeConsultation.followUpPerson.t, doctorName),
            // 随访时间
            LinePatientData(i18nBook.realTimeConsultation.followUpTime.t,
                occurredTime.toLocal().toString().substring(0, 16)),
            LightTitle(i18nBook.realTimeConsultation.followUpRecord.t),
            FExpanded(
              child: _buildFollowUpContent(),
            ),
            FObx(() {
              if (controller.state.followUpVisistInfoList.length > 1) {
                return _buildExamRecordOperationArea();
              } else {
                return FSizedBox();
              }
            }),
          ],
        ),
      );
    });
  }

  /// 构建标题栏
  FWidget _buildTitleBar(String? phone, String patientName) {
    return FElevatedButton(
      child: FText(i18nBook.common.add.t),
      onPressed: () async {
        controller.followUpController.text = '';
        await Get.to(() => AddFollowUpPage());
      },
      businessParent: this,
      name: 'add',
      style: ElevatedButton.styleFrom(
        elevation: 0,
      ),
    );
  }

  /// 随访记录翻页器
  FWidget _buildExamRecordOperationArea() {
    return FColumn(
      children: [
        FContainer(
          color: Colors.white,
          padding: EdgeInsets.symmetric(vertical: 8),
          child: FCenter(
            child: FText(
              '${controller.state.currentFollowUpIndex + 1}/${controller.state.followUpVisistInfoList.length}',
              style: TextStyle(fontSize: 16, color: Colors.grey),
            ),
          ),
        ),
        FRow(
          children: [
            FExpanded(
              flex: 1,
              child: FObx(
                () => FBottomSubmitButton(
                  businessParent: this,
                  name: "ExamRecord",
                  title: i18nBook.remedical.previousPage.t,
                  onPressed: controller.state.currentFollowUpIndex == 0
                      ? null
                      : () async {
                          controller.state.currentFollowUpIndex--;
                          controller.followUpController.text = controller
                                  .state
                                  .followUpVisistInfoList[
                                      controller.state.currentFollowUpIndex]
                                  .clinicalSituation ??
                              '';
                        },
                ),
              ),
            ),
            FExpanded(
              flex: 1,
              child: FObx(
                () => FBottomSubmitButton(
                  businessParent: this,
                  name: "ExamRecord",
                  title: i18nBook.remedical.nextPage.t,
                  onPressed: controller.state.currentFollowUpIndex + 1 ==
                          controller.state.followUpVisistInfoList.length
                      ? null
                      : () async {
                          controller.state.currentFollowUpIndex++;
                          controller.followUpController.text = controller
                                  .state
                                  .followUpVisistInfoList[
                                      controller.state.currentFollowUpIndex]
                                  .clinicalSituation ??
                              '';
                        },
                ),
              ),
            ),
          ],
        ),
      ],
    );
  }

  /// 随访内容
  FWidget _buildFollowUpContent() {
    return FContainer(
      width: double.infinity,
      color: Colors.white,
      padding: EdgeInsets.symmetric(
          horizontal: FSpacings.XXL, vertical: FSpacings.M),
      child: FListView(
        children: [
          FObx(() {
            return FText(
              controller
                      .state
                      .followUpVisistInfoList[
                          controller.state.currentFollowUpIndex]
                      .generalCase ??
                  '',
              style: TextStyle(
                color: Colors.black,
              ),
            );
          }),
        ],
      ),
    );
  }
}
