//
//  YHServiceCenterMainViewModel.swift
//  galaxy
//
//  Created by davidhuangA on 2024/1/23.
//  Copyright © 2024 https://www.galaxy-immi.com. All rights reserved.
//

import UIKit
import SmartCodable
class YHServiceCenterMainViewModel: YHBaseViewModel {
    //合同列表 数据
    var arrContactList : [YHContactItemModel] = []
    
    //评分相关的 本地数据
    var localScoreData : YHScoreDataModel?
}

//请求接口
extension YHServiceCenterMainViewModel {
    //请求合同项
    func getContactList(callBackBlock:@escaping (_ success: Bool,_ error:YHErrorModel?)->()) {
        let strUrl = YHBaseUrlManager.shared.curURL() + YHAllApiName.Order.contractListApi
        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
            }
            printLog("model 是 ==> \(model)")
            let dic = model.data?.peel
            guard let resultModel = YHContactItemHoldModel.deserialize(dict: dic as? [AnyHashable : Any]) else {
                self.arrContactList = []
                callBackBlock(false,nil)
                return
            }
            
            self.arrContactList = resultModel.orders
            callBackBlock(true,nil)
        } failBlock: { err in
            callBackBlock(false,err)
        }
    }
}

//其他方法
extension YHServiceCenterMainViewModel {
    //加载本地 plist数据
    func loadLocalScoreData() {
        if let path = Bundle.main.path(forResource: "scoreOptionData", ofType: "plist") {
            if let plistData = FileManager.default.contents(atPath: path) {
                do {
                    let listDictionary = try PropertyListSerialization.propertyList(from: plistData, options: [], format: nil) as! [String: Any]
                    guard let model = YHScoreDataModel.deserialize(dict: listDictionary) else {
                        localScoreData = nil
                        return
                    }
                    localScoreData = model
                } catch {
                    localScoreData = nil
                }
            } else {
                localScoreData = nil
            }
        } else {
            localScoreData = nil
        }
    }
}

//合同数据结构
struct YHContactItemHoldModel : SmartCodable {
    var orders : [YHContactItemModel] = []
}
