import 'package:anchor_collect_flutter/models/cattle_resume_entity.dart';
import 'package:anchor_collect_flutter/utils/dialog_utils.dart';
import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';

import '../../congifs.dart';
import '../../controllers/global_service.dart';
import '../../utils/sp_helper.dart';
import 'begats_record_state.dart';

class BegatsRecordLogic extends GetxController {
  final BegatsRecordState state = BegatsRecordState();

  late TextEditingController individualController;

  late TextEditingController fatherRegController;
  late TextEditingController motherRegController;

  late TextEditingController ffNumController;
  late TextEditingController fmNumController;
  late TextEditingController mfNumController;
  late TextEditingController mmNumController;

  late TextEditingController fffNumController;
  late TextEditingController ffmNumController;
  late TextEditingController fmfNumController;
  late TextEditingController fmmNumController;
  late TextEditingController mffNumController;
  late TextEditingController mfmNumController;
  late TextEditingController mmfNumController;
  late TextEditingController mmmNumController;

  @override
  void onInit() {
    individualController = TextEditingController();

    fatherRegController = TextEditingController();
    motherRegController = TextEditingController();

    ffNumController = TextEditingController();
    fmNumController = TextEditingController();
    mfNumController = TextEditingController();
    mmNumController = TextEditingController();

    fffNumController = TextEditingController();
    ffmNumController = TextEditingController();
    fmfNumController = TextEditingController();
    fmmNumController = TextEditingController();
    mffNumController = TextEditingController();
    mfmNumController = TextEditingController();
    mmfNumController = TextEditingController();
    mmmNumController = TextEditingController();

    Map<String, dynamic> arguments = Get.arguments;
    state.entity.value = arguments['entity'];

    individualController.text = state.entity.value.registrationNo ?? '';

    fatherRegController.text = state.entity.value.fatherReg ?? '';
    motherRegController.text = state.entity.value.motherReg ?? '';

    ffNumController.text = state.entity.value.ffNum ?? '';
    fmNumController.text = state.entity.value.fmNum ?? '';
    mfNumController.text = state.entity.value.mfNum ?? '';
    mmNumController.text = state.entity.value.mmNum ?? '';

    fffNumController.text = state.entity.value.fffNum ?? '';
    ffmNumController.text = state.entity.value.ffmNum ?? '';
    fmfNumController.text = state.entity.value.fmfNum ?? '';
    fmmNumController.text = state.entity.value.fmmNum ?? '';
    mffNumController.text = state.entity.value.mffNum ?? '';
    mfmNumController.text = state.entity.value.mfmNum ?? '';
    mmfNumController.text = state.entity.value.mmfNum ?? '';
    mmmNumController.text = state.entity.value.mmmNum ?? '';
  }

  Future<void> save() async {
    if (state.entity == null) {
      DialogUtils.showToast('牛只信息无效!');
      return;
    }

    state.entity.value.fatherReg = fatherRegController.text ?? '';
    state.entity.value.motherReg = motherRegController.text ?? '';

    state.entity.value.ffNum = ffNumController.text ?? '';
    state.entity.value.fmNum = fmNumController.text ?? '';
    state.entity.value.mfNum = mfNumController.text ?? '';
    state.entity.value.mmNum = mmNumController.text ?? '';

    state.entity.value.fffNum = fffNumController.text ?? '';
    state.entity.value.ffmNum = ffmNumController.text ?? '';
    state.entity.value.fmfNum = fmfNumController.text ?? '';
    state.entity.value.fmmNum = fmmNumController.text ?? '';
    state.entity.value.mffNum = mffNumController.text ?? '';
    state.entity.value.mfmNum = mfmNumController.text ?? '';
    state.entity.value.mmfNum = mmfNumController.text ?? '';
    state.entity.value.mmmNum = mmmNumController.text ?? '';

    state.entity.value.updateTime = DateTime.now().toString();
    state.entity.value.updateBy = 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.cattleResumeEntitys.put(state.entity.value),
        });
    DialogUtils.showToast('保存系谱成功');
    Get.back();
  }
}
