/*
 * @Author: XianKaiQun
 * @Date: 2020-08-17 16:59:30
 * @LastEditors: XianKaiQun
 * @LastEditTime: 2020-08-17 16:59:50
 * @Description: 
 */
import 'package:app_package/app_package.dart';
import 'package:flutter/material.dart';
import 'package:get_it/get_it.dart';
import 'package:qrcode_package/src/impl/register_service_impl.dart';
import 'package:qrcode_package/src/models/norm.dart';
import 'package:qrcode_package/src/models/norm_detail.dart';
import 'package:wisdom_cli/wisdom_ui.dart';

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

  final communityGlobalStore = GetIt.I.get<CommunityGlobalStore>();

  CommunityModel get community => communityGlobalStore.data;

  String? get type => routingArgs!['type'];

  String? get id => routingArgs!['id'];

  List<NormModel> allList = [];

  Future<void> getNormList() async {
    final res = await RegisterServiceImpl().getNormList({
      'communityId': community.id,
      'type': type,
    });
    allList = res.data ?? [];
    notifyListeners();
  }

  Future<void> searchName(v) async {
    final res = await RegisterServiceImpl().getNormList({
      'communityId': community.id,
      'type': type,
      'name': v,
    });
    allList = res.data ?? [];
    notifyListeners();
  }

  NormModel? checkedItem;
  void chooseItem(data) {
    checkedItem = data;
    notifyListeners();
  }

  Future<void> routeReturn() async {
    if (checkedItem == null) {
      WToastUtil.show('请先选择规范~');
      return;
    }
    Navigator.pop(context, checkedItem);
  }

  /// 跳转规范详情
  toNormDetail(item) async {
    Navigator.pushNamed(context, 'qrcode/norm_picker/detail',
        arguments: {'id': item.id});
  }

  NormDetailModel? normDetail;

  /// 获取规范详情
  Future<void> getNormDetail() async {
    final res = await RegisterServiceImpl().getNormDetail({
      'id': id,
    });
    normDetail = res.data!;
    notifyListeners();
  }
}
