//
//  AttractionCollectionListVC.m
//  gaotingshan
//
//  Created by Mac Book DQ02 on 16/6/13.
//  Copyright © 2016年 Xu Mengzhu. All rights reserved.
//景点收藏列表页面

#import "AttractionCollectionListVC.h"

@interface AttractionCollectionListVC ()

@end

@implementation AttractionCollectionListVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"景点收藏列表";
    self.view.backgroundColor = [UIColor whiteColor];

    
    [Manager setNavi:self.navigationController.navigationBar];
    
    
    //设置导航栏左变返回按钮
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"nav_back"] landscapeImagePhone:nil style:UIBarButtonItemStylePlain target:self action:@selector(backBtnClicked)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
    
    Singleton *singleton = [Singleton defaultSingleton];
    //以下是对webView的一些处理。
//    _webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64)];
//    [_webView setUserInteractionEnabled:YES];
//    [_webView setOpaque:NO]; //设置是否透明。
//    [_webView setScalesPageToFit:YES]; //自动缩放以适应屏幕。
//    _webView.scrollView.bounces = NO;
//    _webView.scrollView.showsVerticalScrollIndicator = NO;
    
    _webView = [Manager screenFrameWebview];
    _webView.delegate = self;
    //加载网页
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/terminal/member/area_favorite_list?sesskey=%@",localRequestUrl, singleton.sesskey]];
    
    [_webView loadRequest:[NSURLRequest requestWithURL:url]];
    [self.view addSubview:_webView];

    
}

#pragma mark webview开始加载执行的函数
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{

    NSLog(@"景点详情url＝%@",request.URL.absoluteString);
    
    if ([request.URL.absoluteString containsString:@"terminal/scenic/area_info?id="]) {
        //景点详情页面（内部跳转）
        
        NSString *encodingString = [request.URL.absoluteString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        
        NSArray *rootArr = [encodingString componentsSeparatedByString:@"&name="];
        NSArray *foreArr= [[rootArr objectAtIndex:0] componentsSeparatedByString:@"id="];
        NSArray *backArr = [[rootArr objectAtIndex:1] componentsSeparatedByString:@"&imgUrl="];
        self.areaId = [foreArr objectAtIndex:1];//存储当前点击的景点的id
        self.currentSpotName = [backArr objectAtIndex:0];//存储当前点击的景点的名字
        
        NSString *areaName = [backArr objectAtIndex:0];//取景点的名字
        
        NSArray *thirdArr = [[backArr objectAtIndex:1] componentsSeparatedByString:@"&keyword="];
        NSString *keyword;
        if (![[thirdArr objectAtIndex:1] isEqualToString:@""]) {
            NSArray *forthA = [[thirdArr objectAtIndex:1] componentsSeparatedByString:@"&sesskey="];
            if(![[forthA objectAtIndex:0] isEqualToString:@""])
            {
                keyword = [forthA objectAtIndex:0];
                NSLog(@"有keyword =%@",keyword);
               areaName = [NSString stringWithFormat:@"%@(%@)",[backArr objectAtIndex:0],keyword];//取景点的名字
            }
            
        }
        
        AttractionDetailController *at = [[AttractionDetailController alloc]init];
        at.titleString = areaName;
        at.code = @"";
        at.areaId = self.areaId;
        UINavigationController *na= [[UINavigationController alloc]initWithRootViewController:at];
        [self presentViewController:na animated:NO completion:nil];
        return NO;

    }
    
    /*
    if ([request.URL.absoluteString containsString:@"terminal/scenic/area_info/call_phone?phone="])
    {
        //拨打电话，调用电话功能
       
        NSArray *ary = [request.URL.absoluteString componentsSeparatedByString:@"phone="];//电话号码截取出来
        NSString *phone = [ary objectAtIndex:1];
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"telprompt:%@",phone]]];
        return NO;
    }
    if ([request.URL.absoluteString containsString:@"area_info?type=ditu&code="]){
        //景点详情页面，地图点击跳转
        NSArray *arr = [request.URL.absoluteString componentsSeparatedByString:@"&code="];
        NSString *code = [arr objectAtIndex:1];
        //景点详情页面，地图点击跳转
        [Manager spotDetailMapClicked:self andName:self.currentSpotName andCode:code];
        
        return NO;
    
    }
    
    if ([request.URL.absoluteString containsString:@"area_info?type=share&id="]) {
        NSArray *arr = [request.URL.absoluteString componentsSeparatedByString:@"&imgUrl="];
        [Manager shareToSinaEvent:self viewController:self andImgUrl:[arr objectAtIndex:1] andSpotname:self.currentSpotName];
        return NO;
    }
    
    if ([request.URL.absoluteString containsString:@"scenic/area_info?type=favorite"]) {
        NSArray *arr = [request.URL.absoluteString componentsSeparatedByString:@"type=favorite&id="];
        NSArray *arr2 = [[arr objectAtIndex:1] componentsSeparatedByString:@"&imgUrl="];
        
        [Manager requestForCollection:self andAreaId:[arr2 objectAtIndex:0]];
        return NO;
    }
*/
    
    return YES;
}



-(void)webViewDidFinishLoad:(UIWebView *)webView{
    _currentUrl = webView.request.URL.absoluteString ;
}

-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
    
}

#pragma mark 返回按钮点击事件
-(void)backBtnClicked
{
        [self dismissViewControllerAnimated:NO completion:nil];
}
- (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
