//
//  YHVisaPaymentVC.swift
//  galaxy
//
//  Created by davidhuangA on 2024/8/19.
//  Copyright © 2024 https://www.galaxy-immi.com. All rights reserved.
//

import UIKit

class YHVisaPaymentVC: YHBaseViewController {

    var items: [String] = ["1", "1","1"] //for test hjl

    lazy var tableView: UITableView = {
        let tableView = UITableView(frame:.zero, style:.grouped)
        tableView.estimatedSectionHeaderHeight = 16.0
        tableView.estimatedSectionFooterHeight = 0.01
        tableView.contentInsetAdjustmentBehavior = .never
        tableView.showsVerticalScrollIndicator = false
        tableView.separatorStyle = .none
        tableView.delegate = self
        tableView.dataSource = self
        tableView.backgroundColor = .clear
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "UITableViewCell")
        tableView.register(YHVisaPaymentTipsCell.self, forCellReuseIdentifier: YHVisaPaymentTipsCell.cellReuseIdentifier)
        tableView.register(YHVisaPayTypeCell.self, forCellReuseIdentifier: YHVisaPayTypeCell.cellReuseIdentifier)
        tableView.register(YHPayMembersCell.self, forCellReuseIdentifier: YHPayMembersCell.cellReuseIdentifier)
        return tableView
    }()
    
    
    //我已悉知，确认
    lazy var rewardBtn: UIButton = {
        let button = UIButton(type: .custom)
        button.titleLabel?.font = UIFont.PFSC_M(ofSize: 15)
        button.contentHorizontalAlignment = .center
        button.setTitle("我已全部缴费，下一步", for: .normal)
        button.setTitle("缴费结果更新中，请耐心等待", for: .disabled)
        button.setTitleColor(UIColor.white, for: .normal)
        button.setTitleColor(UIColor.white, for: .disabled)
        button.layer.cornerRadius = kCornerRadius3
        button.clipsToBounds = true
        button.addTarget(self, action: #selector(clickNextBtn), for: .touchUpInside)
        button.backgroundColor = UIColor.brandMainColor
        return button
    }()
    
    lazy var bottomView: UIView = {
        let view = UIView()
        view.backgroundColor = .clear
        view.addSubview(self.rewardBtn)
        self.rewardBtn.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(10)
            make.left.equalTo(kMargin)
            make.right.equalTo(-kMargin)
            make.height.equalTo(44)
        }
        view.layer.zPosition = 10
        return view
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
    }
}

private extension YHVisaPaymentVC {
    func setupUI() {
        gk_navTitle = "签证缴费"
        gk_navBarAlpha = 1.0
        gk_navBackgroundColor = .white
        view.backgroundColor = UIColor.contentBkgColor
        
        view.addSubview(tableView)
        view.addSubview(bottomView)
        bottomView.snp.makeConstraints { make in
            make.bottom.left.right.equalToSuperview()
            make.height.equalTo(98)
        }
        
        tableView.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(kMargin)
            make.right.equalToSuperview().offset(-kMargin)
            make.top.equalTo(k_Height_NavigationtBarAndStatuBar)
            make.bottom.equalTo(bottomView.snp.top)
        }
    }
    
    
    @objc func clickNextBtn() {
        YHHUD.flash(message: "预计会在1个工作日内更新缴费结果")
        disableSureButton()
    }
    
    func disableSureButton() {
        rewardBtn.isEnabled = false
        rewardBtn.backgroundColor = UIColor.brandMainColor30
    }
}


extension YHVisaPaymentVC: UITableViewDelegate, UITableViewDataSource {
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 3
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if 0 <= indexPath.section && indexPath.section < items.count {
            if indexPath.section == 0 {
                let warningCell = tableView.dequeueReusableCell(withIdentifier: YHVisaPaymentTipsCell.cellReuseIdentifier, for: indexPath)
                return warningCell
            } else if indexPath.section == 1 {
                let approveCell = tableView.dequeueReusableCell(withIdentifier: YHVisaPayTypeCell.cellReuseIdentifier, for: indexPath)
                return approveCell
                
            } else if indexPath.section == 2 {
                let approveCell = tableView.dequeueReusableCell(withIdentifier: YHPayMembersCell.cellReuseIdentifier, for: indexPath)
                return approveCell
            } else {
                
            }
        }
            
        let approveCell = tableView.dequeueReusableCell(withIdentifier: YHVisaPayTypeCell.cellReuseIdentifier, for: indexPath)
        return approveCell
        
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if indexPath.section == 0 {
            return YHVisaPaymentTipsCell.cellH
        }
        
        return UITableView.automaticDimension
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        if 0 <= indexPath.section && indexPath.section < items.count {
            
        }
    }
    
    private func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> CGFloat {
        return 16.0
    }
    
    private func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> CGFloat {
        return 0.01
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        
        let view = UIView()
        return view
    }
    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        
        let view = UIView()
        return view
    }
}
