//
//  YHResignDocumentManagementVC.swift
//  galaxy
//
//  Created by alexzzw on 2024/9/10.
//  Copyright © 2024 https://www.galaxy-immi.com. All rights reserved.
//

import UIKit

class YHResignDocumentManagementVC: YHBaseViewController {
    
    enum RowType {
        case header(_ title: String, _ detail: String)
        case content(_ title: String, _ status: YHResignDocumentStatus)
    }
    private var datas: [[RowType]] {
        let firstSetcion: [RowType] = [.header("​续期递交文书", "(王虎、百里)"), .content("QMAS文件", .awaitingSignature), .content("解释说明", .pendingConfirmation)]
        let secondSetcion: [RowType] = [.header("​补件递交文书", "(王虎来、百里电、汉寒、王全新...)"), .content("QMAS文件", .finalizing), .content("解释说明", .underReview)]
        let thirdSetcion: [RowType] = [.header("​​拒签递交文书", "(王虎、百里)"), .content("QMAS文件", .awaitingSignature), .content("解释说明", .completed)]
        
        return [firstSetcion, secondSetcion, thirdSetcion]
    }
    
    private lazy var tableView: UITableView = {
        let view = UITableView(frame:.zero, style:.grouped)
        view.estimatedSectionHeaderHeight = 16.0
        view.estimatedSectionFooterHeight = 0.01
        view.sectionHeaderHeight = 16.0
        view.sectionFooterHeight = 0.01
        view.contentInsetAdjustmentBehavior = .never
        view.showsVerticalScrollIndicator = false
        view.separatorStyle = .none
        view.delegate = self
        view.dataSource = self
        view.backgroundColor = .clear
        view.rowHeight = 52
        view.tableFooterView = UITableViewHeaderFooterView()
        view.register(YHResignDocumentHeaderCell.self, forCellReuseIdentifier: YHResignDocumentHeaderCell.cellReuseIdentifier)
        view.register(YHResignDocumentContentCell.self, forCellReuseIdentifier: YHResignDocumentContentCell.cellReuseIdentifier)
        return view
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
    }
}

extension YHResignDocumentManagementVC {
    private func setupUI() {
        gk_navTitle = "续期文书管理"
        gk_navBarAlpha = 1.0
        gk_navBackgroundColor = .white
        
        view.backgroundColor = UIColor.contentBkgColor
        view.addSubview(tableView)
        
        tableView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(k_Height_NavigationtBarAndStatuBar)
            make.bottom.equalToSuperview()
        }
    }
}

extension YHResignDocumentManagementVC: UITableViewDelegate, UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        datas.count
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        guard datas.count > section else {
            return 0
        }
        return datas[section].count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard datas.count > indexPath.section else {
            return UITableViewCell()
        }
        let sectionArr = datas[indexPath.section]
        guard sectionArr.count > indexPath.row else {
            return UITableViewCell()
        }
        let item = sectionArr[indexPath.row]
        var cellType: YHResignRoundCellType = .mid
        
        if sectionArr.count == 1 {
            cellType = .single
        } else if indexPath.row == 0 {
            cellType = .top
        } else if indexPath.row == sectionArr.count - 1 {
            cellType = .bottom
        } else {
            cellType = .mid
        }
        
        switch item {
        case let .header(title, detail):
            if let cell = tableView.dequeueReusableCell(withIdentifier: YHResignDocumentHeaderCell.cellReuseIdentifier) as? YHResignDocumentHeaderCell {
                cell.setupCellInfo(title: title, detail: detail, cellType: cellType)
                return cell
            }
        case let .content(title, status):
            if let cell = tableView.dequeueReusableCell(withIdentifier: YHResignDocumentContentCell.cellReuseIdentifier) as? YHResignDocumentContentCell {
                cell.setupCellInfo(title: title, cellType: cellType, status: status)
                return cell
            }
        }
        return UITableViewCell()
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        guard datas.count > indexPath.section else {
            return
        }
        let sectionArr = datas[indexPath.section]
        guard sectionArr.count > indexPath.row else {
            return
        }
        let item = sectionArr[indexPath.row]
        
        switch item {
        case .header:
            return
        case let .content(title, status):
            let controller = YHResignDocumentDetailViewController(.explanatoryStatement, status)
            navigationController?.pushViewController(controller)
        }
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        return UIView()
    }
    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        return UIView()
    }
}
