//
//  BillDetailListController.swift

//
//  Created by Leon on 2025/9/16.
//

import UIKit

/// 流水明细
class BillDetailListController : CLBaseTableController{
    var titleView: TitleView!
    
    var data: BillListResponse?
    var bills = [BillListInfo]()
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupViews()
        setupDatas()
        setupEvents()
    }
    
    private func setupViews() {
        let title = "Transaction Details"
        navigationView.alpha0Title = title
        
        titleView = {
            let v = TitleView()
            v.title = title
            return v
        }()
        titleView.frame = CGRect(x: 0, y: 0, width: UIScreen.width, height: titleView.preCalculateHeight())
        tableView.tableHeaderView = titleView
        tableView.register(BillListCell.self, forCellReuseIdentifier: "BillListCell")
        tableView.snp.remakeConstraints { make in
            make.leading.trailing.bottom.equalToSuperview()
            make.top.equalTo(navigationView.snp.bottom)
        }
        
        addRefreshHeaderFooter()
    }
    
    private func setupDatas(){
        tableView.mj_header?.beginRefreshing()
    }
    
    override func loadData() {
        
        var request = BillListRequest()
        request.page = RequestPageData()
        request.page?.ps = 20
        let thePage = page
        request.page?.pn = thePage
        request.type = "BALANCE"
        
        let dict = request.toNonNilDictionary()
        
        WalletProvider.request(.billList(params: dict), modelType: BillListResponse.self) {[weak self] result in
            self?.tableView.mj_header?.endRefreshing()
            self?.tableView.mj_footer?.endRefreshing()

            switch result {
            case .success(let model):
                self?.data = model
                let theBills = model?.pageList?.datas ?? []
                if thePage == 1{
                    self?.bills = theBills
                    self?.tableView.reloadData()
                    self?.view.setupEmpty(empty: theBills.count <= 0, msg: "暂无流水明细")
                    self?.tableView.mj_footer?.resetNoMoreData()
                }else{
                    self?.bills.append(contentsOf: theBills)
                    if theBills.count <= 0 {
                        self?.tableView.mj_footer?.endRefreshingWithNoMoreData()
                    }
                }
            case .failure:
                break
            }
        }
        
        // "暂无流水明细"
    }
    
    private func setupEvents(){
        
    }
    
    // MARK: - TableView
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return bills.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "BillListCell", for: indexPath) as! BillListCell
        let data = bills[indexPath.row]
        cell.config(data)
        return cell
    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print("didSelectRowAt \(indexPath.row)")
    }
    
    override func scrollViewDidScroll(_ scrollView: UIScrollView) {
        NaviAlphaHandle.changeNaviTitleAlpha(scrollView: scrollView, titleLabel: navigationView.titleLabel)
    }
}
