//
//  UserInfoVC.m
//  gaotingshan
//
//  Created by Mac Book DQ02 on 16/6/2.
//  Copyright © 2016年 Xu Mengzhu. All rights reserved.
//

#import "UserInfoVC.h"


@interface UserInfoVC ()

@end

@implementation UserInfoVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor colorWithRed:241/255. green:241/255. blue:241/255. alpha:0.999];
     self.navigationItem.title = @"基本信息";
    
//    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"nav_bg"] forBarMetrics:(UIBarMetricsDefault)];
//    //改变字体颜色
//    NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
//    textAttrs[NSForegroundColorAttributeName] = [UIColor whiteColor];
//    self.navigationController.navigationBar.titleTextAttributes = textAttrs;
    
    [Manager setNavi:self.navigationController.navigationBar];
    
    
#pragma - 设置导航栏左边的按钮
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"nav_back"] landscapeImagePhone:nil style:UIBarButtonItemStylePlain target:self action:@selector(leftBarBtnClicked:)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
    
    Singleton *single = [Singleton defaultSingleton];
    
    _webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-64)];
    _webView.delegate = self;
    
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/terminal/userinfo_index?userId=%@",localRequestUrl,single.userid]]];
    [_webView loadRequest:request];
    [self.view addSubview:_webView];
}

#pragma mark - 左边Button的点击事件
- (void)leftBarBtnClicked:(UIButton *)sender {
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"addVirtualEventFresh" object:nil];//通知添加虚拟赛事页，刷新页面状态
    
    [self dismissViewControllerAnimated:NO completion:nil];
}

#pragma mark webview代理事件
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    NSLog(@"requestUrl = %@",request.URL.absoluteString);
    if ([request.URL.absoluteString containsString:@"userinfo_save/success"]) {
        //保存成功后，截取出昵称，要发送通知给我的，左视图，做视图修改，修改昵称
        NSArray *ary = [request.URL.absoluteString componentsSeparatedByString:@"nickName="];
        NSString *base64NickAndSex = [ary objectAtIndex:1];
        NSArray *nickAndSex = [base64NickAndSex componentsSeparatedByString:@"&sex="];
        NSString *base64Nick = [nickAndSex objectAtIndex:0];
        NSString *sex = [nickAndSex objectAtIndex:1];
        //base64解密
        NSData *data = [[NSData alloc] initWithBase64EncodedString:base64Nick options:NSDataBase64DecodingIgnoreUnknownCharacters];
        NSString *nick= [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
        Singleton *single = [Singleton defaultSingleton];
        single.nickName = nick;
        single.sex = [NSNumber numberWithInt:[sex intValue]];
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"ToAssignValueAndReLoadWebView" object:nil];//我的
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"changeNickFromUsrInfoVC" object:nick];//左视图
        //点击竞赛右上角的➕，添加虚拟赛事，信息页面是自己新开的页面，所以要通知，添加虚拟赛事首次加载的网页，刷新。或者，返回按钮的事件中，也要通知刷新上一个页面
         [[NSNotificationCenter defaultCenter] postNotificationName:@"addVirtualEventFresh" object:nil];//通知添加虚拟赛事页，刷新页面状态
        
        [self dismissViewControllerAnimated:NO completion:nil];
        return NO;
    }
    return YES;
}

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


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