WatchListViewController.m 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428
  1. //
  2. // WatchListViewController.m
  3. // RedAnt ERP Mobile
  4. //
  5. // Created by Ray on 14-8-1.
  6. // Copyright (c) 2014年 United Software Applications, Inc. All rights reserved.
  7. //
  8. #import "WatchListViewController.h"
  9. #import "RANetwork.h"
  10. #import "ModelItemCell.h"
  11. #import "DetailViewController.h"
  12. #import "LoginViewController.h"
  13. #import "MainViewController.h"
  14. #import "TearSheetParamViewController.h"
  15. #import "EditModelPriceViewController.h"
  16. #import "ContactListViewController.h"
  17. #import "DefaultTableHeaderView.h"
  18. #import "DefaultAppearance.h"
  19. #import "SortItemViewController.h"
  20. #import "SortButton.h"
  21. #import "UIColor+JK_HEX.h"
  22. #import "CartUtils.h"
  23. #import "RAPDFViewController.h"
  24. #import "JKMessageBoxController.h"
  25. #define ALERT_DEL 1025
  26. //#import "PDFViewController.h"
  27. @interface WatchListViewController ()
  28. @property (strong, nonatomic) IBOutlet UIBarButtonItem *emailBtn;
  29. @property (strong, nonatomic) IBOutlet UIBarButtonItem *printBtn;
  30. @property (nonatomic,strong) SortItemViewController *sortItemController;
  31. @property (nonatomic,assign) int sortIndex;
  32. @property (nonatomic,strong) SortButton *sortButton;
  33. @property (nonatomic,strong) UILabel *qtyLabel;
  34. @property (nonatomic,strong) NSOperationQueue *dataOperationQueue;
  35. @property (nonatomic,copy) NSString *print_url;
  36. @end
  37. @implementation WatchListViewController
  38. - (NSOperationQueue *)dataOperationQueue {
  39. if (!_dataOperationQueue) {
  40. _dataOperationQueue = [[NSOperationQueue alloc] init];
  41. _dataOperationQueue.maxConcurrentOperationCount = 1;
  42. }
  43. return _dataOperationQueue;
  44. }
  45. -(void) logout
  46. {
  47. self.content_data = nil;
  48. }
  49. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  50. {
  51. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  52. if (self) {
  53. // Custom initialization
  54. }
  55. return self;
  56. }
  57. -(void) reload_container_getdata:(bool) update_data
  58. {
  59. [super reload_container_getdata:update_data];
  60. if(update_data)
  61. [self reload_data];
  62. else
  63. [self.itemListTable reloadData];
  64. }
  65. - (IBAction)onAddtoCart:(id)sender {
  66. [CartUtils add_to_cart:self selectorholder:self selector:@selector(addtocart)];
  67. return;
  68. UIApplication * app = [UIApplication sharedApplication];
  69. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  70. MainViewController* main_vc=(MainViewController*)appDelegate.main_vc;
  71. if(appDelegate.bLogin==false)
  72. {
  73. LoginViewController * loginvc =[ self.storyboard instantiateViewControllerWithIdentifier:@"LoginViewController"];
  74. // loginvc.delegate = self;
  75. // loginvc.modalPresentationStyle = UIModalPresentationFormSheet;//有三种状态,自己看看是哪种
  76. loginvc.returnValue = ^(bool blogin){
  77. // UIApplication * app = [UIApplication sharedApplication];
  78. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  79. if(blogin)
  80. {
  81. if(/*appDelegate.user_type==USER_ROLE_EMPLOYEE*/ true)
  82. {
  83. [main_vc checklogin:false];
  84. if(appDelegate.can_create_order)
  85. { NSString* msg =@"";
  86. if(appDelegate.contact_id.length>0)
  87. {
  88. msg = [msg stringByAppendingString:@"\n\nCustomer:"];
  89. msg = [msg stringByAppendingString:appDelegate.customerInfo[@"customer_name"]];
  90. }
  91. UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Add to cart", nil) message:msg delegate:self cancelButtonTitle:NSLocalizedString(@"Cancel", nil) otherButtonTitles:NSLocalizedString(@"Add to pending order", nil),NSLocalizedString(@"Add to new order", nil), nil];
  92. // alert.
  93. [alert show];
  94. }
  95. else
  96. {
  97. UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Add to cart", nil) message:NSLocalizedString(@"", nil) delegate:self cancelButtonTitle:NSLocalizedString(@"Cancel", nil) otherButtonTitles:NSLocalizedString(@"Add to pending order", nil), nil];
  98. // alert.
  99. [alert show];
  100. }
  101. }
  102. else
  103. {
  104. [main_vc checklogin:true];
  105. }
  106. }
  107. // self.offset = 0;
  108. // [self.content_data removeAllObjects];
  109. // [self loadpage];
  110. //
  111. // [[self navigationController] setNavigationBarHidden:YES animated:NO];
  112. };
  113. UINavigationController* navi = [[UINavigationController alloc] initWithRootViewController:loginvc] ;
  114. // [self hackModalSheetSize:CGSizeMake(450, 200) ofVC:navi];
  115. navi.modalPresentationStyle = UIModalTransitionStyleCrossDissolve;//有三种状态,自己看看是哪种
  116. [self presentViewController:navi animated:YES completion:^{
  117. // navi.view.superview.bounds = CGRectMake(0, 0, 480, 320);
  118. DebugLog(@"LoginViewController present.........");
  119. // self.btop = false;
  120. // <#code#>
  121. }];
  122. }
  123. else
  124. {
  125. if(/*appDelegate.user_type==USER_ROLE_EMPLOYEE&&/*appDelegate.contact_id==nil&&*/appDelegate.order_code==nil)
  126. {
  127. [main_vc checklogin:false];
  128. if(appDelegate.can_create_order)
  129. {
  130. NSString* msg =@"";
  131. if(appDelegate.contact_id.length>0)
  132. {
  133. msg = [msg stringByAppendingString:@"\n\nCustomer:"];
  134. msg = [msg stringByAppendingString:appDelegate.customerInfo[@"customer_name"]];
  135. }
  136. UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Add to cart", nil) message:msg delegate:self cancelButtonTitle:NSLocalizedString(@"Cancel", nil) otherButtonTitles:NSLocalizedString(@"Add to pending order", nil),NSLocalizedString(@"Add to new order", nil), nil];
  137. // alert.
  138. [alert show];
  139. }
  140. else
  141. {
  142. UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Add to cart", nil) message:NSLocalizedString(@"", nil) delegate:self cancelButtonTitle:NSLocalizedString(@"Cancel", nil) otherButtonTitles:NSLocalizedString(@"Add to pending order", nil), nil];
  143. // alert.
  144. [alert show];
  145. }
  146. }
  147. else
  148. {
  149. if(appDelegate.order_code==nil)
  150. [ self neworder];
  151. else
  152. [self addtocart];
  153. }
  154. }
  155. }
  156. -(void) addtocart
  157. {
  158. NSMutableArray* checked = [[NSMutableArray alloc] init];
  159. NSMutableArray* checked_iid = [[NSMutableArray alloc] init];
  160. int count = [[self.content_data valueForKey:@"count"] intValue];
  161. for(int i=0;i<count;i++)
  162. {
  163. NSMutableDictionary * item_json = [[self.content_data objectForKey:[NSString stringWithFormat:@"item_%d",i]] mutableCopy];
  164. bool check = [[item_json valueForKey:@"check"] boolValue];
  165. if(check)
  166. {
  167. /*
  168. if ([[item_json valueForKey:@"available"] isEqualToString:@"In Production"] || [[item_json valueForKey:@"available"] integerValue] == 0) {
  169. [RAUtils message_alert:@"There ars some products is not available" title:@"Warning" controller:self];
  170. return;
  171. }
  172. */
  173. [checked addObject:[NSNumber numberWithInt:[[item_json valueForKey:@"product_id"] intValue]]];
  174. [checked_iid addObject:[NSNumber numberWithInt:[[item_json valueForKey:@"item_id"] intValue]]];
  175. }
  176. }
  177. if(checked.count==0)
  178. {
  179. [RAUtils message_alert:@"You must select at least one model." title:@"Cannot Add to cart." controller:self] ;
  180. return;
  181. }
  182. NSString * ids = [checked_iid componentsJoinedByString:@","];
  183. self.btnAddtoCart.enabled = false;
  184. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  185. NSDictionary* return_json = [RANetwork move_wish2cart:ids];
  186. dispatch_async(dispatch_get_main_queue(), ^{
  187. self.btnAddtoCart.enabled = true;
  188. if([[return_json valueForKey:@"result"] intValue]==2)
  189. {
  190. // int result=[[return_json valueForKey:@"result"] intValue];re
  191. // if(result==2)
  192. // {
  193. //successed.
  194. // NSString* order_code = [return_json valueForKey:@"order_code"];
  195. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  196. // appDelegate.order_code = order_code;
  197. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  198. // appDelegate.order_code = order_code;
  199. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  200. [((MainViewController*)appDelegate.main_vc) reloadWish:true immediately:true];
  201. [RAUtils message_alert:nil title:@" Multiple items moved to Cart" controller:self] ;
  202. // [self end_select];
  203. // }
  204. }
  205. else
  206. {
  207. [self reload_data];
  208. [RAUtils message_alert:[return_json valueForKey:@"err_msg"] title:@"Move To Cart" controller:self] ;
  209. }
  210. });
  211. });
  212. }
  213. -(void) neworder
  214. {
  215. UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait" title:@"Create Order"];
  216. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  217. NSDictionary* return_json = [RANetwork new_Order];
  218. dispatch_async(dispatch_get_main_queue(), ^{
  219. [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  220. if([[return_json valueForKey:@"result"] intValue]==2)
  221. {
  222. int result=[[return_json valueForKey:@"result"] intValue];
  223. if(result==2)
  224. {
  225. //successed.
  226. NSString* order_code = [return_json valueForKey:@"orderCode"];
  227. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  228. appDelegate.order_code = order_code;
  229. appDelegate.order_status = [[return_json valueForKey:@"orderStatus"] intValue];
  230. [self addtocart];
  231. // [self ReloadData];
  232. // if(self.shopCartBlock!=nil)
  233. // {
  234. // UIImage* img=[self photoStackView:self.photoStack photoForIndex:0];
  235. //
  236. //
  237. // CGRect iv_rect = CGRectMake(self.btnaddCart.center.x-50, self.btnaddCart.center.y-50, 100, 100);
  238. // UIImageView* iv = [[UIImageView alloc] initWithFrame:iv_rect];
  239. // iv.image = img;
  240. // // [self.contentView addSubview:iv];
  241. // self.shopCartBlock(iv);
  242. //
  243. // }
  244. }
  245. }
  246. else
  247. {
  248. [RAUtils message_alert:[return_json valueForKey:@"err_msg"] title:@"Add To Cart" controller:self] ;
  249. }
  250. });
  251. });
  252. }
  253. //- (IBAction)onCreateTearSheet:(id)sender {
  254. //
  255. //
  256. // self.pdf_url= nil;
  257. // NSMutableArray* checked = [[NSMutableArray alloc] init];
  258. //
  259. // NSMutableArray* checked_iid = [[NSMutableArray alloc] init];
  260. //
  261. // int count = [[self.content_data valueForKey:@"count"] intValue];
  262. // for(int i=0;i<count;i++)
  263. // {
  264. // NSMutableDictionary * item_json = [[self.content_data objectForKey:[NSString stringWithFormat:@"item_%d",i]] mutableCopy];
  265. // bool check = [[item_json valueForKey:@"check"] boolValue];
  266. // if(check)
  267. // {
  268. // [checked addObject:[NSNumber numberWithInt:[[item_json valueForKey:@"product_id"] intValue]]];
  269. // [checked_iid addObject:[NSNumber numberWithInt:[[item_json valueForKey:@"item_id"] intValue]]];
  270. // }
  271. // }
  272. // // disable for debug
  273. // if(checked.count==0)
  274. // {
  275. // [RAUtils message_alert:@"You must select at least one model." title:@"Cannot Create Portfolio." controller:self] ;
  276. // return;
  277. // }
  278. //
  279. //
  280. //
  281. // NSMutableDictionary * params= [[NSMutableDictionary alloc] init];
  282. //
  283. // NSString * string = [checked componentsJoinedByString:@","];
  284. //
  285. // NSString * string_iid = [checked_iid componentsJoinedByString:@","];
  286. // [params setValue:string forKey:@"product_ids"];
  287. // [params setValue:string_iid forKey:@"item_ids"];
  288. // TearSheetParamViewController * VC =[ self.storyboard instantiateViewControllerWithIdentifier:@"TearSheetParamViewController"];
  289. //
  290. //
  291. // VC.url_type = URL_REMOTE;
  292. // VC.request_url=URL_DM_PARAMS;
  293. //
  294. // VC.params = params;
  295. //
  296. //// VC.delegate=self;
  297. //
  298. // VC.return_url=^(NSString* url,NSString* path, bool remove_item)
  299. // {
  300. // self.pdf_url = url;
  301. // self.path = path;
  302. // self.remove_item = remove_item;
  303. // };
  304. ////AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  305. // VC.onDismiss=^(NSString* catelog_name)
  306. // {
  307. //
  308. // };
  309. //
  310. // //
  311. // // if(checked.count==count)
  312. // // {
  313. // // orderinfoVC.have_tail = true
  314. // // }
  315. //
  316. // UINavigationController* navi = [[UINavigationController alloc] initWithRootViewController:VC] ;
  317. //
  318. //
  319. //
  320. //
  321. //
  322. // navi.modalPresentationStyle = UIModalPresentationPageSheet;//有三种状态,自己看看是哪种
  323. // [self presentViewController:navi animated:YES completion:^{
  324. //
  325. // DebugLog(@"TearSheetParamViewController present.........");
  326. //
  327. // // self.btop = false;
  328. // // <#code#>
  329. // }];
  330. //
  331. // [self.btnselect setTitle:@"Select all"];
  332. //
  333. //
  334. //}
  335. - (void)viewDidLoad
  336. {
  337. [super viewDidLoad];
  338. DebugLog(@"%@",NSStringFromCGRect(self.itemListTable.frame));
  339. UIRefreshControl *ref = [[UIRefreshControl alloc]init];
  340. ref.tag = 200 ;
  341. ref.attributedTitle = [[NSAttributedString alloc]initWithString:@"Pull to refresh"];
  342. ref.tintColor = [UIColor colorWithRed:0.10 green:0.68 blue:0.94 alpha:0.7];
  343. // ref.hidden = true;
  344. [ref addTarget:self action:@selector(manually_refresh) forControlEvents:UIControlEventValueChanged];
  345. [self.itemListTable addSubview:ref];
  346. DebugLog(@"%@",NSStringFromCGRect(self.itemListTable.frame));
  347. // [[UIToolbar appearance] setBackgroundColor: [UIColor whiteColor]];
  348. self.label_net_err.layer.borderColor = [UIColor darkGrayColor].CGColor;
  349. self.label_net_err.layer.borderWidth = 2.0;
  350. self.label_net_err.layer.cornerRadius=15;
  351. self.label_net_err.layer.masksToBounds=true;
  352. CGPoint center = self.mum.center;
  353. self.mum.frame = CGRectMake(0, 0, 100, 100);
  354. self.mum.center = center;
  355. self.mum.backgroundColor = [UIColor colorWithHEX:0xA5A5A5 alpha:0.5];
  356. self.mum.layer.cornerRadius = 5.0f;
  357. self.mum.layer.masksToBounds = YES;
  358. self.sortIndex = 0;
  359. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(refresh_btn_click:)];
  360. // tap.minimumPressDuration = 0.8; //定义按的时间
  361. [self.label_net_err addGestureRecognizer:tap];
  362. // [self reload_data];
  363. }
  364. -(void)manually_refresh
  365. {
  366. UIRefreshControl *reF = (UIRefreshControl *)[self.view viewWithTag:200];
  367. if(self.isrefreshing)
  368. {
  369. [reF endRefreshing];
  370. return;
  371. }
  372. reF.attributedTitle = [[NSAttributedString alloc]initWithString:@"Refreshing"];
  373. [self performSelector:@selector(reload_data) withObject:nil afterDelay:1];
  374. // DebugLog(@"refresh!!!!!!!!");
  375. }
  376. - (IBAction)onSelectClick:(id)sender {
  377. // self.total=0;
  378. if([self.btnselect.title isEqualToString:@"Select all"])
  379. {
  380. int count =[[self.content_data valueForKey:@"count"] intValue];
  381. for(int i=0;i<count;i++)
  382. {
  383. NSMutableDictionary * item_json = [[self.content_data objectForKey:[NSString stringWithFormat:@"item_%d",i]] mutableCopy];
  384. [item_json setValue:@"1" forKey:@"check"];
  385. ModelItemCell* cell=(ModelItemCell*)[self.itemListTable cellForRowAtIndexPath:[NSIndexPath indexPathForItem:i inSection:0]];
  386. cell.img_checkmark.hidden=false;
  387. // double totalprice =[[item_json valueForKey:@"subtotal_price"] doubleValue];
  388. [self.content_data setObject:item_json forKey:[NSString stringWithFormat:@"item_%d",i]];
  389. }
  390. [self.btnselect setTitle:@"Deselect all"];
  391. }
  392. else
  393. {
  394. int count =[[self.content_data valueForKey:@"count"] intValue];
  395. for(int i=0;i<count;i++)
  396. {
  397. NSMutableDictionary * item_json = [[self.content_data objectForKey:[NSString stringWithFormat:@"item_%ld",(long)i]] mutableCopy];
  398. [item_json setValue:@"0" forKey:@"check"];
  399. ModelItemCell* cell=(ModelItemCell*)[self.itemListTable cellForRowAtIndexPath:[NSIndexPath indexPathForItem:i inSection:0]];
  400. cell.img_checkmark.hidden=true;
  401. [self.content_data setObject:item_json forKey:[NSString stringWithFormat:@"item_%d",i]];
  402. }
  403. [self.btnselect setTitle:@"Select all"];
  404. // self.btnselect.titleLabel.text=@"Select all";
  405. }
  406. }
  407. - (IBAction)onDeleteClick:(id)sender {
  408. NSMutableArray* checked = [[NSMutableArray alloc] init];
  409. int count = [[self.content_data valueForKey:@"count"] intValue];
  410. for(int i=0;i<count;i++)
  411. {
  412. NSMutableDictionary * item_json = [[self.content_data objectForKey:[NSString stringWithFormat:@"item_%d",i]] mutableCopy];
  413. int check = [[item_json valueForKey:@"check"] intValue];
  414. if(check==1)
  415. {
  416. [checked addObject:[NSNumber numberWithInt:[[item_json valueForKey:@"item_id"] intValue]]];
  417. }
  418. }
  419. // disable for debug
  420. if(checked.count==0)
  421. return;
  422. // NSMutableDictionary * params= [[NSMutableDictionary alloc] init];
  423. NSString * string = [checked componentsJoinedByString:@","];
  424. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  425. NSDictionary* cart_json = [RANetwork watchlist_remove:string];
  426. dispatch_async(dispatch_get_main_queue(), ^{
  427. if([[cart_json valueForKey:@"result"] intValue]==2)
  428. {
  429. [self reload_data];
  430. [self.btnselect setTitle:@"Select all"];
  431. }
  432. else
  433. {
  434. [RAUtils message_alert:[cart_json valueForKey:@"err_msg"] title:@"Remove Model From Wish List" controller:self] ;
  435. }
  436. });
  437. });
  438. }
  439. -(void) viewWillAppear:(BOOL)animated
  440. {
  441. [super viewWillAppear:animated];
  442. [[self navigationController] setNavigationBarHidden:YES animated:NO];
  443. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  444. #ifdef OFFLINE_MODE
  445. // 离线隐藏Email
  446. self.emailBtn.enabled = !appDelegate.offline_mode;
  447. // 离线隐藏Print
  448. self.printBtn.enabled = !appDelegate.offline_mode;
  449. #endif
  450. }
  451. -(void) operation_reload_data
  452. {
  453. if (self.dataOperationQueue.operationCount > 1) {
  454. return;
  455. }
  456. dispatch_async(dispatch_get_main_queue(), ^{
  457. if(self.isrefreshing)
  458. return;
  459. // [self.content_data removeAllObjects];
  460. // [self.itemListTable reloadData];
  461. self.isrefreshing=true;
  462. self.label_net_err.hidden=true;
  463. self.itemListTable.hidden=true;
  464. self.mum.center = self.view.center;
  465. self.mum.hidden = false;
  466. [self.mum startAnimating];
  467. UIRefreshControl *reF = (UIRefreshControl *)[self.view viewWithTag:200];
  468. [reF endRefreshing];
  469. reF.attributedTitle = [[NSAttributedString alloc]initWithString:@"Pull to refresh"];
  470. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  471. NSDictionary* cart_json = [RANetwork request_WatchList:self.sortIndex];
  472. dispatch_async(dispatch_get_main_queue(), ^{
  473. [self.mum stopAnimating];
  474. self.isrefreshing=false;
  475. if (self.dataOperationQueue.operationCount > 1) {
  476. return ;
  477. }
  478. int result=[[cart_json valueForKey:@"result"] intValue];
  479. self.content_data = [cart_json mutableCopy];
  480. [self.itemListTable reloadData];
  481. self.print_url = [cart_json objectForKey:@"wishlist_print_url"];
  482. if(result==2||result==1||result==0)
  483. {
  484. self.itemListTable.hidden=false;
  485. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  486. appDelegate.wish_count =[[self.content_data valueForKey:@"count"] intValue];
  487. }
  488. else
  489. if(result==RESULT_NET_ERROR)
  490. {
  491. self.label_net_err.hidden=false;
  492. self.itemListTable.hidden=true;
  493. }
  494. else
  495. {
  496. [RAUtils message_alert:[cart_json valueForKey:@"err_msg"] title:@"Loading Wish list" controller:self] ;
  497. }
  498. self.isrefreshing=false;
  499. });
  500. });
  501. });
  502. }
  503. -(void) reload_data {
  504. __weak typeof(self) weakself = self;
  505. NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{
  506. if (weakself) {
  507. __strong typeof(weakself) strongself = weakself;
  508. [strongself operation_reload_data];
  509. }
  510. }];
  511. [self.dataOperationQueue addOperation:operation];
  512. }
  513. - (void)didReceiveMemoryWarning
  514. {
  515. [super didReceiveMemoryWarning];
  516. // Dispose of any resources that can be recreated.
  517. }
  518. //-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
  519. //{
  520. //
  521. // [super didRotateFromInterfaceOrientation:fromInterfaceOrientation];
  522. //
  523. //
  524. //
  525. //// [self.itemListTable reloadData];
  526. // DebugLog(@"%@",NSStringFromCGRect(self.itemListTable.frame));
  527. //
  528. //
  529. //}
  530. #pragma mark - Table view data source
  531. //- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
  532. //{
  533. // return 0;
  534. //}
  535. //- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
  536. //{
  537. // return 0;
  538. //}
  539. //- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
  540. //{
  541. // UIView* myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 0.0001)];
  542. // return myView;
  543. //
  544. //}
  545. //- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
  546. //{
  547. // UIView* myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 0.0001)];
  548. //// myView.backgroundColor = [UIColor colorWithRed:0.10 green:0.68 blue:0.94 alpha:0.7];
  549. //// UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 90, 22)];
  550. //// titleLabel.textColor=[UIColor whiteColor];
  551. //// titleLabel.backgroundColor = [UIColor clearColor];
  552. //// if(section==0)
  553. //// titleLabel.text=NSLocalizedString(@"display_items", nil);
  554. //// else
  555. //// titleLabel.text=NSLocalizedString(@"hide_items", nil);
  556. //// [titleLabel sizeToFit];
  557. //// [myView addSubview:titleLabel];
  558. ////
  559. // return myView;
  560. //}
  561. //
  562. //- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
  563. // if(section==0)
  564. // return nil;
  565. // else
  566. // return @"detail section";
  567. //}
  568. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
  569. {
  570. return 33;
  571. }
  572. -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
  573. {
  574. NSString* value =[DefaultAppearance get_noneappearance_value:@"DefaultTableHeaderView" valuename:@"title_text_color"];
  575. if(value==nil)
  576. value=@"";
  577. unsigned long color = strtoul([value UTF8String],0,16);
  578. NSString *sectionTitle = @"Wish List";
  579. DefaultTableHeaderView* myView = [[DefaultTableHeaderView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 33)];
  580. // UIButton *sortButton = [UIButton buttonWithType:UIButtonTypeCustom];
  581. // sortButton.frame = CGRectMake(20, 5.5, (33 - 5.5 * 2), (33 - 5.5 * 2));
  582. // [sortButton setImage:[UIImage imageNamed:@"check_1_24"] forState:UIControlStateNormal];
  583. // [sortButton addTarget:self action:@selector(wishlistSortButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
  584. // [myView addSubview:sortButton];
  585. [myView addSubview:self.sortButton];
  586. UILabel *titlelabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 5.5, tableView.bounds.size.width, 22)];
  587. titlelabel.textColor=UIColorFromRGB(color);
  588. titlelabel.textAlignment=NSTextAlignmentCenter;
  589. titlelabel.autoresizingMask=UIViewAutoresizingFlexibleWidth;
  590. titlelabel.text=sectionTitle;
  591. titlelabel.center = myView.center;
  592. myView.autoresizesSubviews=true;
  593. [myView addSubview:titlelabel];
  594. [myView addSubview:self.qtyLabel];
  595. return myView;
  596. }
  597. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  598. {
  599. return 1;
  600. }
  601. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  602. {
  603. // NSDictionary * item_json = [self.content_data objectForKey:@"items"];
  604. int count =[[self.content_data valueForKey:@"count"] intValue];
  605. return count;
  606. }
  607. - (IBAction)imgbtnClicked:(UIButton *)sender {
  608. UITableViewCell* cell =(UITableViewCell*) sender.superview.superview;
  609. NSIndexPath * indexPath = [self.itemListTable indexPathForCell:cell];
  610. // DebugLog(@"shouldchangeedit %d_%d",indexPath.section,indexPath.row);
  611. NSDictionary * item_json = [self.content_data objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]];
  612. NSString* product_id = [item_json valueForKey:@"product_id"];
  613. NSString *name = [item_json objectForKey:@"description"];
  614. NSRange subRange = [name rangeOfString:@"\n"];
  615. name = [name substringToIndex:subRange.location];
  616. DetailViewController* dvc = [self.storyboard instantiateViewControllerWithIdentifier:@"DetailViewController" ];
  617. // dvc
  618. dvc.product_id=product_id;
  619. dvc.category_id=nil;
  620. dvc.ispush=true;
  621. dvc.model_name = name;
  622. [dvc reload];
  623. [self.navigationController pushViewController:dvc animated:true];
  624. }
  625. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  626. {
  627. // if(tableView==self.itemListTable)
  628. // {
  629. NSString *CellIdentifier = @"WatchListItemCell";
  630. ModelItemCell * cell= [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
  631. NSDictionary * item_json = [self.content_data objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]];
  632. // Button 设置了大小约束,Size就不会随内容变化
  633. [cell.btnImage setContentMode:UIViewContentModeScaleAspectFit];
  634. [cell.btnImage setContentEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
  635. [cell.btnImage setContentHorizontalAlignment:UIControlContentHorizontalAlignmentFill];
  636. [cell.btnImage setContentVerticalAlignment:UIControlContentVerticalAlignmentFill];
  637. cell.btnImage.imageView.contentMode = UIViewContentModeScaleAspectFit;
  638. NSString* img_url = [item_json valueForKey:@"img"];
  639. NSString* description = [item_json valueForKey:@"description"];
  640. // NSString* identifier = [item_json valueForKey:@"identifier"];
  641. // NSString* attribute = [item_json valueForKey:@"attribute"];
  642. // NSString* currency = [item_json valueForKey:@"currency"];
  643. // int count =[[item_json valueForKey:@"count"] intValue];
  644. NSString* price =[item_json valueForKey:@"tear_sheet_price"];
  645. // cell.labelAttribute.text = attribute;
  646. // cell.labelCurrency.text = currency;
  647. cell.labelDescription.text = description;
  648. // cell.labelIdentifier.text = identifier;
  649. cell.labelPrice.text = price;
  650. // cell.editCount.text =[NSString stringWithFormat:@"%d",count];
  651. // NSDictionary* val_json =[cadedate_json objectForKey:[NSString stringWithFormat:@"val_%d",i]];
  652. int check = [[item_json valueForKey:@"check"] intValue];
  653. if(check==1)
  654. cell.img_checkmark.hidden=false;
  655. else
  656. cell.img_checkmark.hidden=true;
  657. // UIImageView* iv =cell.btnImage.imageView;
  658. // [cell.btnImage setBackgroundImage:[UIImage imageNamed:@"loading_s"] forState:UIControlStateNormal];
  659. [cell.btnImage setImage:[UIImage imageNamed:@"loading_s"] forState:UIControlStateNormal];
  660. NSString* file_name=[img_url lastPathComponent];
  661. NSData* img_data=[iSalesDB load_cached_img:file_name loadFrom:img_url];
  662. if(img_data!=nil)
  663. {
  664. UIImage * img =[UIImage imageWithData:img_data];
  665. // [cell.btnImage setBackgroundImage:img forState:UIControlStateNormal];
  666. [cell.btnImage setImage:img forState:UIControlStateNormal];
  667. }
  668. else
  669. {
  670. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  671. NSData* downloadimg_data = [NSData dataWithContentsOfURL:[NSURL URLWithString:img_url]];
  672. dispatch_async(dispatch_get_main_queue(), ^{
  673. if(downloadimg_data!=nil)
  674. {
  675. [iSalesDB cache_img:downloadimg_data filename:file_name saveTo:img_url];
  676. UIImage * img =[UIImage imageWithData:downloadimg_data];
  677. // [cell.btnImage setBackgroundImage:img forState:UIControlStateNormal];
  678. [cell.btnImage setImage:img forState:UIControlStateNormal];
  679. }
  680. else
  681. // [cell.btnImage setBackgroundImage:[UIImage imageNamed:@"notfound_s"] forState:UIControlStateNormal];
  682. [cell.btnImage setImage:[UIImage imageNamed:@"notfound_s"] forState:UIControlStateNormal];
  683. });
  684. });
  685. }
  686. cell.backgroundColor = [UIColor whiteColor];// [UIColor whiteColor];;
  687. int qty = [item_json[@"cart_count"] integerValue];
  688. cell.qty_Label.text = [NSString stringWithFormat:@"QTY: %d",qty];
  689. return cell;
  690. }
  691. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  692. {
  693. ModelItemCell* cell = (ModelItemCell*)[tableView cellForRowAtIndexPath:indexPath];
  694. cell.selected = false;
  695. // self.dirty = true;
  696. NSMutableDictionary * item_json = [[self.content_data objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]] mutableCopy];
  697. int check = [[item_json valueForKey:@"check"] intValue];
  698. if(check==1)
  699. {
  700. [item_json setValue:@"0" forKey:@"check"];
  701. cell.img_checkmark.hidden=true;
  702. [self.btnselect setTitle: @"Select all"];
  703. }
  704. else
  705. {
  706. [item_json setValue:@"1" forKey:@"check"];
  707. cell.img_checkmark.hidden=false;
  708. }
  709. [self.content_data setObject:item_json forKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]];
  710. }
  711. - (UITableViewCellEditingStyle)tableView:(UITableView *)tv editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
  712. return UITableViewCellEditingStyleDelete;
  713. }
  714. //#pragma mark - UIAlertViewDelegate
  715. //// Called when a button is clicked. The view will be automatically dismissed after this call returns
  716. //- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  717. //{
  718. //
  719. //
  720. //
  721. //}
  722. - (BOOL)isPureFloat:(NSString*)string{
  723. NSScanner* scan = [NSScanner scannerWithString:string];
  724. float val;
  725. return[scan scanFloat:&val] && [scan isAtEnd];
  726. }
  727. #pragma mark 在滑动手势删除某一行的时候,显示出更多的按钮
  728. - (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
  729. {
  730. // 添加一个删除按钮
  731. self.indexPath=indexPath;
  732. UITableViewRowAction *deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"Delete" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
  733. DebugLog(@"delete click");
  734. UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Delete confirm", nil) message:NSLocalizedString(@"Are you sure remove model from wishlist?", nil) delegate:self cancelButtonTitle:NSLocalizedString(@"NO", nil) otherButtonTitles:NSLocalizedString(@"YES", nil), nil];
  735. alert.tag = ALERT_DEL;
  736. // alert.
  737. [alert show];
  738. }];
  739. deleteRowAction.backgroundColor = UIColorFromRGB(0x336699);
  740. // 添加一个编辑按钮
  741. // UITableViewRowAction *editRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@" Edit "handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
  742. //
  743. // DebugLog(@"edit click");
  744. //
  745. //
  746. //
  747. // UIViewController* parentvc=self;
  748. //
  749. //
  750. //
  751. //
  752. // EditModelPriceViewController * vc =[ parentvc.storyboard instantiateViewControllerWithIdentifier:@"EditModelPriceViewController"];
  753. //
  754. //
  755. // NSMutableDictionary * item_json = [[self.content_data objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]] mutableCopy];
  756. //
  757. //
  758. //
  759. // vc.set_watch_list=true;
  760. // vc.discount=[[item_json valueForKey:@"discount"] floatValue];
  761. // vc.price = [[item_json valueForKey:@"tear_sheet_price"] floatValue];
  762. // vc.cart_id = [NSString stringWithFormat:@"%d",[[item_json valueForKey:@"item_id"] intValue]];
  763. // // vc.delegate = parentvc.self;
  764. // // loginvc.modalPresentationStyle = UIModalPresentationFormSheet;//有三种状态,自己看看是哪种
  765. // vc.onSetValue = ^(){
  766. // [self reload_data];//[main_vc checklogin:true];
  767. // };
  768. //
  769. //
  770. // UINavigationController* navi = [[UINavigationController alloc] initWithRootViewController:vc] ;
  771. //
  772. //
  773. //
  774. //
  775. //
  776. // // [self hackModalSheetSize:CGSizeMake(450, 200) ofVC:navi];
  777. //
  778. // navi.modalPresentationStyle = UIModalTransitionStyleCrossDissolve;//有三种状态,自己看看是哪种
  779. // [parentvc presentViewController:navi animated:YES completion:^{
  780. //
  781. // // navi.view.superview.bounds = CGRectMake(0, 0, 480, 320);
  782. //
  783. // DebugLog(@"EditModelPriceViewController present.........");
  784. //
  785. // // self.btop = false;
  786. // // <#code#>
  787. // }];
  788. //
  789. // [tableView reloadRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationMiddle];
  790. //
  791. //
  792. // }];
  793. // editRowAction.backgroundColor = UIColorFromRGB(0xff9933);
  794. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  795. if(appDelegate.user_type==USER_ROLE_EMPLOYEE)
  796. return @[deleteRowAction];
  797. else
  798. return @[deleteRowAction];
  799. }
  800. - (void)tableView:(UITableView *)tableView
  801. commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
  802. }
  803. #pragma mark - UIAlertViewDelegate
  804. // Called when a button is clicked. The view will be automatically dismissed after this call returns
  805. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  806. {
  807. UIApplication * app = [UIApplication sharedApplication];
  808. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  809. // if(alertView.tag==1234)
  810. // {
  811. //
  812. // // availability warring;
  813. //
  814. //
  815. // if(buttonIndex!=alertView.cancelButtonIndex)
  816. // {
  817. // if(appDelegate.order_code==nil)
  818. // {
  819. // [self neworder];
  820. // }
  821. // else
  822. // {
  823. // [self addtocart];
  824. // }
  825. // }
  826. // return;
  827. // }
  828. // int count=[self.quantity_text.text intValue];
  829. if(alertView.tag==ALERT_DEL)
  830. {
  831. if(buttonIndex!=alertView.cancelButtonIndex)
  832. {
  833. [self.itemListTable reloadRowsAtIndexPaths:@[self.indexPath]withRowAnimation:UITableViewRowAnimationMiddle];
  834. NSMutableDictionary * item_json = [[self.content_data objectForKey:[NSString stringWithFormat:@"item_%ld",(long)self.indexPath.row]] mutableCopy];
  835. NSString *ids=[NSString stringWithFormat:@"%d",[[item_json valueForKey:@"item_id"] intValue]];
  836. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  837. NSDictionary* cart_json = [RANetwork watchlist_remove:ids];
  838. dispatch_async(dispatch_get_main_queue(), ^{
  839. if([[cart_json valueForKey:@"result"] intValue]==2)
  840. {
  841. [self reload_data];
  842. }
  843. else
  844. {
  845. [RAUtils message_alert:[cart_json valueForKey:@"err_msg"] title:@"Delete Model" controller:self] ;
  846. }
  847. });
  848. });
  849. }
  850. return;
  851. }
  852. MainViewController* main_vc=(MainViewController*)appDelegate.main_vc;
  853. if(buttonIndex==alertView.cancelButtonIndex)
  854. {
  855. }
  856. else if(buttonIndex==1)
  857. {
  858. //open exist
  859. OrderListViewController* ovc =[ self.storyboard instantiateViewControllerWithIdentifier:@"OrderListViewController"];
  860. ovc.showNavibar = true;
  861. ovc.selectOrder = ^(NSMutableDictionary* order_detail){
  862. if(appDelegate.order_code==nil)
  863. {
  864. [self neworder];
  865. }
  866. else
  867. {
  868. [self addtocart];
  869. [main_vc checklogin:true];
  870. }
  871. };
  872. ovc.init_style = OL_OPEN;
  873. ovc.onCancel = ^(){
  874. [main_vc checklogin:true];
  875. };
  876. [self.navigationController pushViewController:ovc animated:true];
  877. }
  878. else
  879. {
  880. //create new;
  881. if(appDelegate.customerInfo==nil)// select contact if current contact not exist
  882. {
  883. ContactListViewController* cvc = [self.storyboard instantiateViewControllerWithIdentifier:@"ContactListViewController" ];
  884. cvc.showNavibar = true;
  885. cvc.contact_type = @"Sales_Order_Customer";
  886. cvc.returnValue = ^(NSMutableDictionary* value,NSIndexPath* source){
  887. appDelegate.contact_id=[value valueForKey:@"customer_cid"];
  888. appDelegate.customerInfo = value;
  889. if(appDelegate.order_code==nil)
  890. [self neworder];
  891. // [main_vc checklogin:true];
  892. // [self handle_action_return:value indexPath:indexPath action:ACTION_FILL_SECTION];
  893. //
  894. // if(self.returnValue)
  895. // self.returnValue(value);
  896. };
  897. cvc.onCancel = ^(){
  898. [RAUtils message_alert:@"Cannot create order without cursomer infomation." title:@"" controller:self];
  899. };
  900. cvc.onReset = ^(){
  901. [main_vc checklogin:true];
  902. };
  903. [self.navigationController pushViewController:cvc animated:true];
  904. }
  905. else
  906. {
  907. [self neworder];
  908. }
  909. }
  910. }
  911. #pragma mark - sort button
  912. - (void)wishlistSortButtonClicked:(UIButton *)sender {
  913. DebugLog(@"wish list sort button clicked");
  914. [self.view addSubview:self.sortItemController.view];
  915. }
  916. - (SortItemViewController *)sortItemController {
  917. if (!_sortItemController) {
  918. _sortItemController = [[SortItemViewController alloc] initWithTableOrigin:CGPointMake(40, 40)];
  919. _sortItemController.sortIndex = self.sortIndex;
  920. _sortItemController.sortData = @[
  921. @{@"title":@"Last",@"icon":@"TX_18"},
  922. @{@"title":@"First",@"icon":@"TS_18"},
  923. @{@"title":@"Item number a-z",@"icon":@"IX_18"},
  924. @{@"title":@"Item number z-a",@"icon":@"IS_18"},
  925. @{@"title":@"Description",@"icon":@"DX_18"},
  926. ];
  927. __weak typeof(self) weakSelf = self;
  928. _sortItemController.sortBlock = ^(int sort){
  929. weakSelf.sortIndex = sort;
  930. [weakSelf reload_data];
  931. };
  932. }
  933. // _sortItemController.sortIndex = self.sortIndex;
  934. return _sortItemController;
  935. }
  936. - (SortButton *)sortButton {
  937. if (!_sortButton) {
  938. _sortButton = [SortButton sortButtonWithHeight:33];
  939. [_sortButton addTarget:self action:@selector(wishlistSortButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
  940. }
  941. return _sortButton;
  942. }
  943. - (void)setSortIndex:(int)sortIndex {
  944. _sortIndex = sortIndex;
  945. NSString *selectedImageName = @"";
  946. switch (sortIndex) {
  947. case 0:{
  948. selectedImageName = @"TX_22";
  949. }
  950. break;
  951. case 1:{
  952. selectedImageName = @"TS_22";
  953. }
  954. break;
  955. case 2:{
  956. selectedImageName = @"IX_22";
  957. }
  958. break;
  959. case 3:{
  960. selectedImageName = @"IS_22";
  961. }
  962. break;
  963. case 4:{
  964. selectedImageName = @"DX_22";
  965. }
  966. break;
  967. default:
  968. break;
  969. }
  970. self.sortButton.imageView.image = [UIImage imageNamed:selectedImageName];
  971. }
  972. - (UILabel *)qtyLabel {
  973. if (!_qtyLabel) {
  974. _qtyLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.itemListTable.bounds.size.width - 200, 5.5, 40, 22)];
  975. _qtyLabel.text = @"QTY";
  976. _qtyLabel.textAlignment = NSTextAlignmentCenter;
  977. _qtyLabel.autoresizingMask= UIViewAutoresizingFlexibleLeftMargin;
  978. }
  979. return _qtyLabel;
  980. }
  981. #pragma mark - rotation
  982. - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
  983. self.sortItemController.view.frame = self.view.bounds;
  984. }
  985. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
  986. return YES;
  987. }
  988. - (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
  989. self.sortItemController.view.frame = self.view.bounds;
  990. self.qtyLabel.frame = CGRectMake(self.itemListTable.bounds.size.width - 200, 5.5, 40, 22);
  991. }
  992. #pragma mark - Action
  993. - (IBAction)printWishlistClick:(UIBarButtonItem *)sender {
  994. if ([[self.content_data valueForKey:@"count"] intValue] < 1) {
  995. [RAUtils message_alert:@"Please add wishlist first" title:@"Warning" controller:self];
  996. return;
  997. }
  998. if (self.print_url.length == 0) {
  999. [RAUtils message_alert:@"There is no resource to print" title:@"Warning" controller:self];
  1000. }
  1001. RAPDFViewController *ViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"RAPDFViewController"];
  1002. ViewController.url = self.print_url;
  1003. ViewController.canSave = false;
  1004. ViewController.isLocalfile=NO;
  1005. NSString* subject;
  1006. subject =@"Wishlist Print";
  1007. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1008. NSString* customer_email= [appDelegate.customerInfo valueForKey:@"customer_email"];
  1009. NSMutableArray* send_to = [[NSMutableArray alloc]init];
  1010. if(customer_email.length>0)
  1011. {
  1012. send_to=[[customer_email componentsSeparatedByString:NSLocalizedString(@";", nil)] mutableCopy];
  1013. }
  1014. ViewController.mail_to = send_to;
  1015. // ViewController.attachment_name = [NSString stringWithFormat:@"NPD_Wish_List %@.pdf",cur_time];
  1016. /*
  1017. #ifdef BUILD_HOMER
  1018. ViewController.filename = [NSString stringWithFormat:@"HOMER_Wish_List.pdf"];
  1019. #endif
  1020. #if defined(BUILD_NPD) || defined(BUILD_USAI)
  1021. ViewController.filename = [NSString stringWithFormat:@"NPD_Wish_List.pdf"];
  1022. #endif
  1023. */
  1024. ViewController.filename = [NSString stringWithFormat:@"%@_Wish_List.pdf",COMPANY_SHORT_NAME];
  1025. ViewController.mail_subject = subject;
  1026. ViewController.hidenavi = false;
  1027. [self.navigationController pushViewController:ViewController animated:YES];
  1028. }
  1029. - (IBAction)emailWishlistClick:(UIBarButtonItem *)sender {
  1030. if ([[self.content_data valueForKey:@"count"] intValue] < 1) {
  1031. [RAUtils message_alert:@"Please add wishlist first" title:@"Warning" controller:self];
  1032. return;
  1033. }
  1034. JKMessageBoxController *emailAddrVC = [JKMessageBoxController messageBoxControllerWithTip:@"Please enter email address"];
  1035. emailAddrVC.textFiled.secureTextEntry = NO;
  1036. emailAddrVC.yesButtonTitle = @"send";
  1037. __weak typeof(emailAddrVC) weakVC = emailAddrVC;
  1038. __weak typeof(self) weakself = self;
  1039. emailAddrVC.textHandler = ^(NSString *text){
  1040. // 验证邮箱格式是否正确
  1041. // NSString *match = EMAIL_MATCHES;
  1042. // NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",match];
  1043. // BOOL isEmailAddr = [predicate evaluateWithObject:text];
  1044. BOOL isEmailAddr = YES;
  1045. if (isEmailAddr) {
  1046. // 验证是邮件地址,发送邮件
  1047. [weakVC dismissViewControllerAnimated:YES completion:^{
  1048. UIAlertView *waitting_alert = [RAUtils waiting_alert:@"Sending Email" title:@"Waiting"];
  1049. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  1050. NSDictionary *result = [RANetwork quoteWishlist2EmailAddr:text];
  1051. DebugLog(@"email wishlist result: %@",result);
  1052. dispatch_async(dispatch_get_main_queue(), ^{
  1053. [waitting_alert dismissWithClickedButtonIndex:0 animated:YES];
  1054. if ([[result objectForKey:@"result"] integerValue] != RESULT_TRUE) {
  1055. UIAlertController *errorAlertVC = [UIAlertController alertControllerWithTitle:@"Warning" message:[NSString stringWithFormat:@"The email send to %@ failed",text] preferredStyle:UIAlertControllerStyleAlert];
  1056. UIAlertAction *action = [UIAlertAction actionWithTitle:@"ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  1057. }];
  1058. [errorAlertVC addAction:action];
  1059. [weakself presentViewController:errorAlertVC animated:YES completion:nil];
  1060. }else {
  1061. [RAUtils message_alert:nil title:@"Email was sent successfully" controller:weakself];
  1062. }
  1063. });
  1064. });
  1065. }];
  1066. } else {
  1067. // 非邮件地址,警告
  1068. [weakVC warning:@"Please enter right email address"];
  1069. }
  1070. };
  1071. [self presentViewController:emailAddrVC animated:YES completion:nil];
  1072. }
  1073. @end