import 'package:fis_i18n/i18n.dart';
import 'package:fis_jsonrpc/rpc.dart';
import 'package:fis_theme/theme.dart';
import 'package:fis_ui/index.dart';
import 'package:fis_ui/values/font_sizes.dart';
import 'package:flutter/material.dart';
import 'package:flyinsono/architecture/router/router.dart';
import 'package:flyinsono/views/desktop/real_time_consultation/controller/real_time_consultation_controller.dart';
import 'package:fis_common/index.dart';
import 'package:flyinsono/views/desktop/real_time_consultation/widgets/disable_space_input_wrapper.dart';

class AddFollowUpVisitView extends FStatefulWidget {
  AddFollowUpVisitView({
    super.key,
    required this.realTimeConsultationController,
  });
  final RealTimeConsultationController realTimeConsultationController;

  @override
  FState<AddFollowUpVisitView> createState() => _AddFollowUpVisitViewState();
}

///添加会诊随访
class _AddFollowUpVisitViewState extends FState<AddFollowUpVisitView> {
  late final controller = widget.realTimeConsultationController;
  AddFollowUpVisitInfoRequest addFollowUpVisitInfoRequest =
      new AddFollowUpVisitInfoRequest();
  String visitRecord = "";
  final textInputController = TextEditingController();
  @override
  FWidget build(BuildContext context) {
    return _buildMainContent();
  }

  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.isNotNullOrEmpty
                ? contentText
                : i18nBook.remedical.notFill.t,
            style: const TextStyle(
              fontSize: FFontSizes.H6,
              color: Colors.black87,
            ),
            overflow: TextOverflow.ellipsis,
          ),
          flex: 3,
        ),
      ],
    );
  }

  FWidget _buildMainContent() {
    if (controller.state.consultationsRecordInfos.patientDatas == null) {
      _getConsultationDetail();
    }
    final patientDatas =
        controller.state.consultationsRecordInfos.patientDatas ?? [];
    Map<String, String> patientValues =
        controller.getPatientValue(patientDatas);
    return FSimpleDialog(
      title: FText(
        i18nBook.realTimeConsultation.followUp.t,
        style: TextStyle(
          color: Colors.white,
          fontSize: FFontSizes.H5,
        ),
      ),
      okString: i18nBook.common.confirm.t,
      cancelString: i18nBook.common.cancel.t,
      isDefault: true,
      children: [
        FContainer(
          width: 600,
          height: 400,
          margin: EdgeInsets.only(left: 20, right: 20, top: 15, bottom: 15),
          child: FColumn(children: [
            _buildCommonWidget(i18nBook.common.fullName.t,
                controller.state.consultationsRecordInfos.patientName ?? ""),
            _buildSpaceWidget(),
            _buildCommonWidget(
                i18nBook.common.mobileNumber.t, patientValues['Phone'] ?? ""),
            _buildSpaceWidget(),
            _buildVisitRecordWidget(),
          ]),
        )
      ],
      onOk: () {
        controller.visitController
            .addFollowUpVisitInfo(patientValues['Phone'], visitRecord);
      },
      onCancel: () {
        router.back(result: {'isCancel': true});
      },
    );
  }

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

  ///创建随访记录行
  FWidget _buildVisitRecordWidget() {
    return FFlex(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        FExpanded(
          child: FText.rich(TextSpan(
            children: [
              TextSpan(
                text: i18nBook.realTimeConsultation.followUpRecord.t,
                style: const TextStyle(
                  fontWeight: FontWeight.bold,
                  fontSize: FFontSizes.H6,
                ),
              ),
              TextSpan(
                text: '*',
                style: const TextStyle(
                  fontWeight: FontWeight.bold,
                  fontSize: FFontSizes.H6,
                  color: Colors.red,
                ),
              ),
            ],
          )),
          flex: 1,
        ),
        FExpanded(
          child: DisableSpaceInputWrapper(
            controller: textInputController,
            child: FTextField(
              maxLength: 500,
              maxLines: 10,
              decoration: InputDecoration(
                enabledBorder: OutlineInputBorder(
                  borderSide: BorderSide(
                    color: Colors.white.withOpacity(0.5),
                    width: 0.5,
                    style: BorderStyle.solid,
                  ),
                ),
                focusedBorder: OutlineInputBorder(
                  borderSide: BorderSide(
                    color: FTheme.ins.colorScheme.primary,
                    width: 0.5,
                    style: BorderStyle.solid,
                  ),
                ),
                filled: true,
              ),
              controller: textInputController,
              onChanged: ((value) {
                visitRecord = value.trim();
              }),
            ),
          ),
          flex: 3,
        ),
      ],
    );
  }

  ///获取会诊详情
  Future<void> _getConsultationDetail() async {
    await controller.findConsultationDetailAsync();
    setState(() {});
  }
}
