//
//  NoteDetailViewController.m
//  Macalline
//
//  Created by JianjiYuan on 14-12-6.
//  Copyright (c) 2014年 JianjiYuan. All rights reserved.
//

#import "NoteDetailViewController.h"

@interface NoteDetailViewController () < BRNetworkMgrDelegate >

@property (nonatomic, strong) UIWebView *webVNote;

@property (nonatomic, strong) NSDictionary *noticeDic;
@end

@implementation NoteDetailViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        
    }
    return self;
}
-(id) initWithNotice:(NSDictionary*)dic
{
    self = [super init];
    if (self) {
        // Custom initialization
        self.noticeDic = dic;
    }
    return self;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    if (self.noticeDic.count != 0) {
        self.title = @"提醒详情";
        self.view.backgroundColor = RGBColor(247, 247, 247, 1);
        UILabel* lblTitle = [[UILabel alloc] init];
        lblTitle.font = [UIFont boldSystemFontOfSize: 17];
        lblTitle.textAlignment = NSTextAlignmentCenter;
        lblTitle.text = [self.noticeDic objectForKey:@"title"];
        [self.view addSubview:lblTitle];
        [lblTitle autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsMake(5, 15, 10, 15) excludingEdge:ALEdgeBottom];
        [lblTitle autoSetDimension:ALDimensionHeight toSize:22];
        
        UILabel* lblTime = [[UILabel alloc] init];
        lblTime.font = [UIFont systemFontOfSize:14];
        lblTime.textAlignment = NSTextAlignmentCenter;
        lblTime.text = [self.noticeDic objectForKey:@"time"];
        [self.view addSubview:lblTime];
        [lblTime autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:lblTitle withOffset:4];
        [lblTime autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:19];
        [lblTime autoSetDimension:ALDimensionHeight toSize:22];
        
        NSString *strTitle = [self.noticeDic objectForKey:@"content"];
        CGFloat h = [strTitle boundingRectWithSize:CGSizeMake(CGRectGetWidth(self.view.frame)-20, CGFLOAT_MAX)
                                      options:NSStringDrawingUsesFontLeading|NSStringDrawingUsesLineFragmentOrigin
                                   attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]}
                                      context:nil].size.height;
        
        UILabel* lblContent = [[UILabel alloc] init];
        lblContent.font = [UIFont systemFontOfSize:14];
        lblContent.textAlignment = NSTextAlignmentLeft;
        lblContent.text = [self.noticeDic objectForKey:@"content"];
        [self.view addSubview:lblContent];
        [lblContent autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:lblTime withOffset:10];
        [lblContent autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:15];
        [lblContent autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:15];
        [lblContent autoSetDimension:ALDimensionHeight toSize:h];
        
    }else
    {
        self.title = @"公告详情";
        [self setViewLayoutConstraint];
        [self requestDatas];
    }
}

- (void)setViewLayoutConstraint
{
    [self.webVNote autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
}

- (void)requestDatas
{
    [UIApplication showLoadingView:kTextShowInProgress];
    NSString *interface = @"queryNoticeDetailsEX";
    NSDictionary *dicParam = @{@"noticeId":self.dictNotice[@"id"]?:@""};
    NSOperation *operation = [self.networkMgr operationWithURL:kBaseEXURL(interface)
                                                                interface:interface
                                                                   params:dicParam
                                                                 dataType:BRNetMgrDataTypeJSON
                                                                   method:BRNetMgrMethodPost];
    [self.networkMgr startOperation:operation];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark =============  BRNetworkMgr Delegate  =============
- (void)httpOperationSuccess:(AFHTTPRequestOperation *)operation
                      result:(NSDictionary *)result
{
    NSString *htmlString = result[@"noticeDetails"][@"content"];
    [self.webVNote loadHTMLString:htmlString baseURL:nil];
    [[NSNotificationCenter defaultCenter] postNotificationName:kShouldReQueryCountInit object:nil];
    
}

- (void)httpOperationFailed:(AFHTTPRequestOperation *)operation
                      error:(NSError *)error
{
    
}

- (UIWebView *)webVNote
{
    if (!_webVNote) {
        self.webVNote = [UIWebView newAutoLayoutView];
        _webVNote.scalesPageToFit = YES;
        [self.view addSubview:_webVNote];
    }
    return _webVNote;
}

@end
