//
//  YHQuestionAndAnswerViewController.swift
//  galaxy
//
//  Created by davidhuangA on 2024/3/17.
//  Copyright © 2024 https://www.galaxy-immi.com. All rights reserved.
//

import UIKit
/*
 问题答疑列表
 */

class YHQuestionAndAnswerViewController: YHBaseViewController {
    
    private let certificateReqVM : YHCertificateViewModel = YHCertificateViewModel()
    
    private lazy var topBkg : UIView = {
        let imagV = UIImageView()
        imagV.image = UIImage(named: "service_center_head_bkg")
        imagV.contentMode = .scaleAspectFill
        return imagV
        
    }()
    
    var homeTableView: UITableView = {
        let tableView = UITableView(frame: .zero, style: .grouped)
        tableView.backgroundColor = .clear
        tableView.separatorStyle = .none
        tableView.showsHorizontalScrollIndicator = false
        tableView.showsVerticalScrollIndicator = false
        tableView.rowHeight = UITableView.automaticDimension
        tableView.register(YHQuestionAndAnswerCell.self,forCellReuseIdentifier: YHQuestionAndAnswerCell.cellReuseIdentifier)
        return tableView
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        loadData()
    }
}

extension YHQuestionAndAnswerViewController {
    func loadData() {
        if let orderID = UserDefaults.standard.value(forKey: "orderIdForPreview") {
            let param = ["order_id":orderID,"node":"1","state":"1"]
            
            
            certificateReqVM.getQuestionAndAnserList(params: param) { success, error in
                if success == true {
                } else {
                    YHHUD.flash(message: error?.errorMsg ?? "请求出错")
                }
                
                self.homeTableView.reloadData()
            }
        } else {
            printLog("error : orderID 为空")
        }
    }
    
    func setupUI() {
        gk_navTitle = "问题答疑"
        gk_navBarAlpha = 1.0
        
       
        view.addSubview(homeTableView)
        homeTableView.snp.makeConstraints { make in
            make.edges.equalTo(0)
        }
        homeTableView.delegate = self
        homeTableView.dataSource = self
    }
}

// MARK: - UITableViewDelegate 和 UITableViewDataSource
extension YHQuestionAndAnswerViewController : UITableViewDelegate,UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        return certificateReqVM.arrQuestionAndAnswer.count
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return kMargin
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let view = UIView(frame: CGRect(x: 0, y: 0, width: KScreenWidth, height: kMargin))
        return view
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0
    }
    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        return UIView()
    }
    
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableView.automaticDimension
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell0 = tableView.dequeueReusableCell(withClass: YHQuestionAndAnswerCell.self)
        cell0.selectionStyle = .none
        
        if indexPath.row < certificateReqVM.arrQuestionAndAnswer.count {
            cell0.dataModel = certificateReqVM.arrQuestionAndAnswer[indexPath.row]
        }
        return cell0
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        printLog("点击了 tableView Cell \(indexPath.section)")
    }
}

