//
//  AdvisoryTableViewCell.swift
//  MobilePortal
//
//  Created by xiejiangbo on 2019/3/14.
//  Copyright © 2019 yin chen. All rights reserved.
//

import UIKit

class AdvisoryTableViewCell: BaseTableViewCell {

    var xinfo = AdvisoryTableViewCellModel()
    
    lazy var titleLabel: UILabel = {
        let titleLabel = UILabel.init()
        titleLabel.textAlignment = .left
        titleLabel.font = UIFont.systemFont(ofSize: 15*WHIDTH_RATIO())
        titleLabel.numberOfLines = 0
        titleLabel.textColor = blackTitleColor
        //titleLabel.backgroundColor = UIColor.red
        return titleLabel
    }()
    
    lazy var timeLabel: UILabel = {
        let timeLabel = UILabel.init()
        timeLabel.font = UIFont.systemFont(ofSize: 13*WHIDTH_RATIO())
        timeLabel.numberOfLines = 0
        timeLabel.tintColor = grayColor
        return timeLabel
    }()
    lazy var subImageView: UIImageView = {
        let subImageView = UIImageView.init()
        //subImageView.contentMode = .scaleToFill
        subImageView.contentMode = .scaleAspectFill
        subImageView.clipsToBounds = true
        return subImageView
    }()
    override func awakeFromNib() {
        super.awakeFromNib()
        self.separatorInset=UIEdgeInsetsMake(0, 10*WHIDTH_RATIO(), 0, 0)
        self.addSubview(titleLabel)
        self.addSubview(timeLabel)
        self.addSubview(subImageView)
    }
    override func configurateTheCell(_ info: BaseCellModel) {
        xinfo = info as! AdvisoryTableViewCellModel
        var titleLabelW = CGFloat()
        if xinfo.imageUrl!.count == 0  {
            titleLabelW = SCREEN_WHIDTH()-20*WHIDTH_RATIO()
            subImageView.isHidden = true
        }else{
            subImageView.sd_setImage(with: URL(string: xinfo.imageUrl!), placeholderImage: UIImage.init(named: "center_image"), completed: { (image, error, SDImageCacheTypeNone, url) in
                if (error != nil) {print(error ?? Error.self)}
            })
            subImageView.isHidden = false
            titleLabelW = SCREEN_WHIDTH()-125*WHIDTH_RATIO()
        }
        let titleLabelH = getTextHeigh(textStr: xinfo.titleStr!, font: UIFont.systemFont(ofSize: 15*WHIDTH_RATIO()), width: titleLabelW)
        titleLabel.frame = CGRect(x:10*WHIDTH_RATIO(), y: (self.height-titleLabelH)/2-5*HEIGHT_RATIO(), width:titleLabelW , height:titleLabelH)
        let timeLabelH = 14
        let timeLabelW = titleLabelW
        timeLabel.frame = CGRect(x:10*WHIDTH_RATIO(), y: titleLabel.originY+titleLabelH+5*HEIGHT_RATIO(), width:timeLabelW , height:CGFloat(timeLabelH))
        subImageView.frame = CGRect(x:SCREEN_WHIDTH()-115*WHIDTH_RATIO(), y: 10*HEIGHT_RATIO(), width:105*WHIDTH_RATIO() , height:self.height-20*HEIGHT_RATIO())
        
        timeLabel.textColor = grayColor
        titleLabel.textColor = blackTitleColor
        titleLabel.text = xinfo.titleStr
        timeLabel.text = xinfo.timeStr
        
        
    }
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        if selected {
            self.xinfo.block()
        }
        // Configure the view for the selected state
    }

}
class AdvisoryTableViewCellModel: BaseCellModel {
    var titleStr:String?
    var imageUrl:String?
    var timeStr:String?
    var block:selectBlock!
    var h:CGFloat!
}
