//
//  InsuredMemberSelectViewController.m
//  Open
//
//  Created by 雷俊博 on 2019/8/3.
//  Copyright © 2019 黄江涛 . All rights reserved.
//

#import "InsuredMemberSelectViewController.h"

#import "Constants.h"
#import "Tools.h"

#import "InsuredMemberTableViewCell.h"
#import "InsuredMemberAddViewController.h"
#import "InsuredInfoController.h"
#import "CZQAddressPicker.h"

@interface InsuredMemberSelectViewController ()<UITableViewDelegate,UITableViewDataSource,UIScrollViewDelegate>
{
    BOOL edit;
    SocialInsurancePayUserListModel * selectedModel;
    
}

@property (nonatomic, strong)UITableView *tableView;

@property (nonatomic, strong)UIView *footerView;

@property (nonatomic, strong)UIView *backgroundView;

@property (nonatomic, strong)UIButton *btn_add;

@property (nonatomic, strong)NSArray *dataArray;
@property (nonatomic, strong)UIButton* bottomBtn;

@end

@implementation InsuredMemberSelectViewController
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [GTCountSDK trackCountEvent:@"home_shebaojiaofei_selectPeople" withArgs:@{}];
    
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor colorWithHex:0xf5f5f5];
    
    self.dataArray = [PayUserInfoManager sharedManager].model.userList;
    
    
    [self.view addSubview:self.backgroundView];
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.bottomBtn];
    
    //起点发生变化
   // self.edgesForExtendedLayout=UIRectEdgeBottom;
    
    [self configNavBar];
    
    if (self.dataArray.count > 0) {
        NSIndexPath* indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
        [self.tableView.delegate tableView:self.tableView didSelectRowAtIndexPath:indexPath];
    }
    //self.navigationController.navigationBar.translucent = NO;
    
   
   
}

- (void)configNavBar
{
   UIButton *rightButton = [[UIButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - 70,100, 50, 50)];
   [rightButton setTitle:@"编辑" forState:UIControlStateNormal];
    [rightButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    rightButton.titleLabel.font = [UIFont systemFontOfSize:15*WIDTHRADIU];
    [rightButton addTarget:self action:@selector(btnEditClick:) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    UIBarButtonItem* rightItem = [[UIBarButtonItem alloc]initWithCustomView:rightButton];
    self.navigationItem.rightBarButtonItem = rightItem;
    
  
}
#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    NSLog(@"%f",scrollView.contentOffset.y);
    CGFloat offsetY = scrollView.contentOffset.y;
    CGRect frame = self.backgroundView.frame;
    
    frame.origin.y = -offsetY;
    
    self.backgroundView.frame = frame;
}

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SocialInsurancePayUserListModel* model = self.dataArray[indexPath.row];
    
    InsuredMemberTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    cell.hidden = !edit;
    
    cell.selectionStyle =  UITableViewCellSelectionStyleNone;
    cell.lb_name.text = model.user_name;
    cell.lb_iconName.text = [model.user_name substringFromIndex:model.user_name.length - 2];
    cell.lb_IdNum.text = [NSString stringWithFormat:@"%@**************%@",[model.id_card substringToIndex:2],[model.id_card substringFromIndex:model.id_card.length - 2]];
    
    if (model.isSelected) {
        [cell.btn_selected setImage:[UIImage imageNamed:@"InsuredMember_selectedicon"] forState:UIControlStateNormal];
    }else{
        [cell.btn_selected setImage:[UIImage imageNamed:@"InsuredMember_unselectedicon"] forState:UIControlStateNormal];
    }
    cell.btn_delete.tag = indexPath.row;
    
    [cell.btn_delete addTarget:self action:@selector(modifyClick:) forControlEvents:UIControlEventTouchUpInside];
    return cell;
}

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

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    if (!edit) {
        
        if (selectedModel) {
            selectedModel.isSelected = NO;
            
            
 //         NSInteger index =  [self.dataArray indexOfObject:selectedModel];
//              [tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0] animated:NO scrollPosition:UITableViewScrollPositionNone];
        }
        SocialInsurancePayUserListModel* model = self.dataArray[indexPath.row];
           model.isSelected = YES;
           selectedModel = model;
//           [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
//           [tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
        [tableView reloadData];
    }
   
}
//- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    if (!edit) {
//        SocialInsurancePayUserListModel* model = self.dataArray[indexPath.row];
//          model.isSelected = NO;
//          [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
//    }
//
//}

#pragma mark - event resposne
- (void)btnClick:(UIButton *)sender
{
    if (sender == self.btn_add) {
        
        InsuredMemberAddViewController *vc = [[InsuredMemberAddViewController alloc]init];
        vc.title = @"参保人信息";
        vc.type = CTUPLoadInfoTypeNew;
        vc.sbmodel = self.model;
        vc.selectCity_code = self.selectCity_code;
        vc.selectCityName = self.selectCityName;
        vc.hukou_type = self.hukou_type;
        vc.hukou = self.hukou;
        vc.citys = self.citys;
        vc.wModel = self.wModel;
        [self.navigationController pushViewController:vc animated:YES];
    }
}

- (void)btnEditClick:(UIButton *)sender
{
    sender.selected = !sender.selected;
    edit = sender.selected;
    if (sender.selected) {
        [sender setTitle:@"完成" forState:UIControlStateNormal];
    }else{
        [sender setTitle:@"编辑" forState:UIControlStateNormal];
    }
    [self.tableView reloadData];
}
- (void)modifyClick:(UIButton *)sender
{
    SocialInsurancePayUserListModel* model = self.dataArray[sender.tag];
    
    InsuredMemberAddViewController *vc = [[InsuredMemberAddViewController alloc]init];
    vc.title = @"信息修改";
    vc.model = model;
    vc.type = CTUPLoadInfoTypeModify;
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)confirmClick:(UIButton *)sender
{
    
    if (self.dataArray.count > 0) {
        
        InsuredInfoController* vc = [[InsuredInfoController alloc]init];
        vc.title = @"参保信息";
        vc.model = self.model;
        vc.selectCity_code = self.selectCity_code;
        vc.selectCityName = self.selectCityName;
        vc.hukou_type = self.hukou_type;
        vc.hukou = self.hukou;
        vc.payUserModel = selectedModel;
        vc.citys = self.citys;
        vc.wModel = self.wModel;
        
        [self.navigationController pushViewController:vc animated:YES];
        
       

             
    }else{
        [Tools showMessage:@"请先添加参保人"];
    }
    
}


#pragma mark - getters and setters
- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight - cl_kNavigationBarHeight - 45) style:UITableViewStyleGrouped];
        //_tableView.backgroundView = self.backgroundView;
        
        
        
        _tableView.backgroundColor = [UIColor clearColor];
       
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.separatorStyle =  UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerClass:[InsuredMemberTableViewCell class] forCellReuseIdentifier:@"cell"];
        _tableView.tableFooterView = self.footerView;
        
       

        
    }
    return _tableView;
}

- (UIView *)footerView
{
    if (!_footerView) {
        _footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 93 * WIDTHRADIU)];
        _footerView.backgroundColor = [UIColor clearColor];
        
        [_footerView addSubview:self.btn_add];
        
    }
    return _footerView;
}

- (UIView *)backgroundView
{
    if (!_backgroundView) {
        _backgroundView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, ScreenHeight- 45)];
        _backgroundView.backgroundColor = [UIColor colorWithHex:0xf5f5f5];
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 60 * WIDTHRADIU)];
        view.backgroundColor = K_MainColor;
        [_backgroundView addSubview:view];
    }
    return _backgroundView;
}

- (UIButton *)btn_add
{
    if (!_btn_add) {
        _btn_add = [UIButton buttonWithType:UIButtonTypeCustom];
        _btn_add.frame = CGRectMake(10 * WIDTHRADIU, 15 * WIDTHRADIU, ScreenWidth - 20 * WIDTHRADIU, 78 * WIDTHRADIU);
        [_btn_add setTitle:@"新增人员" forState:UIControlStateNormal];
        [_btn_add setImage:[UIImage imageNamed:@"InsuredMember_addicon"] forState:UIControlStateNormal];
        [_btn_add setTitleColor:K_MainColor forState:UIControlStateNormal];
        _btn_add.backgroundColor = [UIColor whiteColor];
        _btn_add.titleLabel.font = [UIFont systemFontOfSize:14 * WIDTHRADIU];
        
         _btn_add.layer.cornerRadius = 10 * WIDTHRADIU;
        _btn_add.layer.shadowColor = K_MainColor.CGColor;
        _btn_add.layer.shadowOffset = CGSizeMake(0, 0);
        _btn_add.layer.shadowOpacity = 0.2;
        _btn_add.layer.shadowRadius = 2 * WIDTHRADIU;
        
        [_btn_add layoutButtonWithEdgeInsetsStyle:GLButtonEdgeInsetsStyleLeft imageTitleSpace:5];
        
        [_btn_add addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _btn_add;
}

- (UIButton *)bottomBtn
{
    if (!_bottomBtn) {
        _bottomBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _bottomBtn.frame = CGRectMake(0, ScreenHeight - 45, ScreenWidth, 45);
        _bottomBtn.backgroundColor = K_MainColor;
        [_bottomBtn setTitle:@"确定" forState:UIControlStateNormal];
        [_bottomBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _bottomBtn.titleLabel.font = [UIFont systemFontOfSize:15];
        [_bottomBtn addTarget:self action:@selector(confirmClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _bottomBtn;
}

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