import 'package:get/get.dart';
import 'package:vitalapp/architecture/defines.dart';
import 'package:vitalapp/managers/interfaces/patient.dart';
import 'package:vitalapp/pages/controllers/home_nav_mixin.dart';
import 'package:vitalapp/store/store.dart';
import 'state.dart';

class PatientDetailController extends FControllerBase with HomeNavMixin {
  final state = PatientDetailState();
  final _patientManager = Get.find<IPatientManager>();

  @override
  void onInit() {
    loadData();
    super.onInit();
  }

  /// 打开信息页
  void gotoInfo() {
    Get.toNamed(
      "/patient/info",
      parameters: {"code": state.code},
    );
  }

  /// 前往随访页
  void gotoFollowUp() {
    Get.toNamed(
      "/check/follow_up",
      parameters: {"patientCode": state.code, "patientName": state.name},
    );
  }

  /// 前往随访记录页
  void gotoFollowUpRecord() {
    Get.toNamed(
      "/check/follow_up_record",
      parameters: {"patientCode": state.code, "patientName": state.name},
    );
  }

  /// 前往健康体检页
  void gotoHealthCheck() {
    Get.toNamed(
      "/check/form",
      parameters: {"patientCode": state.code},
    );
  }

  /// 前往签约页
  void gotoContract() {
    Get.toNamed(
      "/contract/package_list",
      parameters: {"patientCode": state.code},
    );
  }

  /// 前往转诊页
  void gotoReferral() {
    //
  }

  /// 前往健康检测页
  void gotoExam() {
    //
    Get.toNamed(
      "/medical",
      parameters: {"patientCode": state.code},
    );
  }

  /// 前往诊疗记录页
  void gotoExamRecord() {
    //
    Get.toNamed(
      "/medical/records",
      parameters: {"patientCode": state.code},
    );
  }

  ///前往体检记录
  void gotoHealthCheckRecord() {
    Get.toNamed(
      "/check/healthCheckRecord",
      parameters: {"patientCode": state.code},
    );
  }

  void gotoContractRecords() {
    Get.toNamed(
      "/contract/contract_records",
      parameters: {"patientCode": state.code},
    );
  }

  Future<void> loadData() async {
    // final p = Get.parameters;
    // state.code = p["code"]!;
    state.code = Store.user.currentSelectPatientInfo?.code ?? '';
    final dto = Store.user.currentSelectPatientInfo!;
    dto.birthday = dto.birthday!.toLocal();
    state.updateDto(dto);
  }

  Future<bool> setCrowdLabelsAsync(
      List<String> carowLabels, List<String> crowdNames) async {
    final result =
        await _patientManager.setCrowdLabelsAsync(state.code, carowLabels);
    if (result) {
      state.updateCrowd(carowLabels, crowdNames);
    }
    return result;
  }
}
