// // SelectUploadOrderViewController.m // iSales-NPD // // Created by Ray on 9/3/16. // Copyright © 2016 United Software Applications, Inc. All rights reserved. // #import "SelectUploadOrderViewController.h" #import "iSalesDB.h" #import #import "SelectOrderTableViewCell.h" #import "DefaultTableHeaderView.h" #import "DefaultAppearance.h" @interface SelectUploadOrderViewController () @property (nonatomic,strong) NSMutableArray *sync_data; @property (strong, nonatomic) IBOutlet UITableView *syncDataTableView; @property (strong, nonatomic) IBOutlet UIButton *uploadButton; @end @implementation SelectUploadOrderViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. // self.syncDataTableView.delegate = self; // self.syncDataTableView.dataSource = self; UIBarButtonItem *closeButton = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"close"] imageWithRenderingMode:UIImageRenderingModeAutomatic] style:UIBarButtonItemStylePlain target:self action:@selector( onCloseClick:)]; self.navigationItem.rightBarButtonItem = closeButton; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (NSMutableArray *)sync_data { if (!_sync_data) { _sync_data = [NSMutableArray array]; NSString *sql = @"select decrypt(c.company_name),o.so_id,o.sync_data from (select so_id,sync_data,customer_cid from offline_order where sync_data not null) as o join offline_contact as c on c.contact_id = o.customer_cid ;"; [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) { const char *cn = (char *)sqlite3_column_text(stmt,0); if (cn == NULL) { cn = ""; } const char *_id = (char *)sqlite3_column_text(stmt,1); if (_id == NULL) { _id = ""; } const char *data = (char *)sqlite3_column_text(stmt,2); if (data == NULL) { data = ""; } NSString *company_name = [NSString stringWithUTF8String:cn]; NSString *so_id = [NSString stringWithUTF8String:_id]; NSString *sync_data_str = [NSString stringWithUTF8String:data]; NSDictionary *dic = [@{ @"check" : @(1), @"company_name" : company_name, @"so_id" : so_id, @"sync_data" : sync_data_str } mutableCopy]; [_sync_data addObject:dic]; }]; } return _sync_data; } - (IBAction)uploadButtonClicked:(UIButton *)sender { NSMutableArray *uploadArray = [NSMutableArray array]; for (NSDictionary *dic in self.sync_data) { int check = [dic[@"check"] integerValue]; NSString *so_id = dic[@"so_id"]; if (check == 1) { [uploadArray addObject:so_id]; } } [self dismissViewControllerAnimated:true completion:^{ if (self.returnValue ) { self.returnValue(uploadArray); } ; }]; } #pragma mark - dataSource - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.sync_data.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { SelectOrderTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SelectOrderTableViewCell" forIndexPath:indexPath]; NSDictionary *dic = [self.sync_data objectAtIndex:indexPath.row]; int check = [dic[@"check"] integerValue]; NSString *so_id = dic[@"so_id"]; NSString *company_name = dic[@"company_name"]; if (check) cell.checkedButton.selected = YES; cell.labelsoid.text = so_id; cell.labelcompany.text = company_name; __weak typeof(self) weakSelf = self; __weak typeof(tableView) weakTable = tableView; // NSIndexPath *idxPath = [NSIndexPath indexPathForRow:indexPath.row inSection:0]; cell.checkBlock = ^{ [weakSelf tableView:weakTable didSelectRowAtIndexPath:indexPath]; }; return cell; } #pragma mark - delegate - (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { NSString* value =[DefaultAppearance get_noneappearance_value:@"DefaultTableHeaderView" valuename:@"title_text_color"]; if(value==nil) value=@""; unsigned long color = strtoul([value UTF8String],0,16); DefaultTableHeaderView* myView = [[DefaultTableHeaderView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 0.0001)]; myView.layer.shadowPath =[UIBezierPath bezierPathWithRect:myView.bounds].CGPath; myView.layer.shadowColor = [UIColor blackColor].CGColor; myView.layer.shadowOffset = CGSizeMake(0, 0); myView.layer.shadowOpacity = 0.5; myView.layer.shadowRadius = 2.0; UILabel *solabel = [[UILabel alloc] initWithFrame:CGRectMake(48, 2, 100, 22)]; solabel.textColor=UIColorFromRGB(color); solabel.backgroundColor = [UIColor clearColor]; solabel.text=NSLocalizedString(@"SO#", nil); [solabel sizeToFit]; [myView addSubview:solabel]; UILabel *userlabel = [[UILabel alloc] initWithFrame:CGRectMake(48, 21, 100, 22)]; userlabel.textColor=UIColorFromRGB(color); userlabel.backgroundColor = [UIColor clearColor]; userlabel.text=NSLocalizedString(@"Contact", nil); [userlabel sizeToFit]; [myView addSubview:userlabel]; myView.autoresizesSubviews=true; myView.autoresizingMask = UIViewAutoresizingFlexibleWidth; // return myView; } - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return 44; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 86; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { SelectOrderTableViewCell *cell = (SelectOrderTableViewCell *)[tableView cellForRowAtIndexPath:indexPath]; cell.selected = NO; cell.checkedButton.selected = !cell.checkedButton.selected; NSMutableDictionary *dic = (NSMutableDictionary *)[self.sync_data objectAtIndex:indexPath.row]; int check = [dic[@"check"] integerValue]; if (cell.checkedButton.selected) { check = 1; } else { check = 0; } [dic setValue:[NSNumber numberWithInteger:check] forKey:@"check"]; } //- (BOOL - (void)onCloseClick:(UIButton *)sender { [self dismissViewControllerAnimated:true completion:nil]; } @end