//
//  YHServiceCenterSecondViewController.swift
//  galaxy
//
//  Created by davidhuangA on 2024/1/24.
//  Copyright © 2024 https://www.galaxy-immi.com. All rights reserved.
//

import UIKit

//服务中心
class YHServiceCenterSecondViewController: YHBaseViewController {
    
//    private let serviceCenterMainReqVM : YHServiceCenterMainViewModel = YHServiceCenterMainViewModel()
    
    private var arrData : [YHContactMainItemStatusModel] = []
    private var unfoldFlag : Bool = true //是否展开
    private lazy var downArrowImagev : UIImageView =  {
        let imagV = UIImageView(frame: CGRect(x: KScreenWidth - 16 - 32, y: 17, width: 24, height: 24))
        imagV.image = UIImage(named: "service_center_down_arrow")
        imagV.contentMode = .scaleAspectFill
        return imagV
    }()
    
    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.register(YHContractMainItemStatusCell.self,forCellReuseIdentifier: YHContractMainItemStatusCell.cellReuseIdentifier)
        return tableView
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
        loadData()
    }
    
}

extension YHServiceCenterSecondViewController {
    func loadData() {
//        self.serviceCenterMainReqVM.getContactList { success, error in
//            if success == true {
//                
//            }
//            self.homeTableView.reloadData()
//        }
    }
    
    func setupUI() {
        gk_navTitle = "服务中心"
        gk_navBarAlpha = 1.0
        gk_navLineHidden = false
        
        let arr = [1,2,3]
        for item in arr {
            let model = YHContactMainItemStatusModel()
            model.type = item
            model.isFinished = false
            arrData.append(model)
        }
        
        view.addSubview(topBkg)
        topBkg.snp.makeConstraints { make in
            make.top.left.right.equalToSuperview()
            make.height.equalTo(216)
        }
        
        
        homeTableView.delegate = self
        homeTableView.dataSource = self
        view.addSubview(homeTableView)
        homeTableView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(k_Height_NavigationtBarAndStatuBar)
            make.bottom.equalToSuperview()
        }
        
    }
    
    @objc func clickUnfoldView() {
        unfoldFlag = !unfoldFlag
        if unfoldFlag == true {
            UIView.animate(withDuration: 0.25) {
                self.downArrowImagev.transform = CGAffineTransform.identity
            }
        } else {
            UIView.animate(withDuration: 0.25) {
                self.downArrowImagev.transform = CGAffineTransform(rotationAngle: .pi)
            }
        }
        homeTableView.reloadSections(IndexSet(integer: 0), with: UITableView.RowAnimation.automatic)
    }
}

// MARK: - UITableViewDelegate 和 UITableViewDataSource
extension YHServiceCenterSecondViewController : UITableViewDelegate,UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if unfoldFlag == true {
            return arrData.count
        }
        return 0
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 60
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let view = UIView(frame: CGRect(x: 0, y: 0, width: KScreenWidth, height: 60))
        view.backgroundColor = .clear
        
        let lable = UILabel(frame: CGRect(x: 16, y: 0, width: 200, height: 60))
        lable.text = "身份申请"
        lable.font = kFont(size: 17)
        view.addSubview(lable)
        view.addSubview(self.downArrowImagev)
        
        let tap = UITapGestureRecognizer(target: self, action: #selector(clickUnfoldView))
        view.addGestureRecognizer(tap)
        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 YHContractMainItemStatusCell.cellHeight
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let newCell = tableView.dequeueReusableCell(withClass: YHContractMainItemStatusCell.self)
        newCell.selectionStyle = .none
        
        newCell.dataModel = arrData[indexPath.row]
        return newCell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        printLog("点击了 \(indexPath.section)section  --- \(indexPath.row)row")
        
        navigationController?.pushViewController(YHInformationPerfectListVC())
    }
}
