WatchListViewController.m 41 KB

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