//
//  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.Family.familyInfoApi
//        let strUrl = "http://192.168.34.187:8808/" + YHAllApiName.Family.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 }
             
            
            printLog("model 是 ==> \(json)")
            let dic = json.data as? [AnyHashable : Any]
            guard let resultModel = YHFamilyMemberGroupInfo.deserialize(dict: dic) else {

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

       let strUrl = YHBaseUrlManager.shared.curURL() + YHAllApiName.Family.familyInfoApi
//       let strUrl = "http://192.168.34.187:8808/" + YHAllApiName.Family.familyInfoApi
       
       let _ = YHNetRequest.postRequest(url: strUrl, params:params) {[weak self] json, code in
           
           if isShowLoading {
               YHHUD.hide()
           }
           guard let self = self else { return }
           printLog("model 是 ==> \(json)")
           if json.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.Family.deleteFamilyMemberApi
        
        let _ = YHNetRequest.postRequest(url: strUrl, params:params) { json, code in
            YHHUD.hide()
            printLog("model 是 ==> \(json)")
            if json.code == NetWorkCode.success.rawValue {
                callBackBlock(true,nil)
            } else {
                callBackBlock(false, nil)
            }
            
        } failBlock: { err in
            callBackBlock(false,err)
        }
    }
    // 提交所有家庭成员信息
    func submitAllFamilyInfo(orderId:Int, callBackBlock:@escaping (_ success: Bool,_ error:YHErrorModel?)->()) {
        
        YHHUD.show(.progress(message: "数据加载中..."))

        let strUrl = YHBaseUrlManager.shared.curURL() + YHAllApiName.Family.submitAllFamilyInfoApi
        
        let _ = YHNetRequest.postRequest(url: strUrl, params:["order_id":orderId]) { json, code in
            YHHUD.hide()
            printLog("model 是 ==> \(json)")
            if json.code == NetWorkCode.success.rawValue {
                callBackBlock(true,nil)
            } else {
                callBackBlock(false, nil)
            }
            
        } failBlock: { err in
            callBackBlock(false,err)
        }
    }
}
