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

#import "CheckResultViewController.h"

@interface CheckResultViewController ()< UIGestureRecognizerDelegate>
{
    UILabel* lblCheckRange;
    UILabel* lblCheckTime;
}
@property (nonatomic, strong) UIView *descView;

@end

@interface CheckResultTableViewCell : UITableViewCell

@property (nonatomic, strong) UILabel *lblSmallAreaName;
@property (nonatomic, strong) UILabel *lblCheckResult;
@property (nonatomic, strong) UILabel *lblCheckResultForSelf;
@property (nonatomic, strong) UILabel *lblEvaluate;
@end

@implementation CheckResultViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"抽查结果";
    
    _descView = [[UIView alloc] init];
    _descView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:_descView];
    [_descView autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsMake(0, 0, 0, 0) excludingEdge:ALEdgeBottom];
    [_descView autoSetDimension:ALDimensionHeight toSize:70];
    
    
    UILabel* lblCheckRangePre = [[UILabel alloc] init];
    lblCheckRangePre.textColor = [UIColor darkGrayColor];
    lblCheckRangePre.font = [UIFont systemFontOfSize:13];
    lblCheckRangePre.text = @"抽查范围:";
    [_descView addSubview:lblCheckRangePre];
    [lblCheckRangePre autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:13];
    [lblCheckRangePre autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:15];
    
    lblCheckRange = [[UILabel alloc] init];
    lblCheckRange.textColor = [UIColor darkGrayColor];
    lblCheckRange.font = [UIFont systemFontOfSize:13];
    lblCheckRange.text = @"2015-03-23 ~ 2015-06-27";
    [_descView addSubview:lblCheckRange];
    [lblCheckRange autoAlignAxis:ALAxisHorizontal toSameAxisOfView:lblCheckRangePre];
    [lblCheckRange autoPinEdge:ALEdgeLeft toEdge:ALEdgeRight ofView:lblCheckRangePre withOffset:5];
    
    UILabel* lblCheckTimePre = [[UILabel alloc] init];
    lblCheckTimePre.textColor = [UIColor darkGrayColor];
    lblCheckTimePre.font = [UIFont systemFontOfSize:13];
    lblCheckTimePre.text = @"抽查时间:";
    [_descView addSubview:lblCheckTimePre];
    [lblCheckTimePre autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:lblCheckRangePre withOffset:8];
    [lblCheckTimePre autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:15];
    
    lblCheckTime = [[UILabel alloc] init];
    lblCheckTime.textColor = [UIColor darkGrayColor];
    lblCheckTime.font = [UIFont systemFontOfSize:13];
    lblCheckTime.text = @"2015-03-23  16:40";
    [_descView addSubview:lblCheckTime];
    [lblCheckTime autoAlignAxis:ALAxisHorizontal toSameAxisOfView:lblCheckTimePre];
    [lblCheckTime autoPinEdge:ALEdgeLeft toEdge:ALEdgeRight ofView:lblCheckTimePre withOffset:5];
    
    self.tableVContent.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self setViewLayoutConstraint];
    
    //    UIButton *btnHome = [UIButton buttonWithType:UIButtonTypeCustom];
    //    [btnHome setBackgroundImage:[UIImage imageNamed:@"NavBar/btn_nav_home.png"] forState:UIControlStateNormal];
    //    [btnHome setFrame:CGRectMake(0, 0, 25, 25)];
    //    [btnHome addTarget:self action:@selector(btnHomeClick) forControlEvents:UIControlEventTouchUpInside];
    //
    //    [self.navigationController navbarRightItemCustomized:@[btnHome] onwer:self.navigationItem];
    
    [self requestDatas];
    
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
//    if ([[UIApplication getObjectType:Number key:kShouldRequestDataWhenBack] boolValue]) {
//        [self requestDatas];
//    }
}

- (void)setViewLayoutConstraint
{
    [self.tableVContent autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsMake(0, 0, 10, 0) excludingEdge:ALEdgeTop];
    [self.tableVContent autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:_descView];
}

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

- (void)btnHomeClick
{
    for (UIViewController *controller in self.navigationController.viewControllers) {
        if ([controller isKindOfClass:NSClassFromString(@"TabBarViewController")]) {
            [self.navigationController popToViewController:controller animated:YES];
        }
    }
}


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

#pragma mark =============  BRNetworkMgr Delegate  =============
- (void)httpOperationSuccess:(AFHTTPRequestOperation *)operation
                      result:(NSDictionary *)result
{
    if ([operation.userInfo[@"interface"] isEqualToString:@"querySurveyResultEX"])
    {
        lblCheckRange.text = [NSString stringWithFormat:@"%@ ~ %@",result[@"surveyStartDate"],result[@"surveyEndDate"]];
        lblCheckTime.text = result[@"surveyTime"];
        
        self.arrContents = [result[@"areaScoreList"] validDataOfClass:[NSArray class]];
        [self.tableVContent reloadData];
        
        if (self.arrContents.count == 0) {
            self.imageVNoResult.hidden = NO;
        }
        else
        {
            self.imageVNoResult.hidden = YES;
        }
    }
}

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




- (void)configureCell:(CheckResultTableViewCell *)cell forIndexPath:(NSIndexPath *)indexPath
{
    cell.lblSmallAreaName.text = self.arrContents[indexPath.row][@"areaName"];
    cell.lblCheckResult.text = [NSString stringWithFormat:@"%@",self.arrContents[indexPath.row][@"score"]];
    cell.lblCheckResultForSelf.text = [NSString stringWithFormat:@"%@",self.arrContents[indexPath.row][@"subjectiveScore"]];
    cell.lblEvaluate.text = self.arrContents[indexPath.row][@"remark"];
}

#pragma mark =============  UITableView DataSource  =============

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellIdentifier = @"TaskListCell";
    CheckResultTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell = [[CheckResultTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleGray;
        
    }
    [self configureCell:cell forIndexPath:indexPath];
    
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arrContents.count;
//    return 7;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 35;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 35;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *viewHeader = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 35)];
    //viewHeader.backgroundColor = [UIColor colorWithRed:0.2431 green:0.7137 blue:0.9373 alpha:1];
    viewHeader.backgroundColor = [UIColor colorWithRed:0.9333 green:0.9333 blue:0.9333 alpha:1];
    
    UILabel *lblPoint = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 70, 35)];
    lblPoint.textColor = [UIColor darkGrayColor];
    lblPoint.backgroundColor = [UIColor clearColor];
    lblPoint.font = [UIFont boldSystemFontOfSize:13];
    lblPoint.textAlignment = NSTextAlignmentCenter;
    lblPoint.text = @"小区名称";
    [viewHeader addSubview:lblPoint];
    
    UILabel *lblResult = [[UILabel alloc] initWithFrame:CGRectMake(70, 0, 70, 35)];
    lblResult.textColor = [UIColor darkGrayColor];
    lblResult.backgroundColor = [UIColor clearColor];
    lblResult.font = [UIFont boldSystemFontOfSize:13];
    lblResult.textAlignment = NSTextAlignmentCenter;
    lblResult.text = @"评分结果";
    [viewHeader addSubview:lblResult];
    
    UILabel *lblResultSelf= [[UILabel alloc] initWithFrame:CGRectMake(140, 0, 70, 35)];
    lblResultSelf.textColor = [UIColor darkGrayColor];
    lblResultSelf.backgroundColor = [UIColor clearColor];
    lblResultSelf.font = [UIFont boldSystemFontOfSize:13];
    lblResultSelf.textAlignment = NSTextAlignmentCenter;
    lblResultSelf.text = @"自由评分";
    [viewHeader addSubview:lblResultSelf];
    
    UILabel *lblEvalute = [[UILabel alloc] initWithFrame:CGRectMake(210, 0, 110, 35)];
    lblEvalute.textColor = [UIColor darkGrayColor];
    lblEvalute.backgroundColor = [UIColor clearColor];
    lblEvalute.font = [UIFont boldSystemFontOfSize:13];
    lblEvalute.textAlignment = NSTextAlignmentCenter;
    lblEvalute.text = @"评价";
    [viewHeader addSubview:lblEvalute];
    return viewHeader;
}
-(void)searchClick
{
    NSLog(@"SearchClick");
}
#pragma mark =============  UITableView Delegate  =============

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//    self.selectedIndexPath = indexPath;
//    DLog(@"You Click At Section: %d Row: %d", (int)indexPath.section, (int)indexPath.row);
//    BATaskDetailViewController* vc = [[BATaskDetailViewController alloc] init];
//    vc.shopId = self.arrContents[indexPath.row][@"id"];
//    vc.taskId = self.arrContents[indexPath.row][@"taskId"];
//    [self.navigationController pushViewController:vc animated:YES];
}
@end

@implementation CheckResultTableViewCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.lblSmallAreaName = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 70, 34.5)];
        _lblSmallAreaName.textColor = [UIColor darkGrayColor];
        _lblSmallAreaName.backgroundColor = [UIColor clearColor];
        _lblSmallAreaName.font = [UIFont systemFontOfSize:13];
        _lblSmallAreaName.textAlignment = NSTextAlignmentCenter;
        [self.contentView addSubview:_lblSmallAreaName];
        
        self.lblCheckResult = [[UILabel alloc] initWithFrame:CGRectMake(70, 0, 70, 34.5)];
        _lblCheckResult.textColor = [UIColor darkGrayColor];
        _lblCheckResult.backgroundColor = [UIColor clearColor];
        _lblCheckResult.font = [UIFont systemFontOfSize:13];
        _lblCheckResult.textAlignment = NSTextAlignmentCenter;
        [self.contentView addSubview:_lblCheckResult];
        
        self.lblCheckResultForSelf = [[UILabel alloc] initWithFrame:CGRectMake(140, 0, 70, 34.5)];
        _lblCheckResultForSelf.textColor = [UIColor darkGrayColor];
        _lblCheckResultForSelf.backgroundColor = [UIColor clearColor];
        _lblCheckResultForSelf.font = [UIFont systemFontOfSize:13];
        _lblCheckResultForSelf.textAlignment = NSTextAlignmentCenter;
        [self.contentView addSubview:_lblCheckResultForSelf];
        
        self.lblEvaluate = [[UILabel alloc] initWithFrame:CGRectMake(210, 0, 110, 34.5)];
        _lblEvaluate.textColor = [UIColor darkGrayColor];
        _lblEvaluate.backgroundColor = [UIColor clearColor];
        _lblEvaluate.font = [UIFont systemFontOfSize:13];
        _lblEvaluate.textAlignment = NSTextAlignmentCenter;
        [self.contentView addSubview:_lblEvaluate];
        
        UIView *viewLine = [[UIView alloc] initWithFrame:CGRectMake(0, 34.5, 350, 0.5)];
        viewLine.backgroundColor = [UIColor colorWithRed:0.7333 green:0.7333 blue:0.7333 alpha:1];
        [self.contentView addSubview:viewLine];
        
    }
    return self;
}

@end