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

import UIKit

class YHWorkExperienceViewModel: YHBaseViewModel {
    var mainModel: YHWorkExperienceDetailModel = YHWorkExperienceDetailModel()
    var exampleModels: [YHWorkExampleModel]?
    var projectModels: [YHWorkExperienceProjectModel]?
    override init() {
        super.init()
    }
    
    func getBaseDataSource() -> [YHSectionWorkExperienceModel] {
        let item = YHWorkExperienceModel(id: .id1, isNeed: true, title: "用人单位", isUserKeyBoard: false, prompts: "请输入", message: mainModel.company_name ?? "", type: .unit)
        let item1 = YHWorkExperienceModel(id: .id2, isNeed: true, title: "用人单位性质", isUserKeyBoard: false, prompts: "请输入", message: mainModel.industry ?? "", type: .nature)
        let item2 = YHWorkExperienceModel(id: .id3, isNeed: false, title: "用人单位介绍", isUserKeyBoard: true, prompts: "如方便提供，请填写", message: mainModel.company_introduce ?? "")
        let item3 = YHWorkExperienceModel(id: .id4, isNeed: false, title: "用人单位官网", isUserKeyBoard: true, prompts: "如有，请输入官网链接", message: mainModel.company_website ?? "")
        let item4 = YHWorkExperienceModel(id: .id5, isNeed: true, title: "出生国家/地区", isUserKeyBoard: false, prompts: nil, message: mainModel.location?.country ?? "", type: .country)
        var string = ""
        if let array = mainModel.location?.area {
            for item in array {
                string = string + item
            }
        }
        let item5 = YHWorkExperienceModel(id: .id6, isNeed: true, title: "出生城市", isUserKeyBoard: false, prompts: "请选择", message: string, type: .address)
        let item55 = YHWorkExperienceModel(id: .id7, isNeed: true, title: "出生城市", isUserKeyBoard: true, prompts: "请输入", message: mainModel.location?.foreign ?? "")
        let item6 = YHWorkExperienceModel(id: .id8, isNeed: true, title: "职位", isUserKeyBoard: true, prompts: "请输入", message: mainModel.position ?? "")
        let item7 = YHWorkExperienceModel(id: .id9, isNeed: true, title: "入职年月", isUserKeyBoard: false, prompts: "请选择", message: mainModel.entry_time ?? "", type: .time)
        let item8 = YHWorkExperienceModel(id: .id10, isNeed: true, title: "离职年月", isUserKeyBoard: false, prompts: "请选择", message: mainModel.departure_time ?? "", type: .time)
        let item9 = YHWorkExperienceModel(id: .id11, isNeed: true, title: "工作证明文件", isUserKeyBoard: false, prompts: "请选择", message: mainModel.work_certificate ?? "", type: .certificate)
        let item10 = YHWorkExperienceModel(id: .id12, isNeed: false, title: "职责性质", isUserKeyBoard: true, prompts: "请选择", message: mainModel.duty ?? "")
        let item11 = YHWorkExperienceModel(id: .id13, isNeed: false, title: "相关工作经验是否属于国际工作经验", isUserKeyBoard: false, prompts: "", message: "\(mainModel.international_work_experience ?? 0)", leftButtonString: "是", rightButtonString: "否")
        let addressCountry = mainModel.location?.country ?? ""
        var section = YHSectionWorkExperienceModel()
        if addressCountry.contains("中国") {
            section = YHSectionWorkExperienceModel(title: "企业信息", models: [item, item1, item2, item3, item4, item5, item6, item7, item8, item9, item10, item11])
        } else {
            section = YHSectionWorkExperienceModel(title: "企业信息", models: [item, item1, item2, item3, item4, item55, item6, item7, item8, item9, item10, item11])
        }
        
        let item12 = YHWorkExperienceModel(id: .id14, isNeed: true, title: "企业人数规模", isUserKeyBoard: true, prompts: "请输入", message: "\(mainModel.company_member_total ?? 0)")
        let item13 = YHWorkExperienceModel(id: .id15, isNeed: false, title: "行政架构层级", isUserKeyBoard: false, prompts: "请输入", message: "\(mainModel.administrative_level ?? 0)", type: .structure)
        let item14 = YHWorkExperienceModel(id: .id16, isNeed: true, title: "下属管理人数", isUserKeyBoard: true, prompts: "请输入", message: "\(mainModel.company_manage_total ?? 0)")
        let item15 = YHWorkExperienceModel(id: .id17, isNeed: false, title: "公司营业额/港元 (近一年)", isUserKeyBoard: true, prompts: "请输入具体数字", message: "\(mainModel.turnover ?? 0)")
        let item16 = YHWorkExperienceModel(id: .id18, isNeed: false, title: "公司业务性质/范畴/所属行业", isUserKeyBoard: true, prompts: "请输入，100字内", message: mainModel.business_nature ?? "")
        let item17 = YHWorkExperienceModel(id: .id19, isNeed: true, title: "职位水平类别", isUserKeyBoard: false, prompts: "请选择", message: "\(mainModel.professional_level ?? 0)", type: .level)
        let item18 = YHWorkExperienceModel(id: .id20, isNeed: true, title: "高管证明文件", isUserKeyBoard: false, prompts: "请选择", message: mainModel.executives_certificate ?? "", type: .prove)
        let item19 = YHWorkExperienceModel(id: .id21, isNeed: true, title: "高管在职开始时间", isUserKeyBoard: false, prompts: "请选择", message: mainModel.top_manager_start_at ?? "", type: .time)
        let item20 = YHWorkExperienceModel(id: .id22, isNeed: true, title: "高管在职结束时间", isUserKeyBoard: false, prompts: "请选择", message: mainModel.top_manager_end_at ?? "", type: .time)
        var section1 = YHSectionWorkExperienceModel()
        if mainModel.professional_level == 1 {
            section1 = YHSectionWorkExperienceModel(title: "企业规模", models: [item12, item13, item14, item15, item16, item17, item18, item19, item20])
        } else {
            section1 = YHSectionWorkExperienceModel(title: "企业规模", models: [item12, item13, item14, item15, item16, item17])
        }
        
        return [section, section1]
    }
    
    func updateModel(_ item: YHWorkExperienceModel) {
        guard let type = item.id else { return }
        switch type {
        
        case .id1: // 用人单位
            mainModel.company_name = item.message
        case .id2: // 用人单位性质
            mainModel.industry = item.message
        case .id3: // 用人单位介绍
            mainModel.company_introduce = item.message
        case .id4: // 用人单位官网
            mainModel.company_website = item.message
        case .id5: // 工作地区国家
            mainModel.location?.country = item.message
        case .id6: // 工作城市 国内
            mainModel.location?.area = item.value
        case .id7: // 工作城市国外
            mainModel.location?.foreign = item.message
        case .id8: // 职位
            mainModel.position = item.message
        case .id9: // 入职年月
            mainModel.entry_time = item.message
        case .id10: // 离职年月
            mainModel.departure_time = item.message
        case .id11: // 工作证明文件
            mainModel.work_certificate = item.message
        case .id12: // 职责性质
            mainModel.duty = item.message
        case .id13: // 是否国际工作经验
            mainModel.international_work_experience = Int(item.value?.first ?? "0")
        case .id14: // 企业人数规模
            mainModel.company_member_total = Int(item.message ?? "0")
        case .id15: // 行政架构层级
            mainModel.administrative_level = Int(item.value?.first ?? "0")
        case .id16: // 下属管理人数
            mainModel.company_manage_total = Int(item.message ?? "0")
        case .id17: // 公司营业额/港元
            mainModel.turnover = Int(item.message ?? "0")
        case .id18: // 公司性质/范畴/所属行业
            mainModel.business_nature = item.message
        case .id19: // 职位水平类型
            mainModel.professional_level = Int(item.value?.first ?? "0")
        case .id20: // 高管证明文件
            mainModel.executives_certificate = item.message
        case .id21: // 高管在职开始时间
            mainModel.top_manager_start_at = item.message
        case .id22: // 高管在职结束时间
            mainModel.top_manager_end_at = item.message
        }
    }
    
    func updateWduty(_ value: String) {
        mainModel.wduty = value
    }
    
    func updateHighlights(_ value: String) {
        mainModel.highlights = value
    }
    
    func updateIsProjectIntroduction(isOn: Bool) {
        if isOn {
            mainModel.is_project_introduction = 1
        } else {
            mainModel.is_project_introduction = 0
        }
    }
    
    func updateNotNeedUploadFile(value: Int) {
        mainModel.not_need_upload_file = value
    }
    
    func requestWorkExample(callBackBlock:@escaping (_ success: Bool?, _ error:YHErrorModel?)->()) {
        let strUrl = YHBaseUrlManager.shared.curURL() + "frontend/workInfo/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.exampleModels = result as? [YHWorkExampleModel]
            callBackBlock(true, nil)
        } failBlock: { err in
            callBackBlock(false,err)
        }
    }
    
    func requestWorkDetail(_ workId: String, callBackBlock:@escaping (_ success: Bool?, _ error:YHErrorModel?)->()) {
        let strUrl = YHBaseUrlManager.shared.curURL() + "frontend/workInfo/detail?id=\(workId)"
        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 = YHWorkExperienceDetailModel.deserialize(dict: dic as? [AnyHashable : Any]) else {
                callBackBlock(false,nil)
                return
            }
            self.mainModel = result
            callBackBlock(true, nil)
        } failBlock: { err in
            callBackBlock(false,err)
        }
    }
    
    // 0保存 1下一步
    func saveWorkDetail(_ saveType: Int, callBackBlock:@escaping (_ success: Bool?, _ error:YHErrorModel?)->()) {
        let area: [String] = mainModel.location?.area ?? []
        let location = ["area": area,
                       "country": mainModel.location?.country ?? "",
                       "details": mainModel.location?.details ?? "",
                       "foreign": mainModel.location?.foreign ?? ""] as [String : Any]
        let params: [String : Any] = ["administrative_level": mainModel.administrative_level ?? 0,
                    "business_nature": mainModel.business_nature ?? "",
                    "business_remark":mainModel.business_remark ?? "",
                    "company_introduce": mainModel.company_introduce ?? "",
                    "company_manage_total": mainModel.company_manage_total ?? 0,
                    "company_member_total": mainModel.company_member_total ?? 0,
                    "company_name": mainModel.company_name ?? "",
                    "company_website": mainModel.company_website ?? "",
                    "create_from_appid": mainModel.create_from_appid ?? 0,
                    "created_at": mainModel.created_at ?? "",
                    "customer_center_id": mainModel.customer_center_id ?? 0,
                    "deleted_at": mainModel.deleted_at ?? "",
                    "departure_time": mainModel.departure_time ?? "",
                    "duty": mainModel.duty ?? "",
                    "entry_time": mainModel.entry_time ?? "",
                    "executives_certificate": mainModel.executives_certificate ?? "",
                    "file": mainModel.file ?? [],
                    "highlights": mainModel.highlights ?? "",
                    "id": mainModel.id ?? 0,
                    "industry": mainModel.industry ?? "",
                    "international_work_experience": mainModel.international_work_experience ?? 0,
                    "is_executives": mainModel.is_executives ?? 0,
                    "is_famous_enterprise": mainModel.is_famous_enterprise ?? 0,
                    "is_project_introduction": mainModel.is_project_introduction ?? 0,
                    "is_show": mainModel.is_show ?? 0,
                    "is_synthetic": mainModel.is_synthetic ?? 0,
                    "location": location,
                    "location_aboard": mainModel.location_aboard ?? 0,
                    "master_of_important_project": mainModel.master_of_important_project ?? 0,
                    "match_listing": mainModel.match_listing ?? 0,
                    "not_need_upload_file": mainModel.not_need_upload_file ?? 0,
                    "old_id": mainModel.old_id ?? 0,
                    "operator_english_name": mainModel.operator_english_name ?? "",
                    "operator_id": mainModel.operator_id ?? 0,
                    "operator_name": mainModel.operator_name ?? "",
                    "order_id": mainModel.order_id ?? 0,
                    "position": mainModel.position ?? "",
                    "professional_level": mainModel.professional_level ?? 0,
                    "professional_other": mainModel.professional_other ?? "",
                    "projects": mainModel.projects ?? [],
                    "remark": mainModel.remark ?? "",
                    "step": mainModel.step ?? 0,
                    "top_manager_end_at": mainModel.top_manager_end_at ?? "",
                    "top_manager_start_at": mainModel.top_manager_start_at ?? "",
                    "turnover": mainModel.turnover ?? 0,
                    "update_from_appid": mainModel.update_from_appid ?? 0,
                    "updated_at": mainModel.updated_at ?? "",
                    "wduty": mainModel.wduty ?? "",
                    "work_certificate": mainModel.work_certificate ?? "",
                    "verify": saveType
                    ]
        let strUrl = YHBaseUrlManager.shared.curURL() + "frontend/workInfo/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)
        }
    }
    
    func requestWorkInfoProject(_ workId: String, callBackBlock:@escaping (_ success: [YHWorkExperienceProjectModel]?, _ error:YHErrorModel?)->()){
        let strUrl = YHBaseUrlManager.shared.curURL() + "frontend/workInfo/project/list?work_id=\(workId)"
        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([],error)
                return
            }
            let dic = model.data?.peel
            guard let resultArray = [YHWorkExperienceProjectModel].deserialize(array: dic as? [Any]) else {
                callBackBlock([],nil)
                return
            }
            self.projectModels = resultArray as? [YHWorkExperienceProjectModel]
            callBackBlock(self.projectModels, nil)
        } failBlock: { err in
            callBackBlock([],err)
        }
    }
    
    func requestDeleteWorkExperienceProject(_ orderId: String, callBackBlock:@escaping (_ success: Bool?, _ error:YHErrorModel?)->()) {
        let params: [String : Any] = ["id": orderId]
        let strUrl = YHBaseUrlManager.shared.curURL() + "frontend/workInfo/project/del"
        let _ = YHNetRequest.postRequest(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
            }
            callBackBlock(true, nil)
        } failBlock: { err in
            callBackBlock(false, err)
        }
    }
    
}
