//
//  homeCell.m
//  YBEducation
//
//  Created by IOS1 on 2020/2/28.
//  Copyright © 2020 IOS1. All rights reserved.
//

#import "homeCell.h"

@implementation homeCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
-(void)setModel:(courseModel *)model{
    _model = model;
    _smModel = nil;
    [_thumbImgV sd_setImageWithURL:[NSURL URLWithString:_model.thumb]];
    _titleL.text = _model.courseName;
    if ([_model.sort isEqual:@"0"]) {
        _typeL.text = @"内容";

        _middleL.text = @"";
        _middleL.textColor = color96;
        _courseTypeL.text = _model.lesson;
        _courseTypeL.hidden = NO;
    }else{
        _courseTypeL.text = @"";
        _courseTypeL.hidden = YES;
        if ([_model.sort isEqual:@"1"]) {
            _typeL.text = @"课程";
            _middleL.textColor = color96;
            if (_model.lesson.length > 0) {
                _middleL.text = _model.lesson;
            }else{
                _middleL.text = @"尚未添加内容";
            }
        }else{
            _typeL.text = @"直播";
            if ([model.islive isEqual:@"1"]) {
                _middleL.text = @"正在直播";
                _middleL.textColor = normalColors;
            }else{
                _middleL.text = _model.lesson;
                _middleL.textColor = color96;
            }
        }

    }
    [_iconImgV sd_setImageWithURL:[NSURL URLWithString:_model.avatar]];
    _nameL.text = _model.user_nickname;
    if ([_model.paytype intValue] == 0) {
        _rightL.textColor = normalColors;
        _rightL.font = SYS_Font(12);
        _rightL.attributedText = [[NSAttributedString alloc]initWithString:@"免费"];
    }else if ([_model.paytype intValue] == 1) {
        _rightL.textColor = RGB_COLOR(@"#FF1B20", 1);
        _rightL.font = [UIFont boldSystemFontOfSize:15];

        if ([_model.ifbuy isEqual:@"1"]) {
            _rightL.attributedText = [[NSAttributedString alloc] initWithString:_model.payval];
        }else{
            _rightL.attributedText = [self getAttWord:[NSString stringWithFormat:@"¥%@",_model.payval]];
        }
    }else {
        _rightL.textColor = RGB_COLOR(@"#4385FF", 1);
        _rightL.font = SYS_Font(12);
        _rightL.attributedText = [[NSAttributedString alloc]initWithString:@"密码"];

    }
    if ([_model.ismaterial isEqual:@"1"]) {
        _jiaocaiBtn.hidden = NO;
    }else{
        _jiaocaiBtn.hidden = YES;
    }
    _selectBtn.selected = _model.isSelected;
    _iconScrollView.hidden = YES;
}
- (void)setSmModel:(setMealModel *)smModel{
    _smModel = smModel;
    _model = nil;
    _jiaocaiBtn.hidden = YES;
    [_thumbImgV sd_setImageWithURL:[NSURL URLWithString:_smModel.thumb]];
    _titleL.text = _smModel.name;
    _typeL.text = @"套餐";
    _rightL.font = [UIFont boldSystemFontOfSize:15];
    _rightL.attributedText = [self getAttWord:[NSString stringWithFormat:@"¥%@",_smModel.price]];
    _rightL.textColor = RGB_COLOR(@"#FF1B20", 1);
    _courseTypeL.text = @"";
    _courseTypeL.hidden = YES;
    _middleL.text = [NSString stringWithFormat:@"%@课程",_smModel.nums];
    _middleL.textColor = color96;
    _iconScrollView.hidden = NO;
    [_iconScrollView removeAllSubViews];
    for (int i = 0; i < _smModel.teacher.count; i ++) {
        UIImageView *imageV = [[UIImageView alloc]init];
        imageV.layer.cornerRadius = 7;
        imageV.layer.masksToBounds = YES;
        imageV.contentMode = UIViewContentModeScaleAspectFill;
        imageV.clipsToBounds = YES;
        [_iconScrollView addSubview:imageV];
        [imageV sd_setImageWithURL:[NSURL URLWithString:minstr([_smModel.teacher[i] valueForKey:@"avatar"])]];
        [imageV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(_iconScrollView);
            make.left.equalTo(_iconScrollView).offset(i*24);
            make.height.width.mas_equalTo(14);
        }];
    }
    if (_smModel.ismaterial) {
        _jiaocaiBtn.hidden = NO;
    }else{
        _jiaocaiBtn.hidden = YES;
    }

    _selectBtn.selected = _smModel.isSelected;

}
- (NSAttributedString *)getAttWord:(NSString *)str{
    NSMutableAttributedString *muAtt = [[NSMutableAttributedString alloc]initWithString:str];
    [muAtt addAttribute:NSFontAttributeName value:SYS_Font(11) range:NSMakeRange(0, 1)];
    return muAtt;
}
- (IBAction)selectBtnClick:(id)sender {
    if (self.delegate) {
        [self.delegate doSelectButtonClick:_model?_model:_smModel];
    }

}
@end
