WatchListViewController.m 38 KB

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