//
//  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.test_systemFont(ofSize: 13)
        titleLabel.numberOfLines = 0
//        titleLabel.textColor = blackTitleColor
        titleLabel.textColor = UIColorFromRGB(rgbValue: 0x23262A)
//        titleLabel.backgroundColor = UIColor.yellow
        return titleLabel
    }()
    
    lazy var timeLabel: UILabel = {
        let timeLabel = UILabel.init()
        timeLabel.font = UIFont.test_systemFont(ofSize: 12)
        timeLabel.numberOfLines = 0
//        timeLabel.tintColor = grayColor
//        timeLabel.backgroundColor = UIColor.red
        timeLabel.textColor = UIColorFromRGB(rgbValue: 0x8792A4)
        return timeLabel
    }()
    lazy var subImageView: UIImageView = {
        let subImageView = UIImageView.init()
        //subImageView.contentMode = .scaleToFill
        subImageView.contentMode = .scaleAspectFill
        subImageView.clipsToBounds = true
        //设置遮罩
        subImageView.layer.masksToBounds = true
        //设置圆角半径(宽度的一半)，显示成圆形。
        subImageView.layer.cornerRadius = 3.3
        return subImageView
    }()
    override func awakeFromNib() {
        super.awakeFromNib()
        self.separatorInset=UIEdgeInsetsMake(0, SCREEN_WHIDTH(), 0, 0)
        self.contentView.addSubview(titleLabel)
        self.contentView.addSubview(timeLabel)
        self.contentView.addSubview(subImageView)
    }
    override func configurateTheCell(_ info: BaseCellModel) {
        xinfo = info as! AdvisoryTableViewCellModel
        var titleLabelW = CGFloat()
        var titleLabelH = CGFloat()
        if xinfo.imageUrl!.count == 0  {
            self.separatorInset=UIEdgeInsetsMake(0, 12.67, 0, 12.67)
            titleLabelW = SCREEN_WHIDTH()-30*WHIDTH_RATIO()
            subImageView.isHidden = true
            titleLabelH = getTextHeigh(textStr: xinfo.titleStr!, font: UIFont.test_systemFont(ofSize: 14), width: titleLabelW)
            titleLabel.numberOfLines = 0
            titleLabel.frame = CGRect(x:15*WHIDTH_RATIO(), y: 25*HEIGHT_RATIO(), width:titleLabelW , height:titleLabelH)
//            timeLabel.frame = CGRect(x:titleLabel.originX, y: titleLabel.originY+titleLabelH+14.33*HEIGHT_RATIO(), width: titleLabelW, height:11)
        }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
            /// 图片
            subImageView.frame = CGRect(x:15*WHIDTH_RATIO(), y: 10*HEIGHT_RATIO(), width:105*WHIDTH_RATIO() , height:self.height-20*HEIGHT_RATIO())
            titleLabelW = SCREEN_WHIDTH()-145*WHIDTH_RATIO()
//            titleLabelH = getTextHeigh(textStr: xinfo.titleStr!, font: UIFont.systemFont(ofSize: 14), width: titleLabelW)
            titleLabelH = 15
            titleLabel.numberOfLines = 1
            titleLabel.frame = CGRect(x:subImageView.originX+subImageView.width+15*WHIDTH_RATIO(), y: 20*HEIGHT_RATIO(), width:titleLabelW , height:titleLabelH)
//            timeLabel.frame = CGRect(x:titleLabel.originX, y: titleLabel.originY+titleLabelH+15*HEIGHT_RATIO(), width: titleLabelW, height:11)
        }
        timeLabel.frame = CGRect(x:titleLabel.originX, y: self.height-26*HEIGHT_RATIO(), width: titleLabelW, height:11)
        titleLabel.text = xinfo.titleStr
        timeLabel.text = xinfo.timeStr
//        print(xinfo.titleStr!, 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!
}
