/*
 * @Author: XianKaiQun
 * @Date: 2020-07-28 17:45:42
 * @LastEditors: XianKaiQun
 * @LastEditTime: 2020-08-10 16:29:49
 * @Description: 
 */
import 'package:flutter/material.dart';
import 'package:qrcode_package/src/impl/security_service_impl.dart';
import 'package:qrcode_package/src/models/clean_security_list.dart';
import 'package:qrcode_package/src/models/qrcode_detail.dart';
import 'package:wisdom_cli/wisdom_cli.dart';

class PrivateSecurityListStore with ChangeNotifier {
  final BuildContext context, providerContext;
  late Map<String, dynamic> routingArgs;
  PrivateSecurityListStore(this.context, this.providerContext) {
    routingArgs = (ModalRoute.of(context)?.settings.arguments ??
            new Map<String, dynamic>()) as Map<String, dynamic>? ??
        {};
  }

  final scrollViewKey = GlobalKey<WisScrollViewState>();
  QrcodeDetailModel? data;
  String? get id => routingArgs['id'];
  late Map<String, dynamic> params = {
    'pageNum': 1,
    'pageSize': 20,
  };
  List<Map>? qrEnum = [];
  List<Map>? statusEnum = [];
  List<CleanSecurityListModel> list = [];

  route2detail(CleanSecurityListModel item) {
    Navigator.pushNamed(context, 'qrcode/security_detail',
        arguments: {'id': item.id});
  }

  route2Form() {
    Navigator.pushNamed(context, 'qrcode/security', arguments: {
      'id': id,
    });
  }

  Future onInit() async {
    await Future.wait(
      [getList(isLoad: false), getDetail()],
    );
  }

  getQrEnum() async {
    final res = await SecurityServiceImpl.instance!.getQrcodetypeEnum();
    qrEnum = res.data;
  }

  getStatusEnum() async {
    final res = await SecurityServiceImpl.instance!.getEquipmentStatusEnum();
    statusEnum = res.data;
  }

  ///清洁记录列表
  Future getList({bool isLoad = true}) async {
    if (!isLoad) params['pageNum'] = 1;
    var res = await SecurityServiceImpl.instance!.getSecurityList({
      ...params,
      'qrcodeManagementId': id,
    });
    final _data = res.data;
    final _list = _data?.list ?? [];
    if ((_data?.total ?? 0) == 0) {
      scrollViewKey.currentState?.isEmpty();
    } else if ((_data?.pages ?? 0) <= (_data?.pageNum ?? 0)) {
      scrollViewKey.currentState?.isLast();
    } else {
      params['pageNum']++;
    }
    isLoad ? list.addAll(_list) : list = _list;
    notifyListeners();
  }

  ///详情
  Future getDetail() async {
    final res = await SecurityServiceImpl.instance!.getQrcodeDetail(id);
    data = res.data;
    notifyListeners();
  }
}
