// // TearSeetParamViewController.m // RedAnt ERP Mobile // // Created by Ray on 11/11/15. // Copyright © 2015 United Software Applications, Inc. All rights reserved. // #import "TearSheetParamViewController.h" #import "RANetwork.h" #import "RAPDFViewController.h" #import "MainViewController.h" #import "RASingleton.h" #import "TextUtils.h" #import "JKTimerManager.h" @interface TearSheetParamViewController () @end @implementation TearSheetParamViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. UIBarButtonItem *closeButton = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"close"] imageWithRenderingMode:UIImageRenderingModeAutomatic] style:UIBarButtonItemStylePlain target:self action:@selector(onCloseClick:)]; //[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Close", nil) style:UIBarButtonItemStylePlain target:self action:@selector(onCloseClick:)]; // closeButton.title = @"Close"; // closeButton.tintColor = UIColorFromRGB(0x996633); self.navigationItem.leftBarButtonItem = closeButton; UIBarButtonItem *previewbtn = nil;//[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"", nil) style:UIBarButtonItemStylePlain target:self action:@selector(onSaveClick:)]; previewbtn = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"preview"] imageWithRenderingMode:UIImageRenderingModeAutomatic] style:UIBarButtonItemStylePlain target:self action:@selector(onPreviewClick:)]; // previewbtn.tintColor = UIColorFromRGB(0x996633); UIBarButtonItem *savebtn = nil;//[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"", nil) style:UIBarButtonItemStylePlain target:self action:@selector(onSaveClick:)]; savebtn = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"save"] imageWithRenderingMode:UIImageRenderingModeAutomatic] style:UIBarButtonItemStylePlain target:self action:@selector(onSaveClick:)]; // savebtn.tintColor = UIColorFromRGB(0x996633); _savebtn = savebtn; UIBarButtonItem *fixedItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil]; fixedItem.width = 0.0f; NSMutableArray * items = [[NSMutableArray alloc]init]; [items addObject:previewbtn]; [items addObject:fixedItem]; [items addObject:savebtn]; // [items addObject:item2]; self.navigationItem.rightBarButtonItems=items; // self.navigationItem.rightBarButtonItem = previewbtn; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (void)onCloseClick:(UIButton *)sender { [self.navigationController dismissViewControllerAnimated:true completion:^{ ; }]; } - (void)onPreviewClick:(UIButton *)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 // // */ // }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 "]]]; [pdf_url appendString:[valuefrom stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]]; // NSString* value=[self getValue:valuefrom]; // [ret setValue:value forKey:valuefrom]; } upparams[@"pdfUrl"]=pdf_url; upparams[@"product_ids"]=self.product_ids; upparams[@"item_ids"]=self.item_ids; // pdf_url=[pdf_url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; // __block UIAlertController * waitalert = [RAUtils waiting_alert:self title:@"Processing" completion:^{ PopWaitAlert * pop = [RAUtils waiting_pop:@"Processing" completion:nil]; [RANetwork request_portfoliopreview:upparams completionHandler:^(NSMutableDictionary *result) { NSDictionary* editor_json = result; // [waitalert dismissViewControllerAnimated:YES completion:nil]; // [waitalert dismissViewControllerAnimated:YES completion:^{ [pop hide]; if([[editor_json valueForKey:@"result"] intValue]==2) { // NSMutableDictionary * customerInfo=[self create_cusromer_info_from_table]; AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate]; NSString* url = [editor_json valueForKey:@"pdf_path"]; NSString* path = [editor_json valueForKey:@"pdf_real_path"]; bool isLocalFile = [[editor_json valueForKey:@"isLocalFile"] boolValue]; RAPDFViewController *ViewController = [[UIStoryboard storyboardWithName:@"wkweb" bundle:nil] instantiateViewControllerWithIdentifier:@"RAPDFViewController"]; ViewController.url = url; ViewController.canSave = true; ViewController.isLocalfile=isLocalFile; // ViewController.mail_content = [self.content_data valueForKey:@"email_content"]; ViewController.filename = [NSString stringWithFormat:@"%@.pdf",[TextUtils legalFilename:catelog_name]]; ViewController.save_name =catelog_name; ViewController.save_note =internal_note; //ViewController.filename = catelog_name; NSString* company= [appDelegate.customerInfo valueForKey:@"customer_name"]; if(company==nil) company=@""; // NSString* customer_email= [appDelegate.customerInfo valueForKey:@"customer_email"]; // NSMutableArray* send_to = [[NSMutableArray alloc]init]; // if(customer_email.length>0) // [send_to addObject:customer_email]; // ViewController.mail_to = send_to; // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate]; NSString* customer_email= [appDelegate.customerInfo valueForKey:@"customer_email"]; NSMutableArray* send_to = [[NSMutableArray alloc]init]; if(customer_email.length>0) { send_to=[[customer_email componentsSeparatedByString:NSLocalizedString(@";", nil)] mutableCopy]; // customer_email compo // [send_to addObject:customer_email]; } ViewController.mail_to = send_to; NSString* subject; // if (company.length==0) { // NSString* cur_time =[RAUtils current_date]; subject =catelog_name;//[NSString stringWithFormat:@"NPD Product List %@",cur_time]; // } // else // { // subject = [NSString stringWithFormat:@"Tear sheet for %@",company ]; // } // ViewController.attachment_name = [NSString stringWithFormat:@"%@.pdf",catelog_name];//[NSString stringWithFormat:@"NPD_Product_List %@.pdf",cur_time]; ViewController.mail_subject = subject; // ViewController.onLoadSuccess =^(NSString* url) // { // // }; ViewController.onSavePDF=^(NSString* name,NSString* file) { NSString* del_ids = nil; if(bremove) del_ids=[self.params valueForKey:@"item_ids"]; #ifdef OFFLINE_MODE if (appDelegate.offline_mode) { [upparams setValue:url forKey:@"pdfPath"]; } else #endif { [upparams setValue:path forKey:@"pdfPath"]; } [upparams setValue:name forKey:@"pdfName"]; [upparams setValue:internal_note forKey:@"pdfNote"]; [upparams setValue:del_ids forKey:@"itemIds"]; [upparams setValue:pdf_url forKey:@"configureParams"]; // __block UIAlertController * waitalert_save = [RAUtils waiting_alert:self title:@"Saving..." completion:^{ PopWaitAlert * pop = [RAUtils waiting_pop:@"Saving..." completion:nil]; [RANetwork request_save_portfolio:upparams completionHandler:^(NSMutableDictionary *result) { NSDictionary* json =result; // [waitalert_save dismissViewControllerAnimated:YES completion:^{ [pop hide]; if([[json valueForKey:@"result"] intValue]==2) { [RAUtils message_box:@"Portfolio" message:@"Portfolio saved. You can find it from View Portfolios" completion:nil]; // [RAUtils alert_view:nil title: ] ; [self.navigationController dismissViewControllerAnimated:true completion:^{ #ifdef RA_NOTIFICATION [ActiveViewController Notify:@"PortfolioViewController,PDFListViewController" Message:RA_NOTIFICATION_RELOAD_DATA]; #else [((MainViewController*)appDelegate.main_vc) reloadPortfolio:true immediately:true]; [((MainViewController*)appDelegate.main_vc) reloadDocuments:true immediately:false]; #endif // if(self.onDismiss) // self.onDismiss(catelog_name); }]; } else { [RAUtils message_box:@"Save Portfolio" message:[json valueForKey:@"err_msg"] completion:nil]; } // }]; }]; // }]; }; // ViewController. = self.function_name; // ViewController.behavior =BEHAVIOR_SEARCH; ViewController.hidenavi = false; [self.navigationController pushViewController:ViewController animated:YES]; // [[self navigationController] setNavigationBarHidden:NO animated:NO]; /* if(self.return_url) self.return_url(url,path,bremove); [self.navigationController dismissViewControllerAnimated:true completion:^{ if(self.onDismiss) self.onDismiss(catelog_name); }]; */ } else { [RAUtils message_box:@"Create Portfolio" message:[editor_json valueForKey:@"err_msg"] completion:nil]; } // }]; }]; } - (void)onSaveClick:(UIButton *)sender { // add params check here. [self.lastedit endEditing:true]; [self.lasttextview endEditing:true]; NSMutableDictionary* upparams = [self check_cancommit:true]; if(self.cancommit==false) return; NSMutableString* pdf_url = [[self.content_data_download valueForKey:@"pdf_url"] mutableCopy]; // DebugLog([RAUtils dict2string:self.content_data_download]); bool bremove=false; NSString* catelog_name= nil; NSString* internal_note = nil; NSArray *keys= [upparams allKeys]; //遍历keys for(int i=0;i<[keys count];i++) { //得到当前key NSString *key=[keys objectAtIndex:i]; if([key isEqualToString:@"remove_item"]) { bremove=[[upparams valueForKey:key]boolValue]; continue; } if([key isEqualToString:@"catalog_name"]) { catelog_name=[upparams valueForKey:key]; } if([key isEqualToString:@"internal_note"]) { internal_note=[upparams valueForKey:key]; } [pdf_url appendString:@"&"]; [pdf_url appendString:key]; //如果key不是pic,说明value是字符类型,比如name:Boris NSString* valuefrom = [upparams valueForKey:key]; valuefrom = [NSString stringWithFormat:@"%@",valuefrom]; [pdf_url appendString:@"="]; // [pdf_url appendString:[valuefrom stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; // [pdf_url appendString:[valuefrom stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]]; // NSString* value=[self getValue:valuefrom]; // [ret setValue:value forKey:valuefrom]; } upparams[@"product_ids"]=self.product_ids; upparams[@"item_ids"]=self.item_ids; __block NSString* saveName = catelog_name; __block NSString* saveNote = internal_note; NSString* del_ids = nil; if(bremove) del_ids=[self.params valueForKey:@"item_ids"]; // pdf_url=[pdf_url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"Save pdf" message:nil preferredStyle:UIAlertControllerStyleAlert]; //block代码块取代了delegate [alertControl addTextFieldWithConfigurationHandler:^(UITextField *textField) { textField.text = saveName; }]; UIAlertAction *actionOne = [UIAlertAction actionWithTitle:@"Save" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) { UITextField *name = alertControl.textFields.firstObject; if(name.text.length==0) { [RAUtils message_box:@"Name is missing" message:@"Cannot save without name. Save canceled." completion:nil]; } else { saveName = name.text; self.savebtn.enabled =false; [upparams setValue:pdf_url forKey:@"pdfUrl"]; [upparams setValue:saveName forKey:@"pdfName"]; [upparams setValue:del_ids forKey:@"itemIds"]; [upparams setValue:saveNote forKey:@"pdfNote"]; // __block UIAlertController * waitalert = [RAUtils waiting_alert:self title:@"Processing" completion:^{ PopWaitAlert * pop = [RAUtils waiting_pop:@"Processing" completion:nil]; [RANetwork request_save_portfoliodirectly:upparams completionHandler:^(NSMutableDictionary *result) { NSDictionary* editor_json =result; // [waitalert dismissViewControllerAnimated:YES completion:^{ [pop hide]; if([[editor_json valueForKey:@"result"] intValue]==2) { // [RAUtils alert_view:nil title:@"Portfolio saved. You can find it from View Portfolios" ] ; NSString* url = [editor_json valueForKey:@"pdf_path"]; bool isLocalFile = [editor_json valueForKey:@"isLocalFile"]; [self.navigationController dismissViewControllerAnimated:true completion:^{ #ifdef RA_NOTIFICATION [ActiveViewController Notify:@"PortfolioViewController,PDFListViewController" Message:RA_NOTIFICATION_RELOAD_DATA]; #else AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate]; [((MainViewController*)appDelegate.main_vc) reloadPortfolio:true immediately:true]; [((MainViewController*)appDelegate.main_vc) reloadDocuments:true immediately:false]; #endif if(self.onSaved) self.onSaved(url,saveName,isLocalFile); }]; } else { [RAUtils message_box:@"Create Portfolio" message:[editor_json valueForKey:@"err_msg"] completion:nil]; } // }]; }]; // }]; } }]; UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) { DebugLog(@"Cancel"); }]; [alertControl addAction:actionOne]; [alertControl addAction:alertthree]; //UIAlertControllerStyle类型为UIAlertControllerStyleAlert可以添加addTextFieldWithConfigurationHandler:^(UITextField *textField) [self presentViewController:alertControl animated:YES completion:nil]; } /* #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. } */ #pragma mark - Urgency - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [self start_urgency_timer]; } -(void) viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; [self cancel_urgency_timer]; } - (void)dealloc { AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate; [appDelegate.urgencyDic removeObjectForKey:[NSString stringWithFormat:@"level_%d",self.level]]; [appDelegate.urgencyDic setValue:[NSNumber numberWithInteger:self.level] forKey:@"count"]; } - (void)start_urgency_timer { if (!self.editable) { return; } __weak typeof(self) weakSelf = self; AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate; [appDelegate.urgencyDic setValue:RASingleton.sharedInstance.user forKey:@"user"]; #ifdef OFFLINE_MODE [appDelegate.urgencyDic setValue:[NSNumber numberWithBool:appDelegate.offline_mode] forKey:@"offline_mode"]; #endif [[JKTimerManager sharedTimerManager] scheduledDispatchTimerWithName:self.urgency_timer_name timeInterval:1 queue:nil repeats:YES action:^{ NSMutableDictionary *urgencyDic = [NSMutableDictionary dictionary]; // DebugLog(@"timer start...."); [urgencyDic setValue:weakSelf.content_data_download forKey:[NSString stringWithFormat:@"%@_restored_data",self.class_name]]; [urgencyDic setValue:weakSelf.class_name forKey:@"class_name"]; [appDelegate.urgencyDic setValue:urgencyDic forKey:[NSString stringWithFormat:@"level_%d",weakSelf.level]]; [appDelegate.urgencyDic setValue:[NSNumber numberWithInteger:weakSelf.level + 1] forKey:@"count"]; }]; } - (void)cancel_urgency_timer { [[JKTimerManager sharedTimerManager] cancelTimerWithName:self.urgency_timer_name]; } @end