import 'package:domain/entity/response/job_template_edit_index_entity.dart';
import 'package:domain/entity/response/u_k_template_detail_entity.dart';
import 'package:domain/repository/uk_job_repository.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/log_utils.dart';
import 'package:shared/utils/util.dart';

import 'template_add_state.dart';

class TemplateAddController extends GetxController with DioCancelableMixin {
  final UKJobRepository _labourRepository = Get.find();
  final LabourTemplateAddState state = LabourTemplateAddState();

  // 获取添加或者编辑的详情
  void _fetchAddEditIndexDetail() async {
    //获取到数据
    Future<HttpResult<UKTemplateDetailEntity>> taskFuture;
    if (Utils.isNotEmpty(state.templateId) && state.templateId != "0") {
      //编辑
      taskFuture = _labourRepository.fetchJobTemplateDetail(templateId: state.templateId, cancelToken: cancelToken);
    } else {
      //新增
      taskFuture = _labourRepository.fetchJobTemplateIndex(cancelToken: cancelToken);
    }

    var result = await taskFuture;

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

      var templateNameController = state.formData['template_name']!['controller'];
      var descController = state.formData['desc']!['controller'];
      var noteController = state.formData['note']!['controller'];
      templateNameController.text = state.indexEntity?.row?.jobTitle ?? "";
      descController.text = state.indexEntity?.row?.description ?? "";
      noteController.text = state.indexEntity?.row?.note ?? "";

      //默认CheckBox
      state.selectedCertificateList = state.indexEntity?.certificateList?.where((e) => e.checked == "checked").map((e) => e.value!).toList() ?? [];
      Log.d("当前选中的证书：${state.selectedCertificateList}");
      state.selectedVehicleList = state.indexEntity?.vehicleList?.where((e) => e.checked == "checked").map((e) => e.value!).toList() ?? [];
      Log.d("当前选中的交通工具：${state.selectedVehicleList}");

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

  /// 提交
  void doSubmit() async {
    var templateNameController = state.formData['template_name']!['controller'];
    var descController = state.formData['desc']!['controller'];
    var noteController = state.formData['note']!['controller'];

    String templateTitle = templateNameController.text.toString();
    String desc = descController.text.toString();
    String note = noteController.text.toString();

    //只校验模版的名称
    if (Utils.isEmpty(templateTitle)) {
      ToastEngine.show("Enter Job Template Title".tr);
      return;
    }

    Future<HttpResult> taskFuture;
    if (Utils.isNotEmpty(state.templateId) && state.templateId != "0") {
      taskFuture = _labourRepository.editJobTemplateSubmit(
        templateId: state.templateId,
        jobTitle: templateTitle,
        certificate: state.selectedCertificateList,
        vehicle: state.selectedVehicleList,
        desc: desc,
        note: note,
        cancelToken: cancelToken,
      );
    } else {
      taskFuture = _labourRepository.addJobTemplateSubmit(
        jobTitle: templateTitle,
        certificate: state.selectedCertificateList,
        vehicle: state.selectedVehicleList,
        desc: desc,
        note: note,
        cancelToken: cancelToken,
      );
    }

    var result = await taskFuture;

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

      //根据类型刷新
      state.cb?.call(state.templateId);

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

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