PerfectInformationViewController.m 9.5 KB
Newer Older
曹云霄's avatar
曹云霄 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
//
//  PerfectInformationViewController.m
//  Lighting
//
//  Created by 曹云霄 on 2017/2/9.
//  Copyright © 2017年 上海勾芒科技有限公司. All rights reserved.
//

#import "PerfectInformationViewController.h"
#import "PerfectInformationTableViewCell.h"
#import "DateSelectedViewController.h"

@interface PerfectInformationViewController ()<UITableViewDelegate,UITableViewDataSource,ReturnTableviewcellIndexpathdelegate,UITextFieldDelegate>

@property (nonatomic,strong) NSArray *itemNameArray;
@property (nonatomic,strong) NSMutableArray *informationArray;

@end

@implementation PerfectInformationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self uiConfigAction];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    PerfectInformationTableViewCell *informationCell = [tableView dequeueReusableCellWithIdentifier:@"PerfectInformationTableViewCell" forIndexPath:indexPath];
    informationCell.itemInputTextField.text = [BaseViewController isBlankString:self.informationArray[indexPath.section][indexPath.row]]?nil:self.informationArray[indexPath.section][indexPath.row];
    informationCell.itemNameLabel.text = self.itemNameArray[indexPath.section][indexPath.row];
    informationCell.itemInputTextField.placeholder = informationCell.itemNameLabel.text;
    [self textFieldIsEditing:indexPath withCell:informationCell];
    return informationCell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSArray *array = self.informationArray[section];
    return array.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.informationArray.count;
}

#pragma mark -是否可编辑
- (void)textFieldIsEditing:(NSIndexPath *)indexPath withCell:(PerfectInformationTableViewCell *)cell
{
    if (indexPath.section) {
        cell.itemInputTextField.userInteractionEnabled = YES;
        cell.itemNameLabel.textColor = [UIColor blackColor];
        cell.itemInputTextField.textColor = [UIColor blackColor];
        cell.itemInputTextField.tag = indexPath.row;
        cell.itemInputTextField.delegate = self;
        [self setUpSelectBox:cell];

    }else {
        cell.itemInputTextField.userInteractionEnabled = NO;
        cell.itemNameLabel.textColor = [UIColor grayColor];
        cell.itemInputTextField.textColor = [UIColor grayColor];
    }
}

#pragma mark -UITextFieldDelegate
- (void)textFieldDidEndEditing:(UITextField *)textField
{
    self.informationArray[1][textField.tag] = textField.text;
    [self inputIsCompleted];
}

#pragma mark -设置选择框
- (void)setUpSelectBox:(PerfectInformationTableViewCell *)cell
{
    UITapGestureRecognizer *dataSelectTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dateSelection)];
    UITapGestureRecognizer *genderSelectTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(genderSelection:)];
    if ([cell.itemInputTextField.placeholder isEqualToString:@"生日"]) {
        [cell.itemInputTextField addGestureRecognizer:dataSelectTap];
    }else if ([cell.itemInputTextField.placeholder isEqualToString:@"性别"]) {
        [cell.itemInputTextField addGestureRecognizer:genderSelectTap];
    }else {
        [cell.itemInputTextField removeGestureRecognizer:dataSelectTap];
        [cell.itemInputTextField removeGestureRecognizer:genderSelectTap];
    }
}

#pragma mark -日期选择框
- (void)dateSelection
{
    WS(weakSelf);
    PerfectInformationTableViewCell *cell = [self.perfectInformationTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:2 inSection:1]];
    DateSelectedViewController *datevc = [[DateSelectedViewController alloc]init];
    [datevc setSelectedDateBlock:^(NSDate *selectedDate) {
        cell.itemInputTextField.text = [weakSelf dateAsString:selectedDate];
        weakSelf.informationArray[1][2] = cell.itemInputTextField.text;
        [weakSelf inputIsCompleted];
    }];
    datevc.preferredContentSize = CGSizeMake(300, 250);
    datevc.modalPresentationStyle = UIModalPresentationFormSheet;
    UIPopoverPresentationController *pop = datevc.popoverPresentationController;
    pop.permittedArrowDirections = UIPopoverArrowDirectionAny;
    pop.sourceView = datevc.view;
    [self presentViewController:datevc animated:YES completion:nil];
}

#pragma mark -性别选择
- (void)genderSelection:(UITapGestureRecognizer *)tap
{
    PopoverViewController *popover = [[PopoverViewController alloc]init];
    popover.datasArray = @[@"男",@"女"];
    popover.delegate = self;
    popover.isString = YES;
    popover.contentSize = CGSizeMake(100, 100);
    popover.preferredContentSize = CGSizeMake(100, 100);
    popover.modalPresentationStyle = UIModalPresentationPopover;
    UIPopoverPresentationController *pop = popover.popoverPresentationController;
    pop.permittedArrowDirections = UIPopoverArrowDirectionUp;
    pop.sourceView = popover.view;
    pop.barButtonItem = [[UIBarButtonItem alloc]initWithCustomView:tap.view];
    [self presentViewController:popover animated:YES completion:nil];
}

#pragma mark -ReturnTableviewcellIndexpathdelegate
- (void)returnCellindexpathwithrow:(NSString *)type withCellTitle:(NSString *)title withSelected:(NSInteger)selected
{
    PerfectInformationTableViewCell *cell = [self.perfectInformationTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:1]];
    cell.itemInputTextField.text = title;
    self.informationArray[1][1] = title;
    [self inputIsCompleted];
}

#pragma mark -时间转换NSDate转NSString
- (NSString*)dateAsString:(NSDate*)date {
    
    NSDateFormatter *formatter=[[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    NSString * timeString = [formatter stringFromDate:date];
    return timeString;
}

#pragma mark -UI
- (void)uiConfigAction
{
    self.perfectInformationTableView.tableFooterView = [UIView new];
    self.title = @"个人资料";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStyleDone target:self action:@selector(modifyshoppersInformation)];
    self.navigationItem.rightBarButtonItem.enabled = [Shoppersmanager manager].shoppers.employee.isComplete;
}

#pragma mark -判断输入是否完成
- (void)inputIsCompleted
{
    for (id object in self.informationArray[1]) {
        if ([BaseViewController isBlankString:object]) {
            return;
        }
    }
    [Shoppersmanager manager].shoppers.employee.isComplete = NO;
    self.navigationItem.rightBarButtonItem.enabled = YES;
}

#pragma mark -完成修改导购个人信息
- (void)modifyshoppersInformation
{
    if ([Shoppersmanager manager].shoppers.employee.isComplete) {
        [self dismissViewControllerAnimated:YES completion:nil];return;
    }
曹云霄's avatar
曹云霄 committed
170 171 172
    if (![self.informationArray[1][3] isTelephone]) {
        [XBLoadingView showHUDViewWithText:@"手机号码格式不正确"];return;
    }
曹云霄's avatar
曹云霄 committed
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
    WS(weakSelf);
    [XBLoadingView showHUDViewWithDefault];
    RsEmployeeRequest *employ = [[RsEmployeeRequest alloc]init];
    employ.employee = [Shoppersmanager manager].shoppers.employee;
    employ.employee.realName = self.informationArray[1][0];
    employ.employee.gender = self.informationArray[1][1];
    employ.employee.birthday = [NSString stringWithFormat:@"%@ 00:00:00",self.informationArray[1][2]];
    employ.employee.mobilePhone = self.informationArray[1][3];
    employ.employee.isComplete = YES;
    [HTTP networkRequestWithURL:SERVERREQUESTURL(MODITYshoppersINFORMATION)  withRequestType:ZERO withParameter:employ withReturnValueBlock:^(id returnValue) {
        
        [XBLoadingView hideHUDViewWithDefault];
        if (RESULT(returnValue)) {
            [Shoppersmanager manager].shoppers.employee.realName = employ.employee.realName;
            [XBLoadingView showHUDViewWithSuccessText:@"修改成功" completeBlock:^{
                [Notification postNotificationName:CHANGEGUIDENAME object:employ.employee.realName];
                [weakSelf dismissViewControllerAnimated:YES completion:nil];
            }];
        }else
        {
            [XBLoadingView showHUDViewWithText:MESSAGE(returnValue)];
        }
    }withFailureBlock:^(NSError *error) {
        [XBLoadingView hideHUDViewWithDefault];
    }];
}

#pragma mark -lazy
- (NSMutableArray *)informationArray
{
    if (!_informationArray) {
        LoginResult *guide = [Shoppersmanager manager].shoppers;
        NSMutableArray *mutableArray1 = [NSMutableArray arrayWithObjects:[self disposeNil:guide.employee.userName],[self disposeNil:guide.employee.region],[self disposeNil:guide.employee.office],[self disposeNil:guide.employee.reseller],[self disposeNil:guide.employee.store], nil];
        NSMutableArray *mutableArray2 = [NSMutableArray arrayWithObjects:[self disposeNil:guide.employee.realName],[self disposeNil:guide.employee.gender],[self disposeNil:guide.employee.birthday],[self disposeNil:guide.employee.mobilePhone], nil];
        _informationArray = [NSMutableArray arrayWithObjects:mutableArray1,mutableArray2, nil];
    }
    return _informationArray;
}

- (NSArray *)itemNameArray
{
    if (!_itemNameArray) {
        _itemNameArray = @[
                           @[@"账户名",@"所属大区",@"所属办事处",@"所属经销商",@"所属店名",@"value"],
                           @[@"姓名",@"性别",@"生日",@"手机号"]];
    }
    return _itemNameArray;
}

#pragma mark -处理属性为空避免字典崩溃
- (id)disposeNil:(NSString *)string
{
    if ([BaseViewController isBlankString:string]) {
        return [NSNull null];
    }
    return [[string componentsSeparatedByString:@" "] firstObject];
}

@end