HomerModelDetailHeaderCell.m 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276
  1. //
  2. // HomerModelDetailHeaderCell.m
  3. // iSales-HOMER
  4. //
  5. // Created by Jack on 2017/10/25.
  6. // Copyright © 2017年 United Software Applications, Inc. All rights reserved.
  7. //
  8. #import "HomerModelDetailHeaderCell.h"
  9. #import "RANetwork.h"
  10. #import "MainViewController.h"
  11. #import "LoginViewController.h"
  12. #import "ContactListViewController.h"
  13. #import "RAUtils.h"
  14. #import "OrderListViewController.h"
  15. #import "ImageScrollerViewController.h"
  16. #import "CartUtils.h"
  17. #import "CustomIOSAlertView.h"
  18. #import "TextUtils.h"
  19. #import "ContentPreviewController.h"
  20. @implementation HomerModelDetailHeaderCell
  21. @synthesize celldelegate;
  22. - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
  23. {
  24. self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
  25. if (self) {
  26. // Initialization code
  27. }
  28. return self;
  29. }
  30. - (void)awakeFromNib
  31. {
  32. [super awakeFromNib];
  33. self.quantity_text.delegate = self;
  34. // MDHTMLLabelVerticalAlignmentCenter = 0,
  35. // MDHTMLLabelVerticalAlignmentTop = 1,
  36. self.informationLabel.numberOfLines = 0;
  37. // self.informationLabel.verticalAlignment=MDHTMLLabelVerticalAlignmentTop;
  38. // self.informationLabel.backgroundColor = [UIColor yellowColor];
  39. // self.selector_Button.layer.borderWidth = 0.5;
  40. // self.selector_Button.layer.borderColor = [[UIColor lightGrayColor] CGColor];
  41. // self.selector_Button.layer.cornerRadius = 0;
  42. [self.descriptionRTLabel setLineBreakMode: RTTextLineBreakModeWordWrapping];
  43. [self.descriptionRTLabel setTextAlignment:RTTextAlignmentCenter];
  44. [self.descriptionRTLabel setFont:[UIFont systemFontOfSize:22]];
  45. [self addObserver:self
  46. forKeyPath:@"stack_contents"
  47. options:(NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld)
  48. context:@"content changed"];
  49. [self generate_information];
  50. }
  51. -(void) dealloc
  52. {
  53. [self removeObserver:self forKeyPath:@"stack_contents"];
  54. }
  55. - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
  56. {
  57. if([keyPath isEqualToString:@"stack_contents"])
  58. {
  59. [self.playerView stopVideo];
  60. self.playerView.hidden=true;
  61. }
  62. }
  63. -(void) generate_information
  64. {
  65. NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  66. [arr_addr addObject:[NSString stringWithFormat:@"<font style='font-size:15px;font-family:Helvetica' ><b>Availability:</b> %@</font>",self.count_Label.text]];
  67. if(self.incomingcount_Label.text.length>0)
  68. [arr_addr addObject:[NSString stringWithFormat:@"<font style='font-size:15px;font-family:Helvetica' ><b>Incoming stock:</b> %@</font>",self.incomingcount_Label.text]];
  69. if(self.etaval_label.text.length>0)
  70. [arr_addr addObject:[NSString stringWithFormat:@"<font style='font-size:15px;font-family:Helvetica' ><b>Next Arrival Date:</b> %@</font>",self.etaval_label.text]];
  71. [arr_addr addObject:[NSString stringWithFormat:@"<font style='font-size:15px;font-family:Helvetica' ><b>Availability in 7-10 days:</b> %@</font>",self.hmlg_stock_count_lb.text]];
  72. if(self.cqyt_label.text.length>0)
  73. [arr_addr addObject:[NSString stringWithFormat:@"<font style='font-size:15px;color:red';font-family:Helvetica><b>%@</b></font>",self.cqyt_label.text]];
  74. NSString * information_str = [RAConvertor arr2string:arr_addr separator:@"<br>" trim:true];
  75. // UIFont *font=[UIFont systemFontOfSize:14];
  76. //// label.font=font;
  77. NSDictionary *optoins=@{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType};
  78. NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[information_str dataUsingEncoding:NSUnicodeStringEncoding] options:optoins documentAttributes:nil error:nil];
  79. self.informationLabel.attributedText=attrStr;
  80. }
  81. - (IBAction)add_toPortfolio:(id)sender {
  82. UIApplication * app = [UIApplication sharedApplication];
  83. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  84. if (appDelegate.user && appDelegate.user_type == USER_ROLE_CUSTOMER) {
  85. [[NSNotificationCenter defaultCenter] postNotificationName:No_Rights_Notification object:nil];
  86. return;
  87. }
  88. MainViewController* main_vc=(MainViewController*)appDelegate.main_vc;
  89. if(appDelegate.bLogin==false)
  90. {
  91. UIViewController *vc= [RAUtils getViewController:self];
  92. LoginViewController * loginvc =[ vc.storyboard instantiateViewControllerWithIdentifier:@"LoginViewController"];
  93. loginvc.returnValue = ^(bool blogin){
  94. if(blogin)
  95. {
  96. if(self.set_update_data)
  97. self.set_update_data(true);
  98. [main_vc checklogin:true];
  99. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  100. NSDictionary* return_json = [RANetwork add_toPortfolio:self.product_id withScreen:ScreenCodeModelInfo];
  101. dispatch_async(dispatch_get_main_queue(), ^{
  102. if([[return_json valueForKey:@"result"] intValue]==2)
  103. {
  104. [((MainViewController*)appDelegate.main_vc) reloadWish:true immediately:false];
  105. if(self.WatchlistBlock!=nil)
  106. {
  107. UIImage* img=[self photoStackView:self.photoStack photoForIndex:0];
  108. CGRect iv_rect = CGRectMake(self.btnaddPortfolio.center.x-50, self.btnaddPortfolio.center.y-50, 100, 100);
  109. UIImageView* iv = [[UIImageView alloc] initWithFrame:iv_rect];
  110. iv.image = img;
  111. self.PortfolioBlock(iv);
  112. }
  113. }
  114. else
  115. {
  116. [RAUtils message_alert:[return_json valueForKey:@"err_msg"] title:@"Add to portfolio" controller:vc] ;
  117. }
  118. });
  119. });
  120. }
  121. };
  122. UINavigationController* navi = [[UINavigationController alloc] initWithRootViewController:loginvc] ;
  123. navi.modalPresentationStyle = UIModalTransitionStyleCrossDissolve;//有三种状态,自己看看是哪种
  124. [vc presentViewController:navi animated:YES completion:^{
  125. }];
  126. }
  127. else
  128. {
  129. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  130. NSDictionary* return_json = [RANetwork add_toPortfolio:self.product_id withScreen:ScreenCodeModelInfo];
  131. dispatch_async(dispatch_get_main_queue(), ^{
  132. if([[return_json valueForKey:@"result"] intValue]==2)
  133. {
  134. [((MainViewController*)appDelegate.main_vc) reloadWish:true immediately:false];
  135. if(self.WatchlistBlock!=nil)
  136. {
  137. UIImage* img=[self photoStackView:self.photoStack photoForIndex:0];
  138. CGRect iv_rect = CGRectMake(self.btnaddPortfolio.center.x-50, self.btnaddPortfolio.center.y-50, 100, 100);
  139. UIImageView* iv = [[UIImageView alloc] initWithFrame:iv_rect];
  140. iv.image = img;
  141. self.PortfolioBlock(iv);
  142. }
  143. }
  144. else
  145. {
  146. UIViewController *vc= [RAUtils getViewController:self];
  147. [RAUtils message_alert:[return_json valueForKey:@"err_msg"] title:@"Add to portfolio" controller:vc] ;
  148. }
  149. });
  150. });
  151. }
  152. }
  153. - (IBAction)add_toWatchList:(id)sender {
  154. UIApplication * app = [UIApplication sharedApplication];
  155. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  156. MainViewController* main_vc=(MainViewController*)appDelegate.main_vc;
  157. if(appDelegate.bLogin==false)
  158. {
  159. UIViewController *vc= [RAUtils getViewController:self];
  160. LoginViewController * loginvc =[ vc.storyboard instantiateViewControllerWithIdentifier:@"LoginViewController"];
  161. loginvc.returnValue = ^(bool blogin){
  162. if(blogin)
  163. {
  164. if(self.set_update_data)
  165. self.set_update_data(true);
  166. [main_vc checklogin:true];
  167. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  168. NSDictionary* return_json = [RANetwork add_toWatchList:self.product_id withScreen:ScreenCodeModelInfo];
  169. dispatch_async(dispatch_get_main_queue(), ^{
  170. if([[return_json valueForKey:@"result"] intValue]==2)
  171. {
  172. [((MainViewController*)appDelegate.main_vc) reloadWish:true immediately:false];
  173. if(self.WatchlistBlock!=nil)
  174. {
  175. UIImage* img=[self photoStackView:self.photoStack photoForIndex:0];
  176. CGRect iv_rect = CGRectMake(self.btnaddWish.center.x-50, self.btnaddWish.center.y-50, 100, 100);
  177. UIImageView* iv = [[UIImageView alloc] initWithFrame:iv_rect];
  178. iv.image = img;
  179. self.WatchlistBlock(iv);
  180. }
  181. }
  182. else
  183. {
  184. [RAUtils message_alert:[return_json valueForKey:@"err_msg"] title:@"Add Wish List" controller:vc] ;
  185. }
  186. });
  187. });
  188. }
  189. };
  190. UINavigationController* navi = [[UINavigationController alloc] initWithRootViewController:loginvc] ;
  191. navi.modalPresentationStyle = UIModalTransitionStyleCrossDissolve;//有三种状态,自己看看是哪种
  192. [vc presentViewController:navi animated:YES completion:^{
  193. }];
  194. }
  195. else
  196. {
  197. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  198. NSDictionary* return_json = [RANetwork add_toWatchList:self.product_id withScreen:ScreenCodeModelInfo];
  199. dispatch_async(dispatch_get_main_queue(), ^{
  200. if([[return_json valueForKey:@"result"] intValue]==2)
  201. {
  202. [((MainViewController*)appDelegate.main_vc) reloadWish:true immediately:false];
  203. if(self.WatchlistBlock!=nil)
  204. {
  205. UIImage* img=[self photoStackView:self.photoStack photoForIndex:0];
  206. CGRect iv_rect = CGRectMake(self.btnaddWish.center.x-50, self.btnaddWish.center.y-50, 100, 100);
  207. UIImageView* iv = [[UIImageView alloc] initWithFrame:iv_rect];
  208. iv.image = img;
  209. self.WatchlistBlock(iv);
  210. }
  211. }
  212. else
  213. {
  214. UIViewController *vc= [RAUtils getViewController:self];
  215. [RAUtils message_alert:[return_json valueForKey:@"err_msg"] title:@"Add Wish List" controller:vc] ;
  216. }
  217. });
  218. });
  219. }
  220. }
  221. - (IBAction)add_toCart:(id)sender {
  222. UIViewController *vc= [RAUtils getViewController:self];
  223. [CartUtils add_to_cart:vc selectorholder:self selector:@selector(addtocart)];
  224. return;
  225. }
  226. -(void) addtocart
  227. {
  228. int count=[self.quantity_text.text intValue];
  229. #ifdef MPACK
  230. if (count % self.step != 0) {
  231. UIViewController *vc= [RAUtils getViewController:self];
  232. [RAUtils message_alert:[NSString stringWithFormat:@"Sold in quantities of %d",(int)(self.stepper.stepValue)] title:@"Add to cart" controller:vc];
  233. return;
  234. }
  235. #endif
  236. int c=[self.count_Label.text intValue];
  237. DebugLog(@"count label : %@ , convert:%d", self.count_Label.text ,c);
  238. NSString* ct=[self.count_Label.text stringByReplacingOccurrencesOfString:@"\n" withString:@""];
  239. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  240. if(/*c<count*/!appDelegate.can_create_backorder && c == 0)
  241. {
  242. NSString *msg = nil;
  243. BOOL continueAdd = YES;
  244. // #ifdef BUILD_NPD
  245. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  246. if (appDelegate.user_type == USER_ROLE_CUSTOMER) {
  247. if ([Singleton sharedInstance].currentOrderIsMerged) { // Purchase Order不允许购买数量大于库存
  248. msg = [NSString stringWithFormat:@"Item is OUT OF STOCK and not available to order from %@.",COMPANY_SHORT_NAME];
  249. continueAdd = NO;
  250. } else { // Shop Order购买数量大于库存需要确认
  251. msg = [NSString stringWithFormat:@"Item is OUT OF STOCK and not available to order from %@.\nAdd to cart anyway?",COMPANY_SHORT_NAME];
  252. continueAdd = YES;
  253. }
  254. }
  255. if (appDelegate.user_type == USER_ROLE_EMPLOYEE) { // Employee不允许购买数量大于库存
  256. msg = @"Item is OUT OF STOCK and not available to order.";
  257. continueAdd = NO;
  258. }
  259. // #endif
  260. UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:[NSString stringWithFormat:@"Availability %@",ct ] message:msg preferredStyle:UIAlertControllerStyleAlert];
  261. UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
  262. if (continueAdd) {
  263. [self addtocart:count];
  264. }
  265. }];
  266. [alertControl addAction:alertthree];
  267. UIAlertAction *alertcancel = [UIAlertAction actionWithTitle:@"No" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  268. }];
  269. if (continueAdd) {
  270. [alertControl addAction:alertcancel];
  271. }
  272. UIViewController *vc= [RAUtils getViewController:self];
  273. [vc presentViewController:alertControl animated:YES completion:nil];
  274. }
  275. else
  276. {
  277. [self addtocart:count];
  278. }
  279. }
  280. -(void)Hide_selector:(bool) bhide
  281. {
  282. // self.selector_Button.hidden=bhide;
  283. // self.selector_dorpdown.hidden=bhide;
  284. // self.selector_imageView.hidden=bhide;
  285. //
  286. }
  287. -(void) addtocart:(int) count
  288. {
  289. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  290. if(self.cqty>0)
  291. {
  292. UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:[NSString stringWithFormat:@"QTY: %d of this model already in cart. Continue ?",self.cqty] message:nil preferredStyle:UIAlertControllerStyleAlert];
  293. //block代码块取代了delegate
  294. UIAlertAction *actionOne = [UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
  295. int c=self.cqty+count;
  296. int m=c%self.step;
  297. if(m!=0&&appDelegate.alert_sold_in_quantities)
  298. {
  299. UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:[NSString stringWithFormat:@"Sold in quantities of %d, Are you sure?",self.step] message:nil preferredStyle:UIAlertControllerStyleAlert];
  300. //block代码块取代了delegate
  301. UIAlertAction *actionOne = [UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
  302. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  303. NSDictionary* return_json = [RANetwork add_toCart:self.product_id count:count name:self.model_label.text];
  304. dispatch_async(dispatch_get_main_queue(), ^{
  305. if([[return_json valueForKey:@"result"] intValue]==2)
  306. {
  307. // NSString* order_code = [return_json valueForKey:@"order_code"];
  308. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  309. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  310. if(self.shopCartBlock!=nil)
  311. {
  312. UIImage* img=[self photoStackView:self.photoStack photoForIndex:0];
  313. CGRect iv_rect = CGRectMake(self.btnaddCart.center.x-50, self.btnaddCart.center.y-50, 100, 100);
  314. UIImageView* iv = [[UIImageView alloc] initWithFrame:iv_rect];
  315. iv.image = img;
  316. // [self.contentView addSubview:iv];
  317. self.shopCartBlock(iv);
  318. self.cqty +=count;
  319. if(self.set_cqty)
  320. self.set_cqty(self.cqty);
  321. }
  322. }
  323. else
  324. {
  325. UIViewController *vc= [RAUtils getViewController:self];
  326. NSString *msg = [return_json valueForKey:@"err_msg"];
  327. NSString *title = @"Add To Cart";
  328. if ([msg hasPrefix:@"Out of Stock.\n"]) {
  329. title = @"Add To Cart: Out of Stock";
  330. msg = [msg substringFromIndex:[@"Out of Stock.\n" length]];
  331. self.btnNotifyMe.enabled = YES;
  332. self.btnaddCart.enabled = NO;
  333. }
  334. [RAUtils message_alert:msg title:title controller:vc] ;
  335. }
  336. });
  337. });
  338. }];
  339. UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"No" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  340. DebugLog(@"No");
  341. }];
  342. [alertControl addAction:actionOne];
  343. [alertControl addAction:alertthree];
  344. //UIAlertControllerStyle类型为UIAlertControllerStyleAlert可以添加addTextFieldWithConfigurationHandler:^(UITextField *textField)
  345. UIViewController *vc= [RAUtils getViewController:self];
  346. [vc presentViewController:alertControl animated:YES completion:nil];
  347. }
  348. else
  349. {
  350. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  351. NSDictionary* return_json = [RANetwork add_toCart:self.product_id count:count name:self.model_label.text];
  352. dispatch_async(dispatch_get_main_queue(), ^{
  353. if([[return_json valueForKey:@"result"] intValue]==2)
  354. {
  355. // NSString* order_code = [return_json valueForKey:@"order_code"];
  356. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  357. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  358. if(self.shopCartBlock!=nil)
  359. {
  360. UIImage* img=[self photoStackView:self.photoStack photoForIndex:0];
  361. CGRect iv_rect = CGRectMake(self.btnaddCart.center.x-50, self.btnaddCart.center.y-50, 100, 100);
  362. UIImageView* iv = [[UIImageView alloc] initWithFrame:iv_rect];
  363. iv.image = img;
  364. // [self.contentView addSubview:iv];
  365. self.shopCartBlock(iv);
  366. self.cqty +=count;
  367. if(self.set_cqty)
  368. self.set_cqty(self.cqty);
  369. }
  370. }
  371. else
  372. {
  373. UIViewController *vc= [RAUtils getViewController:self];
  374. NSString *msg = [return_json valueForKey:@"err_msg"];
  375. NSString *title = @"Add To Cart";
  376. if ([msg hasPrefix:@"Out of Stock.\n"]) {
  377. title = @"Add To Cart: Out of Stock";
  378. msg = [msg substringFromIndex:[@"Out of Stock.\n" length]];
  379. self.btnNotifyMe.enabled = YES;
  380. self.btnaddCart.enabled = NO;
  381. }
  382. [RAUtils message_alert:msg title:title controller:vc] ;
  383. }
  384. });
  385. });
  386. }
  387. }];
  388. UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"No" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  389. DebugLog(@"No");
  390. }];
  391. [alertControl addAction:actionOne];
  392. [alertControl addAction:alertthree];
  393. //UIAlertControllerStyle类型为UIAlertControllerStyleAlert可以添加addTextFieldWithConfigurationHandler:^(UITextField *textField)
  394. UIViewController *vc= [RAUtils getViewController:self];
  395. [vc presentViewController:alertControl animated:YES completion:nil];
  396. }
  397. else
  398. {
  399. int c=self.cqty+count;
  400. int m=c%self.step;
  401. if(m!=0&&appDelegate.alert_sold_in_quantities)
  402. {
  403. UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:[NSString stringWithFormat:@"Sold in quantities of %d, Are you sure?",self.step] message:nil preferredStyle:UIAlertControllerStyleAlert];
  404. //block代码块取代了delegate
  405. UIAlertAction *actionOne = [UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
  406. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  407. NSDictionary* return_json = [RANetwork add_toCart:self.product_id count:count name:self.model_label.text];
  408. dispatch_async(dispatch_get_main_queue(), ^{
  409. if([[return_json valueForKey:@"result"] intValue]==2)
  410. {
  411. // NSString* order_code = [return_json valueForKey:@"order_code"];
  412. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  413. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  414. if(self.shopCartBlock!=nil)
  415. {
  416. UIImage* img=[self photoStackView:self.photoStack photoForIndex:0];
  417. CGRect iv_rect = CGRectMake(self.btnaddCart.center.x-50, self.btnaddCart.center.y-50, 100, 100);
  418. UIImageView* iv = [[UIImageView alloc] initWithFrame:iv_rect];
  419. iv.image = img;
  420. // [self.contentView addSubview:iv];
  421. self.shopCartBlock(iv);
  422. self.cqty +=count;
  423. if(self.set_cqty)
  424. self.set_cqty(self.cqty);
  425. }
  426. }
  427. else
  428. {
  429. UIViewController *vc= [RAUtils getViewController:self];
  430. NSString *msg = [return_json valueForKey:@"err_msg"];
  431. NSString *title = @"Add To Cart";
  432. if ([msg hasPrefix:@"Out of Stock.\n"]) {
  433. title = @"Add To Cart: Out of Stock";
  434. msg = [msg substringFromIndex:[@"Out of Stock.\n" length]];
  435. self.btnNotifyMe.enabled = YES;
  436. self.btnaddCart.enabled = NO;
  437. }
  438. [RAUtils message_alert:msg title:title controller:vc] ;
  439. }
  440. });
  441. });
  442. }];
  443. UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"No" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  444. DebugLog(@"No");
  445. }];
  446. [alertControl addAction:actionOne];
  447. [alertControl addAction:alertthree];
  448. //UIAlertControllerStyle类型为UIAlertControllerStyleAlert可以添加addTextFieldWithConfigurationHandler:^(UITextField *textField)
  449. UIViewController *vc= [RAUtils getViewController:self];
  450. [vc presentViewController:alertControl animated:YES completion:nil];
  451. }
  452. else
  453. {
  454. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  455. NSDictionary* return_json = [RANetwork add_toCart:self.product_id count:count name:self.model_label.text];
  456. dispatch_async(dispatch_get_main_queue(), ^{
  457. if([[return_json valueForKey:@"result"] intValue]==2)
  458. {
  459. // NSString* order_code = [return_json valueForKey:@"order_code"];
  460. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  461. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  462. if(self.shopCartBlock!=nil)
  463. {
  464. UIImage* img=[self photoStackView:self.photoStack photoForIndex:0];
  465. CGRect iv_rect = CGRectMake(self.btnaddCart.center.x-50, self.btnaddCart.center.y-50, 100, 100);
  466. UIImageView* iv = [[UIImageView alloc] initWithFrame:iv_rect];
  467. iv.image = img;
  468. // [self.contentView addSubview:iv];
  469. self.shopCartBlock(iv);
  470. self.cqty +=count;
  471. if(self.set_cqty)
  472. self.set_cqty(self.cqty);
  473. }
  474. }
  475. else
  476. {
  477. UIViewController *vc= [RAUtils getViewController:self];
  478. NSString *msg = [return_json valueForKey:@"err_msg"];
  479. NSString *title = @"Add To Cart";
  480. if ([msg hasPrefix:@"Out of Stock.\n"]) {
  481. title = @"Add To Cart: Out of Stock";
  482. msg = [msg substringFromIndex:[@"Out of Stock.\n" length]];
  483. self.btnNotifyMe.enabled = YES;
  484. self.btnaddCart.enabled = NO;
  485. }
  486. [RAUtils message_alert:msg title:title controller:vc] ;
  487. }
  488. });
  489. });
  490. }
  491. }
  492. }
  493. - (void)setSelected:(BOOL)selected animated:(BOOL)animated
  494. {
  495. [super setSelected:selected animated:animated];
  496. // Configure the view for the selected state
  497. }
  498. - (IBAction)selectorOnClick:(id)sender {
  499. DebugLog(@"select click");
  500. [celldelegate SelectorClicked:(UIButton*) sender];
  501. // [self performSegueWithIdentifier:@"selector_popover" sender:self];
  502. }
  503. #pragma mark -
  504. #pragma mark Deck DataSource Protocol Methods
  505. -(NSUInteger)numberOfPhotosInPhotoStackView:(PhotoStackView *)photoStack {
  506. return [self.photos count];
  507. }
  508. -(UIImage *)photoStackView:(PhotoStackView *)photoStack photoForIndex:(NSUInteger)index {
  509. if(self.photos.count==0)
  510. return nil;
  511. return [self.photos objectAtIndex:index];
  512. }
  513. #pragma mark -
  514. #pragma mark Deck Delegate Protocol Methods
  515. -(void)photoStackView:(PhotoStackView *)photoStackView willStartMovingPhotoAtIndex:(NSUInteger)index {
  516. // User started moving a photo
  517. }
  518. -(void)photoStackView:(PhotoStackView *)photoStackView willFlickAwayPhotoFromIndex:(NSUInteger)fromIndex toIndex:(NSUInteger)toIndex {
  519. // User flicked the photo away, revealing the next one in the stack
  520. }
  521. -(void)photoStackView:(PhotoStackView *)photoStackView didRevealPhotoAtIndex:(NSUInteger)index {
  522. self.pageControl.currentPage = index;
  523. }
  524. - (UIView *)createVideoView:(NSString*) videoID
  525. {
  526. UIView *demoView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 640, 360)];
  527. // UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 270, 180)];
  528. // [imageView setImage:[UIImage imageNamed:@"demo"]];
  529. UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 620, 10)];
  530. titleLabel.font = [UIFont boldSystemFontOfSize:20];
  531. titleLabel.text=@"a separated player will show here:";
  532. titleLabel.numberOfLines = 0;
  533. titleLabel.lineBreakMode =NSLineBreakByWordWrapping;
  534. // titleLabel.textAlignment = NSTextAlignmentRight;
  535. titleLabel.textAlignment = NSTextAlignmentCenter;
  536. CGSize constraintkey = CGSizeMake(620, 20000.0f);//key label width is 40% cell width;
  537. CGSize sizetitle = [@"a separated player will show here:" sizeWithFont:[UIFont systemFontOfSize:20.0] constrainedToSize:constraintkey lineBreakMode:NSLineBreakByWordWrapping];
  538. //[titleLabel sizeToFit];
  539. // titleLabel.center.x = demoView.center.x;
  540. titleLabel.frame = CGRectMake(10, 10, 620, sizetitle.height);
  541. [demoView addSubview:titleLabel];
  542. UILabel *lineLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, titleLabel.frame.origin.y+ titleLabel.frame.size.height+20, 610, 10)];
  543. lineLabel.font = [UIFont boldSystemFontOfSize:17];
  544. lineLabel.text=videoID;
  545. lineLabel.numberOfLines = 0;
  546. lineLabel.lineBreakMode =NSLineBreakByWordWrapping;
  547. CGSize sizemsg = [videoID sizeWithFont:[UIFont boldSystemFontOfSize:17.0] constrainedToSize:constraintkey lineBreakMode:NSLineBreakByWordWrapping];
  548. lineLabel.frame = CGRectMake(15, titleLabel.frame.origin.y+ titleLabel.frame.size.height+20, 630, sizemsg.height);
  549. //[lineLabel sizeToFit];
  550. [demoView addSubview:lineLabel];
  551. demoView.frame = CGRectMake(0, 0, 640, lineLabel.frame.origin.y+lineLabel.frame.size.height+25);
  552. return demoView;
  553. }
  554. -(void)photoStackView:(PhotoStackView *)photoStackView didSelectPhotoAtIndex:(NSUInteger)index {
  555. DebugLog(@"selected %lu", (unsigned long)index);
  556. NSDictionary* item= self.stack_contents[[NSString stringWithFormat:@"item_%ld",index]];
  557. if([item[@"type"] isEqualToString:@"video"])
  558. {
  559. NSString* video_code=item[@"code"];
  560. // [TextUtils expression_varable:video regex:<#(NSString *)#>]
  561. NSString* video_id=[self.playerView Embed2VID:video_code];
  562. NSDictionary *playerVars = @{
  563. @"playsinline" : @1,
  564. @"autoplay" : @(1),
  565. @"rel":@0,
  566. @"showinfo": @0,
  567. @"modestbranding":@0,
  568. @"enablejsapi":@1
  569. };
  570. [self.playerView loadWithVideoId:video_id playerVars:playerVars];
  571. for(UIView *v in self.playerView.subviews)
  572. {
  573. if([v isKindOfClass:[UIButton class] ])
  574. [self.playerView bringSubviewToFront:v];
  575. }
  576. self.playerView.webView.allowsPictureInPictureMediaPlayback=false;
  577. self.playerView.hidden=false;
  578. return;
  579. } else {
  580. ContentPreviewController *preVC = [[UIStoryboard storyboardWithName:@"PhotoList" bundle:nil] instantiateViewControllerWithIdentifier:@"ContentPreviewController"];
  581. preVC.content = self.stack_contents;
  582. // preVC.currentIndex = index;
  583. [preVC setOffset:index];
  584. UIViewController *superVC= [RAUtils getViewController:self];
  585. [superVC.navigationController pushViewController:preVC animated:true];
  586. return;
  587. }
  588. // }
  589. // 调用展示窗口
  590. ImageScrollerViewController *imgShow = [[ImageScrollerViewController alloc] initWithSourceData:[self.photos mutableCopy] withIndex:index];
  591. UIViewController *vc= [RAUtils getViewController:self];
  592. [vc.navigationController pushViewController:imgShow animated:true];
  593. }
  594. -(void) neworder
  595. {
  596. UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait" title:@"Create Order"];
  597. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  598. NSDictionary* return_json = [RANetwork new_Order];
  599. dispatch_async(dispatch_get_main_queue(), ^{
  600. [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  601. if([[return_json valueForKey:@"result"] intValue]==2)
  602. {
  603. int result=[[return_json valueForKey:@"result"] intValue];
  604. if(result==2)
  605. {
  606. //successed.
  607. NSString* order_code = [return_json valueForKey:@"orderCode"];
  608. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  609. appDelegate.order_code = order_code;
  610. int count=[self.quantity_text.text intValue];
  611. [self addtocart:count];
  612. }
  613. }
  614. else
  615. {
  616. UIViewController *vc= [RAUtils getViewController:self];
  617. NSString *msg = [return_json valueForKey:@"err_msg"];
  618. NSString *title = @"Add To Cart";
  619. if ([msg hasPrefix:@"Out of Stock.\n"]) {
  620. title = @"Add To Cart: Out of Stock";
  621. msg = [msg substringFromIndex:[@"Out of Stock.\n" length]];
  622. self.btnNotifyMe.enabled = YES;
  623. self.btnaddCart.enabled = NO;
  624. }
  625. [RAUtils message_alert:msg title:title controller:vc] ;
  626. }
  627. });
  628. });
  629. }
  630. #pragma mark - UIAlertViewDelegate
  631. // Called when a button is clicked. The view will be automatically dismissed after this call returns
  632. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  633. {
  634. UIApplication * app = [UIApplication sharedApplication];
  635. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  636. if(alertView.tag==1234)//库存小于0
  637. {
  638. // availability warring;
  639. int count=[self.quantity_text.text intValue];
  640. if(buttonIndex!=alertView.cancelButtonIndex)
  641. {
  642. if(appDelegate.order_code==nil)
  643. {
  644. [self neworder];
  645. }
  646. else
  647. {
  648. [self addtocart:count];
  649. }
  650. }
  651. return;
  652. }
  653. // int count=[self.quantity_text.text intValue];
  654. MainViewController* main_vc=(MainViewController*)appDelegate.main_vc;
  655. if(buttonIndex==alertView.cancelButtonIndex)
  656. {
  657. }
  658. else if(buttonIndex==1)
  659. {
  660. //open exist
  661. UIViewController *vc= [RAUtils getViewController:self];
  662. OrderListViewController* ovc =[ vc.storyboard instantiateViewControllerWithIdentifier:@"OrderListViewController"];
  663. ovc.showNavibar = true;
  664. ovc.selectOrder = ^(NSMutableDictionary* order_detail){
  665. if(appDelegate.order_code==nil)
  666. {
  667. [self neworder];
  668. }
  669. else
  670. {
  671. if(self.set_update_data)
  672. self.set_update_data(true);
  673. int model_count = [[order_detail valueForKey:@"model_count"] intValue];
  674. for(int i=0;i<model_count;i++)
  675. {
  676. NSDictionary* item = [order_detail objectForKey:[NSString stringWithFormat:@"item_%d",i]];
  677. NSString* product_id = [item valueForKey:@"product_id"];
  678. if([product_id isEqualToString:self.product_id])
  679. {
  680. self.cqty = [[item valueForKey:@"QTY"]intValue];
  681. if(self.set_cqty)
  682. self.set_cqty(self.cqty);
  683. break;
  684. }
  685. }
  686. int count=[self.quantity_text.text intValue];
  687. [self addtocart:count];
  688. [main_vc checklogin:true];
  689. }
  690. };
  691. ovc.init_style = OL_OPEN;
  692. ovc.onCancel = ^(){
  693. [main_vc checklogin:true];
  694. };
  695. [vc.navigationController pushViewController:ovc animated:true];
  696. }
  697. else
  698. {
  699. //create new;
  700. if(appDelegate.customerInfo==nil)// select contact if current contact not exist
  701. {
  702. UIViewController *vc= [RAUtils getViewController:self];
  703. ContactListViewController* cvc = [vc.storyboard instantiateViewControllerWithIdentifier:@"ContactListViewController" ];
  704. cvc.showNavibar = true;
  705. cvc.contact_type = @"Sales_Order_Customer";
  706. cvc.returnValue = ^(NSMutableDictionary* value,NSIndexPath* source){
  707. appDelegate.contact_id=[value valueForKey:@"customer_cid"];
  708. appDelegate.customerInfo = value;
  709. if(appDelegate.order_code==nil)
  710. [self neworder];
  711. [main_vc checklogin:true];
  712. };
  713. cvc.onCancel = ^(){
  714. UIViewController *vc= [RAUtils getViewController:self];
  715. [RAUtils message_alert:@"Cannot create order without cursomer infomation." title:@"New Order" controller:vc];
  716. };
  717. cvc.onReset = ^(){
  718. [main_vc checklogin:true];
  719. };
  720. [vc.navigationController pushViewController:cvc animated:true];
  721. }
  722. else
  723. {
  724. [self neworder];
  725. }
  726. }
  727. }
  728. - (void)valueChanged:(UIStepper *)sender {
  729. self.quantity_text.text = [NSString stringWithFormat:@"%d",(int)sender.value ];
  730. [celldelegate stepClicked:(int)sender.value];
  731. }
  732. #pragma mark -
  733. #pragma mark Actions
  734. -(void)ClearPhotos
  735. {
  736. NSMutableArray *photosMutable = [self.photos mutableCopy];
  737. [photosMutable removeAllObjects];
  738. self.photos = photosMutable;
  739. [self.photoStack reloadData];
  740. }
  741. - (void)AddPhoto :(UIImage*)photo {
  742. NSMutableArray *photosMutable = [self.photos mutableCopy];
  743. [photosMutable addObject:photo];
  744. self.photos = photosMutable;
  745. [self.photoStack reloadData];
  746. self.pageControl.numberOfPages = [self.photos count];
  747. if(self.photos.count<2-1)//1 for qrcode;
  748. self.photoStack.showBorder=false;
  749. else
  750. self.photoStack.showBorder=true;
  751. }
  752. #pragma mark -
  753. #pragma mark Setup
  754. -(void)setup {
  755. // int availability = [self.count_Label.text intValue];
  756. self.stepper.minimumValue= self.step;
  757. self.stepper.maximumValue = INTMAX_MAX;
  758. self.stepper.stepValue= self.step;
  759. self.stepper.value= self.quantity;
  760. [self.stepper addTarget:self action:@selector(valueChanged:) forControlEvents:UIControlEventValueChanged];
  761. if(true)//(self.photos==nil)
  762. {
  763. self.photos = [NSArray array];
  764. }
  765. if(true)//(self.photoStack==nil)
  766. {
  767. self.photoStack.dataSource = self;
  768. self.photoStack.delegate = self;
  769. // [self addSubview:self.photoStack];
  770. self.pageControl.numberOfPages = [self.photos count];
  771. }
  772. }
  773. - (IBAction)notifyMeButtonClicked:(UIButton *)sender {
  774. if (self.notifyMeBlock) {
  775. self.notifyMeBlock();
  776. }
  777. }
  778. #pragma mark - TextField Delegate
  779. - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
  780. NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
  781. int buyQTY = [newString intValue];
  782. self.stepper.value = buyQTY;
  783. return YES;
  784. }
  785. #pragma mark - Private
  786. /**检查是否缺货*/
  787. - (void)checkAvailability:(int)availability buyQTY:(int)buyQty cartQTY:(int)cartQty { // availability不可以通过textField.text取得
  788. //#ifdef BUILD_NPD
  789. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  790. if (!appDelegate.bLogin || buyQty < 1) {
  791. return;
  792. }
  793. #ifdef OFFLINE_MODE
  794. if (appDelegate.offline_mode) {
  795. return;
  796. }
  797. #endif
  798. if (appDelegate.user_type == USER_ROLE_CUSTOMER) { // customer
  799. if (!appDelegate.order_code) { // 未打开订单状态
  800. if ((buyQty + cartQty) > availability) {
  801. self.btnaddCart.enabled = NO;
  802. self.btnNotifyMe.enabled = YES;
  803. } else {
  804. self.btnaddCart.enabled = YES;
  805. self.btnNotifyMe.enabled = NO;
  806. }
  807. } else {
  808. // 打开订单状态
  809. if (![Singleton sharedInstance].currentOrderIsMerged) {
  810. // Shop Order
  811. self.btnaddCart.enabled = YES;
  812. // 默认未缺货
  813. self.btnNotifyMe.enabled = NO;
  814. if ((buyQty + cartQty) > availability) { // 缺货
  815. self.btnNotifyMe.enabled = YES;
  816. }
  817. } else {
  818. // Purchase Order
  819. if ((buyQty + cartQty) > availability) { // 缺货
  820. self.btnaddCart.enabled = NO;
  821. self.btnNotifyMe.enabled = YES;
  822. } else { // 未缺货
  823. self.btnNotifyMe.enabled = NO;
  824. self.btnaddCart.enabled = YES;
  825. }
  826. }
  827. }
  828. } else { // empoyee
  829. if ((buyQty + cartQty) > availability) { // 缺货
  830. self.btnaddCart.enabled = NO;
  831. self.btnNotifyMe.enabled = YES;
  832. } else { // 未缺货
  833. self.btnNotifyMe.enabled = NO;
  834. self.btnaddCart.enabled = YES;
  835. }
  836. }
  837. //#endif
  838. }
  839. - (IBAction)introduceBtnClick:(UIButton *)sender {
  840. if (self.introduceBlock) {
  841. self.introduceBlock(sender);
  842. }
  843. }
  844. - (IBAction)descriptionBtnClick:(UIButton *)sender {
  845. if (self.descriptionBlock) {
  846. self.descriptionBlock(sender);
  847. }
  848. }
  849. - (IBAction)OnClosePlayer:(id)sender {
  850. [self.playerView stopVideo];
  851. self.playerView.hidden=true;
  852. }
  853. @end