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

import UIKit

class YHCompanySelectViewController: YHBaseViewController {

    public var backLocationStringController: ((String) -> Void)?
    var searchView: UITextField!
    var sureButton: UIButton!
    var tableView: UITableView!
    var searchDataSource: [YHCompanyModelListModel]?
    var viewModel: YHCompanyViewModel?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        gk_navTitle = "用人单位"
        viewModel = YHCompanyViewModel()
        setView()
        getData()
    }
    
    func getData() {
        tableView.reloadData()
        
    }
    
    func setView() {
        view.backgroundColor = .white
        searchView = {
            let searchBar = UITextField()
            searchBar.placeholder = "请输入用人单位全称"
            searchBar.backgroundColor = UIColor(hex: 0xf8f9fb)
            searchBar.keyboardType = .default
            searchBar.delegate = self
            searchBar.layer.cornerRadius = 21
            searchBar.leftView = UIView(frame: CGRect(x: 0, y: 0, width: 16, height: 42))
            searchBar.leftViewMode = .always
            return searchBar
        }()
        view.addSubview(searchView)
        searchView.snp.makeConstraints { make in
            make.top.equalTo(k_Height_NavigationtBarAndStatuBar + 16)
            make.height.equalTo(42)
            make.left.equalTo(16)
            make.right.equalTo(-16)
        }
        
        sureButton = {
            let button = UIButton(type: .custom)
            button.backgroundColor =  UIColor(hex:0x2274ee)
            button.titleLabel?.font = UIFont.PFSC_M(ofSize: 16)
            button.contentHorizontalAlignment = .center
            button.setTitle("保存", for: .normal)
            button.setTitleColor( UIColor(hex:0xffffff), for: .normal)
            button.layer.cornerRadius = 18
            button.addTarget(self, action: #selector(submit), for: .touchUpInside)
            return button
        }()
        view.addSubview(sureButton)
        sureButton.snp.makeConstraints { make in
            make.centerY.equalTo(searchView.snp.centerY)
            make.height.equalTo(36)
            make.width.equalTo(80)
            make.right.equalTo(-16)
        }
        
        tableView = {
            let tableView = UITableView(frame:.zero, style:.plain)
            if #available(iOS 11.0, *) {
                tableView.contentInsetAdjustmentBehavior = .never
            }
            tableView.backgroundColor = .clear
            tableView.separatorStyle = .none
            tableView.delegate = self
            tableView.dataSource = self
            tableView.register(cellWithClass: YHCompanyTableViewCell.self)
            return tableView
        }()
        view.addSubview(tableView)
        tableView.snp.makeConstraints { make in
            make.top.equalTo(k_Height_NavigationtBarAndStatuBar + 78)
            make.bottom.equalTo(-k_Height_safeAreaInsetsBottom() - 64)
            make.left.right.equalTo(view)
        }
    }
    
    @objc func submit() {
        if let backLocationStringController = backLocationStringController {
            backLocationStringController(searchView.text ?? "")
            self.navigationController?.popViewController()
        }
    }
}

extension YHCompanySelectViewController: UITextFieldDelegate {
    func textFieldDidChangeSelection(_ textField: UITextField) {
        if textField.text == "" {
            tableView.isHidden = true
        } else {
            viewModel?.requestCompany(textField.text ?? "") { [weak self] success, error in
                guard let self = self else { return }
                self.searchDataSource = success
                self.tableView.isHidden = false
                self.tableView.reloadData()
            }
        }
    }
}

extension YHCompanySelectViewController: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return searchDataSource?.count ?? 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withClass: YHCompanyTableViewCell.self)
        cell.titleLabel.text = searchDataSource?[indexPath.row].company_name
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 44
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let text = searchDataSource?[indexPath.row].company_name ?? ""
        if let backLocationStringController = backLocationStringController {
            backLocationStringController(text)
            self.navigationController?.popViewController()
        }
    }
}

class YHCompanyTableViewCell: UITableViewCell {
    var titleLabel: UILabel!
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        setupUI()
    }
    
    func setupUI() {
        titleLabel = {
            let label = UILabel()
            label.font = UIFont.PFSC_R(ofSize: 13)
            label.textColor = UIColor.mainTextColor
            return label
        }()
        contentView.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.left.equalTo(22)
            make.centerY.equalToSuperview()
            make.height.equalTo(20)
        }
    }
}

