//
//  YHCertificateResourceUploadVC.swift
//  galaxy
//
//  Created by edy on 2024/3/13.
//  Copyright © 2024 https://www.galaxy-immi.com. All rights reserved.
//

import UIKit
import JXSegmentedView
import UniformTypeIdentifiers
import IQKeyboardManagerSwift

class YHCertificateResourceUploadVC: YHBaseViewController {

    var orderId : Int?
    var items:[YHSupplementInfo] = []
    var viewModel: YHCertificateViewModel = YHCertificateViewModel()

    lazy var tableView: UITableView = {
        
        let tableView = UITableView(frame:.zero, style:.grouped)

        if #available(iOS 11.0, *) {
            tableView.contentInsetAdjustmentBehavior = .never
        }
        tableView.estimatedSectionHeaderHeight = 14.0
        tableView.estimatedSectionFooterHeight = 1.0
        tableView.showsVerticalScrollIndicator = false
        tableView.separatorStyle = .none
        tableView.delegate = self
        tableView.dataSource = self
        tableView.backgroundColor = UIColor(hexString:"#F8F8F8")
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "UITableViewCell")
        tableView.register(YHCertificateInfoCell.self, forCellReuseIdentifier: YHCertificateInfoCell.cellReuseIdentifier)
        return tableView
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = .white
        self.gk_navigationBar.isHidden = true
        createUI()
        requestList()
    }
    
    func createUI() {
        view.addSubview(tableView)
        tableView.snp.makeConstraints { make in
            make.top.left.right.equalToSuperview()
            make.height.equalTo(KScreenHeight - k_Height_NavigationtBarAndStatuBar - YHMaterialListViewController.segmentHeight)
        }
    }
}

extension YHCertificateResourceUploadVC: 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 {
        
        let item = items[indexPath.section]
        let cell = tableView.dequeueReusableCell(withIdentifier: YHCertificateInfoCell.cellReuseIdentifier, for: indexPath)
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableView.automaticDimension
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
//        if true {
//            
//            selectUploadFileFromICouldDrive()
//            return
//        }
        
        if true {
            
            YHCertificateUploadSheetView.sheetView.show {
               [weak self] actionType in
               guard let self = self else { return }
               print("%d", actionType.rawValue)
            }
            return 
        }
                
        YHCertificateFilterView.sheetView.show()
        return
        
        print("ZZZ")
        let vc = YHUploadContentVC()
        self.navigationController?.pushViewController(vc)
    }

    private func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> CGFloat {
        return 14.0
    }
    
    private 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()
    }
}

// MARK: - JXSegmentedListContainerViewListDelegate
extension YHCertificateResourceUploadVC: JXSegmentedListContainerViewListDelegate {
    func listView() -> UIView {
        return view
    }
}

extension YHCertificateResourceUploadVC {
    
    func requestList() {
        
        let params = [
                        "order_id" : orderId ?? 0,
                        "node": "1",
                        "state": "1",
                        "category_id": "20",
                        "status_all": "",
                        "keywork": "",
                        "template_cate_id": "",
                        "supplement_ids": []] as [String: Any]
        // state: 3:待上传 2:已驳回 1:已通过 0:审核中
        viewModel.getCertificateList(params: params) {
            
            [weak self] listInfo, error in
            guard let self = self else { return }
            
            items.removeAll()
            if let listInfo = listInfo {
                let arr = listInfo.supplementList
                items.append(contentsOf: arr)
                self.tableView.reloadData()
            }
            
            if let errorMsg = error?.errorMsg, errorMsg.count > 0 {
                YHHUD.flash(message: errorMsg, duration:3)
            }
        }
    }
}
