//
//  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 as? [AnyHashable : Any]
            
            guard var dic = dic else { return }
            
#if DEBUG

if let child = dic["child"], child is [[String: Any]] {
    var child = child as! [[String: Any]]
    
    for (index, dic) in child.enumerated() {
        var d = dic
        d["certificates"] = [String: Any]()
        child[index] = d
    }
    dic["child"] = child
    
}
            
if let brother = dic["brother"], brother is [[String: Any]] {
    var brother = brother as! [[String: Any]]
    
    for (index, dic) in brother.enumerated() {
        var d = dic
        d["certificates"] = [String: Any]()
        
//        ["cnIdentityCard":"",
//         "passport":"",
//         "hkMacaoPass":""]
        
        brother[index] = d
    }
    dic["brother"] = brother
}
#endif

            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 addOrSaveFamilyMember(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) { json, code in
            YHHUD.hide()
            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)
        }
    }
}
