/*
 * @Author       : WuWei
 * @LastEditors  : WuWei
 * @Date         : 2023-10-17 15:19:47
 * @LastEditTime : 2023-11-06 15:31:45
 * @Description  : Do not edit
 */
// import 'package:intl/intl.dart';
// import 'package:app_package/app_package.dart';
import 'package:flutter/material.dart';
import 'package:metered_package/src/models/record_model.dart';
import 'package:wisdom_cli/wisdom_cli.dart';
// import 'package:api_package/meterReading_record.get.dart'
//     as recordHouseInterface;
// import 'package:api_package/meterReading_common_record.get.dart'
//     as recordCommonInterface;
import '../models/detail_model.dart';
import '../service/meter_service.dart';

class PrivateDetailStore with ChangeNotifier {
  final BuildContext context, providerContext;
  late Map<String, dynamic> routingArgs;

  PrivateDetailStore(this.context, this.providerContext) {
    routingArgs = (ModalRoute.of(context)?.settings.arguments ??
            new Map<String, dynamic>()) as Map<String, dynamic>? ??
        {};
    initData();
  }
  String get id => routingArgs['id'];
  String get source => routingArgs['source'];

  final scrollViewKey = GlobalKey<WisScrollViewState>();

  late List<RecordModel> list = [];

  Future initData() async {
    await getDetail();
    await getList();
  }

  DetailModel detail = DetailModel();

  Future getDetail() async {
    final res = await MeterService.getMeterDetail({
      "meterId": id,
      "property": source == 'house' ? 0 : 1,
    });
    detail = res.data ?? DetailModel();
    notifyListeners();
  }

  Future getList() async {
    final queryParameters = {
      'meterId': id,
      'pageNum': 1,
      'pageSize': 100,
    };
    if (source == 'house') {
      final res =
          await MeterService.getResidentMeterReadingHisrory(queryParameters);
      list = res.data?.list ?? [];
    } else {
      final res =
          await MeterService.getCommonMeterReadingHisrory(queryParameters);
      list = res.data?.list ?? [];
    }
    notifyListeners();
  }

  bool isDelete = false;

  /// 撤销抄表记录
  Future<void> cancelMeterRead(RecordModel item) async {
    print(item.readerTime);
    final boolean = await WModalUtil.showSync(
      '撤销后无法恢复，是否确认撤销抄表？',
      title: '撤销提示',
      cancelText: '取消',
      confirmText: '确认',
    );
    if (boolean != true) return;
    final res = await MeterService.postCancelMeterRead({
      "meterId": id,
      "property": source == 'house' ? 0 : 1,
      "time": item.readerTime,
    });

    if (res.data == 1) {
      isDelete = true;
      notifyListeners();
      WToastUtil.show('撤销抄表成功！');
      await initData();
    }
  }
}
