//
//  ContactAdvanceSearchViewController.m
//  RedAnt ERP Mobile
//
//  Created by Ray on 12/7/15.
//  Copyright © 2015 United Software Applications, Inc. All rights reserved.
//

#import "ContactAdvanceSearchViewController.h"
#import "iSalesNetwork.h"
@interface ContactAdvanceSearchViewController ()

@end

@implementation ContactAdvanceSearchViewController
-(void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
            [[self navigationController] setNavigationBarHidden:NO animated:NO];
    //    self.offset = 0;
    //    [self.content_data removeAllObjects];
    //    [self loadpage];
    
   // [[self navigationController] setNavigationBarHidden:!self.showNavibar animated:NO];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    
    UIBarButtonItem *closeButton = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"close"] imageWithRenderingMode:UIImageRenderingModeAutomatic]
                                                                    style:UIBarButtonItemStylePlain
                                                                   target:self
                                                                   action:@selector( onBackClick:)];
    
//    closeButton.tintColor = UIColorFromRGB(0x996633);
    self.navigationItem.leftBarButtonItem = closeButton;
    
    
    
    UIBarButtonItem *searchButton = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"search_bar_button"] imageWithRenderingMode:UIImageRenderingModeAutomatic]
                                                                    style:UIBarButtonItemStylePlain
                                                                   target:self
                                                                   action:@selector( onSearchClick:)];
    
  //  searchButton.tintColor = UIColorFromRGB(0x996633);
    self.navigationItem.rightBarButtonItem = searchButton;
    
    
//    if (self.data_init==nil) {
//        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//            
//            NSDictionary* editor_json = [iSalesNetwork request_CustomerInfo:self.contactId];
//            
//            dispatch_async(dispatch_get_main_queue(), ^{
//                
//                
//                
//                if([[editor_json valueForKey:@"result"] intValue]==2)
//                {
//                    self.data_init = [[editor_json objectForKey:@"customerInfo" ] mutableCopy];
//                    
//                    [self fillData];
//                    
//                    
//                }
//                else
//                {
//                    [RAUtils message_alert:[editor_json valueForKey:@"err_msg"] title:@"Loading Contact Info Failed." controller:self] ;
//                }
//                
//                
//            });
//        });
//    }
//    else
//    {
        [self fillData];
 //   }
    
    
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)onBackClick:(UIButton *)sender {
    
    
    [self.navigationController popViewControllerAnimated:(false)];
    //    [self.navigationController dismissViewControllerAnimated:true completion:^{
    //        ;
    //    }];
}
- (IBAction)onCloseClick:(id)sender {
    [self dismissViewControllerAnimated:true completion:nil];
}
-(void) request_fill
{
    
}
- (void) fillData
{
    int section_count=[[self.content_data_download valueForKey:@"section_count"] intValue];
    
    
    //    NSDictionary* data = [self.data_init objectForKey:@"customerInfo"];
    for(int i=0;i<section_count;i++)
    {
        NSMutableDictionary * sectionjson= [[self.content_data_download objectForKey:[NSString stringWithFormat:@"section_%d",i]] mutableCopy];
        int count = [[sectionjson valueForKey:@"count"] intValue];
        for(int j=0;j<count;j++)
        {
            NSMutableDictionary * itemjson=[[sectionjson objectForKey:[NSString stringWithFormat:@"item_%d",j]] mutableCopy];
            
            NSString* control=[itemjson valueForKey:@"control"];
            
            NSString* name = [itemjson valueForKey:@"name"];
            
            NSString* value = [self.data_init valueForKey:name];
            
            [itemjson setValue:value forKey:@"value"];
            
            if([control isEqualToString:@"img"])
            {
                {
                    NSString* img_url = [itemjson valueForKey:@"img_url_0"];
                    
                    NSString* url_value = [self.data_init valueForKey:img_url];
                    [itemjson setValue:url_value forKey:@"img_url_0"];
                }
                {
                    NSString* img_url = [itemjson valueForKey:@"img_url_1"];
                    
                    NSString* url_value = [self.data_init valueForKey:img_url];
                    [itemjson setValue:url_value forKey:@"img_url_1"];
                }
                {
                    NSString* img_url = [itemjson valueForKey:@"img_url_2"];
                    
                    NSString* url_value = [self.data_init valueForKey:img_url];
                    [itemjson setValue:url_value forKey:@"img_url_2"];
                }
            }
            else if([control isEqualToString:@"enum"])
            {
//                NSMutableDictionary * cadedate = [itemjson objectForKey:@"cadedate"];
                NSArray* checked = [value componentsSeparatedByString:NSLocalizedString(@",", nil)];
                NSMutableDictionary* cadejson=[[itemjson objectForKey:@"cadedate"] mutableCopy];
                int count = [[cadejson valueForKey:@"count"] intValue];
                // bool setvalue = false;
                for(int cc=0;cc<count;cc++)
                {
                    NSMutableDictionary* valjson=[[cadejson objectForKey:[NSString stringWithFormat:@"val_%d",cc]] mutableCopy];
                    
                    NSString* value_id=[NSString stringWithFormat:@"%@",[valjson valueForKey:@"value_id"]];
                    
                    int index=[checked indexOfObject:value_id];
                    if(NSNotFound!=index&& checked!=nil)
                    {
                        
                        valjson[@"check"]=[NSNumber numberWithInt:1];
                        cadejson[[NSString stringWithFormat:@"val_%d",cc]]=valjson;
                    }
                }
                
                itemjson [@"cadedate"]= cadejson;
            }
            
            [sectionjson setObject:itemjson forKey:[NSString stringWithFormat:@"item_%d",j]];
            
            
        }
        [self.content_data_download setObject:sectionjson forKey:[NSString stringWithFormat:@"section_%d",i]];
    }
    self.content_data_control=[self translate_json:self.content_data_download changed:self.changed_data];
    
    //    self.navigationItem.title =  [self.content_data_download valueForKey:@"title"] ;
    [self.editorTable reloadData];
}
- (IBAction)onSearchClick:(id)sender {
    
    // add params check here.
    [self.lastedit endEditing:true];
    [self.lasttextview endEditing:true];
//    self.cancommit=true;
//    NSMutableDictionary* upparams=[[NSMutableDictionary alloc] init];
//    //    [upparams setValue:[self.params valueForKey:@"cart2Checkbox"] forKey:@"cart2Checkbox"];
//    int section_count=[[self.content_data_download valueForKey:@"section_count"] intValue];
//    for(int i=0;i<section_count;i++)
//    {
//        NSMutableDictionary * sectionjson = [[self.content_data_download objectForKey:[NSString stringWithFormat:@"section_%d",i ]] mutableCopy];
//        int item_count = [[sectionjson valueForKey:@"count"] intValue];
//        for(int j=0;j<item_count;j++)
//        {
//            NSMutableDictionary * itemjson=[[sectionjson objectForKey:[NSString stringWithFormat:@"item_%d",j ]] mutableCopy];
//            NSString* key = [itemjson valueForKey:@"name"];
//            if(key==nil || key.length==0)
//                continue;
//            if([[itemjson valueForKey:@"control"] isEqualToString:@"enum"])
//            {
//                NSString* single_select = [itemjson valueForKey:@"single_select"];
//                if([single_select isEqualToString:@"true"])
//                {
//                    NSDictionary* cadejson=[itemjson objectForKey:@"cadedate"];
//                    int count = [[cadejson valueForKey:@"count"] intValue];
//                    bool setvalue = false;
//                    for(int cc=0;cc<count;cc++)
//                    {
//                        NSDictionary* valjson=[cadejson objectForKey:[NSString stringWithFormat:@"val_%d",cc]];
//                        if([[valjson valueForKey:@"check"]intValue]==1)
//                        {
//                            [upparams setValue:[valjson valueForKey:@"value_id"] forKey:[itemjson valueForKey:@"name"]];
//                            setvalue=true;
//                            break;
//                        }
//                    }
//                    if([[itemjson valueForKey:@"required"] isEqualToString: @"true"] && setvalue==false)
//                    {
//                        self.cancommit = false;
//                    }
//                }
//                else
//                {
//                    
//                    NSMutableArray* checked = [[NSMutableArray alloc] init];
//                    NSDictionary* cadejson=[itemjson objectForKey:@"cadedate"];
//                    int count = [[cadejson valueForKey:@"count"] intValue];
//                    // bool setvalue = false;
//                    for(int cc=0;cc<count;cc++)
//                    {
//                        NSDictionary* valjson=[cadejson objectForKey:[NSString stringWithFormat:@"val_%d",cc]];
//                        if([[valjson valueForKey:@"check"]intValue]==1)
//                        {
//                            
//                            [checked addObject:[NSNumber numberWithInt:[[valjson valueForKey:@"value_id"] intValue]]];
//                            //                            [upparams setValue:[valjson valueForKey:@"value_id"] forKey:[itemjson valueForKey:@"name"]];
//                            //                            setvalue=true;
//                            //                            break;
//                        }
//                    }
//                    if([[itemjson valueForKey:@"required"] isEqualToString: @"true"]&&checked.count==0)
//                    {
//                        self.cancommit = false;
//                    }
//                    else
//                    {
//                        NSString * string = [checked componentsJoinedByString:@","];
//                        [upparams setValue:string forKey:[itemjson valueForKey:@"name"]];
//                    }
//                }
//                /*
//                 upparams setValue:itemjson forKey:<#(NSString *)#>
//                 
//                 */
//            }else
//                if([[itemjson valueForKey:@"control"] isEqualToString:@"action"])
//                {
//                    if([[itemjson valueForKey:@"required"] isEqualToString: @"true"] && [itemjson objectForKeyedSubscript:@"data"]==nil)
//                        self.cancommit = false;
//                }
//                else if([[itemjson valueForKey:@"control"] isEqualToString:@"switch"])
//                {
//                    NSString* valuestr = [itemjson valueForKey:@"value"];
//                    [upparams setValue:valuestr forKey:[itemjson valueForKey:@"name"]];
//                    
//                    NSString* value = [itemjson valueForKey:@"value"];
//                    NSMutableDictionary* boolitem=nil;
//                    NSMutableDictionary* subjson=nil;
//                    if([value isEqualToString:@"true"])
//                    {
//                        boolitem =[[itemjson objectForKey:@"true"] mutableCopy];
//                        
//                    }
//                    else
//                    {
//                        boolitem =[[itemjson objectForKey:@"false"] mutableCopy];
//                        
//                    }
//                    subjson=[[boolitem objectForKey:@"sub_item"] mutableCopy];
//                    if( subjson!=nil /*&& !active*/)
//                    {
//                        int sub_count = [[subjson valueForKey:@"count"] intValue];
//                        for(int l=0;l<sub_count;l++)
//                        {
//                            NSMutableDictionary * modify_item=[[subjson objectForKey:[NSString stringWithFormat:@"item_%d",l ]] mutableCopy];
//                            
//                            
//                            
//                            
//                            if([modify_item valueForKey:@"value"]!=nil && ![[modify_item valueForKey:@"value"]isEqualToString:@""])
//                                [upparams setValue:[modify_item valueForKey:@"value"] forKey:[modify_item valueForKey:@"name"]];
//                            else
//                            {
//                                if([[modify_item valueForKey:@"required"] isEqualToString: @"true"] )
//                                    self.cancommit = false;
//                            }
//                        }
//                        
//                        
//                        
//                    }
//                }
//                else
//                    if([[itemjson valueForKey:@"control"] isEqualToString:@"img"])
//                    {
//                        if([itemjson valueForKey:@"avalue"]!=nil && ![[itemjson valueForKey:@"avalue"]isEqualToString:@""])
//                            [upparams setValue:[itemjson valueForKey:@"avalue"] forKey:[itemjson valueForKey:@"name"]];
//                        else
//                        {
//                            if([[itemjson valueForKey:@"required"] isEqualToString: @"true"] )
//                                self.cancommit = false;
//                        }
//                    }
//                    else
//                    {
//                        if([itemjson valueForKey:@"value"]!=nil && ![[itemjson valueForKey:@"value"]isEqualToString:@""])
//                            [upparams setValue:[itemjson valueForKey:@"value"] forKey:[itemjson valueForKey:@"name"]];
//                        else
//                        {
//                            if([[itemjson valueForKey:@"required"] isEqualToString: @"true"] )
//                                self.cancommit = false;
//                        }
//                    }
//            upparams= [self subitem_param:itemjson addto:upparams ];
//        }
//    }
//    if(self.cancommit==false)
//    {
////        UIAlertView * alert = [[UIAlertView alloc] initWithTitle: @"Some requried field is missing" message:@"Fields with * mark can not be empty." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
//
////        [alert show];
//        
//        
//        [RAUtils alert_view:@"Fields with * mark cannot be empty." title:@"Some Requried Fields Are Missing."];
//        
//        self.content_data_control = [self translate_json:self.content_data_download changed:self.changed_data];
//        //DebugLog(@"CONTENT translate:%@",[iSalesNetwork DataTOjsonString:self.content_data]);
//        //[self.editorTable reloadData ];
//        [self.editorTable reloadData];
//        return;
//    }
    
    
    NSMutableDictionary* upparams = [self check_cancommit:true];
    if(self.cancommit==false)
        return;
    
    if(self.returnValue)
        self.returnValue(upparams);
    
    [self.navigationController popViewControllerAnimated:false];
    
    
    
}

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