//
//  YHCerAppointViewModel.swift
//  galaxy
//
//  Created by edy on 2024/8/19.
//  Copyright © 2024 https://www.galaxy-immi.com. All rights reserved.
//

import UIKit

class YHCerAppointViewModel: NSObject {
    
    var serviceArr:[YHServiceItem] = []
    
    func getServicelist(_ callBack: @escaping (_ success: Bool,_ error: YHErrorModel?)->()) {

        let strUrl = YHBaseUrlManager.shared.curURL() + YHAllApiName.MakeCertificate.servicelist
        let _ = YHNetRequest.getRequest(url: strUrl) {
            [weak self] json, code in
            //1. json字符串 转 对象
            guard let self = self else { return }
            printLog(json)
            
            if json.code == 200 {
                let dic = json.data
                guard let list = [YHServiceItem].deserialize(array: dic as? [Any]) else {
                    let err = YHErrorModel(errorCode: YHErrorCode.dictParseError.rawValue, errorMsg: YHErrorCode.dictParseError.description())
                    self.serviceArr = []
                    callBack(false, err)
                    return
                }
                self.serviceArr.removeAll()
                for item in list {
                    if let item = item {
                        self.serviceArr.append(item)
                    }
                }
                callBack(true, nil)
                
            } else {
                let error : YHErrorModel = YHErrorModel(errorCode:Int32(json.code), errorMsg: json.msg)
                self.serviceArr = []
                callBack(false, error)
            }
            
        } failBlock: { err in
            self.serviceArr = []
            callBack(false, err)
        }
    }
}
