//
//  YHMyDocumentsDetailViewModel.swift
//  galaxy
//
//  Created by EDY on 2024/3/15.
//  Copyright © 2024 https://www.galaxy-immi.com. All rights reserved.
//

import UIKit

class YHMyDocumentsDetailViewModel: YHBaseViewModel {
    var mainModel: YHMyDocumentDetailModel?
    var urlModel: YHMyDocumentsUrlModel?
    override init() {
        super.init()
    }
    
    func requestDocumentsDetail(_ supplementId: String, callBackBlock:@escaping (_ success: Bool?, _ error: YHErrorModel?)->()) {
        let params: [String : Any] = ["supplement_id": supplementId]
        let strUrl = YHBaseUrlManager.shared.curURL() + YHAllApiName.Documents.detail
        let _ = YHNetRequest.getRequest(url: strUrl, params: params) { [weak self] json, code in
            //1. json字符串 转 对象
            guard let self = self else { return }
            if json.code == 200 {
                let dic = json.data
                guard let result = YHMyDocumentDetailModel.deserialize(from: dic as? Dictionary) else {
                    callBackBlock(false,nil)
                    return
                }
                self.mainModel = result
                callBackBlock(true, nil)
            } else {
                let error : YHErrorModel = YHErrorModel(errorCode:Int32(json.code), errorMsg: json.msg)
                callBackBlock(false, error)
            }
        } failBlock: { err in
            callBackBlock(false, err)
        }
    }
    
    
    func requestDocumentsTemporaryUrl(_ supplementId: String, callBackBlock:@escaping (_ success: Bool, _ error: YHErrorModel?)->()) {
        let params: [String : Any] = ["supplement_id": supplementId]
        let strUrl = YHBaseUrlManager.shared.curURL() + YHAllApiName.Documents.temporaryUrl
        let _ = YHNetRequest.getRequest(url: strUrl, params: params) { [weak self] json, code in
            //1. json字符串 转 对象
            guard let self = self else { return }
            if json.code == 200 {
                let dic = json.data
                guard let result = YHMyDocumentsUrlModel.deserialize(from: dic as? Dictionary) else {
                    callBackBlock(false,nil)
                    return
                }
                self.urlModel = result
                callBackBlock(true, nil)
            } else {
                let error : YHErrorModel = YHErrorModel(errorCode:Int32(json.code), errorMsg: json.msg)
                callBackBlock(false, error)
            }
        } failBlock: { err in
            callBackBlock(false, err)
        }
    }
}
