//
//  signatureNameViewController.m
//  Open
//
//  Created by 看看智能 on 2018/1/12.
//  Copyright © 2018年 黄江涛 . All rights reserved.
//

#import "signatureNameViewController.h"

@interface signatureNameViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@property (weak, nonatomic) IBOutlet UILabel *texttttt;

@end

@implementation signatureNameViewController


-(ThirdPersonInformationViewController *)thirdVC
{
    if (!_thirdVC) {
        _thirdVC =[ThirdPersonInformationViewController sharedInstance];
    }
    return _thirdVC;
    
}
-(FourthlyViewController *)fourthVC
{
    if (!_fourthVC) {
        _fourthVC =[FourthlyViewController sharedInstance];
    }
    return _fourthVC;
}
-(FirthlyViewController *)FirthVC
{
    if (!_FirthVC) {
        _FirthVC =[FirthlyViewController sharedInstance];
    }
    return _FirthVC;
    
}


- (IBAction)reSignPhoto:(id)sender {
    
    [self.drawView doBack];
    
}

- (IBAction)Writr:(id)sender {
    self.isPhoto = NO;
    self.imageSignation.hidden =YES;
     //self.drawView.hidden =NO;
}
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:YES];
    
    if (![self.selfSignation isEqualToString:@"本人签字"]) {
         [MobClick endLogPageView:@"就业采集"];
  }
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

}
-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    UIButton * button_2 = [UIButton buttonWithType:UIButtonTypeCustom];
    button_2.frame = CGRectMake(0, 0, 100, 49);
    button_2.titleLabel.textAlignment =NSTextAlignmentRight;
    
    if ([self.selfSignation isEqualToString:@"本人签字"]) {
        [button_2 setTitle:@"     下一步" forState:UIControlStateNormal];
        self.title =@"被采集人";
        self.texttttt.text =@"请 选择签字 或 选择拍照";
    }else{
        [button_2 setTitle:@"     完成" forState:UIControlStateNormal];
        self.title =@"采集人签名";
         self.texttttt.text =@"请 选择签字";
        self.photoButton.hidden = YES;
        self.photoLibButton.hidden =YES;
    }
    [button_2 addTarget:self action:@selector(buttonClick_1) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * backButtonItem_2 = [[UIBarButtonItem alloc] initWithCustomView:button_2];
    self.navigationItem.rightBarButtonItem = backButtonItem_2;
    
    self.navigationController.navigationItem.rightBarButtonItem =backButtonItem_2;
    
}





- (void)viewDidLoad {
    [super viewDidLoad];
    
//    if ( [self.selfSignation isEqualToString:@"本人签字"]&&  [self.PhotoImage_1 isEqualToString:@""] ) {
//        self.photoButton.hidden =NO;
//        self.wirteButton.hidden = YES;
//    }else if ( [self.selfSignation isEqualToString:@"本人签字"] && ![self.PhotoImage_1 isEqualToString:@""]){
//        self.photoButton.hidden =YES;
//        self.wirteButton.hidden = NO;
//    }else if (! [self.secondPage isEqualToString:@"第二个界面"] ){
//        self.photoButton.hidden =NO;
//        self.wirteButton.hidden = NO;
//    }
    
    
    
  [self.drawView initDrawingView];
     self.drawView.color = [UIColor blackColor];
    self.drawView.lineWidth = 2.0;
    self.isPhoto = NO;
    

//    // 截取画板尺寸
//    CGImageRef sourceImageRef = [image CGImage];
//    CGImageRef newImageRef = CGImageCreateWithImageInRect(sourceImageRef, CGRectMake(0, 0, self.drawView.bounds.size.width, _selfdrawView.bounds.size.height));
//    UIImage *newImage = [UIImage imageWithCGImage:newImageRef];
    
    
}
-(NSString *)getImage{
    //UIGraphicsBeginImageContext(self.drawView.bounds.size);
    UIGraphicsImageRenderer*re=[[UIGraphicsImageRenderer alloc] initWithSize:self.drawView.bounds.size];
    [self.drawView.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    NSData *small = UIImagePNGRepresentation(image);
    return [small base64EncodedStringWithOptions:0];
}


-(void)buttonClick_1{

    
    //获取本人
    if ([self.selfSignation isEqualToString:@"本人签字"]) {
         AppDelegate *delegate=(AppDelegate*)[[UIApplication sharedApplication]delegate];
     self.secondPage =@"第二个界面";
        if (self.isPhoto == YES) {
            
            delegate.mySelfSignation =self.PhotoImage_1;
            self.imageSignation.hidden =YES;
        }else{
               delegate.mySelfSignation =  [self getImage];
            
            
        }
        
        
        UIStoryboard *sb  =[UIStoryboard storyboardWithName:@"Main" bundle:nil];
        signatureNameViewController *name =[sb instantiateViewControllerWithIdentifier:@"signatureNameViewController"];
       
            name.secondPage =@"第二个界面";
       
        if ([self.shiye isEqualToString:@"失业"]) {
            name.shiye =@"失业";
        }else if([self.shiye isEqualToString:@"其他"]){
            name.shiye =@"其他";
            name.asdasdasfdsg =_asdasdasfdsg;
        }else{
           name.shiye =@"";
            
        }
            [self.navigationController  pushViewController:name animated:YES];
    }else{
     
        if ([self.shiye isEqualToString:@"失业"]) {
            [self shissssye];
        }else if([self.shiye isEqualToString:@"其他"]){
            [self qita];
        }else{
            [self jiuye];
        }
    
    }
    
}

#pragma mark -logic逻辑代码
-(void)logicRuns_shiye:(JobUtil *)JobUtil {
    //手动
    AppDelegate *delegate=(AppDelegate*)[[UIApplication sharedApplication]delegate];
    if ([delegate.mySelfSignation isEqualToString:@""]) {
        return;
    }
    //采集人签字
    self.selfSignation =   delegate.mySelfSignation ;
    if ([[self getImage] isEqualToString:@""]) {
        return;
    }
    //被采集人签字

    if (self.isPhoto == YES ) {
        if (!self.caiJiSignation) {
            self.caiJiSignation =[self getImage];
        }
        
    }else{
        self.caiJiSignation =[self getImage];
    }
    
    //雷俊博.这一段代码根本没用到,造成内存泄漏
    /*
   NSInteger intString = 0;
    if ([JobUtil isNumberYes:self.FirthVC.mouthIncomeExtension.text]) {
    //intString = [[JobUtil netStrNull:self.thirdVC.income] intValue];
        intString = [self.thirdVC.income.text  intValue];
    }else{
        intString =1;
    }
    */
    
    
    
    
}
#pragma mark - 相册



#pragma mark - 照片
- (IBAction)makePhoto:(UIButton*)sender {
    //传照片的接口
    self.drawView.hidden =YES;
    if(sender.tag == 5448){
        // 判断系统是否支持相机
        UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
        if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
            imagePickerController.delegate = self; //设置代理
            imagePickerController.allowsEditing = YES;
                imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
                [self presentViewController:imagePickerController animated:YES completion:nil];
            
        }else {
            imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            [self presentViewController:imagePickerController animated:YES completion:nil];
        }   
    }else{
    
    
    __block UIImagePickerController *ipc = [[UIImagePickerController alloc] init];


        ipc.sourceType = UIImagePickerControllerSourceTypeCamera;
    ipc.cameraDevice=UIImagePickerControllerCameraDeviceRear;
    ipc.delegate = self;
    ipc.allowsEditing = NO;
    ipc.navigationBar.barTintColor =[UIColor whiteColor];
    ipc.navigationBar.tintColor = [UIColor whiteColor];
    ipc.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor]};
    ipc.videoQuality = UIImagePickerControllerQualityType640x480;
    dispatch_async(dispatch_get_main_queue(), ^{
        [self presentViewController:ipc animated:YES completion:^{
            ipc = nil;
        }];
    });
    }
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    
    //完成选择
    NSString *type = [info objectForKey:UIImagePickerControllerMediaType];
    if ([type isEqualToString:@"public.image"]) {
        __block UIImage *flippedImage = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
        if (flippedImage == nil) {
            [SVProgressHUD showErrorWithStatus:@"请在设置中打开照片访问权限"];
        
        }
__weak typeof(self) weakSelf = self;
        flippedImage  = [UIImage imageWithCGImage:flippedImage.CGImage
                                            scale:1.0 orientation: UIImageOrientationLeftMirrored];
        dispatch_async(dispatch_get_main_queue(), ^{
            [picker dismissViewControllerAnimated:YES completion:^{
               
                weakSelf.isPhoto = YES;
                weakSelf.imageSignation.hidden = NO;
                weakSelf.imageSignation.image = flippedImage;
                if ([weakSelf.secondPage isEqualToString:@"第二个界面"]) {
                   weakSelf.caiJiSignation =    [weakSelf changImageBase64:flippedImage];
                }else{
                     weakSelf.PhotoImage_1 =    [weakSelf changImageBase64:flippedImage];
                }
                
            }];
        });
    }
}



#pragma mark - 接口
-(void)qita{
    
    JobUtil *jobUtil =[[JobUtil alloc]init];
    NSInteger intString_3 = 0;
    if ([jobUtil isNumberYes:self.FirthVC.mouthIncomeExtension.text]) {
        //   intString = [[jobUtil netStrNull:self.FirthVC.mouthIncomeExtension] intValue];
        intString_3 = [self.FirthVC.mouthIncomeExtension.text  intValue];
    }else{
        intString_3 = 0;
        
    }
    
    //手动
    AppDelegate *delegate=(AppDelegate*)[[UIApplication sharedApplication]delegate];
    if ([delegate.mySelfSignation isEqualToString:@""]) {
        return;
    }
    //采集人签字
    self.selfSignation =   delegate.mySelfSignation ;
    if ([[self getImage] isEqualToString:@""]) {
        return;
    }
    //被采集人签字
  
    if (self.isPhoto == YES ) {
        if (!self.caiJiSignation) {
             self.caiJiSignation =[self getImage];
        }
        
    }else{
          self.caiJiSignation =[self getImage];
    }
    [SVProgressHUD show];
    
    

    
    
    
    [PersonGatherJobNetManager getQITA:self.asdasdasfdsg selfImage:self.selfSignation andCollectIamge:self.caiJiSignation
                          andTrainType:self.FirthVC.needJobKK//培训就业需求
                  andemployeeExtension:[jobUtil netStrNull:self.FirthVC.employeeExtension]  //就业意向
                        andWorkAddress:self.FirthVC.AddressJobKK //理想就业地
               andmouthIncomeExtension:intString_3 //理想月工资
                          andWorlStyle:self.FirthVC.JOBStyle//工作方式
                  andWorkTypeExtension:[jobUtil netStrNull:self.FirthVC.WorkTypeExtension]//意向工种
                        andpeixunGrank:self.FirthVC.peixunGrank_//培训等级
           andentrepreneurialExtension:[jobUtil netStrNull:self.FirthVC.entrepreneurialExtension] //创业意向
                       andDreamAddress:[JobUtil netStrNull_2:self.FirthVC.choosePlace.titleLabel.text]
                      completionHandle:^(PersonJob *model, NSError *error) {
        if (error) {
            return;
        }
        
        if (model.state == 0 ) {
            UIStoryboard *sb = [UIStoryboard  storyboardWithName:@"Main" bundle:nil];
            FirstPersonInformationViewController *secondVC =   [sb instantiateViewControllerWithIdentifier:@"FirstPersonInformationViewController"];
            
            [SVProgressHUD showSuccessWithStatus:@"采集成功"];
            if(![self.navigationController.topViewController isKindOfClass:[secondVC class]]) {
                
                
                [self.navigationController popToRootViewControllerAnimated:YES];
            }
        } else{
            codeSelected *code = [codeSelected MustTakeReturn:model.state];
            [code returnCodeMethod];
            return;
        }

        
        
    }];
    
    
    
}
-(void)TianjiaNew{
    JobUtil *jobUtil =[[JobUtil alloc]init];
    NSInteger intString = 0;
    if ([jobUtil isNumberYes:self.FirthVC.mouthIncomeExtension.text]) {
        //   intString = [[jobUtil netStrNull:self.FirthVC.mouthIncomeExtension] intValue];
        intString = [self.FirthVC.mouthIncomeExtension.text  intValue];
    }else{
        intString = 0;
    }
    
    
    
    
    
    
}



-(void)shissssye{
//    shiye
    
    JobUtil *jobUtil =[[JobUtil alloc]init];
    [self   logicRuns_shiye:jobUtil];
    NSInteger intString = 0;
    if ([jobUtil isNumberYes:self.FirthVC.mouthIncomeExtension.text]) {
     //   intString = [[jobUtil netStrNull:self.FirthVC.mouthIncomeExtension] intValue];
         intString = [self.FirthVC.mouthIncomeExtension.text  intValue];
    }else{
        intString = 0;
        
    }
    [SVProgressHUD show];

    [PersonGatherJobNetManager getUserAdddFived:0
                            andunemployeeReason:@""
                              andunemployeeTime:@""
                              andoriginCompanyl:@""
                                   andTrainType:self.FirthVC.needJobKK
                           andemployeeExtension:[jobUtil netStrNull:self.FirthVC.employeeExtension]
                    andentrepreneurialExtension:[jobUtil netStrNull:self.FirthVC.entrepreneurialExtension]
                        andmouthIncomeExtension:intString
                                 andWorkAddress:self.FirthVC.AddressJobKK
                                 andpeixunGrank:self.FirthVC.peixunGrank_
                           andWorkTypeExtension:[jobUtil netStrNull:self.FirthVC.WorkTypeExtension]
                                   andWorlStyle:self.FirthVC.JOBStyle
                               andselfSigniture:self.selfSignation
                          andcollectorSignature:self.caiJiSignation
                                andDreamAddress:[JobUtil netStrNull_2:self.FirthVC.choosePlace.titleLabel.text]
                               completionHandle:^(PersonJob *model, NSError *error) {
                                   [SVProgressHUD dismiss];
                                   
                                   if (error) {
                                       return;
                                   }
                                   
                                   
                                   if (model.state == 0 ) {
                                       UIStoryboard *sb = [UIStoryboard  storyboardWithName:@"Main" bundle:nil];
                                       FirstPersonInformationViewController *secondVC =   [sb instantiateViewControllerWithIdentifier:@"FirstPersonInformationViewController"];
                                       [SVProgressHUD showSuccessWithStatus:@"采集成功"];
                                       if(![self.navigationController.topViewController isKindOfClass:[secondVC class]]) {
                                           [self.navigationController popToRootViewControllerAnimated:YES];
                                       }
                                   } else{
                                       codeSelected *code = [codeSelected MustTakeReturn:model.state];
                                       [code returnCodeMethod];
                                       return;
                                   }
                               }];
    
    
}
-(void)jiuye{
    JobUtil *jobUtil =[[JobUtil alloc]init];

    NSInteger intString = 0;
    //判断就业人数
    if ([jobUtil isNumberYes:self.thirdVC.laborCount.text]) {
    //    intString = [[jobUtil netStrNull:self.thirdVC.laborCount] intValue];
        intString = [self.thirdVC.laborCount.text intValue];
    }else{
        intString =1;
    }
    
    
    //判断月工资收入
    NSInteger intString_2 = 0;
    if ([jobUtil isNumberYes:self.thirdVC.income.text]) {
     //   intString_2 = [[jobUtil netStrNull:self.thirdVC.income] intValue];
        intString_2 = [self.thirdVC.income.text intValue];
    }else{
        intString_2 =1;
    }
    
    NSInteger intString_3 = 0;
    if ([jobUtil isNumberYes:self.FirthVC.mouthIncomeExtension.text]) {
        //   intString = [[jobUtil netStrNull:self.FirthVC.mouthIncomeExtension] intValue];
        intString_3 = [self.FirthVC.mouthIncomeExtension.text  intValue];
    }else{
        intString_3 = 0;
        
    }
    
    
    
    //获取采集照片
    AppDelegate *delegate=(AppDelegate*)[[UIApplication sharedApplication]delegate];
    if ([delegate.mySelfSignation isEqualToString:@""]) {
        return;
    }
    self.selfSignation =   delegate.mySelfSignation ;
    if ([[self getImage] isEqualToString:@""]) {
        return;
    }

    if (self.isPhoto == YES ) {
        if (!self.caiJiSignation) {
            self.caiJiSignation =[self getImage];
        }
        
    }else{
        self.caiJiSignation =[self getImage];
    }
    [SVProgressHUD show];

    
    
    
    
    [PersonGatherJobNetManager  getUserAdddThreeeId:0
                                        andworkMode:self.thirdVC.chooseWorkKK//单位就业
                                     andcompanyName:[jobUtil netStrNull:self.thirdVC.companyProjectName]
                                      andcompanyTel:[jobUtil netStrNull:self.thirdVC.companyTel]
                              andcompanyProjectName:[jobUtil netStrNull:self.thirdVC.companyProjectName]
                                      andlaborCount:intString //就业人数
                                 andemployeeChannel:self.thirdVC.employeeChannel_
                                        andrgstTime:self.thirdVC.rgstTime.text
                                        andflexType:self.thirdVC.flexType_
                                    andemployeeTime:self.thirdVC.employeeTime.text
                                          andincome:intString_2
                                         andisTrain:self.thirdVC.certKK
                                        andlocation:[jobUtil netStrNull:self.thirdVC.location]
                                        andworkType:[jobUtil netStrssssNull:[NSString stringWithFormat:@"%ld",(long)self.thirdVC.workType_ ]]
                                   andindustryField: [jobUtil netStrssssNull:[NSString stringWithFormat:@"%ld",(long)self.thirdVC.industryField_]]
                              andSelfSignatureImage: [jobUtil  netStrssssNull:self.selfSignation]
                                  andCollectorImage:[jobUtil  netStrssssNull:self.caiJiSignation]
                                          andASFASF:self.thirdVC.cityjob
                                       andTrainType:self.FirthVC.needJobKK//培训就业需求
                               andemployeeExtension:[jobUtil netStrNull:self.FirthVC.employeeExtension]  //就业意向
                                     andWorkAddress:self.FirthVC.AddressJobKK //理想就业地
                            andmouthIncomeExtension:intString_3 //理想月工资
                                       andWorlStyle:self.FirthVC.JOBStyle//工作方式
                               andWorkTypeExtension:[jobUtil netStrNull:self.FirthVC.WorkTypeExtension]//意向工种
                                     andpeixunGrank:self.FirthVC.peixunGrank_//培训等级
                        andentrepreneurialExtension:[jobUtil netStrNull:self.FirthVC.entrepreneurialExtension] //创业意向
                                    andDreamAddress:[JobUtil netStrNull_2:self.FirthVC.choosePlace.titleLabel.text]
                                   completionHandle:^(PersonJob *model, NSError *error) {
                                       [SVProgressHUD dismiss];
                                       if(error){
                                           
     
                                           
                                           
                                           
                                           
                                           
                                           
                                           return;
                                       }
                                       
                                       
                                       if (model.state == 0 ) {
                                           UIStoryboard *sb = [UIStoryboard  storyboardWithName:@"Main" bundle:nil];
                                           FirstPersonInformationViewController *secondVC =   [sb instantiateViewControllerWithIdentifier:@"FirstPersonInformationViewController"];

                                           [SVProgressHUD showSuccessWithStatus:@"采集成功"];
                                           if(![self.navigationController.topViewController isKindOfClass:[secondVC class]]) {


                                               [self.navigationController popToRootViewControllerAnimated:YES];
                                           }
                                       }else{
                                           codeSelected *code = [codeSelected MustTakeReturn:model.state];
                                           [code returnCodeMethod];
                                           return;
                                       }






                                   } ];
    
//      [SVProgressHUD showSuccessWithStatus:@"采集成功"];
//        [self.navigationController popToRootViewControllerAnimated:YES];
//    
    
}



@end
