WatchListViewController.m 36 KB

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