//
//  managementDeviceVC.m
//  gaotingshan
//
//  Created by MacBook Dq01 on 16/3/19.
//  Copyright © 2016年 Mac Book DQ02. All rights reserved.
//

#import "managementDeviceVC.h"
#import "BindingDeviceVC.h"
#import "Singleton.h"

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

@property (nonatomic, strong) UIWebView *webView;

@end

@implementation managementDeviceVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //创建单例实例
    singleton = [Singleton defaultSingleton];
    self.view.backgroundColor = [UIColor whiteColor];
    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(leftBarBtnIsClicked)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
    
#pragma - 设置导航栏右边的Btn
 //   self.navigationItem.rightBarButtonItem = [Manager rightItemTarget:self img:@"add" tag:1000000 action:@selector(rightBarBtnIsClicked)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"add"] landscapeImagePhone:nil style:UIBarButtonItemStylePlain target:self action:@selector(rightBarBtnIsClicked)];
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor whiteColor];
    
    
    //加载webView页面。
    [self loadWebView];

    
}

#pragma mark - webView加载方法；
- (void)loadWebView {
    //webView加载页面.
    _webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    [_webView setUserInteractionEnabled:YES];
    _webView.delegate = self;
    [_webView setOpaque:NO]; //设置是否透明。
    [_webView setScalesPageToFit:YES]; //自动缩放以适应屏幕。
    //加载网页.
   
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/terminal/mydevice?sesskey=%@",localRequestUrl, singleton.sesskey]];
    [_webView loadRequest:[NSURLRequest requestWithURL:url]];
    [self.view addSubview:_webView];
}

- (void)reLoadWebView {
  
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/terminal/mydevice?sesskey=%@",localRequestUrl, singleton.sesskey]];
    [_webView loadRequest:[NSURLRequest requestWithURL:url]];
    [self.view addSubview:_webView];
}

#pragma mark - 导航栏左边按钮被点击
- (void)leftBarBtnIsClicked {
    //回到上一级界面
    [self dismissViewControllerAnimated:YES completion:nil];
    
}

#pragma mark - 导航栏右边➕按钮被点击
- (void)rightBarBtnIsClicked {
    BindingDeviceVC *bindDeviceVC = [[BindingDeviceVC alloc] init];
    UINavigationController *naVC = [[UINavigationController alloc] initWithRootViewController:bindDeviceVC];
    [self presentViewController:naVC animated:NO completion:nil];
    
}


#pragma mark - UIWebViewDelegate methods
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    NSLog(@"管理我的登山设备%@", [request.URL absoluteString]);
    
    if ([request.URL.absoluteString containsString:@"mydevice?deldevice=success&sesskey"]) {
        //发送通知到“MyVC”页面， 重新加载webview。
        [[NSNotificationCenter defaultCenter] postNotificationName:@"reLoadMyVCWebView" object:nil];
        
        //返回上一页面。
        [self dismissViewControllerAnimated:NO completion:nil];
    }
    
    return YES;
}


-(void)webViewDidFinishLoad:(UIWebView *)webView
{
    // [[NSURLCache sharedURLCache] removeAllCachedResponses];
}

-(void)viewWillAppear:(BOOL)animated
{
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/terminal/mydevice?sesskey=%@",localRequestUrl, singleton.sesskey]];
    [_webView loadRequest:[NSURLRequest requestWithURL:url]];
}

-(void)dealloc
{
   
}

///////////////////////////////////
- (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
