//
//  YHFormItemDegreeInfoCell.swift
//  galaxy
//
//  Created by edy on 2024/1/26.
//  Copyright © 2024 https://www.galaxy-immi.com. All rights reserved.
//

import UIKit

class YHFormItemDegreeInfoCell: UITableViewCell {

    static let cellReuseIdentifier = "YHFormItemDegreeInfoCell"

    var deleteBlock: (()->Void)?
    var title:String? {
        didSet {
            
            if let title = title {
                let str = "*"+title
                let attributes: [NSAttributedString.Key: Any] = [
                    .font: UIFont.PFSC_R(ofSize: 14),
                    .foregroundColor: UIColor(hex:0x222222)
                ]
                let questionAttrStr = NSMutableAttributedString(string: str, attributes: attributes)
                let starRange = NSRange(location: 0, length: 1)
                questionAttrStr.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor(hex:0xFF3A3A), range: starRange)
                
                titleLabel.attributedText = questionAttrStr
            }
        }
    }
    
    lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor(hexString:"#333333")
        label.textAlignment = .left
        label.numberOfLines = 0
        label.font = UIFont.PFSC_R(ofSize: 16)
        return label
    }()
    
    lazy var deleteButton: UIButton = {
        
        let btn = UIButton()
        btn.setTitle("删除", for: .normal)
        btn.titleLabel?.font = UIFont.PFSC_R(ofSize: 14)
        btn.setTitleColor(UIColor(hex: 0xF81D22), for: .normal)
        btn.addTarget(self, action: #selector(didClickDeleteBtn(btn:)), for: .touchUpInside)
//        btn.isHidden = true
        return btn
    }()
    
    lazy var degreeView:HKDegreeInfoItemView = {
        let view = HKDegreeInfoItemView(frame: .zero)
        return view
    }()
    
    lazy var areaView:HKDegreeInfoItemView = {
        let view = HKDegreeInfoItemView(frame: .zero)
        return view
    }()
    
    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

        contentView.addSubview(titleLabel)
        contentView.addSubview(deleteButton)
        contentView.addSubview(degreeView)
        contentView.addSubview(areaView)
        
        titleLabel.text = "学位信息"
        
        titleLabel.snp.makeConstraints { make in
            make.height.equalTo(52)
            make.top.equalToSuperview()
            make.left.equalToSuperview().offset(16)
            make.right.equalTo(deleteButton.snp.left)
        }
        
        deleteButton.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: 64, height: 52))
            make.centerY.equalTo(titleLabel)
            make.right.equalToSuperview()
        }
        
        degreeView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(titleLabel.snp.bottom)
            make.height.equalTo(52)
        }
        
        areaView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(degreeView.snp.bottom)
            make.height.equalTo(52)
            make.bottom.equalToSuperview()
        }
    }
    
    @objc func didClickDeleteBtn(btn:UIButton) {
        
        if let deleteBlock = deleteBlock {
            deleteBlock()
        }
    }
}

class HKDegreeInfoItemView: UIView {
    
    lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor(hexString:"#333333")
        label.textAlignment = .left
        label.numberOfLines = 0
        label.font = UIFont.PFSC_R(ofSize: 14)
        return label
    }()
    
    lazy var detailLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor(hexString: "#222222")
        label.textAlignment = .left
        label.font = UIFont.PFSC_R(ofSize: 14)
        label.isHidden = true
        return label
    }()
    
    private lazy var tipsLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor(hexString: "#C0C0C0")
        label.text = "请选择".local
        label.textAlignment = .left
        label.font = UIFont.PFSC_R(ofSize: 14)
        label.isHidden = false
        return label
    }()
    
    lazy var arrowImgView: UIImageView = {
        let imgView = UIImageView(image: UIImage(named: "form_right_arrow"))
        return imgView
    }()
    
    lazy var topLineView:UIView = {
        let view = UIView()
        view.backgroundColor = UIColor(hex: 0xF0F0F0)
        return view
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        createUI()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    
    func createUI() {
        
        self.addSubview(titleLabel)
        self.addSubview(detailLabel)
        self.addSubview(tipsLabel)
        self.addSubview(arrowImgView)
        self.addSubview(topLineView)
        
        titleLabel.text = "XXXXXX"
        detailLabel.text = "xxxxxxxx"
        
        topLineView.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(16)
            make.right.equalToSuperview().offset(-16)
            make.top.equalToSuperview()
            make.height.equalTo(1.0)
        }
        
        titleLabel.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(16)
            make.right.equalTo(detailLabel.snp.left).offset(-8)
            make.width.equalTo(120)
            make.centerY.equalToSuperview()
        }
        
        detailLabel.snp.makeConstraints { make in
            make.right.equalTo(arrowImgView.snp.left)
            make.centerY.equalToSuperview()
        }
        
        tipsLabel.snp.makeConstraints { make in
            make.edges.equalTo(detailLabel)
        }
        arrowImgView.snp.makeConstraints { make in
            make.size.equalTo(CGSizeMake(20, 20))
            make.right.equalToSuperview().offset(-16)
            make.centerY.equalToSuperview()
        }
    }
}

