/*
 * @Author       : WuWei
 * @LastEditors: ChenYaJin
 * @Date         : 2021-09-27 10:22:17
 * @LastEditTime: 2023-08-04 14:22:01
 * @Description  : Do not edit
 */
import 'dart:convert';

import 'package:app_package/app_package.dart';
import 'package:flutter/material.dart';
import 'package:mobx/mobx.dart';
import 'package:task_package/src/impl/qrcode_impl.dart';
import 'package:task_package/src/pages/repair_form/index.dart';
import 'package:wisdom_cli/wisdom_cli.dart';
part 'repair_form_store.g.dart';

class RepairFormStore = _RepairFormStore with _$RepairFormStore, ChangeNotifier;

abstract class _RepairFormStore with Store {
  ///上下文（请勿修改）
  final BuildContext context1;

  ///路由参数（请勿修改）
  RepairFormPageArguments get arguments =>
      RepairFormPageArguments.fromRouteArguments(context1);

  _RepairFormStore(this.context1) {
    ///可在此处理进入页面前的操作，比如响应路由的传参进行赋值。
  }
  get detail => arguments.detail ?? null;

  onInit() async {
    presetName = onGetPresetName();
    typeName = onGetTypeName();
    getTypeList();
  }

  /// 预设清单类型对应操作名称
  @observable
  String? presetName = '';

  /// 获取当前点位类型名称
  @action
  String onGetPresetName() {
    switch (detail?.type) {
      case '451357265515982848':
        return '安保巡逻';
      case '451357265520177153':
        return '设备保养';
      case '451357265520177152':
        return '设备巡检';
      case '522832327887884288':
        return '保洁清洁';
      case '525361915470270464':
        return '绿化养护';
      case '718505360034664448':
        return '品质巡查';
      default:
        return '--';
    }
  }

  /// 预设清单类型对应操作名称
  @observable
  String? typeName = '';

  /// 获取当前点位类型名称
  @action
  String onGetTypeName() {
    switch (detail?.relationType) {
      case 'pointPosition':
        return '点位';
      case 'devicePoint':
        return '设备';
      case 'deviceRoom':
        return '设备间';
      default:
        return '--';
    }
  }

  @observable
  String businessTypeId = '';
  @observable
  String sortId = '';
  @observable
  ObservableList typeList = ObservableList.of([]);
  @observable
  ObservableList sortList = ObservableList.of([]);

  @action
  setType(value) {
    businessTypeId = value;
    getSortList();
  }

  @action
  setSort(value) {
    sortId = value;
  }

  @action
  Future getTypeList() async {
    var res = await QrcodeServiceImpl.instance!.getTypeList();
    typeList = ObservableList.of(jsonDecode(jsonEncode(res.data)) ?? []);
  }

  @action
  Future getSortList() async {
    var res = await QrcodeServiceImpl.instance!
        .getSortList({'businessTypeId': businessTypeId});
    sortList = ObservableList.of(jsonDecode(jsonEncode(res.data)) ?? []);
  }

  Future<void> onHandleRepair() async {
    if (description == '' || images.length == 0) {
      WToastUtil.show('请完成必填项');
      return;
    }
    WLoadingUtil.show();
    String communityId = detail?.nodeData?.communityId;
    var res = await QrcodeServiceImpl.instance!.postTaskEquRepair({
      'communityId': communityId,
      'description': description,
      'photos': images.map((element) => element['id']).toList(),
      'relationSource': detail?.type,
      'relationSourceId': detail?.exPlatformId,
      'relationId': detail?.id,
      'relationType': detail?.relationType,
      'businessTypeId': businessTypeId,
      'sortId': sortId,
    });
    WLoadingUtil.hide();
    if (res != null) {
      await WToastUtil.show('提交报修成功');
      Navigator.of(context1).pop();
    }
  }

  /// 上传图片
  @observable
  ObservableList images = ObservableList.of([]);
  @action
  onSelectedImg(List<FileEntity> files) async {
    final futures =
        files.map((e) => GlobalApi.postUploadImageObject(e.file!)).toList();
    try {
      WLoadingUtil.show();
      final res = await Future.wait(futures);
      images.addAll(res.map((e) => e).toList());
    } finally {
      WLoadingUtil.hide();
    }
  }

  ///表单删除图片
  @action
  onImageRemoveAt(index) {
    images.removeAt(index);
  }

  /// 输入内容
  @observable
  String? description = '';
  @action
  onInput(v) {
    description = v;
  }
}
