import 'package:domain/repository/labour_sg_repository.dart';
import 'package:domain/repository/sg_agency_repository.dart';
import 'package:flutter/cupertino.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:shared/utils/date_time_utils.dart';
import 'package:shared/utils/util.dart';
import 'package:widgets/picker/date_picker_util.dart';
import 'package:widgets/picker/option_pick_util.dart';

import 'contract_rate_specific_state.dart';

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

  // 获取添加或者编辑的详情
  void _fetchAddEditIndexDetail() async {
    final result = await _agencyRepository.fetchContractPositionRateSpecificOption(
      contractId: state.contractId,
      positionId: state.positionId,
      cancelToken: cancelToken,
    );

    if (result.isSuccess) {
      state.optionEntity = result.data;

      TextEditingController agencyController = state.formData['agency_name']!['controller'];
      TextEditingController positionController = state.formData['position']!['controller'];
      agencyController.text = state.optionEntity?.agencyName ?? "";
      positionController.text = state.optionEntity?.position ?? "";

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

  /// 提交
  void doSubmit() async {
    TextEditingController agencyController = state.formData['agency_name']!['controller'];
    TextEditingController positionController = state.formData['position']!['controller'];
    TextEditingController hourlyRateController = state.formData['hourly_rate']!['controller'];

    String agencyName = agencyController.text.toString();
    String position = positionController.text.toString();
    String rate = hourlyRateController.text.toString();

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

    if (Utils.isEmpty(position)) {
      ToastEngine.show("Enter Position".tr);
      return;
    }

    if(state.selectedDate == null){
      ToastEngine.show("Please Select Date".tr);
      return;
    }

    if (Utils.isEmpty(rate)) {
      ToastEngine.show("Enter Hourly Rate".tr);
      return;
    }

    final result = await _agencyRepository.addContractPositionRateSpecificSubmit(
      contractId: state.contractId,
      positionId: state.positionId,
      day: DateTimeUtils.formatDate(state.selectedDate!,format: "yyyy-MM-dd"),
      amount: rate,
      cancelToken: cancelToken,
    );

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

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

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

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

  /// 选择具体的日期
  void pickDate() {
    DatePickerUtil.showCupertinoDatePicker(
      selectedDateTime: state.selectedDate ?? DateTime.now(),
      onDateTimeChanged: (date) {
        state.selectedDate = date;
        update();
      },
      title: "Select Date".tr,
    );
  }
}
