//
//  WebViewController.m
//  RedAnt ERP Mobile
//
//  Created by Ray on 1/21/16.
//  Copyright © 2016 United Software Applications, Inc. All rights reserved.
//

#import "WebViewController.h"
#import "RAUtils.h"
#import "const.h"
@interface WebViewController ()

@end

@implementation WebViewController

- (void)viewWillAppear:(BOOL)animated
{

        [super viewWillAppear:animated];
        [[self navigationController] setNavigationBarHidden:NO animated:NO];
        
}

- (void)viewDidLoad
{
    [super viewDidLoad];
   // self.edgesForExtendedLayout = UIRectEdgeNone;
    // Do any additional setup after loading the view.
    UIRefreshControl *ref = [[UIRefreshControl alloc]init];
    ref.tag = 200 ;
    ref.attributedTitle = [[NSAttributedString alloc]initWithString:@"drag to refresh"];
    ref.tintColor = [UIColor colorWithRed:0.10 green:0.68 blue:0.94 alpha:0.7];
    //    ref.hidden = true;
    [ref addTarget:self action:@selector(changeCell) forControlEvents:UIControlEventValueChanged];
    [self.webView.scrollView addSubview:ref];
    
    
    self.webView.dataDetectorTypes=UIDataDetectorTypeAll;
    
    
    
    UIBarButtonItem *closeButton = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"back"] imageWithRenderingMode:UIImageRenderingModeAutomatic]
                                                                    style:UIBarButtonItemStylePlain
                                                                   target:self
                                                                   action:@selector( onBackClick:)];
    //[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Close", nil) style:UIBarButtonItemStylePlain target:self action:@selector(onCloseClick:)];
    //    closeButton.title = @"Close";
 //   closeButton.tintColor = UIColorFromRGB(0x996633);
    self.navigationItem.leftBarButtonItem = closeButton;
    
    
 
    
    
    //   self.url=@"http://192.168.1.3:8080/npd/test.pdf";
    //    NSURL* url = [NSURL URLWithString:self.url];
    //
    //    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:self.url]        cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:2000];
    //
    //
    //    [self.webView loadRequest:request];
    
    
    [self loadcontent];
}
- (void)onBackClick:(UIButton *)sender {
    
    if (self.backItemDismiss)
        [self.navigationController dismissViewControllerAnimated:YES completion:nil];
    
    [self.navigationController popViewControllerAnimated:FALSE];
}
-(void)ReloadData
{
    
    //    Count ++ ;
    //    [mytabelview reloadData];
    UIRefreshControl *reF = (UIRefreshControl *)[self.view viewWithTag:200];
    
    [reF endRefreshing];
    [self loadcontent];
    
    
}
-(void) loadcontent
{
    self.mum.hidden=false;
    
    
    NSURL *url =[NSURL URLWithString:self.url];
    DebugLog(self.url);
    NSURLRequest *request =[NSURLRequest requestWithURL:url];
    [self.webView loadRequest:request];
    
//    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    
//        
//        NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:self.url]        cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60*5];
//        
//        //设置请求方式为get
//        
//        [request setHTTPMethod:@"GET"];
//        
//        //添加用户会话id
//        
//        [request addValue:@"text/html" forHTTPHeaderField:@"Content-Type"];
//        
//        //连接发送请求
//        
//        
//        NSHTTPURLResponse* urlResponse = nil;
//        
//        NSError *error = [[NSError alloc] init];
//        
//        self.content= [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];
//        
//  
//        
//        dispatch_async(dispatch_get_main_queue(), ^{
//            
//            self.navigationItem.title = self.title;
//            UIRefreshControl *reF = (UIRefreshControl *)[self.view viewWithTag:200];
//            //  self.content=nil;
//            if(self.content!=nil&&self.content.length>0)
//            {
//                [reF removeFromSuperview];
//                
//                
////                // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
////                NSString *path = NSTemporaryDirectory();
////                NSString *filePath = [path stringByAppendingPathComponent:self.filename];
////                //    NSError *error = [[NSError alloc] init];
////                //    BOOL bo = [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:&error];
////                //    NSAssert(bo,@"创建目录失败");
////                [self.content writeToFile:filePath atomically:YES];
////                
////                
////                self.btnshare.enabled =true;
////                
////                
////                self.btnemail.enabled =true;
////                self.btnsave.enabled =true;
////                
////                if(self.onLoadSuccess)
////                    self.onLoadSuccess(self.url);
//                //               int a=0;
//            }
//            else
//            {
//                [RAUtils message_alert:nil  title:@"Open URL Failed" controller:self];
//                //                self.content = [self.url dataUsingEncoding:NSUTF8StringEncoding];
//            }
//            
//            ;
//            //            DebugLog(@"%@",[[NSString alloc] initWithData:self.content encoding:NSASCIIStringEncoding]);
//            //
//            
//            
//            [self.webView loadData:self.content MIMEType:urlResponse.MIMEType textEncodingName:nil baseURL:nil];
//            
//            
//            
//            
//        });
//    });
    
    
    
    
}
-(void)changeCell
{
    UIRefreshControl *reF = (UIRefreshControl *)[self.view viewWithTag:200];
    reF.attributedTitle = [[NSAttributedString alloc]initWithString:@"refreshing"];
    if ([self respondsToSelector:@selector(ReloadData)])
    [self performSelector:@selector(ReloadData) withObject:nil afterDelay:1];
    //    DebugLog(@"refresh!!!!!!!!");
    
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



#pragma mark - web view delegate
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    self.mum.hidden=true;
    
    
}

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