import 'package:domain/entity/response/labour_request_index_entity.dart';
import 'package:domain/entity/response/labour_review_list_entity.dart';
import 'package:domain/repository/labour_repository.dart';
import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';
import 'package:plugin_platform/engine/dialog/dialog_engine.dart';
import 'package:plugin_platform/engine/notify/notify_engine.dart';
import 'package:plugin_platform/engine/toast/toast_engine.dart';
import 'package:plugin_platform/http/dio/dio_cancelable_mixin.dart';
import 'package:plugin_platform/http/http_result.dart';
import 'package:shared/utils/date_time_utils.dart';
import 'package:shared/utils/log_utils.dart';
import 'package:shared/utils/util.dart';
import 'package:widgets/dialog/app_default_dialog.dart';
import 'package:widgets/load_state_layout.dart';
import 'package:widgets/picker/date_picker_util.dart';
import 'package:widgets/widget_export.dart';
import 'casual_report_state.dart';

class CasualReportController extends GetxController with DioCancelableMixin {
  final LabourRepository _labourRepository = Get.find();
  final CasualReportState state = CasualReportState();

  var _curPage = 1;
  var _needShowPlaceholder = true;

  //页面PlaceHolder的展示
  LoadState loadingState = LoadState.State_Success;
  String? errorMessage;

  //刷新页面状态
  void changeLoadingState(LoadState state) {
    loadingState = state;
    update();
  }

  // Refresh 控制器
  final EasyRefreshController refreshController = EasyRefreshController(
    controlFinishRefresh: true,
    controlFinishLoad: true,
  );

  // Refresh 刷新事件
  Future onRefresh() async {
    _curPage = 1;
    fetchList();
  }

  // Refresh 加载事件
  Future loadMore() async {
    _curPage++;
    fetchList();
  }

  // 重试请求
  Future retryRequest() async {
    _curPage = 1;
    _needShowPlaceholder = true;
    fetchList();
  }

  /// 获取服务器数据，通知消息列表
  Future fetchList() async {
    if (_needShowPlaceholder) {
      changeLoadingState(LoadState.State_Loading);
    }

    //请求列表数据
    var listResult = await _labourRepository.fetchLabourReviewList(
      "",
      DateTimeUtils.formatDate(state.selectedDate, format: "yyyy-MM-dd"),
      DateTimeUtils.formatDate(state.selectedDate, format: "yyyy-MM-dd"),
      "",
      curPage: _curPage,
      cancelToken: cancelToken,
    );
    
    // 处理数据
    if (listResult.isSuccess) {
      handleList(listResult.data?.rows);
    } else {
      errorMessage = listResult.errorMsg;
      changeLoadingState(LoadState.State_Error);
    }

    // 最后赋值
    _needShowPlaceholder = false;
  }

  // 处理数据与展示的逻辑
  void handleList(List<LabourReviewListRows>? list) {
    if (list != null && list.isNotEmpty) {
      //有数据，判断是刷新还是加载更多的数据
      if (_curPage == 1) {
        //刷新的方式
        state.datas.clear();
        state.datas.addAll(list);
        refreshController.finishRefresh();

        //更新展示的状态
        changeLoadingState(LoadState.State_Success);
      } else {
        //加载更多
        state.datas.addAll(list);
        refreshController.finishLoad();
        update();
      }
    } else {
      if (_curPage == 1) {
        //展示无数据的布局
        state.datas.clear();
        changeLoadingState(LoadState.State_Empty);
        refreshController.finishRefresh();
      } else {
        //展示加载完成，没有更多数据了
        refreshController.finishLoad(IndicatorResult.noMore);
      }
    }
  }

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

  @override
  void onClose() {
    state.datas.clear();
    super.onClose();
  }

  /// 年月的日期筛选
  void showDatePicker() {

    DatePickerUtil.showCupertinoDatePicker(
      selectedDateTime: state.selectedDate ?? DateTime.now(),
      mode: CupertinoDatePickerMode.monthYear,
      onDateTimeChanged: (date) {
        state.selectedDate = date;
        update();
      },
      title: "Select Date".tr,
    );
  }

}
