import 'package:domain/entity/response/add_edit_revise_view_s_g_entity.dart';
import 'package:domain/repository/job_sg_repository.dart';
import 'package:flutter/material.dart';
import 'package:get/get.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/util.dart';

import 'revise_add_edit_state.dart';

class ReviseAddEditController extends GetxController with DioCancelableMixin {
  final JobSGRepository _jobRepository = Get.find();
  final ReviseAddEditState state = ReviseAddEditState();

  /// 获取首页的数据
  void fetchRequestDetail() async {
    //获取到数据
    Future<HttpResult<AddEditReviseViewSGEntity>> taskFuture;
    if (state.pageType != 0 && Utils.isNotEmpty(state.id) && state.id != "0") {
      taskFuture = _jobRepository.fetchReviseEditIndex(state.id, cancelToken: cancelToken);
    } else {
      taskFuture = _jobRepository.fetchReviseAddIndex(state.id);
    }

    var result = await taskFuture;

    //处理数据
    if (result.isSuccess) {
      state.addEditEntity = result.data;

      TextEditingController reviseHourController = state.formData['revise_hour']!['controller'];
      TextEditingController reasonController = state.formData['reason']!['controller'];

      reviseHourController.text = state.addEditEntity?.reviseHours ?? "";
      reasonController.text = state.addEditEntity?.reviseMsg ?? "";

      update();
    } else {
      ToastEngine.show(result.errorMsg ?? "Network Load Error".tr);
    }
  }

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

  /// 提交
  void doSubmit() async {
    TextEditingController reviseHourController = state.formData['revise_hour']!['controller'];
    TextEditingController reasonController = state.formData['reason']!['controller'];
    String reviseHour = reviseHourController.text.toString();
    String reason = reasonController.text.toString();

    if (Utils.isEmpty(reviseHour)) {
      ToastEngine.show("Enter Revise Hour".tr);
      return;
    } else if (Utils.isEmpty(reason)) {
      ToastEngine.show("Enter Reason".tr);
      return;
    }

    Future<HttpResult> taskFuture;
    if (state.pageType != 0 && Utils.isNotEmpty(state.id) && state.id != "0") {
      taskFuture = _jobRepository.submitReviseEdit(
        state.id,
        reviseHour,
        reason,
        cancelToken: cancelToken,
      );
    } else {
      taskFuture = _jobRepository.submitReviseAdd(
        state.id,
        reviseHour,
        reason,
        cancelToken: cancelToken,
      );
    }

    var result = await taskFuture;

    //处理数据
    if (result.isSuccess) {
      NotifyEngine.showSuccess("Successful".tr);

      //根据类型刷新
      if (state.pageType != 0 && Utils.isNotEmpty(state.id) && state.id != "0") {
        //编辑就发送指定的 reviseId 刷新
        state.cb?.call(state.id ?? "");
      } else {
        //新增的就发送空
        state.cb?.call("");
      }

      Get.back();
    } else {
      ToastEngine.show(result.errorMsg ?? "Network Load Error".tr);
    }
  }
}
