//
//  YHPrincipleReviewListVC.swift
//  galaxy
//
//  Created by edy on 2024/4/29.
//  Copyright © 2024 https://www.galaxy-immi.com. All rights reserved.
//

import UIKit

class YHPrincipleReviewListVC: YHBaseViewController {

    var items: [YHPrincleGroupModel] = []
    lazy var tableView: UITableView = {
        let tableView = UITableView(frame:.zero, style:.grouped)
        tableView.contentInsetAdjustmentBehavior = .never
        tableView.estimatedSectionHeaderHeight = 24.0
        tableView.estimatedSectionFooterHeight = 1.0
        tableView.showsVerticalScrollIndicator = false
        tableView.separatorStyle = .none
        tableView.delegate = self
        tableView.dataSource = self
        tableView.backgroundColor = UIColor.white
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "UITableViewCell")
        tableView.register(YHPrincipleGroupReviewListCell.self, forCellReuseIdentifier: YHPrincipleGroupReviewListCell.cellReuseIdentifier)
        tableView.register(YHPrincipleGroupUploadCell.self, forCellReuseIdentifier: YHPrincipleGroupUploadCell.cellReuseIdentifier)
        tableView.register(YHPrincipleUnapprovedCell.self, forCellReuseIdentifier: YHPrincipleUnapprovedCell.cellReuseIdentifier)
        return tableView
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = .white
        self.gk_navTitle = "原则批"
        createUI()
        
        items.removeAll()
        let m1 = YHPrincleGroupModel()
        m1.fileList = ["新公司在职证明.pdf",
                       "主申人的曾用名证明文件.pdf",
                       "无刑事罪行记录证明.pdf",
                       "新公司在职证明.pdf",
                       "主申人的曾用名证明文件.pdf",
                       "无刑事罪行记录证明.pdf"]
        
        let m2 = YHPrincleGroupModel()
        m2.fileList = ["新公司在职证明.pdf",
                       "主申人的曾用名证明文件.pdf"]
        
        let m3 = YHPrincleGroupModel()
        m3.fileList = []
        
        let m4 = YHPrincleGroupModel()
        m4.fileList = ["新公司在职证明.pdf",
                       "主申人的曾用名证明文件.pdf",
                       "主申人的曾用名证明文件.pdf",
                       "无刑事罪行记录证明.pdf"]
        
        items.append(contentsOf: [m1, m2, m3, m4])
        self.tableView.reloadData()
        
    }
    
    func createUI() {
        view.addSubview(tableView)
        tableView.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(k_Height_NavigationtBarAndStatuBar)
            make.left.right.bottom.equalToSuperview()
        }
    }
}

extension YHPrincipleReviewListVC: UITableViewDelegate, UITableViewDataSource {

    func numberOfSections(in tableView: UITableView) -> Int {
        return items.count
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        if indexPath.section%3 == 0 {
            let cell = tableView.dequeueReusableCell(withIdentifier: YHPrincipleGroupUploadCell.cellReuseIdentifier, for: indexPath) as! YHPrincipleGroupUploadCell
            return cell
            
        } else if indexPath.section%3 == 1 {
            
            let cell = tableView.dequeueReusableCell(withIdentifier: YHPrincipleGroupReviewListCell.cellReuseIdentifier, for: indexPath) as! YHPrincipleGroupReviewListCell
            
            if 0 <= indexPath.section && indexPath.section < items.count {
                
                let model = items[indexPath.section]
                cell.updateModel(model)
                cell.expandClick = {
                    [weak self] isExpand in
                    guard let self = self else { return }
                    model.isExpand = isExpand
                    self.tableView.reloadData()
                }
            }
            return cell
        }
        
        let cell = tableView.dequeueReusableCell(withIdentifier: YHPrincipleUnapprovedCell.cellReuseIdentifier, for: indexPath) as! YHPrincipleUnapprovedCell
        
            if 0 <= indexPath.section && indexPath.section < items.count {
                let model = items[indexPath.section]
                cell.updateModel(model)
                cell.expandClick = {
                    [weak self] isExpand in
                    guard let self = self else { return }
                    model.isExpand = isExpand
                    self.tableView.reloadData()
                }
            }
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableView.automaticDimension
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
       
        if 0 <= indexPath.section && indexPath.section < items.count {
#if DEBUG
            
            if true {
                let vc = YHPrincipleCongratulationVC()
                self.navigationController?.pushViewController(vc)
                return
            }
#endif
        }

    }

    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> CGFloat {
        return 24.0
    }
    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> CGFloat {
        return 1.0
    }
    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        
        return UIView()
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        
        return UIView()
    }
}
