//
//  CoinsIncomeTableController.swift

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

import UIKit
import JXPagingView
class CoinsIncomeTableController: CLBaseTableController {

    var header: CoinsIncomeHeadView!
    
    var data: BillListResponse?
    var bills = [BillListInfo]()
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        setupViews()
        setupDatas()
        setupEvents()
    }
    
    private func setupViews() {
        header = {
            let v = CoinsIncomeHeadView(frame: CGRect(x: 0, y: 0, width: UIScreen.width, height: CoinsIncomeHeadView.heightOfHeader))
            return v
        }()
        
        tableView.tableHeaderView = header
        tableView.register(BillListCell.self, forCellReuseIdentifier: "BillListCell")
        
        addRefreshHeaderFooter()
    }
    
    private func setupDatas(){
        tableView.mj_header?.beginRefreshing()
    }
    
    override func loadData() {
        
        var request = BillListRequest()
        request.page = RequestPageData()
        request.page?.ps = 20
        request.type = "INCOME"
        let thePage = page
        request.page?.pn = thePage
        
        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 handleBillResponse(_ model: BillListResponse?){
        
    }
    
    
    private func setupEvents(){
        header.billListEntryButton.addTarget(self, action: #selector(tapBillDetailEntry), for: .touchUpInside)
    }
    
    // MARK: - Action
    
    @objc private func tapBillDetailEntry(){
        let v = BillDetailListController()
        navigationController?.pushViewController(v, animated: true)
    }

    // 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)")
    }
}

extension CoinsIncomeTableController:JXPagingViewListViewDelegate {
    func listView() -> UIView {
        return view
    }

    func listScrollView() -> UIScrollView {
        return tableView
    }

    func listViewDidScrollCallback(callback: @escaping (UIScrollView) -> Void) {
        listViewDidScrollCallback = callback
    }
}
