//
//  YHFamilyRequest.swift
//  galaxy
//
//  Created by edy on 2024/2/1.
//  Copyright © 2024 https://www.galaxy-immi.com. All rights reserved.
//


class YHFamilyRequestViewModel {
    
    var familyInfo:YHFamilyMemberGroupInfo?

    //请求所有家庭成员信息
     func getFamilyInfo(params:[String:Any], callBackBlock:@escaping (_ success: Bool,_ error:YHErrorModel?)->()) {
        
        let strUrl = YHBaseUrlManager.shared.curURL() + YHAllApiName.Order.familyInfoApi
        
         YHHUD.show(.progress(message: "数据加载中..."))
        
        let _ = YHNetRequest.getRequest(url: strUrl, params:params) {[weak self] json, code in
            YHHUD.hide()
            
            guard let self = self else { return }
            guard let model = NetBaseModel.deserialize(dict: json) else {
                let error : YHErrorModel = YHErrorModel(errorCode:YHErrorCode.dictParseError.rawValue,errorMsg: YHErrorCode.dictParseError.description())
                callBackBlock(false,error)
                return
            }
            printLog("model 是 ==> \(model)")
            let dic = model.data?.peel
            guard let resultModel = YHFamilyMemberGroupInfo.deserialize(dict: dic as? [AnyHashable : Any]) else {

                callBackBlock(false,nil)
                return
            }
            familyInfo = resultModel
            callBackBlock(true,nil)
            
        } failBlock: { err in
            callBackBlock(false,err)
        }
    }
    
    // 添加家庭成员
    func addFamilyMember(params:[String:Any], callBackBlock:@escaping (_ success: Bool,_ error:YHErrorModel?)->()) {
       
       YHHUD.show(.progress(message: "数据加载中..."))

       let strUrl = YHBaseUrlManager.shared.curURL() + YHAllApiName.Order.familyInfoApi
       
       let _ = YHNetRequest.postRequest(url: strUrl, params:params) {[weak self] json, code in
           
           YHHUD.hide()
           guard let self = self else { return }
           guard let model = NetBaseModel.deserialize(dict: json) else {
               let error : YHErrorModel = YHErrorModel(errorCode:YHErrorCode.dictParseError.rawValue,errorMsg: YHErrorCode.dictParseError.description())
               callBackBlock(false,error)
               return
           }
           printLog("model 是 ==> \(model)")
           if model.code == NetWorkCode.success.rawValue {
               callBackBlock(true,nil)
           } else {
               callBackBlock(false, nil)
           }
           
       } failBlock: { err in
           callBackBlock(false,err)
       }
   }
    
    func deleteFamilyMember(params:[String:Any], callBackBlock:@escaping (_ success: Bool,_ error:YHErrorModel?)->()) {
        
        YHHUD.show(.progress(message: "数据加载中..."))

        let strUrl = YHBaseUrlManager.shared.curURL() + YHAllApiName.Order.familyMemberDeleteApi
        
        let _ = YHNetRequest.postRequest(url: strUrl, params:params) {[weak self] json, code in
            YHHUD.hide()
            guard let self = self else { return }
            guard let model = NetBaseModel.deserialize(dict: json) else {
                let error : YHErrorModel = YHErrorModel(errorCode:YHErrorCode.dictParseError.rawValue,errorMsg: YHErrorCode.dictParseError.description())
                callBackBlock(false,error)
                return
            }
            printLog("model 是 ==> \(model)")
            if model.code == NetWorkCode.success.rawValue {
                callBackBlock(true,nil)
            } else {
                callBackBlock(false, nil)
            }
            
        } failBlock: { err in
            callBackBlock(false,err)
        }
    }
}
