/*
 * @Author       : WuWei
 * @LastEditors  : WuWei
 * @Date         : 2022-10-11 16:15:45
 * @LastEditTime : 2023-11-06 16:16:26
 * @Description  : Do not edit
 */

import 'package:get/get.dart';
import 'package:intl/intl.dart';
import 'package:statistics_package/src/models/charge_model.dart';
import 'package:statistics_package/src/models/work_model.dart';
import 'package:statistics_package/src/service/impl/statistics_impl.dart';

class StatisticsIndexStore extends GetxController {
  var communitys = [].obs;
  var communities = [].obs;
  var date = DateFormat('yyyy-MM-dd').format(DateTime.now()).obs;
  RxInt tabIndex = 0.obs;
  var month = DateFormat('yyyy-MM').format(DateTime.now()).obs;

  var feeList = ChargeModel().obs;
  var workList = WorkModel().obs;
  RxList serveList = [].obs;
  RxList releaseList = [].obs;
  RxList announcementList = [].obs;
  RxList facilityList = [].obs;
  RxList powerList = [].obs;
  RxList greenList = [].obs;
  RxList healthList = [].obs;
  RxList securityList = [].obs;
  RxList fireList = [].obs;
  RxList assistList = [].obs;

  /// 设置索引
  onSetTabIndex(index) {
    tabIndex.value = index;
    tabIndex.refresh();
  }

  Future<void> setArguments() async {
    communitys.value = Get.arguments?['communitys'];
    communities.value = Get.arguments?['communities'];
    communitys.refresh();
    communities.refresh();
  }

  Future<void> initList() async {
    Future.wait([
      setArguments(),
      getFeeList(),
      getServeList(),
      getWorkList(),
      getAnnouncementList(),
      getReleaseList(),
    ]);
  }

  /// 筛选时间
  setDate(time) {
    date.value = DateFormat('yyyy-MM-dd').format(time);
    // date.refresh();

    Future.wait([
      getFeeList(),
      getServeList(),
      getWorkList(),
      getAnnouncementList(),
      getReleaseList(),
    ]);
  }

  setMonth(time) {
    month.value = DateFormat('yyyy-MM').format(time);
    month.refresh();
  }

  /// 物业收费
  Future<void> getFeeList() async {
    var res = await StatisticsServiceImpl.instance!.getFee({
      'ids': communitys,
      'date': tabIndex.value == 0 ? (date.value) : (month.value),
    });

    feeList.value = res.data ?? ChargeModel();
    update();
  }

  /// 物业服务
  Future<void> getServeList() async {
    var res = await StatisticsServiceImpl.instance!.getServe({
      'ids': communitys,
      'date': tabIndex.value == 0 ? (date.value) : (month.value),
    });
    serveList.value = res.data ?? [];
    serveList.refresh();
  }

  /// 物业工作
  Future<void> getWorkList() async {
    var res = await StatisticsServiceImpl.instance!.getWork({
      'ids': communitys,
      'date': tabIndex.value == 0 ? (date.value) : (month.value),
    });
    workList.value = res.data ?? WorkModel();
    workList.refresh();
  }

  /// 信息发布
  Future<void> getReleaseList() async {
    var res = await StatisticsServiceImpl.instance!.getRelease({
      'ids': communitys,
      'date': tabIndex.value == 0 ? (date.value) : (month.value),
    });
    releaseList.value = res.data ?? [];
    releaseList.refresh();
  }

  /// 信息公示
  Future<void> getAnnouncementList() async {
    var res = await StatisticsServiceImpl.instance!.getRelease({
      'ids': communitys,
      'date': tabIndex.value == 0 ? (date.value) : (month.value),
    });
    announcementList.value = res.data ?? [];
    announcementList.refresh();
  }

  /// 设施
  Future<void> getFacilityList() async {
    if (facilityList.length > 0) return;
    var res = await StatisticsServiceImpl.instance!.getFacility({
      'ids': communitys,
      'date': tabIndex.value == 0 ? (date.value) : (month.value),
    });
    facilityList.value = res.data ?? [];
    facilityList.refresh();
  }

  /// 供电
  Future<void> getPowerList() async {
    if (powerList.length > 0) return;
    var res = await StatisticsServiceImpl.instance!.getPower({
      'ids': communitys,
      'date': tabIndex.value == 0 ? (date.value) : (month.value),
    });
    powerList.value = res.data ?? [];
    powerList.refresh();
  }

  /// 绿化
  Future<void> getGreenList() async {
    if (greenList.length > 0) return;
    var res = await StatisticsServiceImpl.instance!.getGreen({
      'ids': communitys,
      'date': tabIndex.value == 0 ? (date.value) : (month.value),
    });
    greenList.value = res.data ?? [];
    greenList.refresh();
  }

  /// 卫生设施
  Future<void> getHealthList() async {
     if (healthList.length > 0) return;
    var res = await StatisticsServiceImpl.instance!.getHealth({
      'ids': communitys,
      'date': tabIndex.value == 0 ? (date.value) : (month.value),
    });
    healthList.value = res.data ?? [];
    healthList.refresh();
  }

  /// 安防
  Future<void> getSecurityList() async {
    if (securityList.length > 0) return;
    var res = await StatisticsServiceImpl.instance!.getSecurity({
      'ids': communitys,
      'date': tabIndex.value == 0 ? (date.value) : (month.value),
    });
    securityList.value = res.data ?? [];
    securityList.refresh();
  }

  /// 消防
  Future<void> getFireList() async {
    if (fireList.length > 0) return;
    var res = await StatisticsServiceImpl.instance!.getFire({
      'ids': communitys,
      'date': tabIndex.value == 0 ? (date.value) : (month.value),
    });
    fireList.value = res.data ?? [];
    fireList.refresh();
  }

  /// 资产
  Future<void> getAssistList() async {
    if (assistList.length > 0) return;
    var res = await StatisticsServiceImpl.instance!.getAsset({
      'ids': communitys,
      'date': tabIndex.value == 0 ? (date.value) : (month.value),
    });
    assistList.value = res.data ?? [];
    assistList.refresh();
  }
}
