//
//  AddressEditorViewController.m
//  RedAnt ERP Mobile
//
//  Created by Ray on 8/18/15.
//  Copyright (c) 2015 United Software Applications, Inc. All rights reserved.
//

#import "AddressEditorViewController.h"

#import "iSalesNetwork.h"

@interface AddressEditorViewController ()

@end

@implementation AddressEditorViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.editable = YES;
    
    UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"back"] imageWithRenderingMode:UIImageRenderingModeAutomatic]
                                                                   style:UIBarButtonItemStylePlain
                                                                  target:self
                                                                  action:@selector( onBackClick:)];
 //   [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Back", nil) style:UIBarButtonItemStylePlain target:self action:@selector(onBackClick:)];
    
    self.navigationItem.leftBarButtonItem = backButton;

  
    
//    UIBarButtonItem *closeButton =
    
    
    UIBarButtonItem *saveButton = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"save"] imageWithRenderingMode:UIImageRenderingModeAutomatic]
                                                                   style:UIBarButtonItemStylePlain
                                                                  target:self
                                                                  action:@selector(onSaveClick:)];
    self.navigationItem.rightBarButtonItem = saveButton;
    // 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)onSaveClick:(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
//                {
//                    //not support multi select for now;
//                }
//                /*
//                 upparams setValue:itemjson forKey:<#(NSString *)#>
//                 
//                 */
//            }
//            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 cannot be empty." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
////        //        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Error!" message:@"User&Password can not be empty!" delegate:nil cancelButtonTitle:NSLocalizedString(@"ok", 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;
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        NSDictionary* editor_json = [iSalesNetwork create_Address:upparams];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            
            
            if([[editor_json valueForKey:@"result"] intValue]==2)
            {
                
//                int result = [[editor_json valueForKey:@"result"] intValue];
                
//                if(result==2)
                {
                    [self prepareReturn: nil];
                    [self.navigationController popViewControllerAnimated:true];
                }
                
                
            }
            else
            {
                [RAUtils message_alert:[editor_json valueForKey:@"err_msg"] title:@"Create Address" controller:self] ;
            }
            

            
        });
    });
    
}

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