//
//  YHWorkIntroductionViewModel.swift
//  galaxy
//
//  Created by EDY on 2024/2/21.
//  Copyright © 2024 https://www.galaxy-immi.com. All rights reserved.
//

import UIKit

class YHWorkIntroductionViewModel: YHBaseViewModel {
    var introducetionExampleModels: [YHWorkExampleModel]?

    override init() {
        super.init()
    }
    
    func requestIntroducetionExample(callBackBlock:@escaping (_ success: Bool?, _ error:YHErrorModel?)->()) {
        let strUrl = YHBaseUrlManager.shared.curURL() + "frontend/workInfo/item-example"
        let _ = YHNetRequest.getRequest(url: strUrl) { [weak self] json, code in
            //1. json字符串 转 对象
            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
            }
            let dic = model.data?.peel
            guard let result = [YHWorkExampleModel].deserialize(array: dic as? [Any]) else {
                callBackBlock(false,nil)
                return
            }
            self.introducetionExampleModels = result as? [YHWorkExampleModel]
            callBackBlock(true, nil)
        } failBlock: { err in
            callBackBlock(false,err)
        }
    }
    
    // 0保存 1下一步
    func saveWorkProjectDetail(_ workModel: YHWorkExperienceDetailModel, projectModel: YHWorkExperienceProjectModel, callBackBlock:@escaping (_ success: Bool?, _ error:YHErrorModel?)->()) {
        let params: [String : Any] = ["app_id": workModel.create_from_appid ?? 0,
                    "id": projectModel.id ?? 0,
                    "order_id": workModel.order_id ?? 0,
                    "project_end_time": projectModel.project_end_time ?? "",
                    "project_highlights": projectModel.project_highlights ?? 0,
                    "project_name": projectModel.project_name ?? 0,
                    "project_start_time": projectModel.project_start_time ?? "",
                    "work_id": workModel.id ?? 0
                    ]
        let strUrl = YHBaseUrlManager.shared.curURL() + "frontend/workInfo/project/save"
        let _ = YHNetRequest.postRequest(url: strUrl, params: params) { [weak self] json, code in
            guard let self = self else { return }
            callBackBlock(true, nil)
        } failBlock: { err in
            callBackBlock(false, err)
        }
    }
}

