//
//  YHMainApplicantInformationViewModel.swift
//  galaxy
//
//  Created by EDY on 2024/1/26.
//  Copyright © 2024 https://www.galaxy-immi.com. All rights reserved.
//

import UIKit

class YHMainApplicantInformationViewModel: YHBaseViewModel {
    var mainModel: YHMainInformationModel = YHMainInformationModel()
    
    override init() {
        super.init()
    }
    
    func getBaseDataSource() -> [YHSectionItemModel] {
        let item = YHItemModel(isNeed: true, title: "国家/地区", isUserKeyBoard: false, prompts: "请选择", message: mainModel.nationality, type: .country)
        let section = YHSectionItemModel(title: "国籍", models: [item])
        
        let item4 = YHItemModel(isNeed: true, title: "国家/地区", isUserKeyBoard: false, prompts: "请选择", message: mainModel.address?.country, type: .country)
        var string = ""
        if let array = mainModel.address?.area {
            for item in array {
                string = string + item
            }
        }
        let item1 = YHItemModel(isNeed: true, title: "省市地区", isUserKeyBoard: false, prompts: "请选择", message: string, type: .address)
        
        let item2 = YHItemModel(isNeed: true, title: "详细地址", isUserKeyBoard: true, prompts: "请填写小区、楼栋、单元室等", message: mainModel.address?.details)
        let section1 = YHSectionItemModel(title: "现居住地址", models: [item4, item1, item2])
        
        let item3 = YHItemModel(isNeed: true, title: "是否办理", isUserKeyBoard: false, prompts: "", message: "\(mainModel.has_hk_id ?? 0)", leftButtonString: "是", rightButtonString: "否")
        let section2 = YHSectionItemModel(title: "港澳通行证", models: [item3])
        
        return [section, section1, section2]
    }
    
    func getDocumentInformation() -> [YHSectionItemModel] {
        let item = YHItemModel(isNeed: true, title: "身份证号码", isUserKeyBoard: true, prompts: "请输入", message: mainModel.certificates?.cn_identity_card?.number)
        let item1 = YHItemModel(isNeed: true, title: "签发地", isUserKeyBoard: false, prompts: "请输入", message: mainModel.certificates?.cn_identity_card?.issue_at)
        let item2 = YHItemModel(isNeed: true, title: "签发日期", isUserKeyBoard: false, prompts: "请选择", message: mainModel.certificates?.cn_identity_card?.issue_date_start_at, type: .time)
        let item3 = YHItemModel(isNeed: true, title: "届满日期", isUserKeyBoard: false, prompts: "请选择", message: mainModel.certificates?.cn_identity_card?.issue_date_end_at, type: .time)
        let section = YHSectionItemModel(title: "中国身份证", models: [item, item1, item2, item3])
        
        let item10 = YHItemModel(isNeed: true, title: "身份证号码", isUserKeyBoard: true, prompts: "请输入", message: mainModel.certificates?.hk_macao_pass?.number)
        let item11 = YHItemModel(isNeed: true, title: "签发地", isUserKeyBoard: false, prompts: "请输入", message: mainModel.certificates?.hk_macao_pass?.issue_at)
        let item12 = YHItemModel(isNeed: true, title: "签发日期", isUserKeyBoard: false, prompts: "请选择", message: mainModel.certificates?.hk_macao_pass?.issue_date_start_at, type: .time)
        let item13 = YHItemModel(isNeed: true, title: "届满日期", isUserKeyBoard: false, prompts: "请选择", message: mainModel.certificates?.hk_macao_pass?.issue_date_end_at, type: .time)
        let section1 = YHSectionItemModel(title: "港澳通行证", models: [item10, item11, item12, item13])

        let item4 = YHItemModel(isNeed: false, title: "证件类别", isUserKeyBoard: false, prompts: "请选择", message: mainModel.certificates?.passport?.passport_type, type: .id)
        let item5 = YHItemModel(isNeed: false, title: "证件号码", isUserKeyBoard: true, prompts: "请输入", message: mainModel.certificates?.passport?.number)
        let item6 = YHItemModel(isNeed: false, title: "签发地", isUserKeyBoard: true, prompts: "请输入", message: mainModel.certificates?.passport?.issue_at)
        let item7 = YHItemModel(isNeed: false, title: "签发日期", isUserKeyBoard: false, prompts: "请选择", message: mainModel.certificates?.passport?.issue_date_start_at, type: .time)
        let item8 = YHItemModel(isNeed: false, title: "届满日期", isUserKeyBoard: false, prompts: "请选择", message: mainModel.certificates?.passport?.issue_date_end_at, type: .time)
        let section2 = YHSectionItemModel(title: "护照及其他旅行证件(选填)", models: [item4, item5, item6, item7, item8])
        return [section, section1, section2]
    }
    
    func getDetailDataSource() -> [YHSectionItemModel] {
        let item = YHItemModel(isNeed: true, title: "主申请人姓名", isUserKeyBoard: true, prompts: "请输入", message: mainModel.username)
        let item1 = YHItemModel(isNeed: false, title: "曾用名", isUserKeyBoard: true, prompts: "请与户口本一致，若无则不填", message: mainModel.used_name)
        let item2 = YHItemModel(isNeed: false, title: "婚前姓氏", isUserKeyBoard: true, prompts: "如有则填写，仅支持输入字母", message: mainModel.surname)
        let item3 = YHItemModel(isNeed: true, title: "出生日期", isUserKeyBoard: false, prompts: "请选择", message: mainModel.birthday)
        let item4 = YHItemModel(isNeed: true, title: "出生国家/地区", isUserKeyBoard: false, prompts: nil, message: nil, leftButtonString: "国内", rightButtonString: "国外")
        var string = ""
        if let array = mainModel.birth_place?.area {
            for item in array {
                string = string + item
            }
        }
        let item5 = YHItemModel(isNeed: true, title: "出生城市", isUserKeyBoard: false, prompts: "请选择", message: string, type: .address)
        let item6 = YHItemModel(isNeed: true, title: "性别", isUserKeyBoard: false, prompts: "请选择", message: mainModel.sex, type: .sex)
        let item7 = YHItemModel(isNeed: true, title: "婚姻状况", isUserKeyBoard: false, prompts: "请选择", message: mainModel.married, type: .marry)
        let section = YHSectionItemModel(title: "主申请人信息", models: [item, item1, item2, item3, item4, item5, item6, item7])
        
        let item8 = YHItemModel(isNeed: true, title: "手机号码", isUserKeyBoard: true, prompts: "请输入", message: mainModel.mobile)
        let item9 = YHItemModel(isNeed: true, title: "电子邮箱", isUserKeyBoard: true, prompts: "请输入", message: mainModel.email)
        let section1 = YHSectionItemModel(title: "联系方式", models: [item8, item9])
        
        let item10 = YHItemModel(isNeed: true, title: "是否办理过", isUserKeyBoard: false, prompts: nil, message: "\(mainModel.has_hk_id ?? 0)", leftButtonString: "是", rightButtonString: "否")
        let section2 = YHSectionItemModel(title: "香港身份证", models: [item10])
        return [section, section1, section2]
    }
    
    func getIDCardDataSource() -> [YHSectionItemModel] {
        let item = YHItemModel(isNeed: true, title: "身份证", isUserKeyBoard: false, prompts: "请输入", message: nil)
        let section = YHSectionItemModel(title: "中国身份证(选填)", models: [item])
        let item1 = YHItemModel(isNeed: true, title: "港澳通行证(选填)", isUserKeyBoard: false, prompts: "请输入", message: nil)
        let section1 = YHSectionItemModel(title: "中国身份证(选填)", models: [item])
        return [section, section1]
    }
    
    func requestMainInformation(_ orderID: String, callBackBlock:@escaping (_ success: YHMainInformationModel?, _ error:YHErrorModel?)->()) {
        let strUrl = YHBaseUrlManager.shared.curURL() + "frontend/order-information/get_information?order_id=\(orderID)"
        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(nil,error)
                return
            }
            let dic = model.data?.peel
            guard let result = YHMainInformationModel.deserialize(dict: dic as? [AnyHashable : Any]) else {
                callBackBlock(nil,nil)
                return
            }
            self.mainModel = result
            callBackBlock(result, nil)
        } failBlock: { err in
            callBackBlock(nil,err)
        }
    }
}
