//
//  YHCollegeSearchViewController.swift
//  galaxy
//
//  Created by edy on 2024/2/22.
//  Copyright © 2024 https://www.galaxy-immi.com. All rights reserved.
//

import UIKit

class YHCollegeSearchViewController: YHBaseViewController {
    
    var searchCollegeName:String?
    var selectBlock:((String?)->Void)?
    var colleges:[Any] = [1,2,3]
    
    lazy var searchBar: YHSearchBar = {
        let bar = YHSearchBar.createBar()
        return bar
    }()
    
    lazy var tableView: UITableView = {
        
        let tableView = UITableView(frame:.zero, style:.plain)
        
        if #available(iOS 11.0, *) {
            tableView.contentInsetAdjustmentBehavior = .never
        }
        tableView.showsVerticalScrollIndicator = false
        tableView.backgroundColor = .clear
        tableView.separatorStyle = .none
        tableView.delegate = self
        tableView.dataSource = self
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "UITableViewCell")
        tableView.register(YHCollegeNameCell.self, forCellReuseIdentifier: YHCollegeNameCell.cellReuseIdentifier)
        return tableView
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        createUI()
    }
    
    func createUI() {
        
        self.view.backgroundColor = .white
        self.gk_navTitle = "学校名称".local

        view.addSubview(tableView)
        view.addSubview(searchBar)
        
        if let searchCollegeName = searchCollegeName {
            searchBar.textField.text = searchCollegeName
        }
        searchBar.textChange = {
           [weak self]  text in
            guard let self = self else { return }
        }
        searchBar.confirmBlock = {
            [weak self] in
            guard let self = self else { return }
            if let selectBlock = selectBlock {
                selectBlock(searchBar.textField.text)
                self.navigationController?.popViewController(animated: true)
            }
        }
        
        searchBar.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(16)
            make.right.equalToSuperview().offset(-16)
            make.height.equalTo(YHSearchBar.height)
            make.top.equalToSuperview().offset(k_Height_NavigationtBarAndStatuBar+16)
        }
        
        tableView.snp.makeConstraints { make in
            make.top.equalTo(searchBar.snp.bottom).offset(8)
            make.left.right.bottom.equalToSuperview()
        }
    }
}

extension YHCollegeSearchViewController: UITableViewDelegate, UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return colleges.count
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 36.0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: YHCollegeNameCell.cellReuseIdentifier, for: indexPath) as! YHCollegeNameCell
        cell.titleLabel.text = "XXXXXXXXX"
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if let selectBlock = selectBlock {
            selectBlock("")
            self.navigationController?.popViewController(animated: true)
        }
    }
}
