CNLiveSelectHangYeController.swift 13.7 KB
//
//  CNLiveSelectHangYeController.swift
//  metaCode
//
//  Created by zx on 2024/7/23.
//  我的职业页面

import Foundation
import SnapKit

typealias SelectAddWorkZhiYeBlock = (_ zhiyeStr:String) -> Void

class CNLiveSelectHangYeController:CNLiveBaseViewController, UITableViewDataSource, UITableViewDelegate{
    var isAddWorkJiLu = false//是否是添加工作记录
    var selectAddWorkZhiYeBlock:SelectAddWorkZhiYeBlock?
    
    var currSeleFirIndex = 0
    var currSeleSecIndex = -1
    
    lazy var firstDataArray :Array<MineAuthenticationsChildModel> = {
        let dataArray = Array<MineAuthenticationsChildModel>()
        return dataArray
    }()
    lazy var secDataArray :Array<MineAuthenticationsChildModel> = {
        let dataArray = Array<MineAuthenticationsChildModel>()
        return dataArray
    }()
    let kCNLiveHangYeFirCellIdentifier = "kCNLiveHangYeFirCellIdentifier"
    let kCNLiveHangYeSecCellIdentifier = "kCNLiveHangYeSecCellIdentifier"

    lazy var firstTableView : UITableView = {
        let tableView = UITableView(frame: .zero, style: .plain)
        tableView.dataSource = self
        tableView.delegate = self
        tableView.separatorStyle = .none
        tableView.register(CNLiveHangYeFirCell.self, forCellReuseIdentifier: kCNLiveHangYeFirCellIdentifier)
        tableView.backgroundColor = HexColor("#F6F7FB")
        return tableView
    }()
    lazy var secTableView : UITableView = {
        let tableView = UITableView(frame: .zero, style: .plain)
        tableView.dataSource = self
        tableView.delegate = self
        tableView.separatorStyle = .none
        tableView.tableFooterView = UIView.init()
        tableView.register(CNLiveHangYeSecCell.self, forCellReuseIdentifier: kCNLiveHangYeSecCellIdentifier)
        tableView.backgroundColor = .white
        return tableView
    }()
    lazy var bottomZhiNanWhiteView :UIView = {
        let bottomNavView = UIView()
        bottomNavView.backgroundColor = .white
        return bottomNavView
    }()
    lazy var jiuYeZhiNanBtn: UIButton = {
        let  nextBtn = UIButton(type: .custom)
        nextBtn.setTitle("确认提交", for: .normal)
        nextBtn.setTitleColor(.white, for: .normal)
        nextBtn.backgroundColor = HexColor("#1961FE")
        nextBtn.layer.cornerRadius = 20
        nextBtn.clipsToBounds = true
        nextBtn.addTarget(self, action: #selector(tijiaoBtnAction), for: .touchUpInside)
        return nextBtn
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        titleName = "我的职业"
        navigationBarHidden = false
        
        self.view.addSubview(self.firstTableView)
        self.firstTableView.snp.makeConstraints { make in
            make.top.equalTo(KNavigationHeight)
            make.left.equalToSuperview()
            make.height.equalToSuperview().offset(-KNavigationHeight-60)
            make.width.equalTo(adapt_length(length: 107))
        }
        
        self.view.addSubview(self.secTableView)
        self.secTableView.snp.makeConstraints { make in
            make.top.equalTo(KNavigationHeight)
            make.right.equalToSuperview()
            make.height.equalToSuperview().offset(-KNavigationHeight-60)
            make.left.equalTo(self.firstTableView.snp.right)
        }
        
        self.view.addSubview(self.bottomZhiNanWhiteView)
        self.bottomZhiNanWhiteView.snp.makeConstraints { make in
            make.top.equalTo(self.firstTableView.snp.bottom)
            make.bottom.left.right.equalToSuperview()
        }
        
        self.bottomZhiNanWhiteView.addSubview(self.jiuYeZhiNanBtn)
        self.jiuYeZhiNanBtn.snp.makeConstraints { make in
            make.left.equalTo(15)
            make.right.equalToSuperview().offset(-15)
            make.top.equalTo(10)
            make.height.equalTo(40)
        }

        
        let vsid = UserInfoManager.shared.userInfo.currentUser?.vsid ?? ""
        let sid = UserInfoManager.shared.userInfo.uid
        self.getHangYeData(vsid: vsid, sid: sid)
    }
    
    deinit {
    }
    
    @objc func tijiaoBtnAction(){
        
        if self.isAddWorkJiLu{
            let firModel = self.firstDataArray[self.currSeleFirIndex]
            let secModel = self.secDataArray[self.currSeleSecIndex]
            let hangYeStr = firModel.name+"·"+secModel.name
            self.selectAddWorkZhiYeBlock?(hangYeStr)
            self.navigationController?.viewControllers.forEach({ item in
                if (item.isKind(of: CNLiveVitaeAddWorkController.self)) {
                    self.navigationController?.popToViewController(item, animated: true)
                }
            })
        }else{
            let isRealChengNuo = self.getRealChengNuo()
            if isRealChengNuo == false{
                //弹出承诺弹窗
                self.show17Alert()
            }else{
                //确认后返回到数字资产页面 接口变更行业 变更为对应行业
                self.popZiChanController()
            }

        }
    }
    
    // MARK: - 获取行业数据
    func getHangYeData(vsid:String,sid:String){
        CNLiveMineViewModel.requestsIndustryInfo(channelId: "", type: "2", sid: sid, vsid: vsid) { result, respStatue in
            if respStatue == .success{
                if let res = result as? [MineAuthenticationsChildModel]{
                    //添加每个MineAuthenticationsBaseModel到self.dataArray对应的 频道列表model里
                    if res.count > 0{
                        self.firstDataArray = res
                        self.secDataArray = res[0].child
                        self.firstTableView.reloadData()
                        self.secTableView.reloadData()
                        self.currSeleFirIndex = 0
                        self.currSeleSecIndex = -1
                    }else{
                        //没有行业列表 ,提示暂无内容
                    }
                }
            }else{
                
            }
        }
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if tableView == self.firstTableView{
            return self.firstDataArray.count
        }else{
            return self.secDataArray.count
        }
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if tableView == self.firstTableView{
            //fir
            guard let cell = tableView.dequeueReusableCell(withIdentifier: kCNLiveHangYeFirCellIdentifier) as? CNLiveHangYeFirCell
            else {
                let cell = CNLiveHangYeFirCell()
                return cell
            }
            let authModel = self.firstDataArray[indexPath.row]
            cell.authModel = authModel
            if self.currSeleFirIndex == indexPath.row{
                cell.isSelect = true
            }else{
                cell.isSelect = false
            }
            return cell
        }else{
            //sec
            guard let cell = tableView.dequeueReusableCell(withIdentifier: kCNLiveHangYeSecCellIdentifier) as? CNLiveHangYeSecCell
            else {
                let cell = CNLiveHangYeSecCell()
                return cell
            }
            let authModel = self.secDataArray[indexPath.row]
            cell.authModel = authModel
            if self.currSeleSecIndex == indexPath.row{
                cell.isSelect = true
            }else{
                cell.isSelect = false
            }
            return cell
        }
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if tableView == self.firstTableView{
            self.currSeleFirIndex = indexPath.row
            self.currSeleSecIndex = -1
            self.secDataArray = self.firstDataArray[indexPath.row].child
            self.firstTableView.reloadData()
            self.secTableView.reloadData()

        }else{
            self.currSeleSecIndex = indexPath.row
            self.secTableView.reloadData()
        }
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        50
    }

    func popZiChanController(){
        if let userDic = UserDefaults.standard.dictionary(forKey: UserInfoManager.shared.userInfo.uid){
            let jianli_city = userDic["jianli_city"] as? String
            let jianli_vitaeId = userDic["jianli_vitaeId"] as? String
            let vsid = userDic["vsid"] as? String
            let jianli_profession = userDic["jianli_profession"] as? String ?? ""
            let jianli_professionId = userDic["jianli_professionId"] as? String
            let jianli_icon = userDic["jianli_icon"] as? String ?? ""
            let jianli_phone = userDic["jianli_phone"] as? String ?? ""
            let jianli_selfIntroduction = userDic["jianli_selfIntroduction"] as? String
            let jianli_workTime = userDic["jianli_workTime"] as? String
            let jianli_showAge = userDic["jianli_showAge"] as? Bool ?? false
            let jianli_showAddress = userDic["jianli_showAddress"] as? Bool ?? false
            let jianli_showAwards = userDic["jianli_showAwards"] as? Bool ?? false
            let jianli_showPhone = userDic["jianli_showPhone"] as? Bool ?? false
            let jianli_updateTime = userDic["jianli_updateTime"] as? String ?? ""
            
            let firModel = self.firstDataArray[self.currSeleFirIndex]
            let secModel = self.secDataArray[self.currSeleSecIndex]
            let hangYeStr = firModel.name+"·"+secModel.name
            CNLiveVitaeViewModel.saveVitae(city: jianli_city ?? "", icon: jianli_icon, isSave: false, id: jianli_vitaeId ?? "", phone: jianli_phone, profession: hangYeStr, professionId: String(format: "%d", secModel.industryId), selfIntroduction: jianli_selfIntroduction ?? "", showAddress: jianli_showAddress, showAge: jianli_showAge, showAwards: jianli_showAwards, showPhone:jianli_showPhone, updateTime: jianli_updateTime, vsid: vsid ?? "", workTime: jianli_workTime ?? ""){ result, respStatue in
                if respStatue == .success{
                    
                    self.navigationController?.viewControllers.forEach({ item in
                        if (item.isKind(of: CNLiveDigitalResumeController.self)) {
                            self.navigationController?.popToViewController(item, animated: true)
                        }
                    })
                }
            }
        }else{
            let tempDic = NSMutableDictionary()
            UserDefaults.standard.setValue(tempDic, forKey: UserInfoManager.shared.userInfo.uid)
            UserDefaults.standard.synchronize()
        }


    }
    // MARK: - Alert弹窗
    let delegate = UIApplication.shared.delegate as! AppDelegate
    /// 未认证-inputType17时弹窗背景图
    lazy var alertBgImgView:UIImageView = {
        let alertBgImgView:UIImageView = UIImageView()
        alertBgImgView.tag = 20001
        alertBgImgView.isUserInteractionEnabled = true
        let ges = UITapGestureRecognizer(target: self, action: #selector(alertBgImgViewClickAction))
        alertBgImgView.addGestureRecognizer(ges)

        return alertBgImgView
    }()
    lazy var alertBgView:UIView = {
        let alertBgView:UIView = UIView(frame: CGRect(x: 0, y: 0, width: KSreenWidth, height: KSreenHeight))
        alertBgView.backgroundColor = HexColor("#000000")
        alertBgView.alpha = 0.6
        alertBgView.isUserInteractionEnabled = true
        alertBgView.tag = 20000
        
        let ges = UITapGestureRecognizer(target: self, action: #selector(alertBgViewClickAction))
        alertBgView.addGestureRecognizer(ges)

        return alertBgView
    }()
    // MARK: - 点击弹窗,按类型跳转
    @objc func alertBgImgViewClickAction() {
        //存储本地行业真实承诺
        self.saveRealChengNuo()
        self.hiden17Alert()
        self.popZiChanController()
    }
    func saveRealChengNuo(){
        if var userDic = UserDefaults.standard.dictionary(forKey: UserInfoManager.shared.userInfo.uid){
            userDic.updateValue(true, forKey: "hangYeChengNuo")
            UserDefaults.standard.setValue(userDic, forKey: UserInfoManager.shared.userInfo.uid)
            UserDefaults.standard.synchronize()
        }else{
            let tempDic = NSMutableDictionary()
            UserDefaults.standard.setValue(tempDic, forKey: UserInfoManager.shared.userInfo.uid)
            UserDefaults.standard.synchronize()
        }
    }
    func getRealChengNuo()->Bool{
        if var userDic = UserDefaults.standard.dictionary(forKey: UserInfoManager.shared.userInfo.uid){
            if let isChengNuo = userDic["hangYeChengNuo"] as? Bool{
                if isChengNuo == true{
                    return true
                }else{
                    return false
                }
            }else{
                return false
            }
        }else{
            let tempDic = NSMutableDictionary()
            UserDefaults.standard.setValue(tempDic, forKey: UserInfoManager.shared.userInfo.uid)
            UserDefaults.standard.synchronize()
            return false
        }
        
    }
    @objc func alertBgViewClickAction() {
        //点击蒙版隐藏view
        self.hiden17Alert()
    }
    //弹出弹窗
    func show17Alert(){
        
        delegate.window?.addSubview(self.alertBgView)
        delegate.window?.addSubview(self.alertBgImgView)
        alertBgImgView.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.centerY.equalToSuperview()
            make.width.equalTo(KSreenWidth-120)
            make.height.equalTo((KSreenWidth-120)*330/390)
        }
        
        self.alertBgImgView.image = UIImage(named: "mine_chengnuo")
    }
    func hiden17Alert(){
        delegate.window?.subviews.forEach({ view in
            if view.tag >= 20000{
                view.removeFromSuperview()
            }
        })
    }
}