/*
 * @Author: XianKaiQun
 * @LastEditors: XianKaiQun
 * @Date: 2020-11-09 12:00:43
 * @LastEditTime: 2020-11-09 14:55:55
 */
import 'package:collection/collection.dart' show IterableExtension;
import 'package:flutter/material.dart';
import 'package:mobx/mobx.dart';
import 'package:service_package/src/api/api.dart';
import 'package:service_package/src/models/detail.dart';
import 'package:service_package/src/models/enum.dart';
import 'package:service_package/src/models/qrcode.dart';
import 'package:wisdom_cli/new/utils/export.dart';
import 'index.dart';
part 'store.g.dart';

class SuccessPageStore = _SuccessPageStore
    with _$SuccessPageStore, ChangeNotifier;

abstract class _SuccessPageStore with Store {
  final BuildContext context1;
  SuccessPageArguments get arguments =>
      SuccessPageArguments.fromRouteArguments(context1);
  _SuccessPageStore(this.context1);

  String? get id => arguments.id;

  bool get isRepair => arguments.isRepair == true;

  @observable
  DetailModel? detail;

  @observable
  QrcodeModel? qr;

  @action
  Future<void> getData() async {
    await getReportBusiness();
    await getReportOrRepairStatus();
    if (isRepair) {
      final res = await RequestApi().getRepairDetail(id);
      detail = res.data;
      final res2 = await RequestApi().getRepairQrCode(id);
      qr = res2.data;
    } else {
      final res = await RequestApi().getReportDetail(id);
      detail = res.data;
      final res2 = await RequestApi().getReportQrCode(id);
      qr = res2.data;
    }
  }

  ///业务类型
  @observable
  ObservableList<EnumModel> reportBusiness = ObservableList.of([]);

  @computed
  EnumModel? get report => reportBusiness.lastWhereOrNull(
        (e) => e.code == detail?.businessType,
      );
  @action
  Future<void> getReportBusiness() async {
    final res = await RequestApi().getEnum('reportBusiness');
    reportBusiness = ObservableList.of(res.data ?? []);
  }

  ///状态
  List<EnumModel> reportOrRepairStatus = [];

  EnumModel? get status => reportOrRepairStatus.lastWhereOrNull(
        (e) => e.code == detail?.status,
      );
  Future<void> getReportOrRepairStatus() async {
    final res = await RequestApi().getEnum('reportOrRepairStatus');
    reportOrRepairStatus = res.data ?? [];
  }

  ///退到首页
  pop2mainPage() {
    WNavUtil.instance!.pushNamedAndRemoveUntil(
      'main',
      (route) => false,
    );
  }
}
