import 'package:dio/dio.dart';
import 'package:dio/src/form_data.dart' as formdata;
import 'package:ef/ef.dart';
import 'package:json_annotation/json_annotation.dart';
import 'package:vbvs_app/common/color/appConstants.dart';

part 'mhdevice_share_controller.g.dart';

@JsonSerializable()
class MHDeviceShareModel {
  String? phone;
  int? type = 1;

  String? msg;
  int? show = 0;

  int limit = AppConstants.limit;
  int offset = 0;
  bool isLoading = false;
  bool hasMore = true;

  // List<dynamic>? shareUser = [
  //   {"type": 1},
  //   {"type": 2}
  // ];
  List<dynamic>? shareUser = [];
  Map mainShare = {};

  MHDeviceShareModel();
  static MHDeviceShareModel fromJson(Map<String, dynamic> json) =>
      _$MHDeviceShareModelFromJson(json);
  Map<String, dynamic> toJson() => _$MHDeviceShareModelToJson(this);
}

class MHDeviceShareController extends GetControllerEx<MHDeviceShareModel> {
  MHDeviceShareController() {
    attr = GetModel(MHDeviceShareModel()).obs;
  }

  @override
  void onInit() {
    super.onInit();
    model.shareUser = [
      {
        'userName': '张三',
        'opType': 1, // 允许控制
      },
      {
        'userName': '李四',
        'opType': 2, // 仅查看
      },
      {
        'userName': '王五',
        'opType': 1,
      },
    ];
    update(); // 刷新UI
  }

  // Future<String> sendInvite(String? phone, String mac) async {
  //   if (phone == null || phone.isEmpty) {
  //     return "手机号不能为空";
  //   }
  //   if (!MyUtils.isValidPhoneNumber(phone)) {
  //     return "手机号格式不正确";
  //   }
  //   var type = model.type;
  //   try {
  //     var aa = await ApiService.requestNoInfo.post("/api/device/info/share",
  //         data: formdata.FormData.fromMap(
  //             {"mac": mac, "tel": phone, "type": type}));
  //     return "";
  //   } catch (e) {
  //     if (e is DioError) {
  //       // 返回 DioError 的 message 属性
  //       return e.message!;
  //     } else {
  //       // 处理其他类型的错误
  //       return e.toString();
  //     }
  //   }
  // }

  // Future<void> initData() async {
  //   //todo 请求分享列表
  //   var deviceController = Get.find<GlobalController>();
  //   var query = {
  //     "mac": deviceController.model.deviceMain['mac'],
  //   };
  //   var data = await ApiService.request
  //       .get("/api/device/info/share", data: formdata.FormData.fromMap(query));

  //   if (data.data["data"] != null) {
  //     try {
  //       List<dynamic> tmp = data.data["data"] as List<dynamic>;
  //       model.shareUser = tmp;
  //     } catch (e) {
  //       print(e);
  //     }
  //   } else {
  //     model.shareUser = [];
  //   }
  //   updateAll();
  // }

  // //保存
  // saveShareDevice(Map shareInfo, RxInt type) async {
  //   return await ApiService.request.put("/api/device/info/share",
  //       data: formdata.FormData.fromMap({
  //         "mac": shareInfo['mac'],
  //         "tel": shareInfo['tel'],
  //         "type": type.value
  //       }));
  // }

  // //删除
  // deleteShare(Map shareInfo) async {
  //   return await ApiService.request.delete("/api/device/info/share",
  //       data: formdata.FormData.fromMap(
  //           {"mac": shareInfo['mac'], "tel": shareInfo['tel']}));
  // }
}
