//
//  MyScoreVC.m
//  gaotingshan
//
//  Created by MacBook Dq01 on 16/4/7.
//  Copyright © 2016年 Mac Book DQ02. All rights reserved.
//某一赛事所有成员的成绩列表，或者会有团队成绩

#import "MyScoreVC.h"
#import "Singleton.h"

@interface MyScoreVC () <UIWebViewDelegate> {
    Singleton *singleton;
    
}

@property (nonatomic, strong) UIWebView *webView;


@end

@implementation MyScoreVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    singleton = [Singleton defaultSingleton];
    
    self.navigationItem.title = @"赛事成绩";
    self.view.backgroundColor = [UIColor whiteColor];

//    [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(leftBarBtnItemIsClicked:)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];

#pragma mark --设置导航栏右边朋友圈分享按钮
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"share"] landscapeImagePhone:nil style:UIBarButtonItemStylePlain target:self action:@selector(rightBarBtnItemClicked:)];
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor whiteColor];
    
    //以下是对webView的一些处理。
//    _webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64)];
//    [_webView setUserInteractionEnabled:YES];
//    _webView.delegate = self;
//    [_webView setOpaque:NO]; //设置是否透明。
//    [_webView setScalesPageToFit:YES]; //自动缩放以适应屏幕。
//    _webView.scrollView.bounces = NO;
//    _webView.scrollView.showsVerticalScrollIndicator = NO;
    
    _webView = [Manager screenFrameWebview];
    _webView.delegate = self;
    
    if (![singleton.string isEqualToString:@"9002"]) {
        singleton.userid = @"-1";
    }
    //加载网页
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/wap/event_repeater_ranking?eventId=%@&userId=%@",localRequestUrl, singleton.scoreId,singleton.userid]];
    [_webView loadRequest:[NSURLRequest requestWithURL:url]];
    [self.view addSubview:_webView];


}

#pragma mark - 导航栏左边按钮被点击
- (void)leftBarBtnItemIsClicked:(UIBarButtonItem *)sender {
    if ([_webView canGoBack]) {
        [_webView goBack];
    }else
    {
        //通知“我的赛事管理”页面
        [[NSNotificationCenter defaultCenter] postNotificationName:@"reLoadCompeteDetail" object:nil];
        
        //返回上一页面
        [self dismissViewControllerAnimated:NO completion:nil];
    }
  
   
}


-(void)rightBarBtnItemClicked:(UIBarButtonItem *)sender
{
   if ([WXApi isWXAppInstalled] && [WXApi isWXAppSupportApi])
   {
       //朋友圈分享
     /*  WXMediaMessage *message = [WXMediaMessage message];
       message.title = @"智慧皋亭山－－赛事成绩";
       message.description = @"分享结果";
       
       
       [message setThumbImage:[UIImage imageNamed:@"1111"]];
       
       WXWebpageObject *pageObj = [WXWebpageObject object];
       NSString *str = [NSString stringWithFormat:@"%@&shareFlag=1",self.webView.request.URL.absoluteString];
       pageObj.webpageUrl = str;
       message.mediaObject = pageObj;
       
       SendMessageToWXReq *reg = [[SendMessageToWXReq alloc]init];
       reg.bText = NO;
       reg.message = message;
       reg.scene = WXSceneTimeline;
       
       [WXApi sendReq:reg];*/
       [Manager shareToWXTimeLine:self.webView.request.URL.absoluteString];
       
   }else
   {
       [FileDownloadTool alert:@"您还没安装微信" view:self.view];
   }
       
    
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    NSLog(@"我的成绩:%@", request.URL.absoluteString);
    if ([request.URL.absoluteString containsString:@"event_repeater_ranking_user_info?eventId="]) {
       //跳转到带心率计步的页面
        EachScoreVC *eachScore = [[EachScoreVC alloc] init];
        eachScore.requestUrl= request.URL.absoluteString;
        CustomNavViewController *nav = [[CustomNavViewController alloc] initWithRootViewController:eachScore];
        [self presentViewController:nav animated:YES completion:nil];
        return NO;
    }
    if ([request.URL.absoluteString containsString:@"event_team_user_repeater_ranking"]) {
        //表示要跳转到团队各个成员的成绩页面，不新开页面，但是导航栏标题要改变
        self.navigationItem.title = @"团队成绩";
    }else
    {
        self.navigationItem.title = @"赛事成绩";
    }
    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
