//
//  YHOtherInfoFillViewModel.swift
//  galaxy
//
//  Created by davidhuangA on 2024/2/18.
//  Copyright © 2024 https://www.galaxy-immi.com. All rights reserved.
//

import UIKit

class YHOtherInfoFillViewModel: YHBaseViewModel {
    var otherInfoFillData : YHOtherInfoFillModel?
    
    var arrLanguage : [[String : String]] =  [
        ["id":"1",
         "title": "中文及英文",
         "dec": "国外大学（英语国家）毕业，英文授课，获得学位证。（如为中外联合办学，在中国上课的，不算）",
        ],
        [
            "id": "2",
            "title": "中文及英文",
            "dec": " 有近两年的雅思或托福成绩单（雅思考试G类或A类总分达6分；托福笔试达550分，计算机达213分，网考达80分）",
        ],
        [
            "id": "4",
            "title": "中文及另一种外语",
            "dec": "非英文的外语，如：法文、日文等（需提供相关证书）",
        ],
        [
            "id": "5",
            "title": "仅中文",
            "dec": "",
        ],
        [
            "id": "6",
            "title": "仅英文",
            "dec": "",
        ],
    ]
    
    
    var arrSkill : [[String : String]] =  [
        [
            "title": "学术研究及教育",
            "dec": "幼儿教育、高等教育、学术研究",
            "val": "",
            "value": "1",
        ],
        [
            "title": "建筑、测量、工程及建造",
            "dec": "园景设计、建筑、测量、土木/电机/机器/机构/环境工程",
            "val": "",
            "value": "2",
        ],
        [
            "title": "艺术及文化",
            "dec": "表演艺术、美术、博物馆、图书馆、摄影",
            "val": "",
            "value": "3",
        ],
        [
            "title": "广播及娱乐",
            "dec": "广播及节目制作、电影、录像及电视制作、唱片及音乐出版",
            "val": "",
            "value": "4",
        ],
        [
            "title": "业务支援及人力资源",
            "dec": "业务咨询顾问、公司秘书、行政支援、人事管理、招聘服务、公关关系服务、广告、信息服务",
            "val": "",
            "value": "5",
        ],
        [
            "title": "餐饮服务及旅游",
            "dec": "酒吧、餐厅、酒店、旅行代理",
            "val": "",
            "value": "6",
        ],
        [
            "title": "商业及贸易",
            "dec": "进出口、零售、批发",
            "val": "",
            "value": "7",
        ],
        [
            "title": "金融及会计服务",
            "dec": "会计、银行、保险、证券、投资银行",
            "val": "",
            "value": "8",
        ],
        [
            "title": "医疗保健及兽医服务",
            "dec": "西医药、中医药、牙科服务、医学科技、职业治疗、护理、兽医服务",
            "val": "",
            "value": "9",
        ],
        [
            "title": "资讯科技及电讯",
            "dec": "资讯科技顾问、固网及无线通信",
            "val": "",
            "value": "10",
        ],
        [
            "title": "法律服务",
            "dec": "法律事务",
            "val": "",
            "value": "11",
        ],
        [
            "title": "物流运输",
            "dec": "物流、空运、海运、陆运",
            "val": "",
            "value": "12",
        ],
        [
            "title": "工业制造",
            "dec": "电机、电子、食物及饮料、化工、金属、玩具、纺织、钟表、珠宝、印刷及出版",
            "val": "",
            "value": "13",
        ],
        [
            "title": "体育运动",
            "dec": "体育运动",
            "val": "",
            "value": "14",
        ],
        [
            "title": "其他",
            "example": "",
            "val": "",
            "value": "15",
            "dec": ""
        ],
    ]
}

extension YHOtherInfoFillViewModel {
    
    //获取其他资料信息
    func getOtherInfo( params:[String : Any],callBackBlock:@escaping (_ success: Bool,_ error:YHErrorModel?)->()) {
        //        createOrgBasiceInfoData()
        //        callBackBlock(true,nil)//先返回数据 让界面展示相关的UI
        
        
        let strUrl = YHBaseUrlManager.shared.curURL() + YHAllApiName.Order.otherInfoFillApi
        let _ = YHNetRequest.getRequest(url: strUrl,params: params) { [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 = YHOtherInfoFillModel.deserialize(dict: dic as? [AnyHashable : Any]) else {
                callBackBlock(false,nil)
                return
            }
            
            self.otherInfoFillData = resultModel
            
            handleData()
            
            //根据返回值 组装 真实的数据
            callBackBlock(true,nil)
        } failBlock: { err in
            callBackBlock(false,err)
        }
    }
    
    
    //保存其他资料信息
    func saveOtherInfo( params:[String : Any],callBackBlock:@escaping (_ success: Bool,_ error:YHErrorModel?)->()) {
        let strUrl = YHBaseUrlManager.shared.curURL() + YHAllApiName.Basics.updateBasicInfoApi
        let _ = YHNetRequest.getRequest(url: strUrl,params: params) { [weak self] json, code in
            //1. json字符串 转 对象
            guard self != nil 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 = YHBasicInfoFillModel.deserialize(dict: dic as? [AnyHashable : Any]) else {
            //                callBackBlock(false,nil)
            //                return
            //            }
            //
            //            self.dataModelForBasicInfo = resultModel
            //根据返回值 组装 真实的数据
            callBackBlock(true,nil)
        } failBlock: { err in
            callBackBlock(false,err)
        }
    }
    
}


extension YHOtherInfoFillViewModel {
    func handleData() {
        if let language = self.otherInfoFillData?.language_detail {
            if language.level > 0 {
                let target = String(language.level)
                for(_,item) in arrLanguage.enumerated() {
                    
                    if target == item["id"] {
                        language.levelDes = item["title"] ?? ""
                        break
                    }
                }
            }
        }
        
        
        
        if let profession = self.otherInfoFillData?.profession {
            if profession.option > 0 {
                let target = String(profession.option)
                for(_,item) in arrSkill.enumerated() {
                    
                    if target == item["value"] {
                        profession.optionDes = item["title"] ?? ""
                        break
                    }
                }
            }
        }
    }
}
