//
//  OrderListViewController.m
//  RedAnt ERP Mobile
//
//  Created by Ray on 14-7-7.
//  Copyright (c) 2014年 United Software Applications, Inc. All rights reserved.
//

#import "CommonGridViewController.h"
#import "AppDelegate.h"
#import "iSalesNetwork.h"
//#import "OrderListCell.h"
#import "CommonEditorViewController.h"
//#import "PopupNavigationController.h"
@interface CommonGridViewController ()

@end

@implementation CommonGridViewController


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}


- (void)onAddClick:(UIButton *)sender {
    
    
    
    
    CommonEditorViewController * orderinfoVC =[ self.storyboard instantiateViewControllerWithIdentifier:@"CommonEditorViewController"];
    
    

    UINavigationController* navi = [[UINavigationController alloc] initWithRootViewController:orderinfoVC] ;
    
    
    
    
    
    navi.modalPresentationStyle = UIModalPresentationPageSheet;//有三种状态，自己看看是哪种
    [self presentViewController:navi animated:YES completion:^{
        
        DebugLog(@"login.........");
        
        self.btop = false;
        //  <#code#>
    }];
}
- (void)onSearch:(UIButton *)sender {
    CommonEditorViewController * orderinfoVC =[ self.storyboard instantiateViewControllerWithIdentifier:@"CommonEditorViewController"];
    
    
    
    UINavigationController* navi = [[UINavigationController alloc] initWithRootViewController:orderinfoVC] ;
    
    
    
    
    
    navi.modalPresentationStyle = UIModalPresentationFormSheet;//有三种状态，自己看看是哪种
    [self presentViewController:navi animated:YES completion:^{
        
        DebugLog(@"login.........");
        
        self.btop = false;
        //  <#code#>
    }];
}
-(void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
     AppDelegate *app = (AppDelegate *)[[UIApplication sharedApplication] delegate];
     [app.window removeGestureRecognizer:self.tapGesture];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    UIBarButtonItem *addButton=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(onAddClick:)];
    
    UIBarButtonItem *searchButton=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(onSearch:)];
    
  //  id aa = self.navigationItem;
    
  //  self.navi_item.title = @"ttttt";
    
  //  self.navigationItem.rightBarButtonItem = searchButton;
    self.navi_item.rightBarButtonItems=[NSArray arrayWithObjects:addButton,searchButton, nil];
    
    
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handletapPressGesture:)];
    AppDelegate *app = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    [app.window addGestureRecognizer:tapGesture];
    tapGesture.delegate = self;
    
    self.btop = true;
    self.tapGesture = tapGesture;
    
    
    self.grid.fullrowselect = true;
    self.grid.enableTap = self.enableTap;
    self.grid.enableLongPress = self.enableLongPress;
    self.grid.griddelegate = self;

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    if(!self.btop)
        return NO;
    if ([NSStringFromClass([touch.view class]) isEqualToString:@"SimpleGrid"]) {//如果当前是tableView
        //做自己想做的事
        return NO;
    }
    return YES;
}

-(void)handletapPressGesture:(UITapGestureRecognizer*)sender{

    CGPoint point = [sender locationInView:self.view];
    if (point.x<self.view.frame.origin.x || point.x >self.view.frame.origin.x+self.view.frame.size.width||point.y<self.view.frame.origin.y||point.y>self.view.frame.origin.y+self.view.frame.size.height) {
        [self dismissViewControllerAnimated:YES
                                 completion:^{
                                     //一定要移除手势 否则下次 没有子视图的时候 点击 会崩溃拉
                                     AppDelegate *app = (AppDelegate *)[[UIApplication sharedApplication] delegate];
                                     [app.window removeGestureRecognizer:sender];
                                 }];
    }
     
}
#pragma mark - Longpress Menu
- (void) showDetail:(UIMenuItem*) sender {
    
    
    


    
    
    CommonEditorViewController * orderinfoVC =[ self.storyboard instantiateViewControllerWithIdentifier:@"CommonEditorViewController"];
    
    
//    orderinfoVC.modalPresentationStyle = UIModalPresentationPageSheet;//有三种状态，自己看看是哪种
//    [self presentViewController:orderinfoVC animated:YES completion:^{
//        
//        DebugLog(@"login.........");
//        //  <#code#>
//    }];
////    
    UINavigationController* navi = [[UINavigationController alloc] initWithRootViewController:orderinfoVC] ;
    

    
    
    
    navi.modalPresentationStyle = UIModalPresentationPageSheet;//有三种状态，自己看看是哪种
    [self presentViewController:navi animated:YES completion:^{
        
        DebugLog(@"login.........");
        
            self.btop = false;
        //  <#code#>
    }];
 


    
    
    
//    orderlistVC.delegate = self;

}
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
    if (action == @selector(showDetail:))
    return YES;//显示
    else
    return NO;//不显示
}
- (BOOL)canBecomeFirstResponder{
    return YES;
}

#pragma mark - SimpleGridDelegate
- (void)Tap:(UIView *)view row:(long)x
{
    
}
//- (void) LongPress:(UIView*)view row:(int) x column:(int) y;
- (void) LongPress:(UIView*)view row:(long) x
{
    DebugLog(@"!!!!!!!!!!!!!!!!!");
     NSDictionary *objrecord = [self.grid.data objectForKey:[NSString stringWithFormat:@"item_%ld",x]];
    self.orderID = [objrecord valueForKey:@"id"];
     [self becomeFirstResponder];
    UIMenuItem *detail = [[UIMenuItem alloc] initWithTitle:@"Open" action:@selector(showDetail:)];
    //detail.
    //    [detail setValue:_id forKey:@"_id"];
    
    UIMenuController *menu = [UIMenuController sharedMenuController];
    
    
    
    [menu setMenuItems:[NSArray arrayWithObjects:detail, nil]];
    
    [menu setTargetRect:view.frame inView:view.superview];
    [menu setMenuVisible:YES animated:YES];
}
@end
