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

import UIKit

class YHEducationRequestViewModel {
    // 学历和专业信息
    var educationInfo: YHEducationListInfo?
    // 学历详情信息
    var educationDetailInfo: YHEducationDetailInfo?
    // 专业详情信息
    var qualificationDetailInfo: YHQualificationDetailInfo?
    
    // 请求学历和专业证书信息
    func requestEducationInfo(params:[String:Any], callBackBlock:@escaping (_ success: Bool,_ error:YHErrorModel?)->()) {
       
       YHHUD.show(.progress(message: "数据加载中..."))

       let strUrl = YHBaseUrlManager.shared.curURL() + YHAllApiName.Education.getEducationAndQulificationInfoApi
       
       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 let resultModel = YHEducationListInfo.deserialize(dict: dic as? [AnyHashable : Any]) else {

               callBackBlock(false,nil)
               return
           }
           educationInfo = resultModel
           callBackBlock(true,nil)
           
       } failBlock: { err in
           callBackBlock(false,err)
       }
   }
    
    // 获取学历详情
    func getEducationDetailInfo(params:[String:Any], callBackBlock:@escaping (_ success: Bool,_ error:YHErrorModel?)->()) {
        
        YHHUD.show(.progress(message: "数据加载中..."))

        let strUrl = YHBaseUrlManager.shared.curURL() + YHAllApiName.Education.getEducationDetailInfoApi
        
        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 let resultModel = YHEducationDetailInfo.deserialize(dict: dic as? [AnyHashable : Any]) else {

                callBackBlock(false,nil)
                return
            }
            educationDetailInfo = resultModel
            callBackBlock(true,nil)
            
        } failBlock: { err in
            callBackBlock(false,err)
        }
    }
    
    // 保存学历
    func saveEducationInfo(params:[String:Any], callBackBlock:@escaping (_ success: Bool,_ error:YHErrorModel?)->()) {
        
        YHHUD.show(.progress(message: "数据加载中..."))

        let strUrl = YHBaseUrlManager.shared.curURL() + YHAllApiName.Education.saveEducationInfoApi
        
        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 deleteEducationInfo(id:Int, callBackBlock:@escaping (_ success: Bool,_ error:YHErrorModel?)->()) {
        
        YHHUD.show(.progress(message: "数据加载中..."))

        let strUrl = YHBaseUrlManager.shared.curURL() + YHAllApiName.Education.deleteEducationInfoApi
        
        let _ = YHNetRequest.postRequest(url: strUrl, params:["id" : id]) {[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 deleteQualificationInfo(id:Int, callBackBlock:@escaping (_ success: Bool,_ error:YHErrorModel?)->()) {
        
        YHHUD.show(.progress(message: "数据加载中..."))

        let strUrl = YHBaseUrlManager.shared.curURL() + YHAllApiName.Qualification.deleteQualificationInfoApi
        
        let _ = YHNetRequest.postRequest(url: strUrl, params:["id" : id]) {[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 saveAllEducationInfo(params:[String:Any], callBackBlock:@escaping (_ success: Bool,_ error:YHErrorModel?)->()) {
        
        YHHUD.show(.progress(message: "数据加载中..."))

        let strUrl = YHBaseUrlManager.shared.curURL() + YHAllApiName.Education.saveAllEduAndQuaInfoApi
        
        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 getQualificationDetailInfo(id:String, callBackBlock:@escaping (_ success: Bool,_ error:YHErrorModel?)->()) {
        
        YHHUD.show(.progress(message: "数据加载中..."))

        let strUrl = YHBaseUrlManager.shared.curURL() + YHAllApiName.Qualification.getQualificationDetailInfoApi
        
        let _ = YHNetRequest.getRequest(url: strUrl, params:["id" : id]) {[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 let resultModel = YHQualificationDetailInfo.deserialize(dict: dic as? [AnyHashable : Any]) else {

                callBackBlock(false,nil)
                return
            }
            qualificationDetailInfo = resultModel
            callBackBlock(true,nil)
            
        } failBlock: { err in
            callBackBlock(false,err)
        }
    }
    
    // 保存专业信息
    func saveQualificationInfo(params:[String:Any], callBackBlock:@escaping (_ success: Bool,_ error:YHErrorModel?)->()) {
        
        YHHUD.show(.progress(message: "数据加载中..."))

        let strUrl = YHBaseUrlManager.shared.curURL() + YHAllApiName.Qualification.saveQualificationInfoApi
        
        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)
        }
    }
    
    
}
