import 'package:fis_jsonrpc/rpc.dart';
import 'package:get/get.dart';
import 'package:vnoteapp/architecture/defines.dart';
import 'package:vnoteapp/managers/interfaces/patient.dart';

import 'state.dart';

class PatientListController extends FControllerBase {
  final _patientManager = Get.find<IPatientManager>();

  final state = PatientListState();

  /// 前进到详情页
  void gotoDetail(String code) {
    Get.toNamed(
      "/patient/detail",
      parameters: {"code": code},
    );
  }

  /// 新建档案
  void onCreateClicked() {
    Get.toNamed(
      "/patient/create",
      id: 1001,
      parameters: {"from": "list"},
    );
  }

  /// 筛选
  void onFilterClicked() {
    //
  }

  @override
  void onReady() {
    super.onReady();
    reloadList();
  }

  /// 重新加载列表
  Future<void> reloadList() async {
    state.pageIndex = 0;
    state.dataList = [];
    await loadNextPageList();
  }

  /// 加载下一页列表
  Future<void> loadNextPageList() async {
    final request = PatientPageRequest(
      pageIndex: state.pageIndex + 1,
      pageSize: state.pageSize,
    );
    if (state.searchString.isNotEmpty) {
      // TODO:
      // request.filter = {
      //   "xxx": state.searchString,
      // };
      // request.isFuzzy = true;
    }
    final pagedList = await _patientManager.getPagedList(request);
    if (pagedList != null) {
      state.totalCount = pagedList.dataCount;
      if (pagedList.pageData != null) {
        state.pageIndex = state.pageIndex + 1;
        state.appendDataList(pagedList.pageData!);
      }
    }
  }
}
