//
//  TDQrJoinVC.m
//  smartRhino
//
//  Created by niuzhen on 2019/12/14.
//  Copyright © 2019 tederen. All rights reserved.
//

#import "TDQrJoinVC.h"

@interface TDQrJoinVC ()
@property (weak, nonatomic) IBOutlet UIImageView *IconView;
@property (weak, nonatomic) IBOutlet UILabel *numberL;
@property (weak, nonatomic) IBOutlet UILabel *GroupNameL;
@property (weak, nonatomic) IBOutlet UIButton *joinBtn;
@end

@implementation TDQrJoinVC

+ (TDQrJoinVC *)initTDQrJoinVC{
    TDQrJoinVC *controller = [StoryboardManager.shared.TDGroupInfo instantiateViewControllerWithIdentifier:@"TDQrJoinVC"];
    return controller;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.fd_prefersNavigationBarHidden = YES;
    [self loadCusViewData];
}

- (void)loadCusViewData
{
    [self.IconView sd_setImageWithURL:[NSURL URLWithString:[self.dict objectForKey:@"AvatarUrl"]] placeholderImage:[UIImage imageNamed:@"imagePlaceholder"]];
    self.numberL.text = [NSString stringWithFormat:@"(共%ld人)",(long)[[self.dict objectForKey:@"GroupUserNumber"] integerValue]];
    self.GroupNameL.text = [self.dict objectForKey:@"Name"];
}
- (IBAction)ClickJoinAction:(id)sender {
    NSDictionary * groupDict = @{@"Id":@([[self.dict objectForKey:@"Id"] integerValue])};
    NSLog(@"%@",[groupDict objectForKey:@([[self.dict objectForKey:@"Id"] integerValue])]);
    WS(weakSelf);
    NSDictionary * paraDict = @{@"GroupId":@([[self.dict objectForKey:@"Id"] integerValue]),
                                @"UserIds":@[@([AppUserModel sharedAppUserModel].Id)],
                                @"UserId":@([AppUserModel sharedAppUserModel].Id),
                                @"SourceType":@(1)///申请
    };
//    if ([self.dict[@"IsUser"] boolValue]) {
//        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//            [[NSNotificationCenter defaultCenter] postNotificationName:PUSHTDGROUPCOTROLLER object:nil userInfo:weakSelf.dict];
//            [self.navigationController popToRootViewControllerAnimated:NO];
//        });
//    }else{
    [[HttpManager sharedHttpManager] POSTUrl:Host(APP_Group_Insert_User) parameters:paraDict responseStyle:JOSN success:^(id  _Nonnull responseObject) {
        if ([responseObject[@"Code"] integerValue] == 200) {
            dispatch_async(dispatch_get_main_queue(), ^{
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [[NSNotificationCenter defaultCenter] postNotificationName:PUSHTDGROUPCOTROLLER object:nil userInfo:weakSelf.dict];
                });
            });
        }else{
            dispatch_async(dispatch_get_main_queue(), ^{
                UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"你的申请已提交,请耐心等候!" preferredStyle:(UIAlertControllerStyleAlert)];
                UIAlertAction * sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                    dispatch_async(dispatch_get_main_queue(), ^{
                        [weakSelf.navigationController popToRootViewControllerAnimated:NO];
                    });
                }];
                [alert addAction:sureAction];
                [weakSelf presentViewController:alert animated:YES completion:^{
                    
                }];
            });
        }
    } failure:^(NSError * _Nonnull error) {
        SHOWERROR([ZYCTool handerResultData:error]);
    }];
//    }
}

- (IBAction)ClickbackAction:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}
- (IBAction)ClickMenuAction:(id)sender {
    [super menuAction:sender];
}

@end