//
//  SSCardCollectImagesController.m
//  Open
//
//  Created by 雷俊博 on 2019/11/20.
//  Copyright © 2019 黄江涛 . All rights reserved.
//

#import "SSCardCollectImagesController.h"
#import "SSCardCollectImagesCell.h"
#import "UIImageView+WebCache.h"
#import "CLAmplifyView.h"

@interface SSCardCollectImagesController ()<UITableViewDelegate,UITableViewDataSource,SSCardCollectImagesCellDelegate>

@property (nonatomic, strong)UITableView *tableView;


@end

@implementation SSCardCollectImagesController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self.view addSubview:self.tableView];
    
    [self setupUI];
}

- (void)setupUI
{
    UIButton *btn_back = [UIButton buttonWithType:UIButtonTypeCustom];
    btn_back.frame = CGRectMake(0, 0, 60, 40);
    
    [btn_back setTitle:@"返回" forState:UIControlStateNormal];
    [btn_back setTitleColor:[UIColor colorWithHex:0x4d4d4d] forState:UIControlStateNormal];
    btn_back.titleLabel.font = [UIFont systemFontOfSize:15];
    [btn_back setImage:[[UIImage imageNamed:@"back"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    
    [btn_back addTarget:self action:@selector(backClick:) forControlEvents:UIControlEventTouchUpInside];
    
    [btn_back layoutButtonWithEdgeInsetsStyle:GLButtonEdgeInsetsStyleLeft imageTitleSpace:4];
    
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc]initWithCustomView:btn_back];
    if ([[[[UIDevice currentDevice] systemVersion] substringToIndex:1] intValue]>=7) {
        UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
        negativeSpacer.width = -15;
        self.navigationItem.leftBarButtonItems = @[negativeSpacer, backItem];
    }else{
        self.navigationItem.leftBarButtonItem = backItem;
    }
}

#pragma mark - SSCardCollectImagesCellDelegate
- (void)clickImageView:(UITapGestureRecognizer *)tap
{
    CLAmplifyView *amplifyView = [[CLAmplifyView alloc]initWithFrame:self.view.bounds andGesture:tap andSuperView:self.view];
    
    [[UIApplication sharedApplication].keyWindow addSubview:amplifyView];
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SSCardCollectImagesCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SSCardCollectImagesCell"];
    
    SSCardCollectImageModel *model = self.dataArray[indexPath.section];
    
    cell.lb_date.text = [self getTimestamp:model.createtime];
    
    
    NSString *idnumber = [EnercryTool desA:model.idcard];
    
    if (idnumber.length == 18) {
        cell.lb_cardNumber.text = [NSString stringWithFormat:@"%@****%@",[idnumber substringToIndex:3],[idnumber substringFromIndex:14]];
    }else if (idnumber.length == 15){
         cell.lb_cardNumber.text = [NSString stringWithFormat:@"%@****%@",[idnumber substringToIndex:3],[idnumber substringFromIndex:10]];
    }
    
    
    NSMutableArray *imageurlArray = [[NSMutableArray alloc]init];
    
    if (![NSString blankString:model.photo]) {
        [cell.img1 sd_setImageWithURL:[NSURL URLWithString:model.photo]];
        [imageurlArray addObject:model.photo];
    }
    
    if (![NSString blankString:model.photo02]) {
        [cell.img2 sd_setImageWithURL:[NSURL URLWithString:model.photo02]];
        [imageurlArray addObject:model.photo02];
    }
    
    
    
    cell.delegate = self;
    
    
    return cell;
}


#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 215;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return 0;
    }
    
    return 10;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc]init];
    view.backgroundColor = [UIColor colorWithHex:0xf4f4f4];
    
    return view;
}



#pragma mark - event response
- (void)backClick:(UIButton *)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}



#pragma mark - private methods
- (NSString *)getTimestamp:(NSString*)mStr{
    
    NSTimeInterval interval    =[mStr doubleValue]/1000;
    
    NSDate *date              = [NSDate dateWithTimeIntervalSince1970:interval] ;
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    
    [formatter setTimeZone:[NSTimeZone timeZoneWithName:@"Asia/Beijing"]];
    
    NSString *dateString      = [formatter stringFromDate: date];
    
    NSLog(@"时间戳对应的时间是:%@",dateString);
    
    return dateString;
    
}

#pragma mark - getters and setters
-(UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = [UIColor colorWithHex:0xf4f4f4];
        [_tableView registerNib:[UINib nibWithNibName:@"SSCardCollectImagesCell" bundle:nil] forCellReuseIdentifier:@"SSCardCollectImagesCell"];
    }
    return _tableView;
}

/*
#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.
}
*/

@end
