//
//  spotCheckViewController.m
//  Macalline
//
//  Created by bestRun on 15-8-31.
//  Copyright (c) 2015年 JianjiYuan. All rights reserved.
//

#import "spotCheckViewController.h"
#import "CheckResultViewController.h"
#import "CheckResultChangeViewController.h"
#import "CheckImageViewController.h"

@interface spotListCell : BaseTableViewCell

@property  (nonatomic,strong)  UILabel *lblTime;
@property  (nonatomic,strong)  UILabel *lblRange;
@property  (nonatomic,strong)  UILabel *lblStatus;
@property  (nonatomic,strong)   UIImageView *imageVArrow;
@property  (nonatomic,strong)   UILabel *lblDate;
@property (nonatomic,strong)  UILabel *lblHour;
@property  (nonatomic,strong)   UILabel *lblStarDate;
@property  (nonatomic,strong)   UILabel *lblEndDate;


@end

@implementation spotListCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        

        self.lblTime=[[UILabel alloc]init];
        self.lblTime.userInteractionEnabled = YES;
        self.lblTime.text=@"抽查主题:";
        self.lblTime.font=[UIFont systemFontOfSize:14];
        self.lblTime.textColor=[UIColor darkGrayColor];
        [self addSubview:self.lblTime];
        [self.lblTime autoPinEdge:ALEdgeTop toEdge:ALEdgeTop ofView:self withOffset:10];
        [self.lblTime autoPinEdge:ALEdgeLeft toEdge:ALEdgeLeft ofView:self withOffset:10];
        [self.lblTime autoSetDimension:ALDimensionHeight toSize:21];
        [self.lblTime autoSetDimension:ALDimensionWidth toSize:65];
        
        self.lblTime.textAlignment=NSTextAlignmentLeft;
        
        
        self.lblRange=[[UILabel alloc] init];
        self.lblRange.text=@"抽查时间:";
        self.lblRange.font=[UIFont systemFontOfSize:14];
        self.lblRange.textColor=[UIColor darkGrayColor];
        
        [self addSubview:self.lblRange];
        [self.lblRange autoPinEdge:ALEdgeLeft toEdge:ALEdgeLeft ofView:self withOffset:10];
        [self.lblRange autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:self.lblTime];
        [self.lblRange autoSetDimension:ALDimensionHeight toSize:21];
        [self.lblRange autoSetDimension:ALDimensionWidth toSize:65];
        
        self.lblRange.textAlignment=NSTextAlignmentLeft;
        
        self.lblStatus=[[UILabel alloc] init];
        self.lblStatus.text=@"已完成";
        self.lblStatus.font=[UIFont boldSystemFontOfSize:14];
        self.lblStatus.textColor=[UIColor whiteColor];
        [self addSubview:self.lblStatus];
        [self.lblStatus autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:self.lblRange];
        [self.lblStatus autoPinEdge:ALEdgeRight toEdge:ALEdgeRight ofView:self withOffset:0];
        self.lblStatus.backgroundColor=[UIColor colorWithRed:0.9529 green:0.6471 blue:0.2118 alpha:1];
        self.lblStatus.textAlignment=NSTextAlignmentCenter;
        [self.lblStatus autoSetDimension:ALDimensionHeight toSize:21];
        [self.lblStatus autoSetDimension:ALDimensionWidth toSize:80];
        
        self.imageVArrow = [[UIImageView alloc] initWithFrame:CGRectMake(297, 23.5, 8, 13)];
        _imageVArrow.image = [UIImage imageNamed:@"Task_Point/image_point_arrow.png"];
        [self.contentView addSubview:_imageVArrow];

        
        self.lblDate=[[UILabel alloc] init];
        self.lblDate.text=@"2015-07-19 ";
        self.lblDate.textColor=[UIColor darkGrayColor];
        self.lblDate.font=[UIFont systemFontOfSize:14];
        [self addSubview:self.lblDate];
        self.lblDate.textAlignment=NSTextAlignmentLeft;
        [self.lblDate autoPinEdge:ALEdgeTop toEdge:ALEdgeTop ofView:self.lblTime];
        [self.lblDate autoPinEdge:ALEdgeLeft toEdge:ALEdgeRight ofView:self.lblTime];
        [self.lblDate autoAlignAxis:ALAxisHorizontal toSameAxisOfView:self.lblTime];
        [self.lblDate autoSetDimension:ALDimensionWidth toSize:200];


        
        
        
        
        
        
        self.lblStarDate=[[UILabel alloc] init];
        [self addSubview:self.lblStarDate];
        [self.lblStarDate autoAlignAxis:ALAxisHorizontal toSameAxisOfView:self.lblRange];
        [self.lblStarDate autoPinEdge:ALEdgeLeft toEdge:ALEdgeLeft ofView:self.lblDate];
        [self.lblStarDate autoPinEdge:ALEdgeTop toEdge:ALEdgeTop ofView:self.lblRange];
        self.lblStarDate.textColor=[UIColor darkGrayColor];
        self.lblStarDate.font=[UIFont systemFontOfSize:14];
        self.lblStarDate.textAlignment=NSTextAlignmentLeft;
        self.lblStarDate.text=@"2014-07-03";
        [self.lblStarDate autoSetDimension:ALDimensionWidth toSize:100];
        
        
        
        self.lblEndDate=[[UILabel alloc] init];
        [self addSubview:self.lblEndDate];
        [self.lblEndDate autoAlignAxis:ALAxisHorizontal toSameAxisOfView:self.lblStarDate];
        [self.lblEndDate autoPinEdge:ALEdgeTop toEdge:ALEdgeTop ofView:self.lblStarDate];
        [self.lblEndDate autoPinEdge:ALEdgeLeft toEdge:ALEdgeRight ofView:self.lblStarDate];
        self.lblEndDate.text=@"2015-03-04";
        self.lblEndDate.textAlignment=NSTextAlignmentLeft;
        self.lblEndDate.textColor=[UIColor darkGrayColor];
        self.lblEndDate.font=[UIFont systemFontOfSize:14];
        
        
        
        UILabel *lbltem=[[UILabel alloc] init];
        lbltem.textColor=[UIColor darkGrayColor];
        lbltem.text=@"~";
        [self addSubview:lbltem];
        [lbltem autoPinEdge:ALEdgeTop toEdge:ALEdgeTop ofView:self.lblStarDate];
        [lbltem autoPinEdge:ALEdgeRight toEdge:ALEdgeRight ofView:self.lblStarDate withOffset:-6];
        [lbltem autoAlignAxis:ALAxisHorizontal toSameAxisOfView:self.lblStarDate];
        
        
        self.lblHour=[[UILabel alloc]init];
        self.lblHour.text=@"19:05";
        [self addSubview:self.lblHour];
        [self.lblHour autoAlignAxis:ALAxisHorizontal toSameAxisOfView:self.lblDate];
        [self.lblHour autoPinEdge:ALEdgeTop toEdge:ALEdgeTop ofView:self.lblDate];
        [self.lblHour autoPinEdge:ALEdgeLeft toEdge:ALEdgeRight ofView:self.lblDate];
        [self.lblHour autoAlignAxis:ALAxisVertical toSameAxisOfView:self.lblEndDate];
        self.lblHour.textColor=[UIColor darkGrayColor];
        self.lblHour.textAlignment=NSTextAlignmentCenter;
        self.lblHour.font=[UIFont systemFontOfSize:14];
        
        
        
        UIView *viewLine = [[UIView alloc] initWithFrame:CGRectMake(10, 72, 300, 0.5)];
        viewLine.backgroundColor = [UIColor colorWithRed:0.7333 green:0.7333 blue:0.7333 alpha:1];
        [self.contentView addSubview:viewLine];
        
    }
    return self;
}

@end


@interface spotCheckViewController ()

@property  (nonatomic,strong)   NSMutableArray *arrContent;

@end

@implementation spotCheckViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    _tableVContent = [[UITableView alloc] init];
//    _tableVContent.backgroundColor=[UIColor clearColor];
//    _tableVContent.separatorStyle = UITableViewCellSeparatorStyleNone;
//    _tableVContent.delegate = self;
//    _tableVContent.dataSource = self;
//    [self.view addSubview:_tableVContent];
    [self.tableVContent autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
    
}

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




- (void)httpOperationSuccess:(AFHTTPRequestOperation *)operation
                      result:(NSDictionary *)result
{
    if ([operation.userInfo[@"interface"] isEqualToString:@"querySurveyListEX"])
    {
        _arrContent = [result[@"surveyList"] validDataOfClass:[NSArray class]];
        
        [self.tableVContent reloadData];
    }
}


-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = NO;
    [self requestDatas];
   // self.rdv_tabBarController.navigationController.navigationBarHidden = YES;
//    if ([[UIApplication getObjectType:Number key:kShouldRequestDataWhenBack] boolValue]) {
//        [self requestDatas];
//        [[NSNotificationCenter defaultCenter] postNotificationName:kShouldReQueryCountInit object:nil];
//        [UIApplication saveObject:@NO type:Number key:kShouldRequestDataWhenBack];
//    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)configureCell:(spotListCell*)cell forIndexPath:(NSIndexPath*)indexPath
{
    cell.lblStarDate.text = _arrContent[indexPath.row][@"surveyStartDate"];
    cell.lblEndDate.text = _arrContent[indexPath.row][@"surveyEndDate"];
    cell.lblDate.text = _arrContent[indexPath.row][@"surveyTitle"];
    cell.lblHour.text = @"";
    NSString* str = _arrContent[indexPath.row][@"status"];
    if ([str isEqualToString:@"2"]) {
        cell.lblStatus.text = @"已完成";
        cell.lblStatus.backgroundColor=[UIColor colorWithRed:0.9529 green:0.6471 blue:0.2118 alpha:1];
    }
    else
    {
        cell.lblStatus.text = @"未完成";
        cell.lblStatus.backgroundColor=RGBColor(204, 204, 204, 1);
    }
    // = _arrContent[indexPath.row][@"surveyStartDate"];
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _arrContent.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 72;
}


-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    NSString *cellIndentifier=@"spotList";
    spotListCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIndentifier];
    if(!cell)
    {
        cell=[[spotListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    
    
    [self configureCell:cell forIndexPath:indexPath];
    return cell;
}

#pragma --------------Table delegate-------------
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString* str = _arrContent[indexPath.row][@"status"];
    if ([str isEqualToString:@"2"]) {
        
        CheckResultViewController* vc = [[CheckResultViewController alloc] init];
        vc.surveyID = _arrContent[indexPath.row][@"surveyId"];
        [self.navigationController pushViewController:vc animated:YES];
        
    }
    else if ([str isEqualToString:@"1"]) {
        CheckResultChangeViewController* vc = [[CheckResultChangeViewController alloc] init];
        vc.surveyID = _arrContent[indexPath.row][@"surveyId"];
        [self.navigationController pushViewController:vc animated:YES];
    }
    else
    {
        CheckImageViewController* vc = [[CheckImageViewController alloc] init];
        vc.surveyID = _arrContent[indexPath.row][@"surveyId"];
        [self.navigationController pushViewController:vc animated:YES];
    }
}



@end
