import 'package:fis_i18n/i18n.dart';
import 'package:fis_jsonrpc/rpc.dart';
import 'package:fis_theme/theme.dart';
import 'package:fis_ui/base_define/page.dart';
import 'package:fis_ui/index.dart';
import 'package:flutter/material.dart';
import 'package:flyinsono/architecture/define.dart';
import 'package:flyinsono/architecture/states/states.dart';
import 'package:flyinsono/pages/remedical/consultation/controller.dart';
import 'package:get/get.dart';

/// 手机端绑定病人界面
class PatientBindMobile extends FStatefulWidget implements FPage {
  PatientBindMobile(this.patientCode,
      {this.pageName = 'PatientBindMobile', Key? key})
      : super(
          key: key,
        );
  late final int selectIndex;
  final String? patientCode;
  @override
  final String pageName;
  @override
  FState<PatientBindMobile> createState() => _PatientReferState();
}

class _PatientReferState extends FState<PatientBindMobile> {
  final controller = Get.find<ConsultationController>();

  static BoxDecoration createBorder() {
    final border = BoxDecoration(
      borderRadius: BorderRadius.all(Radius.circular(8)),
      border: Border.all(color: Color.fromRGBO(200, 200, 200, 1)),
    );
    return border;
  }

  SetValidPatientRequest setValidPatientRequest = SetValidPatientRequest();

  @override
  FWidget build(BuildContext context) {
    return FObx(() => controller.state.associatPatientInfos.length == 0
        ? _buildBindPatientPlaceHolder()
        : _buildBindPatientList());
  }

  FWidget _buildBindPatientPlaceHolder() {
    return FContainer(
      margin: EdgeInsets.only(
        top: 180,
      ),
      child: FText(i18nBook.remedical.patientNameToAssociatePlaceholder.t),
    );
  }

  FWidget _buildBindPatientList() {
    return FListView.builder(
      shrinkWrap: true,
      itemCount: controller.state.associatPatientInfos.length,
      itemBuilder: (BuildContext context, int index) {
        return FContainer(
          margin: EdgeInsets.only(
            top: 15,
            bottom: 30,
            left: 15,
            right: 15,
          ),
          decoration: _PatientReferState.createBorder(),
          // color: Colors.redAccent,
          // alignment: Alignment.centerLeft,
          child: FInkWell(
            onTap: () {
              setState(
                () {
                  widget.selectIndex = index;
                  setValidPatientRequest.token = Store.user.token;
                  setValidPatientRequest.newPatientCode = controller
                      .state.associatPatientInfos[index]['patientCode'];
                  setValidPatientRequest.oldPatientCode = widget.patientCode;
                  controller.state.setValidPatientRequest =
                      setValidPatientRequest;
                },
              );
            },
            child: _buildBindPatienItem(index),
          ),
        );
      },
    );
  }

  FWidget _buildBindPatienItem(int index) {
    return FContainer(
      margin: EdgeInsets.symmetric(horizontal: 10),
      decoration: BoxDecoration(
        border: Border(
          bottom: BorderSide(
            color: Color.fromRGBO(235, 235, 235, 1),
            width: 1,
          ),
        ),
      ),
      padding: EdgeInsets.all(10),
      child: FRow(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          FExpanded(
            child: FRow(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                FRow(
                  children: [
                    FSizedBox(
                      width: 15,
                    ),
                    FContainer(
                      width: 60,
                      height: 60,
                      margin: EdgeInsets.only(
                        right: 15,
                      ),
                      decoration: BoxDecoration(
                        borderRadius: BorderRadius.all(
                          Radius.circular(4),
                        ),
                        color: FTheme.ins.colorScheme.primary,
                      ),
                      child: _buildPatienAvatar(index),
                    ),
                    FSizedBox(
                      width: 20,
                    ),
                    _buildPatientInfoTemplate(index)
                  ],
                ),
                _buildSelectedEffect(index)
              ],
            ),
          ),
          FSizedBox(
            width: 15,
          ),
        ],
      ),
    );
  }

  FWidget _buildSelectedEffect(int index) {
    return FContainer(
      width: 30,
      height: 30,
      decoration: BoxDecoration(
        border: Border.all(
          color: Color.fromRGBO(141, 141, 141, 1),
          width: 0.5,
        ),
        borderRadius: BorderRadius.circular(30),
      ),
      child: widget.selectIndex == index
          ? FContainer(
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(30),
                color: Theme.of(context).primaryColor,
              ),
              child: FCenter(
                child: FContainer(
                  child: FIcon(
                    Icons.check,
                    size: 20,
                    color: Colors.white,
                  ),
                ),
              ),
            ) as FWidget
          : FText(''),
    );
  }

  FWidget _buildPatienAvatar(int index) {
    return FCenter(
      child: controller.state.associatPatientInfos[index]['headImgUrl'] == '' ||
              controller.state.associatPatientInfos[index]['headImgUrl'] == null
          ? FText(
              controller.patientInfoController.getAvatar(
                  controller.state.associatPatientInfos[index]['name']),
              style: TextStyle(
                fontSize: 22,
                color: Colors.white,
              ),
            )
          : FClipRRect(
              borderRadius: BorderRadius.circular(
                4,
              ),
              child: FConstrainedBox(
                constraints: BoxConstraints.expand(), // 自适应屏幕
                child: FImage.network(
                  controller.state.associatPatientInfos[index]['headImgUrl'],
                  fit: BoxFit.cover,
                ),
              ),
            ),
    );
  }

  FWidget _buildPatientInfoTemplate(int index) {
    return FColumn(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        FText(
          '${controller.state.associatPatientInfos[index]['name']}',
          style: TextStyle(
            fontWeight: FontWeight.bold,
          ),
        ),
        FSizedBox(
          height: 8,
        ),
        FText(
          '${controller.state.associatPatientInfos[index]['phone']}',
        ),
      ],
    );
  }
}

class BindPatient extends FisView<ConsultationController> implements FPage {
  final String? patientCode;
  final String? phone;
  @override
  final String pageName;
  BindPatient({
    Key? key,
    this.patientCode,
    this.phone,
    this.pageName = 'BindPatient',
  }) : super(key: key);

  @override
  FWidget build(BuildContext context) {
    return FScaffold(
      appBar: FAppBar(
        backgroundColor: FTheme.ins.colorScheme.primary,
        elevation: 0,
        automaticallyImplyLeading: false,
        foregroundColor: Colors.black,
        titleSpacing: 24,
        actions: [
          FGestureDetector(
            name: "confirm",
            businessParent: this,
            onTap: () => controller.patientInfoController.setVaildPatient(
              controller.state.setValidPatientRequest,
            ),
            child: FCenter(
              child: FRow(
                children: [
                  FText(
                    i18nBook.common.confirm.t,
                    style: TextStyle(
                      color: Colors.white,
                    ),
                  ),
                  FSizedBox(
                    width: 20,
                  )
                ],
              ),
            ),
          ),
        ],
        leading: FIconButton(
          name: "back",
          businessParent: this,
          icon: FIcon(
            Icons.arrow_back_ios_new,
            size: 20,
            color: Colors.white,
          ),
          onPressed: () {
            Get.back();
          },
        ),
        title: FSizedBox(
          height: 26,
          child: FText(
            i18nBook.remedical.bindPatient.t,
            style: TextStyle(
              fontSize: 18,
              color: Colors.white,
            ),
          ),
        ),
        centerTitle: true,
      ),
      body: FColumn(
        children: [
          FContainer(
            margin: EdgeInsets.symmetric(
              horizontal: 15,
              vertical: 8,
            ),
            color: Colors.white,
            child: _buildPatientNameInputArea(),
          ),
          FExpanded(
            child: PatientBindMobile(
              patientCode,
            ),
          ),
        ],
      ),
    );
  }

  FWidget _buildPatientNameInputArea() {
    return FRow(
      children: [
        FExpanded(
          child: FContainer(
            height: 38,
            child: FRow(
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                FBorderInput(
                  hintSize: 16,
                  contentSize: 16,
                  maxLength: 20,
                  borderColor: Color.fromRGBO(234, 234, 234, 1),
                  suffixIcon: FMaterial(
                    color: Colors.transparent,
                    child: FIconButton(
                      name: "search",
                      businessParent: this,
                      onPressed: () => controller.patientInfoController
                          .getAssociatPatients(this),
                      icon: FIcon(
                        Icons.search,
                      ),
                    ),
                  ),
                  height: 38,
                  hintText: i18nBook.remedical.patientNamePlaceholder.t,
                  onChanged: (value) => controller.state.patientName = value,
                ),
              ],
            ),
          ),
        ),
      ],
    );
  }
}
