import 'package:anchor_collect_flutter/models/cattle_resume_entity.dart';
import 'package:anchor_collect_flutter/models/dict_entity.dart';
import 'package:anchor_collect_flutter/models/user_model.dart';
import 'package:get/get.dart';
import 'package:isar/isar.dart';

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

class HomeLogic extends GetxController {
  late RxString username = ''.obs;
  late String password = '';

  late int breedingCount = 0;
  late int obsoleteCount = 0;
  late int deadCount = 0;
  late int sellCount = 0;

  @override
  void onReady() {
    // var map = Get.arguments;
    // username.value = map['username'];
    // password = map['password'];
    //
    // update();
  }

  ///获取游客令牌

  void getVisitorToken() async {
    Map<String, String> headMap = {};

    headMap.putIfAbsent('client-version', () => '2.1.1');
    headMap.putIfAbsent('client-channel', () => '1');

    Map<String, String> map = {}; //参数
    String strMd5 = '';

    map.putIfAbsent('code', () => '');
    map.putIfAbsent('userType', () => 'Tourist');
    String apiKey = 'kangeqiu@8868!';
    map.putIfAbsent('from', () => 'GW');

    // RequestClient().post<UserInfo>(APIS.login_getMsg, //url 地址
    //     //是否显示加载中
    //     showLoading: false,
    //     //参数
    //     data: map,
    //     //单个请求添加head
    //     headers: headMap,
    //     onResponse: (response) {
    //       // response.address
    //     },
    //     onError: (e) {
    //
    //       return true; //false 表示没有处理移除  ture 表示处理了异常   默认值 false
    //     }
    // );
  }

  getBreedingCount() async {
    breedingCount = await GlobalService.to.isar.cattleResumeEntitys.filter().statusEqualTo('1')
        .optional(!Config.isAdmin, (q) => q.deptIdEqualTo(SpHelper.getStorage(Config.SP_DEPT_ID))).count();
    obsoleteCount = await GlobalService.to.isar.cattleResumeEntitys.filter().statusEqualTo('2')
        .optional(!Config.isAdmin, (q) => q.deptIdEqualTo(SpHelper.getStorage(Config.SP_DEPT_ID))).count();
    deadCount = await GlobalService.to.isar.cattleResumeEntitys.filter().statusEqualTo('3')
        .optional(!Config.isAdmin, (q) => q.deptIdEqualTo(SpHelper.getStorage(Config.SP_DEPT_ID))).count();
    sellCount = await GlobalService.to.isar.cattleResumeEntitys.filter().statusEqualTo('4')
        .optional(!Config.isAdmin, (q) => q.deptIdEqualTo(SpHelper.getStorage(Config.SP_DEPT_ID))).count();
    update();
  }

  setBreedingCount(){
    breedingCount = 0;
    update();
  }

  forWard(String title) {
    switch (title) {
      case '基础信息':
        Get.toNamed(AppRoute.CATTLE_RESUME_LIST);
        break;
      case '性能测定':
        Get.toNamed(AppRoute.PERFORMANCE_LIST);
        break;
      case '诊疗记录':
        Get.toNamed(AppRoute.MEDICAL_RECORD_LIST);
        break;
      case '配种记录':
        Get.toNamed(AppRoute.BREEDING_RECORD_LIST);
        break;
      case '免疫记录':
        Get.toNamed(AppRoute.IMMUNE_BATCH_LIST);
        break;
      case '称重管理':
        Get.toNamed(AppRoute.WEIGHT_MAN_LIST);
        break;
    }
  }

  @override
  void onInit() {
    getBreedingCount();
    // update();
  }
}
