import 'package:domain/entity/response/agency_detail_entity.dart';
import 'package:domain/repository/labour_sg_repository.dart';
import 'package:domain/repository/sg_agency_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 'agency_add_state.dart';

class AgencyAddController extends GetxController with DioCancelableMixin {
  final SGAgencyRepository _agencyRepository = Get.find();
  final AgencyAddState state = AgencyAddState();

  // 获取添加或者编辑的详情
  void _fetchEditDetail() async {

    //如果是详情数据则请求接口展示
    if (Utils.isNotEmpty(state.agentId) && state.agentId != "0"){

      var result = await _agencyRepository.fetchAgencyEditDetail(pivotId: state.agentId);

      //处理数据
      if (result.isSuccess) {
        AgencyDetailEntity? detailEntity = result.data;

        var agencyNameController = state.formData['agency_name']!['controller'];
        var acraController = state.formData['acra']!['controller'];
        var personController = state.formData['person_in_charge']!['controller'];
        var addressController = state.formData['address']!['controller'];
        var emailController = state.formData['email']!['controller'];
        var phoneController = state.formData['phone']!['controller'];

        agencyNameController.text = detailEntity?.pivot?.agencyName ?? "";
        acraController.text = detailEntity?.pivot?.uenNo ?? "";
        personController.text = detailEntity?.pivot?.contactPerson ?? "";
        addressController.text = detailEntity?.pivot?.address ?? "";
        emailController.text = detailEntity?.pivot?.contactEmail ?? "";
        phoneController.text = detailEntity?.pivot?.contactNo ?? "";

        //默认赋值
        state.statusOption = detailEntity?.statusRadios;

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

    }
  }

  /// 提交
  void doSubmit() async {
    var agencyNameController = state.formData['agency_name']!['controller'];
    var acraController = state.formData['acra']!['controller'];
    var personController = state.formData['person_in_charge']!['controller'];
    var addressController = state.formData['address']!['controller'];
    var emailController = state.formData['email']!['controller'];
    var phoneController = state.formData['phone']!['controller'];

    String agencyName = agencyNameController.text.toString();
    String acra = acraController.text.toString();
    String personName = personController.text.toString();
    String address = addressController.text.toString();
    String email = emailController.text.toString();
    String phone = phoneController.text.toString();

    //校验必填项
    if (Utils.isEmpty(agencyName)) {
      ToastEngine.show("Enter Agency Name".tr);
      return;
    }

    if (Utils.isEmpty(acra)) {
      ToastEngine.show("Enter ACRA NO".tr);
      return;
    }

    Future<HttpResult> taskFuture;
    if (Utils.isNotEmpty(state.agentId) && state.agentId != "0") {
      taskFuture = _agencyRepository.editAgencySubmit(
        pivotId: state.agentId,
        agencyName: agencyName,
        acraNo: acra,
        contactPerson: personName,
        address: address,
        contactNo: phone,
        contactEmail: email,
        status: state.statusId,
        cancelToken: cancelToken,
      );
    } else {
      taskFuture = _agencyRepository.addAgencySubmit(
        agencyName: agencyName,
        acraNo: acra,
        contactPerson: personName,
        address: address,
        contactNo: phone,
        contactEmail: email,
        cancelToken: cancelToken,
      );
    }

    var result = await taskFuture;

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

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

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

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