//
//  YHGrabNumberListCell.swift
//  galaxy
//
//  Created by edy on 2024/8/19.
//  Copyright © 2024 https://www.galaxy-immi.com. All rights reserved.
//

import UIKit

class YHGrabNumberListCell: UITableViewCell {

    static let cellReuseIdentifier = "YHGrabNumberListCell"

    var items:[String] = ["", ""]
    
    lazy var whiteContentView: UIView = {
        let view = UIView()
        view.backgroundColor = .white
        view.layer.cornerRadius = kCornerRadius6
        return view
    }()
    
    lazy var lineView: UIView = {
        let view = UIView()
        view.backgroundColor = .separatorColor
        return view
    }()
    
    lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.numberOfLines = 0
        label.textColor = .mainTextColor
        label.font = UIFont.PFSC_M(ofSize: 17)
        label.text = "赴港办理抢号中批次"
        return label
    }()
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }
    
    func setupUI() {
        
        self.selectionStyle = .none
        self.backgroundColor = .clear
        self.contentView.backgroundColor = .clear
        contentView.addSubview(whiteContentView)
        whiteContentView.addSubview(titleLabel)
        whiteContentView.addSubview(lineView)
        
        whiteContentView.snp.makeConstraints { make in
            make.top.equalTo(16)
            make.left.equalTo(16)
            make.right.equalTo(-16)
            make.bottom.equalTo(0)
        }
        
        titleLabel.snp.makeConstraints { make in
            make.top.equalTo(18)
            make.left.equalTo(18)
            make.right.equalTo(-18)
        }
        
        lineView.snp.makeConstraints { make in
            make.top.equalTo(titleLabel.snp.bottom).offset(12)
            make.left.right.equalToSuperview()
            make.height.equalTo(0.5)
        }
        
        var lastView: YHGrabNumberListView? = nil
        for  (index, _) in items.enumerated() {
            let listView = YHGrabNumberListView(frame: .zero)
            let chineseNum = convertNumberToChineseText(index+1)
            listView.titleLabel.text = "第\(chineseNum)批"
            listView.membersLabel.text = "成员：刘德华德成员：刘德华德、刘德华德、刘德华德、刘德华德、刘德华德、刘德华德、刘德华德、刘德华德、刘德华德"
            whiteContentView.addSubview(listView)
            listView.snp.makeConstraints { make in
                make.left.equalTo(18)
                make.right.equalTo(-18)
                if let last = lastView {
                    make.top.equalTo(last.snp.bottom)
                } else {
                    make.top.equalTo(lineView.snp.bottom)
                }
                
                if index == items.count-1 {
                    make.bottom.equalTo(-16)
                }
            }
            lastView = listView
        }
    }
    
    //阿拉伯数字转文字
    func convertNumberToChineseText(_ number: Int) -> String {
        let numberFormatter = NumberFormatter()
        numberFormatter.locale = Locale(identifier: "zh_CN")
        numberFormatter.numberStyle = .spellOut
        return numberFormatter.string(from: NSNumber(value: number)) ?? ""
    }
}
