/*
 * @Author: XianKaiQun
 * @Date: 2020-07-29 11:47:02
 * @LastEditors  : WuWei
 * @LastEditTime : 2023-02-08 15:48:36
 * @Description: 
 */
import 'package:app_package/app_package.dart';
import 'package:flutter/material.dart';
import 'package:qrcode_package/src/impl/repair_service_impl.dart';
import 'package:qrcode_package/src/models/repair_equipment_detail.dart';
import 'package:wisdom_cli/wisdom_cli.dart';

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

  ///接受参数
  String? get id => routingArgs['id'];

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

  RepairEquipmentDetailModel? data;

  Future getDetail() async {
    final res = await RepairServiceImpl.instance!.getEquipmentDetail(id);
    data = res.data ?? RepairEquipmentDetailModel();
    notifyListeners();
  }

  ///维修前图片
  List<String> beforeImages = [];

  Future onBeforeImagePicker() async {
    final asset =
        await WAssetPickerUtil.image(source: WImageSource.camera, maxCount: 1);
    if (asset.isNotEmpty == true) {
      final res = await GlobalApi.postUploadImage(asset[0].file);
      if (res!.data != null) {
        beforeImages.add(res.data!);
      }
    }
    notifyListeners();
  }

  removeBeforeImage(int i) {
    beforeImages.removeAt(i);
    notifyListeners();
  }

  ///维修后图片
  List<String> afterImages = [];

  Future onAfterImagePicker() async {
    final asset = await WAssetPickerUtil.image(
      source: WImageSource.camera,
      maxCount: 1,
    );
    if (asset.isNotEmpty == true) {
      final res = await GlobalApi.postUploadImage(asset[0].file);
      if (res!.data != null) {
        afterImages.add(res.data!);
      }
    }
    notifyListeners();
  }

  removeAfterImage(int i) {
    afterImages.removeAt(i);
    notifyListeners();
  }

  ///输入框
  String exremark = '';
  onInputChange(String value) {
    exremark = value;
  }

  ///定位 TOD
  // Future<BdLatLng> getLocation() async {
  //   final res = await WisLocaitonUtil.instance.getLocation();
  //   if ((res.latLng?.latitude ?? 0) != 0) {
  //     return res.latLng.toBmap();
  //   }
  //   return null;
  // }

  Future submit() async {
    if (exremark == '' || afterImages.length == 0 || beforeImages.length == 0) {
      WToastUtil.show('请完成选项');
      return;
    }
    // 定位  TOD
    // final loc = await getLocation();
    // if (loc == null) {
    //   return;
    // }
    final params = {
      'afterImg': afterImages,
      'beforeImg': beforeImages,
      'content': exremark,
      'equipmentId': id,
      // 定位  TOD
      // 'latitude': loc.latitude,
      // 'longitude': loc.longitude,
    };
    final res = await RepairServiceImpl.instance!.postService(params);
    if (res.data != null) {
      WModalUtil.showSync(
        '提交成功',
        cancelText: '返回',
        confirmText: '查看详情',
      ).then(
        (value) {
          !value!
              ? Navigator.pop(context)
              : Navigator.popAndPushNamed(
                  context,
                  'qrcode/repair_detail',
                  arguments: {'id': id},
                );
        },
      );
    }
  }
}
