//
//  YHPrincipleGroupReviewListCell.swift
//  galaxy
//
//  Created by edy on 2024/4/29.
//  Copyright © 2024 https://www.galaxy-immi.com. All rights reserved.
//

import UIKit

class YHPrincipleGroupReviewItemView: UIView {
    
    static let height = 55.0
    var iconImgV: UIImageView!
    var titleLabel: UILabel!
    var timeLabel: UILabel!
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    
    func updateModel(_ str: String) {
        titleLabel.text = str
        timeLabel.text = "上传于2023.02.06"
    }
    
    func setupUI() {
        
        iconImgV = UIImageView(image: UIImage(named: "my_cer_type_pdf"))
        self.addSubview(iconImgV)
        
        titleLabel = UILabel()
        titleLabel.font = UIFont.PFSC_M(ofSize: 14)
        titleLabel.textColor = UIColor.mainTextColor
        titleLabel.text = "新公司在职证明.pdf"
        self.addSubview(titleLabel)
        
        timeLabel = UILabel()
        timeLabel.font = UIFont.PFSC_R(ofSize: 11)
        timeLabel.textColor = UIColor.labelTextColor2
        timeLabel.text = "上传于2024.12.12"
        self.addSubview(timeLabel)
        
        iconImgV.snp.makeConstraints { make in
            make.left.equalTo(22)
            make.centerY.equalToSuperview()
            make.width.height.equalTo(31.0)
        }
        
        titleLabel.snp.makeConstraints { make in
            make.bottom.equalTo(self.snp.centerY).offset(-1)
            make.left.equalTo(iconImgV.snp.right).offset(18)
            make.right.equalTo(-16)
            make.height.equalTo(20)
        }
        
        timeLabel.snp.makeConstraints { make in
            make.top.equalTo(self.snp.centerY).offset(1)
            make.left.equalTo(iconImgV.snp.right).offset(18)
            make.height.equalTo(20)
            make.right.equalTo(-16)
        }
    }
}

class YHPrincipleGroupReviewListCell: UITableViewCell {
    
    static let cellReuseIdentifier = "YHPrincipleGroupReviewListCell"
    
    var itemModel:YHPrincleGroupModel?
    var expandClick:((Bool)->())?
    
    var iconImgV: UIImageView!
    var titleLabel: UILabel!
    var grayView: UIView!
    var expandBtn: UIButton!

    lazy var listTitleLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.PFSC_M(ofSize: 17)
        label.textColor = UIColor.mainTextColor
        label.text = "已获得港府甄选名额"
        return label
    }()
    
    lazy var detailLabel: UILabel = {
        let label = UILabel()
        label.numberOfLines = 0
        label.font = UIFont.PFSC_R(ofSize: 12)
        label.textColor = UIColor.labelTextColor2
        label.text = "1.新公司在职证明（在申请期间更换了工作时必传）\n2.主申人的曾用名证明文件，例如户口本副本\n3.主申请人的中国无刑事罪行记录证明书副本（由公安局开具）"
        return label
    }()
    
    lazy var statusLabel: UILabel = {
        let label = UILabel()
        label.textAlignment = .center
        label.font = UIFont.PFSC_R(ofSize: 10)
        label.textColor = UIColor.init(hex: 0xFF9900)
        label.backgroundColor = UIColor.init(hex:0xFF9900, alpha:0.3)
        label.text = "审核中"
        label.layer.cornerRadius = 4.0
        label.clipsToBounds = true
        return label
    }()
    lazy var listView: UIView = {
        let view = UIView()
        return view
    }()
    
    var items: [String] = []
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }
    
    func updateModel(_ model:YHPrincleGroupModel) {
        
        itemModel = model
        items.removeAll()
        items.append(contentsOf: model.fileList)
        grayView.isHidden = !model.isExpand
        
        if !model.isExpand {
            grayView.removeSubviews()
            grayView.snp.remakeConstraints { make in
                make.left.right.equalTo(titleLabel)
                make.top.equalTo(titleLabel.snp.bottom)
                make.height.equalTo(0)
                make.bottom.equalToSuperview()
            }
            return
        }
        
        // 展开状态
        listView.removeSubviews()
        grayView.addSubview(listTitleLabel)
        grayView.addSubview(statusLabel)
        grayView.addSubview(detailLabel)
        grayView.addSubview(listView)
        
        grayView.snp.remakeConstraints { make in
            make.left.right.equalTo(titleLabel)
            make.top.equalTo(titleLabel.snp.bottom).offset(12.0)
            make.bottom.equalToSuperview()
        }
        
        listTitleLabel.snp.remakeConstraints { make in
            make.left.equalTo(16)
            make.top.equalTo(16)
            make.height.equalTo(24)
        }
        
        statusLabel.snp.remakeConstraints { make in
            make.left.equalTo(listTitleLabel.snp.right).offset(8)
            make.size.equalTo(CGSize(width: 38, height: 15))
            make.centerY.equalTo(listTitleLabel)
            make.right.lessThanOrEqualTo(-16)
        }

        detailLabel.snp.remakeConstraints { make in
            make.left.equalTo(16)
            make.right.equalTo(-16)
            make.top.equalTo(listTitleLabel.snp.bottom).offset(28)
        }
        
        listView.snp.remakeConstraints { make in
            make.left.right.equalTo(0)
            make.top.equalTo(detailLabel.snp.bottom).offset(16)
            let listHeight = Double(model.fileList.count) * YHPrincipleGroupReviewItemView.height
            make.height.equalTo(listHeight)
            let bottomMargin = model.fileList.count > 0 ? -16.0 : 0
            make.bottom.equalTo(bottomMargin)
        }
        
        var lastItemView: YHPrincipleGroupReviewItemView? = nil
        for (index, title) in items.enumerated() {
            let itemView = YHPrincipleGroupReviewItemView(frame: .zero)
            itemView.updateModel(title)
            listView.addSubview(itemView)
            itemView.snp.makeConstraints { make in
                make.left.right.equalTo(0)
                make.height.equalTo(YHPrincipleGroupReviewItemView.height)
                if let lastItemView = lastItemView {
                    make.top.equalTo(lastItemView.snp.bottom)
                } else {
                    make.top.equalToSuperview()
                }
            }
            lastItemView = itemView
        }
        self.setNeedsLayout()
        self.layoutIfNeeded()
    }
    
    @objc func didExpandBtnClicked() {
        guard let model = itemModel else { return }
        expandClick?(!model.isExpand)
    }
    
    func setupUI() {
        
        self.selectionStyle = .none
        contentView.backgroundColor = UIColor.white
        
        iconImgV = UIImageView(image: UIImage(named: "custom_service_icon_right"))
        contentView.addSubview(iconImgV)
        
        titleLabel = UILabel()
        titleLabel.font = UIFont.PFSC_R(ofSize: 13)
        titleLabel.textColor = UIColor.mainTextColor
        titleLabel.text = "已获得港府甄选名额"
        contentView.addSubview(titleLabel)
        
        expandBtn = UIButton()
        expandBtn.addTarget(self, action: #selector(didExpandBtnClicked), for: .touchUpInside)
        contentView.addSubview(expandBtn)
        
        grayView = UIView()
        grayView.backgroundColor = UIColor.init(hex: 0xf8f9fb)
        grayView.layer.cornerRadius = 6.0
        grayView.clipsToBounds = true
        contentView.addSubview(grayView)
        
        iconImgV.snp.makeConstraints { make in
            make.left.equalTo(24)
            make.width.height.equalTo(14.0)
            make.centerY.equalTo(titleLabel)
        }
        
        titleLabel.snp.makeConstraints { make in
            make.left.equalTo(iconImgV.snp.right).offset(8)
            make.right.equalTo(-24)
            make.top.equalTo(0)
            make.height.equalTo(22)
        }
        
        expandBtn.snp.makeConstraints { make in
            make.left.right.top.equalToSuperview()
            make.bottom.equalTo(titleLabel)
        }
        
        grayView.snp.makeConstraints { make in
            make.top.equalTo(titleLabel.snp.bottom).offset(12)
            make.left.right.equalTo(titleLabel)
            make.bottom.equalToSuperview()
        }
    }
}
