import 'package:anchor_collect_flutter/models/breeding_record_detail_entity.dart';
import 'package:get/get.dart';
import 'package:uuid/uuid.dart';
import 'package:flutter/material.dart';
import '../../../congifs.dart';
import '../../../controllers/global_service.dart';
import '../../../models/breeding_record_entity.dart';
import '../../../rfid/rfid_plugin.dart';
import '../../../routes/routes.dart';
import '../../../utils/dialog_utils.dart';
import '../../../utils/dict_utils.dart';
import '../../../utils/empty_utils.dart';
import '../../../utils/sp_helper.dart';
import 'breeding_record_detail_state.dart';

class BreedingRecordDetailLogic extends GetxController {
  final BreedingRecordDetailState state = BreedingRecordDetailState();

  // 创建一个Uuid实例
  var uuid = Uuid();

  @override
  void onReady() {
    Map<String, dynamic> arguments = Get.arguments;
    BreedingRecordDetailEntity? detailEntity = arguments['detailEntity'];
    BreedingRecordEntity? entity = arguments['entity'];

    if (entity == null) {
      DialogUtils.showToast('数据信息问题');
      Get.back(result: 'refresh');
    } else {
      if (detailEntity != null) {
        state.entity.value = detailEntity;
        state.isModify = true;
        state.unid = state.entity.value.unid!;
      } else {
        state.entity.value.unid = uuid.v4().hashCode.toString() + uuid.v4().hashCode.toString();
        state.entity.value.cattleresumeId = entity.cattleresumeId;
        state.entity.value.registrationNo = entity.registrationNo;
        state.entity.value.individualNo = entity.individualNo;
        state.entity.value.fUnid = entity.unid;
      }
      refreshDictData();
    }
  }

  ///初始化字典项
  refreshDictData() async {
    state.matingStyle =
        await DictUtils.getDictName('zxgl_cattlemating_matingStyle', state.entity.value.matingStyle ?? '');
    // state.grade = await DictUtils.getDictName('zxgl_cattleresume_grade', state.entity.value.grade ?? '');

    update();
  }

  calculateDueDate(String matingDate){
    // DateFormat dateFormatter = DateFormat('yyyy-MM-dd');
    // DateTime startDate = DateTime(2024, 1, 21);
    // dateFormatter.add(const Duration(days: 128));
    // final dateString = dateFormatter.format(matingDate),
    // DateTime.parse(dateString).;
    update();
  }

  save() async {
    if (await checkRequiredFields()) {
      return;
    }
    DialogUtils.showLoadingDialog('保存中。。。');

    if (state.isModify) {
      state.entity.value.updateTime = DateTime.now().toString();
      state.entity.value.updateBy = SpHelper.getStorage(Config.SP_STR_USERNAME);
    } else {
      state.entity.value.createTime = DateTime.now().toString();
      state.entity.value.createBy = SpHelper.getStorage(Config.SP_STR_USERNAME);
    }
    state.entity.value.deptId = SpHelper.getStorage(Config.SP_DEPT_ID);
    state.entity.value.uploadStatus = '0';

    ///保存配种详情
    await GlobalService.to.isar.writeTxn(() async => {
          await GlobalService.to.isar.breedingRecordDetailEntitys.put(state.entity.value),
        });
    DialogUtils.showToast('保存配种详情成功');
    DialogUtils.dismissDialog();
    Get.back(result: 'refresh');
  }

  Future<bool> checkRequiredFields() async {
    if (!EmptyUtils.isStrNotEmpty(state.entity.value.cattleresumeId)) {
      DialogUtils.showToast('高频耳标或者可视耳标无效');
      return true;
    }
    // if (!EmptyUtils.isStrNotEmpty(state.entity.individualNo)) {
    //   DialogUtils.showToast('必须填写可视耳标');
    //   return true;
    // }
    // if (!EmptyUtils.isStrNotEmpty(state.entity.photoPath) && !EmptyUtils.isStrNotEmpty(state.photoPath)) {
    //   DialogUtils.showToast('必须上传牛只拍照');
    //   return true;
    // }
    // if (!EmptyUtils.isStrNotEmpty(state.entity.circleNo)) {
    //   DialogUtils.showToast('必须选择圈舍');
    //   return true;
    // }
    // if (!EmptyUtils.isStrNotEmpty(state.entity.raiseType)) {
    //   DialogUtils.showToast('必须选择饲养类型');
    //   return true;
    // }
    if (!EmptyUtils.isStrNotEmpty(state.entity.value.matingDate)) {
      DialogUtils.showToast('必须选择配种日期');
      return true;
    }
    if (!EmptyUtils.isStrNotEmpty(state.entity.value.matingStyle)) {
      DialogUtils.showToast('必须选择配种方式');
      return true;
    }
    // if (state.entity.value.drugNumber == null ||state.entity.value.drugNumber == 0) {
    //   DialogUtils.showToast('必须填写用药数量');
    //   return true;
    // }
    // if (state.entity.birthHeavy == null || state.entity.birthHeavy == 0) {
    //   DialogUtils.showToast('必须填写出生重量');
    //   return true;
    // }
    // if (!EmptyUtils.isStrNotEmpty(state.entity.sex)) {
    //   DialogUtils.showToast('必须选择性别');
    //   return true;
    // }
    // if (!EmptyUtils.isStrNotEmpty(state.entity.status)) {
    //   DialogUtils.showToast('必须选择状态');
    //   return true;
    // }
    // if (!EmptyUtils.isStrNotEmpty(state.entity.colour)) {
    //   DialogUtils.showToast('必须填写犊牛毛色');
    //   return true;
    // }
    return false;
  }

  copyEntity(BreedingRecordDetailEntity formerEntity) {
    state.entity.value.id = formerEntity.id;
    state.entity.value.unid = formerEntity.unid;
    state.entity.value.cattleresumeId = formerEntity.cattleresumeId;
    state.entity.value.registrationNo = formerEntity.registrationNo;
    state.entity.value.individualNo = formerEntity.individualNo;
    state.entity.value.fUnid = formerEntity.fUnid;
    state.entity.value.matingStyle = formerEntity.matingStyle;
    state.entity.value.frozenSemenBatch = formerEntity.frozenSemenBatch;
    state.entity.value.matingDate = formerEntity.matingDate;
    state.entity.value.isReturn = formerEntity.isReturn;
    state.entity.value.backLoveDate = formerEntity.backLoveDate;
    state.entity.value.dueDate = formerEntity.dueDate;
    state.entity.value.pregnancy = formerEntity.pregnancy;
    state.entity.value.uploadStatus = formerEntity.uploadStatus;
    state.entity.value.deptId = formerEntity.deptId;
    state.entity.value.searchValue = formerEntity.searchValue;
    state.entity.value.createBy = formerEntity.createBy;
    state.entity.value.createTime = formerEntity.createTime;
    state.entity.value.updateBy = formerEntity.updateBy;
    state.entity.value.updateTime = formerEntity.updateTime;
    state.entity.value.remark = formerEntity.remark;
  }
}
