//
//  YHHomeViewController.swift
//  galaxy
//
//  Created by EDY on 2023/12/20.
//

import UIKit

//服务中心
class YHServiceCenterMainViewController: YHBaseViewController {
    
    private let serviceCenterMainReqVM : YHServiceCenterMainViewModel = YHServiceCenterMainViewModel()
    
    private lazy var topBkg : UIView = {
        let imagV = UIImageView()
        imagV.image = UIImage(named: "service_center_head_bkg")
        imagV.contentMode = .scaleAspectFill
        return imagV
        
    }()
    
    private let loginTipsView : YHLoginTipsView = {
        let view = YHLoginTipsView()
        view.viewType = 0
        view.isHidden = true
        return view
    }()
    
    private let noDataView : YHLoginTipsView = {
        let view = YHLoginTipsView()
        view.viewType = 1
        view.isHidden = true
        return view
    }()
    
    
    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.estimatedRowHeight = 81.0
        tableView.register(YHContractCell.self,forCellReuseIdentifier: YHContractCell.cellReuseIdentifier)
        return tableView
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
        loadData()
    }
}

extension YHServiceCenterMainViewController {
    func loadData() {
        self.serviceCenterMainReqVM.getContactList { success, error in
            if self.serviceCenterMainReqVM.arrContactList.isEmpty {
                self.homeTableView.isHidden = true
                self.noDataView.isHidden = false
            } else {
                self.homeTableView.isHidden = false
                self.noDataView.isHidden = true
            }
            self.homeTableView.reloadData()
        }
    }
    
    func setupUI() {
        gk_navTitle = "服务中心"
        gk_navBarAlpha = 1.0
        
        view.addSubview(topBkg)
        topBkg.snp.makeConstraints { make in
            make.top.left.right.equalToSuperview()
            make.height.equalTo(216)
        }
        
        view.addSubview(loginTipsView)
        loginTipsView.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.centerY.equalToSuperview().offset(-64)
            make.width.equalTo(KScreenWidth)
        }
        
        view.addSubview(noDataView)
        noDataView.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.centerY.equalToSuperview().offset(-64)
            make.width.equalTo(KScreenWidth)
        }
        
        view.addSubview(homeTableView)
        homeTableView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(k_Height_NavigationtBarAndStatuBar)
            make.bottom.equalToSuperview().offset(-k_Height_TabBar)
        }
        homeTableView.delegate = self
        homeTableView.dataSource = self
        
        
        self.homeTableView.isHidden = true
        self.noDataView.isHidden = false
    }
}

// MARK: - UITableViewDelegate 和 UITableViewDataSource
extension YHServiceCenterMainViewController : UITableViewDelegate,UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        return self.serviceCenterMainReqVM.arrContactList.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 81
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell0 = tableView.dequeueReusableCell(withClass: YHContractCell.self)
        cell0.selectionStyle = .none
        
        if indexPath.section < self.serviceCenterMainReqVM.arrContactList.count {
            cell0.dataModel = self.serviceCenterMainReqVM.arrContactList[indexPath.section]
        }
        return cell0
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        printLog("点击了 tableView Cell \(indexPath.row)")
        let vc = YHServiceCenterSecondViewController()
        vc.orderId = serviceCenterMainReqVM.arrContactList[indexPath.row].id
        self.navigationController?.pushViewController(vc)
    }
}
